5.4.1.1 DO Iteration Control

After each iteration of the DO range, the following steps occur:

  1. The value of the increment parameter (e3) is algebraically added to the control variable (v).

  2. The iteration count is decremented as follows:
    [(e2-e1+e3)/e3] - n
    

    where n is the number of the times the loop has executed so far.

  3. The iteration count is evaluated and one of the following actions is taken:

If the control variable has a real data type, the number of iterations of the DO range may not be what you expect because of rounding errors.

You can also terminate execution of a DO statement by using a statement within the range that transfers control outside the loop. The control variable of the DO statement remains defined with its current value.

When execution of a DO loop terminates and other DO loops share its terminal statement, control transfers to the next outermost DO loop in the DO nesting structure (see Section 5.4.1.2). If no other DO loop shares the terminal statement or if the DO statement is outermost, control transfers to the first executable statement after the terminal statement.

You cannot alter the value of the control variable within the range of the DO statement. However, you can use the control variable for reference as a variable within the range.

You can modify the initial, terminal, and increment parameters within the loop without affecting the iteration count.

The range of a DO statement can contain other DO statements, as long as these nested DO loops meet certain requirements. Section 5.4.1.2 describes these requirements.

You can transfer control out of a DO loop, but not into a loop from elsewhere in the program. Exceptions to this rule are described in Sections 5.4.1.3 and 5.4.1.4.

Examples

The following examples show valid and invalid DO iteration control:

Valid

The following statement specifies 25 iterations: K=49 during the final iteration, K=51 after the loop.

DO 100 K=1,50,2

The following statement specifies 27 iterations: J=-2 during the final iteration, J=-4 after the loop.

DO 350 J=50,-2,-2

The following statement specifies 5 iterations: IVAR=5 during the final iteration, IVAR=6 after the loop.

DO 25 IVAR=1,5

The following statement specifies 9 iterations: NUMBER=37 during the final iteration, NUMBER=41 after the loop. The terminating statement of this DO loop must be END DO.

DO NUMBER=5,40,4

Invalid

The following statement shows how a common typing error can cause errors with DO loops-a decimal point is typed in place of a comma. In effect, this statement assigns 2.10 to the real variable DO40M.

DO 40 M=2.10


Previous Page Next Page Table of Contents