DO
variable = start TO stop <BY increment> ;
The iterative DO statement executes a group of statements several times.
The arguments to the DO statement are as follows:
is the name of a variable that indexes the loop. This variable is sometimes called an index variable or a looping variable.
is the starting value for variable.
is the stopping value for variable.
is an increment value.
The start, stop, and increment values should be scalars or expressions that yield scalars.
When the DO group has this form, the statements between the DO and END statements are executed iteratively. The number of times the statements are executed depends on the evaluation of the expressions given in the DO statement.
The index variable starts with the start value and is incremented by the increment value after each iteration. The iterations continue provided that the index variable is less than or equal to the stop value. If a negative increment is used, then iterations continue provided that the index variable is greater than or equal to the stop value. The start, stop, and increment expressions are evaluated only once before the looping starts.
For example, the following statements print the value of i
three times, as shown in Figure 23.96:
do i = 1 to 5 by 2; print "The value of i is:" i; end;
Figure 23.96: Aritmetic Sequences
i | |
---|---|
The value of i is: | 1 |
i | |
---|---|
The value of i is: | 3 |
i | |
---|---|
The value of i is: | 5 |