DO construct
Standard: F77 F90 F95 F2003 F2008 Example program
<Syntax>
do-stmt
block
end-do
The syntax of the DO statement is as follows.
[ construct-name : ] DO [ label ] [ [ , ] control ]
control | ::= | name = expr1 , expr2 [ , expr3 ] |
| | CONCURRENT concurrent-header | |
| | WHILE ( expr ) |
end-do | ::= | continue-stmt |
| | end-do-stmt |
The syntax of the END DO statement is as follows.
END DO [ construct-name ]
If the DO statement has a construct name, the construct shall end with an END DO statement with the same construct name.
If the loop ends with a continue-stmt, the do-stmt must have a label and this must be the label of the CONTINUE statement.
If control is WHILE (expr), the expr shall be a scalar logical expression. If control is CONCURRENT concurrent-header, the construct is a concurrent DO construct. Otherwise, the name shall be a variable, and it and the expressions shall all be scalar integers.
<Semantics>
A (non-concurrent) DO construct executes the statements in its block repeatedly as follows.
- If control is absent, it will loop forever.
- If control is WHILE( expr ), it will loop while expr has the value true (it is only tested at the beginning of each iteration).
-
Otherwise, it will loop ( expr1 − expr2 ) ÷ expr3 times; if the iteration count is negative, it will loop zero times.
If expr3 is omitted, it has the value 1.
The variable will have the value expr1 on the first iteration, and will have expr3 added to it at the end of each iteration.
The concurrent DO construct executes the iterations in any order, and may execute more than one iteration at the same time. The syntax and semantics of the concurrent DO construct are described here.
<Notes>
Any DO loop can be advanced to the next iteration using a CYCLE statement, or terminated by an EXIT statement. Also, transferring control out of the loop (e.g. by GOTO) terminates the loop.
Nested DO loops can share the same ending statement, provided it is not an END DO statement. This is error-prone and considered obsolescent.
A DO loop can end on an executable statement that is not an END DO or CONTINUE statement. For the purposes of looping, this is treated as if the loop ended with CONTINUE and the executable statement was immediately before it, except that transfers (e.g. GOTO) go to the executable statement not to the implied CONTINUE. This is very error-prone and considered obsolescent.
<Related>
CONTINUE statement, CYCLE statement, DO CONCURRENT construct, DO WHILE statement, END DO statement, EXIT statement, GO TO statement