DO
Purpose
The DO statement controls the execution of the statements that follow it, up to and including a specified terminal statement. Together, these statements form a DO construct.
Syntax
- DO_construct_name
- is a name that identifies the DO construct.
- stmt_label
- is the statement label of an executable statement appearing after the DO statement in the same scoping unit. This statement denotes the end of the DO construct.
- var_name
- is a scalar variable name of type integer or real, called the DO variable
- a_expr1, a_expr2, and a_expr3
- are each scalar expressions of type integer or real
Rules
If you specify a DO_construct_name on the DO statement, you must terminate the construct with an END DO and the same DO_construct_name. Conversely, if you do not specify a DO_construct_name on the DO statement, and you terminate the DO construct with an END DO statement, you must not have a DO_construct_name on the END DO statement.
If you specify a statement label in the DO statement, you must terminate the DO construct with a statement that is labeled with that statement label. You can terminate a labeled DO statement with an END DO statement that is labeled with that statement label, but you cannot terminate it with an unlabeled END DO statement. If you do not specify a label in the DO statement, you must terminate the DO construct with an END DO statement.
If the control clause (the clause beginning with var_name) is absent, the statement is an infinite DO. The loop will iterate indefinitely until interrupted (for example, by the EXIT statement).
Examples
INTEGER :: SUM=0
OUTER: DO
INNER: DO M=1,10
READ (5,*) J
IF (J.LE.I) THEN
PRINT *, 'VALUE MUST BE GREATER THAN ', I
CYCLE INNER
END IF
SUM=SUM+J
IF (SUM.GT.500) EXIT OUTER
IF (SUM.GT.100) EXIT INNER
END DO INNER
SUM=SUM+I
I=I+10
END DO OUTER
PRINT *, 'SUM =',SUM
END
Related information
- DO construct
- END (Construct), for details on the END DO statement
- EXIT
- CYCLE
- INDEPENDENT
- ASSERT
- CNCALL
- PERMUTATION
- PARALLEL DO/END PARALLEL DO
