XL Fortran for AIX 8.1

Language Reference


DO Construct

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-'
 
 

DO_statement
See DO for syntax details

END_DO_statement
See END (Construct) for syntax details

terminal_statement
is a statement that terminates the DO construct. See the description below.

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

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.

Range of a DO Construct

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.

Active and Inactive DO Constructs

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.

Executing a DO Statement

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.

  1. The initial parameter, m1, the terminal parameter, m2, and the increment, m3, are established by evaluating the DO statement expressions (a_expr1, a_expr2, and a_expr3, respectively). Evaluation includes, if necessary, conversion to the type of the DO variable according to the rules for arithmetic conversion. (See Arithmetic Conversion.) If you do not specify a_expr3, m3 has a value of 1. m3 must not have a value of zero.
  2. The DO variable becomes defined with the value of the initial parameter (m1).
  3. The iteration count is established, determined by the expression:
    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

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.

Execution of the Range

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.

Terminal Statement Execution

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.

Incrementation Processing

  1. The DO variable, the iteration count, and the increment of the active DO construct whose DO statement was most recently executed, are selected for processing.
  2. The value of the DO variable is increased by the value of m3.
  3. The iteration count is decreased by 1.
  4. Execution continues with loop control processing of the same DO construct whose iteration count was decremented.



Migration Tip:


Use EXIT, CYCLE, and infinite DO
statements instead of a GOTO statement.


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

Examples
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


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]