The FORDO feature allows easy coding of a repeated operation in which the number of repetitions is known in advance. If the criterion for repetition is more complicated, WHILEDO can often be used. Its syntax is:

WHILE boolean expression DO statement

The WHILEDO controls the single statement following DO. If several statements are to be repeated, as is almost always the case, they must be grouped using the <<>> or BEGINEND as in the example below.

The WHILE condition is tested each time before the action following the DO is attempted. If the condition is false to begin with, the action is not performed at all. Make sure that what is to be tested has an appropriate value initially.


Suppose we want to add up a series of terms, generated one by one, until we reach a term which is less than 1/1000 in value. For our simple example, let us suppose the first term equals 1 and each term is obtained from the one before by taking one third of it and adding one third its square. We would write:

        ex:=0; term:=1;  
        while num(term - 1/1000) >= 0  do  
                <<ex := ex+term; term:=(term + term^2)/3>>;  

As long as TERM is greater than or equal to (>=) 1/1000 it will be added to EX and the next TERM calculated. As soon as TERM becomes less than 1/1000 the WHILE test fails and the TERM will not be added.