Language Reference


DO Statement with a WHILE Clause

  • DO WHILE (expression);

  • DO variable = start TO stop <BY increment> WHILE(expression);

The DO WHILE statement executes statements iteratively while a condition is true.

The arguments to the DO WHILE statement are as follows:

expression

is an expression that is evaluated at the top of the loop for being true or false.

variable

is the name of a variable that indexes the loop.

start

is the starting value for the looping variable.

stop

is the stopping value for the looping variable.

increment

is an increment value.

Using a WHILE expression enables you to conditionally execute a set of statements iteratively. The WHILE expression is evaluated at the top of the loop, and the statements inside the loop are executed repeatedly as long as the expression yields a nonzero or nonmissing value.

Note that the incrementing is done before the WHILE expression is tested. The following example demonstrates the incrementing:

x = 1;
do while (x<100);
   x = x + 1;
end;
print x;

Figure 25.111: Result of a DO-WHILE Statement

x
100



The next example increments the starting value by 2:

y = 1;
do i = 1 to 100 by 2 while(y<200);
   y = y # i;
end;
print i y;

Figure 25.112: Result of an Iterative DO-WHILE Statement

i y
11 945