while Instruction

The while instruction constructs a loop subject to a terminating condition.

Syntax

The syntax of the while instruction is:

while condition do [:label]
    [while-instructions]
endwhile [label];

Description

The condition is an expression that must produce a value of type Boolean. The condition is evaluated repeatedly.

While the condition remains true, the optional while-instructions are executed following the evaluation of the condition. The loop terminates when the value of the condition is evaluated as false.

If the condition is initially false, the optional while-instructions are not executed. (For details about evaluating a condition once at the start of a loop, see "Iterating over a Range of Numeric Values", under "foreach Instruction", later in this chapter.)

You can nest while instructions within each other to any level. The break instruction can be used to terminate a while loop prematurely or the continue instruction can be used to cause the next iteration of a while loop to begin.

The optional :label identifier is used to nominate the specific loop (within a group of nested loops) that may be terminated or continued by a break or continue instruction, respectively. When the break or continue instruction is used without a label, only the innermost loop is terminated or continued. For more details, see "break Instruction" or "continue Instruction", later in this chapter.

Example

The following example shows the use of the while instruction.

writeCustomers(customerArray: CustomerArray);
vars
    count : Integer;
    cust  : Customer;
begin
    count := 1;
    while count <= customerArray.size do
        cust  := customerArray[count];
        write "Customer " & count.String & ":" & cust.name;
        count := count + 1;
    endwhile;
end;