next up previous contents index
Next: General Form of DO Up: Control Statements Previous: Guidelines

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



Mario Storti
Wed Nov 4 19:32:56 ART 1998