FORTRAN 77 Language Reference

DO WHILE

The DO WHILE @ statement repeatedly executes a set of statements while the specified condition is true.

DO [ s [,]] WHILE (e)

Parameter 

Description 

s

Label of an executable statement 

e

Logical expression  

Description

Execution proceeds as follows:

  1. The specified expression is evaluated.

  2. If the value of the expression is true, the statements in the range of the DO WHILE loop are executed.

  3. If the value of the expression is false, control is transferred to the statement following the DO WHILE loop.

Terminal Statement

If s is specified, the statement identified by it is called the terminal statement, and it must follow the DO WHILE statement. The terminal statement must not be one of the following statements:

If the terminal statement is a logical IF statement, it can contain any executable statement, except:

If s is not specified, the DO WHILE loop must end with an END DO statement.

DO WHILE Loop Range

The range of a DO WHILE loop consists of all the executable statements that appear following the DO WHILE statement, up to and including the terminal statement.

If a DO WHILE statement appears within the range of another DO WHILE loop, its range must be entirely contained within the range of the outer DO WHILE loop. More than one DO WHILE loop can have the same terminal statement.

If a DO WHILE statement appears within an IF, ELSE IF, or ELSE block, the range of the associated DO WHILE loop must be entirely within that block.

If a block IF statement appears within the range of a DO WHILE loop, the corresponding END IF statement must also appear within the range of that DO WHILE loop.

Terminal Statement Processing

After the terminal statement of a DO WHILE loop is executed, control is transferred back to the corresponding DO WHILE statement.

Restrictions

Jumping into the range of a DO WHILE loop from outside its range can produce unpredictable results.

Comments

The variables used in the e can be modified in any way within the range of the DO WHILE loop.

Examples

Example 1: A DO WHILE without a statement number:


       INTEGER A(4,4), C, R 
       ... 
       C = 4 
       R = 1 
       DO WHILE ( C .GT. R ) 
              A(C,R) = 1 
              C = C - 1 
       END DO 

Example 2: A DO WHILE with a statement number:


       INTEGER A(4,4), C, R 
       ... 
       DO 10 WHILE ( C .NE. R ) 
              A(C,R) = A(C,R) + 1 
       C = C+1 
10     CONTINUE