DO...LOOP

The DOLOOP instruction repeats a block of statements while a condition is TRUE or until a condition becomes TRUE.

Note: Infinite loops are possible with DO loops. Enrichment does not check for infinite loops, so you should set time conditions appropriately.

The syntax for DOLOOP to test the logical expression at the top of the loop is:

DO WHILE logical-expr
   statements
LOOP

Or

DO UNTIL logical-expr
   statements
LOOP

The syntax for DOLOOP to test the logical expression at the bottom of the loop is:

DO
   statements
LOOP WHILE logical-expr

Or

DO
   statements
LOOP UNTIL logical-expr

where logical-expr is the logical expression that will be tested to determine when to leave the loop.

To leave or iterate a DO loop:
DO…
statements
[EXIT]
[EXIT DO]
[ITERATE]
[ITERATE DO]
statements
LOOP…

Hints

  • WHILE continues the loop as long as the logical-expr is TRUE. If the loop is tested at the top and the WHILE expression if FALSE on the first iteration, the loop will not be executed at all.
  • UNTIL continues the loop until the logical-expr becomes TRUE. If the loop is tested at the top and the UNTIL expression is TRUE on the first iteration, the loop will not be executed at all.
  • The loop will always be executed at least once if the loop is tested at the bottom.
  • EXIT and EXIT DO both immediately leave a given loop. EXIT by itself leaves the innermost FOR or DO loop. Since EXIT can also leave a FOR loop, EXIT DO is more explicit. For example:
    DO WHILE %%String != 'Car'
    FOR %%Count = 1 to 10
    %%String = READ(DD:FILE)
    IF %%String = 'Last line' THEN
    EXIT DO
    ENDIF
    NEXT %%Count
    LOOP
    In this example, the EXIT DO statement will leave both the DO loop and the FOR loop. An EXIT by itself would have only left the inner FOR loop.
    • ITERATE or ITERATE DO will cause the next iteration of the loop. The logical-expr is evaluated and the next loop iteration begins at the top of the loop. Any statements below the ITERATE are not executed until the iterations are complete.
    • If you omit the DO on the EXIT DO statement, the EXIT means to exit the innermost loop. If you nest a DO inside a FOR, you can have an EXIT FOR statement that will exit the outer FOR loop.
    • You can use an ITERATE DO substatement to cause the next iteration of the DO loop to begin executing immediately. If you specify ITERATE by itself, it goes to the top of the innermost loop.