DO WHILE

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

Syntax

DO [label[, ] ] WHILE (expr)

label

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

expr

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

Description

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 no label appears in a DO WHILE statement, the DO WHILE loop must be terminated with an END DO statement.

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

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

See Also