next up previous contents index
Next: Guidelines Up: DO-Loops Previous: DO-Loops

General Form of DO Statement

The DO statement has two forms:
DO label , variable = start , limit, step
DO label , variable = start , limit
In the second form the step size is implicitly one.

The label marks the final statement of the loop. It must be attached to an executable statement further on in the program unit. The rules permit this statement to be any executable statement except another control statement, but it strongly recommended that you use the CONTINUE statement here. CONTINUE has no other function except to act as a dummy place-marker.

The comma after the label is optional but, as noted in section 1.4, is a useful precaution.

The variable which follows is known as the loop control variable or loop index; it must be a variable (not an array element) but may have integer, real, or double precision type.

The start, limit, and step values may be expressions of any form of integer, real, or double precision type. If the step value is present it must not be zero, of omitted it is taken as one. The number of iterations is computed before the start of the first one, using the formula:

iterations = MAX(INT(0, (limit - start + step) / step))
Note that if the limit value is less than start the iteration count is zero unless step is negative. A zero iteration count is permitted but means that the contents of the loop will not be executed at all and control is transferred to the first statement after the end of the loop. The loop control variable does not necessarily reach the limiting value, especially if the step-size is larger than one.

Statements within the loop are permitted to alter the value of the expressions used for start, limit, or step but this has no effect on the iteration count which is fixed before the first iteration starts.

The loop control variable may be used in expressions but a new value must not be assigned to it within the loop.

DO-loops may contain other DO-loops completely nested within them provided that a different loop control variable is used in each one. Although it is permissible for two different loops to terminate on the same statement, this can be very confusing. It is much better to use a separate CONTINUE statement at the end of each loop. Similarly complete IF-blocks may be nested within DO-loops, and vice-versa.

Other control statements may be used to transfer control out of the range of a DO-loop but it is illegal to try to jump into a loop from outside it. If you exit from a loop prematurely in this way the loop control variable keeps its current value and may be used outside to determine how many loops were actually executed.

After the normal termination of a DO-loop the loop control variable has the value it had on the last iteration plus one extra increment of the step value. Thus with:

 
       DO 1000, NUMBER = 1,100,3 
1000   CONTINUE
On the last iteration NUMBER would be 99, and on exit from the loop NUMBER would be 102. This provision can be useful in the event of exit from a loop because of some error:
 
       PARAMETER (MAXVAL = 100) 
       REAL X(MAXVAL) 
       DO 15, I = 1,MAXVAL 
            READ(UNIT=*, FMT=*, END=90) X(I) 
15     CONTINUE 
90     NVALS = I - 1
The action of the statement labelled 90 is to set NVALS to the number of values actually read from the file whether there was a premature exit because the end-of-file was detected or it reached the end of the array space at MAXVAL.


next up previous contents index
Next: Guidelines Up: DO-Loops Previous: DO-Loops
Helen Rowlands
8/27/1998