Iterative DO Statement

Description

The iterative DO statement identifies a variable whose value controls the execution of the DO-group and defines the conditions under which the control variable is to be modified and tested.

Execution of an iterative DO causes the start, finish, and increment expressions, as well as the index reference, to be evaluated in an unspecified order. Once evaluated, the finish and increment values are used throughout execution of the group and are not reevaluated. The subscripts, pointers, and so on, in the index reference are also not reevaluated.

The start value is converted, if necessary, to the data type of the index variable, and the converted value is assigned to the index variable.

If a WHILE option is given, the test-expression is evaluated to produce a value b that must be a bit-string of length greater than or equal to one. (See the section DO WHILE and DO UNTIL Statements.) If b is false, execution resumes with the statement following the END statement.

If b is true or a WHILE option is not given, the value of the index variable is compared to the finish value. If the increment is positive or zero, and if the index value is greater than the finish value, execution resumes with the statement following the END statement.

If the increment is negative and the index value is less than the finish value, execution resumes with the statement following the END statement.

If none of the above conditions caused execution of the group to end, the statements of the group are executed. If an UNTIL option is given, the test-expression is evaluated to produce a value b that must be a bit string of length 1. If b is true, execution resumes with the statement following the END statement, the increment value is added to the index variable, and any WHILE test is reevaluated. The new values of b and index are then used to determine if the group should be executed again.

The TO and BY options may be given in any order followed by the WHILE or UNTIL options. Either of the TO or the BY options may be omitted. If the BY option is omitted, a default increment of one is supplied. If the TO option is omitted, index is not compared against limit and the group repeats indefinitely unless stopped by the WHILE or UNTIL option. If both the TO option and the WHILE option are omitted, the DO-group repeats indefinitely. Both the WHILE and UNTIL options may be specified.

Example

DO K = 1 TO 10;
   .
   .  /* EXECUTES 10 TIMES */ 
   .
END;

DO K = 10 TO 1 BY -1;
   .
   .  /* EXECUTES 10 TIMES */
   . 
END;

DO K = 1 TO 1 BY -1;
   .
   .  /* EXECUTES 1 TIME */ 
   .
END;

DO K = 1 TO 10 WHILE(B<0);
   .
   .
   .
END;

In this last example, the DO-group executes 10 times or until B ≥ 0, whichever comes first.

DO A(J) = B+1 TO N BY -M WHILE(T);
   .
   .
   .
END;

In the previous example, the DO-group executes [B-N+2/M] times or until T becomes false, whichever comes first. All executions of the DO-group use the same element of A as the index, even if J has been altered by the execution of the group. These executions also all use the initial values of B+1, N and -M. However, T is repeatedly evaluated.