On this page:
Exit Conditions, ENDLOOP and ABORT Commands, The $loop Variable.
Any loop ends after the specified number of passes, after an ITEM operator has referenced the last item in the list, or when an error occurs.
For an inline loop, these are the only ways in which it can end.
A script loop can also end when a certain condition is met. Exit conditions can be based on calculation results, on the loop index I, on user input, or any combinations thereof, and even on the loop timer pseudo constant TIME.
We have to distinguish between aborting the loop -- that is, skipping the rest of the script before the loop is ended -- and ending the loop after the script has been completed.
When an error occurs the loop is aborted.
When an ITEM operator has referenced the last item in the list (or the first when the item argument is negative), the rest of the script is executed before the loop ends.
Scripts have three more ways to exit a loop: with the commands ENDLOOP and ABORT, and with the loop variable $loop.
These commands can be used in scripts after IF ... THEN, ALSO: or ELSE: conditions.
ENDLOOP means that the rest of the script is executed before the loop ends.
ABORT, as the name says, means that the rest of the script is skipped before the loop ends.
IF ... THEN ABORT does the same as the two lines
IF ... THEN ENDLOOP
ALSO: SKIP
Note that SKIP on its own skips the rest of the script, but does not end the loop!
While ENDLOOP and ABORT only make sense within loops, SKIP can be used in scripts both within and outside of a loop.
I*: lines will not be executed at the end of a script when the loop is exited with ABORT, or when it is aborted due to an error.
In all other cases they will be executed.
As an alternative to ending a loop with ENDLOOP you can use the variable $loop.
When $loop is set to zero within a loop anywhere in the script, and remains zero until the end of the script, the loop is exited after the end of the script has been reached.
The value of $loop is ignored if it is not changed within the loop.
The exit condition is only met when the value of $loop becomes exactly zero. To apply the zero threshold (by default, 1e-16), use the operators SIGN or ISNOT0 when calculating $loop.
The variable $loop does not exist unless you create it. Outside of a loop it is just a variable like any other.
$loop can be useful in scripts that perform iterations, but it may often be easier to use IF ... THEN ENDLOOP instead.
Home, Up: Loops, Prev: Loop Index, Start and End Lines, Next: REPEAT and REPEAT Loops