DO

Statement: Marks the beginning of a DO construct. The DO construct controls the repeated execution of a block of statements or constructs. (This repeated execution is called a loop.)

A DO construct takes one of the following forms:

Syntax

Block Form:

[name:] DO [label[, ] ] [loop-control]
     block
[label] term-stmt

Nonblock Form:

DO label[,] [loop-control]

name
(Optional) Is the name of the DO construct.

label
(Optional) Is a statement label identifying the terminal statement.

loop-control
Is a DO iteration (see Iteration Loop Control) or a DO WHILE statement.

block
Is a sequence of zero or more statements or constructs.

term-stmt
Is the terminal statement for the construct.

Rules and Behavior

A block DO construct is terminated by an END DO or CONTINUE statement. If the block DO statement contains a label, the terminal statement must be identified with the same label. If no label appears, the terminal statement must be an END DO statement.

If a construct name is specified in a block DO statement, the same name must appear in the terminal END DO statement. If no construct name is specified in the block DO statement, no name can appear in the terminal END DO statement.

A nonblock DO construct is terminated by an executable statement (or construct) that is identified by the label specified in the nonblock DO statement. A nonblock DO construct can share a terminal statement with another nonblock DO construct. A block DO construct cannot share a terminal statement.

The following cannot be terminal statements for nonblock DO constructs:

The nonblock DO construct is an obsolescent feature in Fortran 95 and Fortran 90.

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: CONTINUE, CYCLE, EXIT, DO WHILE, Execution Control, DO Constructs

Examples

The following example shows a simple block DO construct (contains no iteration count or DO WHILE statement):

  DO
    READ *, N
    IF (N == 0) STOP
    CALL SUBN
  END DO

The DO block executes repeatedly until the value of zero is read. Then the DO construct terminates.

The following example shows a named block DO construct:

  LOOP_1: DO I = 1, N
            A(I) = C * B(I)
          END DO LOOP_1

The following example shows a nonblock DO construct with a shared terminal statement:

     DO 20 I = 1, N
     DO 20 J = 1 + I, N
  20 RESULT(I,J) = 1.0 / REAL(I + J)

The following two program fragments are also examples of DO statements:

  C   Initialize the even elements of a 20-element real array
  C
      DIMENSION array(20)
      DO j = 2, 20, 2
        array(j) = 12.0
      END DO
  C
  C   Perform a function 11 times
  C
      DO k = -30, -60, -3
        int = j / 3
        isb = -9 - k
        array(isb) = MyFunc (int)
      END DO

The following shows the final value of a DO variable (in this case 11):

    DO j = 1, 10
      WRITE (*, '(I5)') j
    END DO
    WRITE (*, '(I5)') j