Program Execution Controls

FORTRAN-80

4.2 Loop Control Statements

Frequently, a series of operations must be repeated several times (for example, reading a series of entries from an input device and extracting information selective- ly). Rather than repeat the statements to perform these operations for each entry, one can create a loop that causes the same statements to be performed over and over until all entries have been read and processed. This is the function of the DO state- ment.

The CONTINUE statement described in this section is ordinarily used with the DO statement, though it is not limited to this use.

4.2.1 Operation of a DO Loop

The first statement of a DO loop is the DO statement itself. The last statement is a labeled statement whose label is specified in the DO statement. These two statements define the range of the DO loop. The statements making up the body of the DO loop are executed a specific number of times, as defined in the DO statement.

The DO statement includes three values: an initial loop index value, a loop termina- tion value, and an amount by which the initial value is to be incremented or decremented. Each time the loop is performed, a 'DO variable,' previously initializ- ed to the initial index value, is increased or decreased by the incrementl decrement value until the loop termination value is reached. Program execution then continues with the statement following the last statement of the DO loop.

This sequence describes the most common operation of a DO loop. A DO loop can also terminate operation as the result of a RETURN statement executed within the loop, a transfer of control outside the loop, execution of a STOP statement in the program, or program termination for any other reason.

Program control cannot be transferred into a DO loop.

4.2.2 DO Statement

The format of the DO statement is

DO sfl [,] var =e1, e2 [,e3]

where

stl is the statement label of an executable statement and is the label of the last statement in the DO loop.

var is the name of an integer variable, called the 'DO variable.'

eis an integer expression.

In this format, 'eI' is the initial loop index value, 'e2' is the loop termination value, and 'e3' is the loop incrementl decrement amount. If 'e3' is not specified, an incre- ment of one is assumed. The values of 'eI' and 'e2' can be specified such that no iterations are performed (but see the discussion of the 0077/0066 compiler con- trols in section F.2.5).

The last statement of a DO loop, statement 'stl,' must not be an unconditional GO TO, assigned GO TO, arithmetic IF, block IF, ELSE IF, ELSE, END IF, RETURN, STOP, END, or DO statement. If the last statement of the DO loop is a logical IF statement, it can contain any executable statement except a DO, block IF, ELSE IF, ELSE, END IF, END, or another logical IF statement.

4-6

Page 46
Image 46
Intel fortran-80 manual Loop Control Statements, Operation of a do Loop, Do Statement