The WHILE command repeatedly executes a statement while the value of a Boolean expression remains TRUE. You can only use WHILE within a program.


WHILE boolean-expression

    statement block



Serves as the criterion for statement execution. While the expression remains TRUE, statement is repeatedly executed. When the expression becomes FALSE, the execution of statement ceases, and the program continues with the next line. Ensure that something in the statement (or statements) eventually causes the Boolean expression to become FALSE; otherwise, the code becomes an endless loop.

statement block

One or more statements to be executed while the Boolean expression is TRUE. You can execute two or more statements by enclosing them within DO ... DOEND brackets. The DO statement should follow immediately after the WHILE statement.


WHILE Compared to IF

The WHILE statement's main use is as an alternative to the IF...THEN...ELSE comand.When you want one or more statements in your program to execute repeatedly for as long as a Boolean expression remains TRUE, you use WHILE. When you want them to execute only once when a Boolean expression is TRUE, you use IF.

Boolean Constant

You can specify a constant for the Boolean expression. When your statement is WHILE TRUE, make sure to include a BREAK, RETURN, or EXIT statement between DO ... DOEND so the program can finish the loop.

Branching in a Loop

You can use the BREAK, CONTINUE, and GOTO commands to branch within, or out of, a WHILE loop, thereby altering the sequence of statement execution.


Example 10-173 Using a WHILE Loop in a Program

In the following program lines, the statements following DO are executed as long as the Boolean expression count LT 10 is TRUE. Within the loop, the code searches for an instance of some condition and, when it finds one, it adds 1 to count. When count reaches 10, the loop ends. The code in the loop must ensure that count will, at some time, reach 10. Otherwise, the loop will never end.

WHILE count LT 10
    ..." (statements)
    IF ....
      count = count + 1