The DO statement repeatedly executes a set of statements.
DO s [,] loop-control
or
DO loop-control @
where s is a statement number. The form of loop-control is
Parameter |
Description |
variable |
Variable of type integer, real, or double precision. |
e1, e2, e3 |
Expressions of type integer, real or double precision, specifying initial, limit, and increment values respectively. |
The DO statement contains the following constructs.
A labeled DO loop consists of the following:
DO statement
Set of executable statements called a block
Terminal statement, usually a CONTINUE statement
The statement identified by s is called the terminal statement. It must follow the DO statement in the sequence of statements within the same program unit as the DO statement.
The terminal statement should not be one of the following statements:
Unconditional GO TO
Assigned GO TO
Arithmetic IF
Block IF/ELSE IF
ELSE
END IF
RETURN
STOP
END DO
If the terminal statement is a logical IF statement, it can contain any executable statement, except:
DO/DO WHILE
Block IF/ELSE IF
ELSE IF
ELSE
END IF
END
Logical IF
The range of a DO loop consists of all of the executable statements that appear following the DO statement, up to and including the terminal statement.
If a DO statement appears within the range of another DO loop, its range must be entirely contained within the range of the outer DO loop. More than one labeled DO loop can have the same terminal statement.
If a DO statement appears within an IF, ELSE IF, or ELSE block, the range of the associated DO loop must be contained entirely within that block.
If a block IF statement appears within the range of a DO loop, the corresponding END IF statement must also appear within the range of that DO loop.
A block DO loop consists of:
DO statement
Set of executable statements called a block
Terminal statement, an END DO statement
This loop is nonstandard.
Execution proceeds as follows:
The expressions e1, e2, and e3 are evaluated. If e3 is not present, its value is assumed to be one.
The DO variable is initialized with the value of e1.
The iteration count is established as the value of the expression:
MAX (INT ((e2 - e1 + e3)/),e3 0)
The iteration count is zero if either of the following is true:
e1 > e2 and e3 > zero.
e1 < e2 and e3 < zero.
If the -onetrip compile time option is specified, then the iteration count is never less than one.
The iteration count is tested, and, if it is greater than zero, the range of the DO loop is executed.
After the terminal statement of a DO loop is executed, the following steps are performed:
The value of the DO variable, if any, is incremented by the value of e3 that was computed when the DO statement was executed.
The iteration count is decreased by one.
The iteration count is tested, and if it is greater than zero, the statements in the range of the DO loop are executed again.
The DO variable must not be modified in any way within the range of the DO loop.
Control must not jump into the range of a DO loop from outside its range.
In some cases, the DO variable can overflow as a result of an increment that is performed prior to testing it against the final value. When this happens, your program has an error, and neither the compiler nor the runtime system detects it. In this situation, though the DO variable wraps around, the loop can terminate properly.
If there is a jump into the range of a DO loop from outside its range, a warning is issued, but execution continues anyway.
When the jump is from outside to the terminal statement that is CONTINUE, and this statement is the terminal statement of several nested DO loops, then the most inner DO loop is always executed.
N = 0 DO 210 I = 1, 10 J = I DO 200 K = 5, 1 L = K N = N + 1 200 CONTINUE 210 CONTINUE WRITE(*,*)'I =',I, ', J =',J, ', K =',K, & ', N =',N, ', L =',L END demo% f77 -silent DoNest1.f "DoNest1.f", line 4: Warning: DO range never executed demo% a.out I = 11, J = 10, K = 5, N = 0, L = 0 demo%
The inner loop is not executed, and at the WRITE, L is undefined. Here L is shown as 0, but that is implementation-dependent; do not rely on it.
Example 2: The program DoNest2.f (DO variable always defined):
INTEGER COUNT, OUTER COUNT = 0 DO OUTER = 1, 5 NOUT = OUTER DO INNER = 1, 3 NIN = INNER COUNT = COUNT+1 END DO END DO WRITE(*,*) OUTER, NOUT, INNER, NIN, COUNT END
6 5 4 3 15