DO/LOOP statements

Purpose

Define a group of program statements that are executed repetitively as long as a certain condition is met.

Syntax

DO [{WHILE | UNTIL} expression]

  ...

  {statements}

  ...

  [EXIT LOOP]

  [ITERATE LOOP]

  ...

LOOP {{WHILE | UNTIL} expression}

Remarks

expression is a numeric expression, in which non-zero values represent logical TRUE, and zero values represent logical FALSE.  If a string expression is used (i.e., A$ <> ""), PowerBASIC returns TRUE if the length of result of the string expression is greater than zero.

DO/LOOP statements are extremely flexible.  They can be used to create loops for almost any imaginable programming situation.  They allow you to create loops with the test for the terminating condition at the top of the loop, the bottom of the loop, both places, or none of the above.

A DO statement must always be paired with a matching LOOP statement at the bottom of the loop.  Failure to match each DO with a LOOP results in either a compile-time Error 448 ("DO loop expected") or an Error 456 ("LOOP/WEND expected").

The WHILE and UNTIL keywords are used to add tests to a DO/LOOP.  Use the WHILE if the loop should be repeated if expression is TRUE, and terminated if expression is FALSE.  UNTIL has the opposite effect; that is, the loop will be terminated if expression is TRUE, and repeated if FALSE.

For example:

DO WHILE a = 13

  {statements}

LOOP

executes the statements between DO and LOOP as long as a is 13.  If a is not 13 initially, the statements in the loop are never executed.  Conversely:

DO UNTIL a = 13

  {statements}

LOOP

executes the statements between DO and LOOP as long as a is not 13.  If a equals 13 initially, the loop is never executed.

At any point in a DO/LOOP, you can include an EXIT LOOP or ITERATE LOOP statement.  EXIT LOOP causes the loop to terminate, so that execution continues after the terminating loop statement.  ITERATE LOOP causes the loop to continue at the terminating loop statement. 

The WHILE/WEND statements can be used in many cases to perform the same functions as DO/LOOP.  For example, this DO/LOOP:

DO WHILE a < b

  {statements}

LOOP

has the same effect as this WHILE/WEND loop:

WHILE a < b

  {statements}

WEND

When using nested loops, be careful that inner loops do not modify variables that are used by the outer loop's terminating condition test.  For example, the following code was intended to get all 20 elements of a 10x2 array (dimensioned arry(9,1)):

Count1 = 0

DO WHILE Count1 < 10

  FOR Count2 = 0 TO 1

    x = arry(Count1,Count2)

    Count1 = Count1 + 1

  NEXT Count2

LOOP

Because Count1 is incremented within the inner loop, which executes twice for each pass through the outer loop, this code would not get all the array values, but would only get the values for arry(0,0), arry(1,1), arry(2,0), arry(3,1) and so on.  By moving the Count1 = Count1 + 1 statement to just below the NEXT Count2 statement, the code functions as intended.

If an EXIT LOOP statement is used within nested loops, it exits only the current loop, not the entire nest.  Similarly, an ITERATE within nested loops iterates the current loop.  For advice on exiting nested block structures, please refer to the EXIT statement.  The PowerBASIC logical operators can be used to construct multiple test conditions for loop control.  For example:

DO WHILE x < 10 AND y < 10

  {statements}

LOOP

is executed only as long as both x and y are less than 10.  Similarly, the loop:

DO UNTIL X > 10 OR Y > 10

  {statements}

LOOP

is executed until either x or y (or both) is (are) greater than 10.  See the Data Types and Arithmetic Operators topics for more information about using logical operators.

Although the compiler doesn't care about such things, it is a good idea when writing your source code to indent the statements between DO and LOOP.  The same is true of FOR/NEXT loops, WHILE/WEND loops, and multi-line IF statements.  Such indenting makes the appearance of your source code reflect the logical structure of your program, resulting in greater readability.  Indenting is particularly valuable when nesting multiple loops of the same type, since it makes it easier to see which LOOP goes with which DO.

Also see the discussion on the IF statement for notes on PowerBASIC's Short-circuit evaluation and its possible side effects.

See also

EXIT, FOR/NEXT, IF, ITERATE, WHILE/WEND