The DO construct specifies the repeated execution of a statement block. Such a repeated block is called a loop.
The iteration count of a loop can be determined at the beginning of execution of the DO construct, unless it is indefinite.
You can curtail a specific iteration with the CYCLE statement, and the EXIT statement terminates the loop.
>>-DO_statement------------------------------------------------>< >>-statement_block--------------------------------------------->< >>-+-END_DO_statement---+-------------------------------------->< '-terminal_statement-' |
If you specify a DO construct name on the DO statement, you must terminate the construct with an END DO statement with the same 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.
The terminal statement must follow the DO statement and must be executable. See Chapter 10, Statements for a listing of statements that can be used as the terminal statement. If the terminal statement of a DO construct is a logical IF statement, it can contain any executable statement except those statements to which the restrictions on the logical IF statement apply.
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.
Nested, labeled DO and DO WHILE constructs can share the same terminal statement if the terminal statement is labeled, and if it is not an END DO statement.
The range of a DO construct consists of all the executable statements following the DO statement, up to and including the terminal statement. In addition to the rules governing the range of constructs, you can only transfer control to a shared terminal statement from the innermost sharing DO construct.
A DO construct is either active or inactive. Initially inactive, a DO construct becomes active only when its DO statement is executed. Once active, the DO construct becomes inactive only when:
When a DO construct becomes inactive, the DO variable retains the last value assigned to it.
An infinite DO loops indefinitely.
If the loop is not an infinite DO, the DO statement includes an initial parameter, a terminal parameter, and an optional increment.
MAX (INT ( (m2 - m1 + m3) / m3), 0)
Note that the iteration count is 0 whenever:
m1 > m2 and m3 > 0, or
m1 < m2 and m3 < 0
The iteration count cannot be calculated if the DO variable is missing. This is referred to as an infinite DO construct.
+-------------------------------IBM Extension--------------------------------+
The iteration count cannot exceed 2**31 - 1 for integer variables of kind 1, 2, or 4, and cannot exceed 2**63 - 1 for integer variables of kind 8. The count becomes undefined if an overflow or underflow situation arises during the calculation.
+----------------------------End of IBM Extension----------------------------+
At the completion of the DO statement, loop control processing begins.
Loop control processing determines if further execution of the range of the DO construct is required. The iteration count is tested. If the count is not zero, the first statement in the range of the DO construct begins execution. If the iteration count is zero, the DO construct becomes inactive. If, as a result, all of the DO constructs sharing the terminal statement of this DO construct are inactive, normal execution continues with the execution of the next executable statement following the terminal statement. However, if some of the DO constructs sharing the terminal statement are active, execution continues with incrementation processing of the innermost active DO construct.
Statements that are part of the statement block are in the range of the DO construct. They are executed until the terminal statement is reached. Except by incrementation processing, you cannot redefine the DO variable, nor can it become undefined during execution of the range of the DO construct.
Execution of the terminal statement occurs as a result of the normal execution sequence, or as a result of transfer of control, subject to the restriction that you cannot transfer control into the range of a DO construct from outside the range. Unless execution of the terminal statement results in a transfer of control, execution continues with incrementation processing.
Migration Tip:
FORTRAN 77 source I = 0 J = 0 20 CONTINUE I = I + 1 J = J + 1 PRINT *, I IF (I.GT.4) GOTO 10 ! Exiting loop IF (J.GT.3) GOTO 20 ! Iterate loop immediately I = I + 2 GOTO 20 10 CONTINUE END Fortran 90 or Fortran 95 source I = 0 ; J = 0 DO I = I + 1 J = J + 1 PRINT *, I IF (I.GT.4) EXIT IF (J.GT.3) CYCLE I = I + 2 END DO END |
INTEGER :: SUM=0 OUTER: DO INNER: DO 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