Welcome to the Creatures Wiki! Log in and join the community.

LOOP

From Creatures Wiki
Jump to: navigation, search

LOOP is a CAOS command controlling flow.

Usage[edit]

LOOP marks the start of a block of code which is to be looped unboundedly. There are two types of unbounded loop in CAOS: LOOP..EVER and LOOP..UNTL.

..EVER[edit]

Syntax: LOOP .. EVER

LOOP..EVER just repeats the same block of code over and over, until a STOP instruction is reached, or the script is interrupted (if running SLOW).

..UNTL[edit]

Syntax: LOOP .. UNTL cond (condition)

LOOP..UNTL repeats the loop block, but at the end of each iteration cond is evaluated and if it is found to be true, the loop is terminated. STOP and interruptions still break out of the loop, as above.

This is a do { ... } until (cond) loop in many programming languages. To simulate a while loop, you can wrap the whole thing in a DOIF.

Warning!
Do not attempt to break out of any loop using a GOTO or RETN command! It will crash your script. STOP is the only valid instruction for breaking a loop.

Examples[edit]

This uses LOOP..UNTL to almost-replicate an ENUM loop, outputting UNIDs as it goes:

SETA va00 NCLS NULL 0 0 0
TARG va00
LOOP
  OUTV UNID
  OUTS "\n"
  TARG NCLS TARG 0 0 0
UNTL TARG eq va00

LOOP..EVER can be used, and is in the Bootstrap, to continue doing something until a script interrupts.

See also[edit]

External links[edit]