SuivantPrec.Bas prec.BasNiv. sup.

8.3 DO-Loops 

The DO statement controls a block of statements which are executed repeatedly, once for each value of a variable called the loop-control variable. The number of iterations depends on the parameters of the DO statement at the heads of the loop. The first item after the keyword "DO" is the label which is attached to the last statement of the loop. For example:
 *Sum the squares of the first N elements of the array X
       SUM = 0.0
       DO 15, I = 1,N
           SUM = SUM + X(I)**2
 15    CONTINUE
If we had wanted only to sum alternate elements of the array we could have used a statement like:       DO 15,I = 1,N,2 and then the value of I in successive loops would have been 1, 3, 5, etc. The final value would be N if N were odd, or only to N-1 if N were even. If the third parameter is omitted the step-size is one; if it is negative then the steps go downwards. For example
       DO 100,I = 5,1,-1
           WRITE(UNIT=*,FMT=*) I**2
 100   CONTINUE
will produce 5 records containing the values 25, 16, 9, 4, and 1 respectively.

Loops can be nested to any reasonable depth. Thus the following statements will set the two dimensional array FIELD to zero.
       REAL FIELD(NX, NY)
       DO 50, IY = 1,NY
          DO 40, IX = 1.NX
              FIELD(IX,IY) = 0.0
 40       CONTINUE
 50    CONTINUE

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.

Guidelines 

If you a loop-control variable of any type other than integer there is a risk that rounding errors will accumulate as it is incremented repeatedly. In addition, if the expressions for the start, limit, and step values are not of integer type the number of iterations may not be what you expect because the formula uses the INT function (not NINT). None of these problems can occur if integer quantities are used throughout the DO statement.

SuivantPrec.Bas prec.HautNiv. sup.