Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 7/13/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

DO WHILE

Statement: Executes the range of a DO construct while a specified condition remains true.

[name:] DO [label[, ]] WHILE (expr)

   block

[END DO [name]]

name

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

label

(Optional) Is a label specifying an executable statement in the same program unit.

expr

Is a scalar logical (test) expression enclosed in parentheses.

block

Is a sequence of zero or more statements or constructs that make up the DO range.

Description

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

Before each execution of the DO range, the logical expression is evaluated. If it is true, the statements in the body of the loop are executed. If it is false, the DO construct terminates and control transfers to the statement following the loop.

If END DO is specified, it terminates the construct. If END DO is not specified, when all of the iterations have completed execution, the loop terminates, and the DO construct becomes inactive.

If no label appears in a DO WHILE statement, the DO WHILE loop must be terminated with an END DO statement. See the description of the DO statement for the semantics of labeled and block forms of DO loops.

You can transfer control out of a DO WHILE loop but not into a loop from elsewhere in the program.

Terminating a DO WHILE loop with an executable statement other than a DO WHILE or a CONTINUE statement is a deleted feature in the Fortran Standard. Intel® Fortran fully supports features deleted in the Fortran Standard.

The labeled form of a DO WHILE loop is an obsolescent feature in the Fortran Standard.

Example

The following example shows a DO WHILE statement:

  CHARACTER*132 LINE
  ...
  I = 1
  DO WHILE (LINE(I:I) .EQ. ' ')
    I = I + 1
  END DO

The following examples show required and optional END DO statements:

  Required                 Optional
  DO WHILE (I .GT. J)         DO 10 WHILE (I .GT. J)
    ARRAY(I,J) = 1.0            ARRAY(I,J) = 1.0
    I = I - 1                   I = I - 1
  END DO                   10 END DO

The following shows another example:

  CHARACTER(1) input
  input = ' '
  DO WHILE ((input .NE. 'n') .AND. (input .NE. 'y'))
     WRITE (*, '(A)') 'Enter y or n: '
     READ (*, '(A)') input
  END DO