DO Forever loop
Use the DO FOREVER
command to repeat a block until the global
loop limit is reached or a LEAVE
or EXIT
command is found.
VARSET X = 1
DO FOREVER
DISPLAY !X
VARSET X DELTA(1)
IF “!X” > 20 THEN LEAVE
END
Use the ITERATE
command at any point in the block to start the next
iteration of the currently active loop from the DO
statement again.
Use the LEAVE
command to exit the currently active loop and resume
processing following the corresponding END
statement.
ITERATE
and LEAVE
can exit only the
currently active block.
The ITERATE
and LEAVE
commands can be used
only to exit the currently active block.
Loops of this type are protected from running forever by accident by the global loop limit
OPTIONS LIMIT
, whose default is 100.
- The
VARSCAN
command has the ability to perform anITERATE
orLEAVE
action in the event of not finding any match by using theACTION
keyword. This negates the need for a separateIF/THEN/ITERATE
orIF/THEN/LEAVE
statement. - By default, every statement in a
DO
block is listed in the log every time it is run, for each iteration of the block. This can result in extremely large logs and might not be the desired result. To reduce unwanted log traffic, setOPTIONS MSGLEVEL(0)
before theEND
statement of the loop andOPTIONS MSGLEVEL(1)
after theEND
statement. This results in all commands within the loop being listed once for the first full iteration of the loop, and from that point only statements which end in a non-zero return code are listed. The complete list of all commands will be resumed after the loop has been exited.For example:VARSET X = 1 DO FOREVER DISPLAY !X VARSET X DELTA(1) IF “!X” > 20 THEN LEAVE OPTIONS MSGLEVEL(0) END OPTIONS MSGLEVEL(1)