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.
For example:
VARSET X = 1
DO FOREVER
DISPLAY !X
VARSET X DELTA(1)
IF “!X” > 20 THEN LEAVE
ENDUse 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.
Note:
- The
VARSCANcommand has the ability to perform anITERATEorLEAVEaction in the event of not finding any match by using theACTIONkeyword. This negates the need for a separateIF/THEN/ITERATEorIF/THEN/LEAVEstatement. - By default, every statement in a
DOblock 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 theENDstatement of the loop andOPTIONS MSGLEVEL(1)after theENDstatement. 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)