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
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.

Note:
  • The VARSCAN command has the ability to perform an ITERATE or LEAVE action in the event of not finding any match by using the ACTION keyword. This negates the need for a separate IF/THEN/ITERATE or IF/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, set OPTIONS MSGLEVEL(0) before the END statement of the loop and OPTIONS MSGLEVEL(1) after the END 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)