DO...LOOP                                                           Statement

DO [WHILE | UNTIL condition]

  [statements]

  [EXIT DO]

  [statements]

LOOP

or

DO

  [statements]

  [EXIT DO]

  [statements]

LOOP [WHILE | UNTIL condition]

Description

DO...LOOP repeats a block of statements WHILE a given condition is TRUE, or UNTIL a given condition becomes TRUE. The required component, condition, is any expression that can be evaluated as TRUE or FALSE. The optional component, statements, are the statements executed during the body of the loop. Any number of optional EXIT DO statements can be used to exit a loop before it is finished. DO...LOOP statements can be nested, and any EXIT DO statements in a nested loop transfer execution to one level above the loop where the EXIT DO occurs.

Placing the WHILE/UNTIL clause directly after DO causes condition to be evaluated before the first loop is executed, if condition is FALSE statements are never executed; placing the WHILE/UNTIL clause after LOOP causes the body of the loop to be executed before condition is evaluated, statements will always be executed, at least once.

Example

REM DO...LOOP Example

'DO...LOOP repeats a block of statements

DIM Counter

Counter = 1

'Loop that prints 1 to 5

DO WHILE Counter < 6

  PRINT "DO WHILE...LOOP:", Counter

  Counter = Counter + 1

LOOP

PRINT

'Infinite loop that uses EXIT DO toterminate

Counter = 1000

DO

  PRINT "Infinite Loop:", Counter

  IF Counter >= 1000000 THEN

    EXIT DO

  Counter = Counter * 10

LOOP

Output

DO WHILE...LOOP:     1

DO WHILE...LOOP:     2

DO WHILE...LOOP:     3

DO WHILE...LOOP:     4

DO WHILE...LOOP:     5

Infinite Loop:       1000

Infinite Loop:       10000

Infinite Loop:       100000

Infinite Loop:       1000000

Related Items

EXIT, FOR EACH...NEXT, FOR...NEXT, WHILE...WEND