HP 3000 Manuals

Using the Implied DO Loop [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

Using the Implied DO Loop 

The implied DO loop is used with the READ, WRITE, and PRINT statements.
An implied DO loop contains a list of data elements to be read or
written, and a set of indexing parameters.  Following is an implied DO
loop:

     PRINT *, (apple, i = 1,3)

where apple is the index parameter and i is the data element.

The statement above prints the value of apple three times.  If apple is
initialized to 35.6, the output would look like this:

     35.6 35.6 35.6

If the list of an implied DO loop contains several variables, each of the
variables in the list is input or output for each pass through the loop.
For example, the statement:

     READ *, (a,b,c, j = 1,2)

is equivalent to the list-directed statement:

     READ *, a, b, c, a, b, c

An implied DO loop is often used to input or output arrays and array
elements.  For example, the statements:

     READ b(10)
     PRINT *, (b(i), i=1,10)

result in the array b written in the following order:

     b(1)  b(2)  b(3)  b(4)  b(5)  b(6)  b(7)  b(8)  b(9)  b(10)

If an unsubscripted array name is used in this list, the entire array is
transmitted.  For example, the statements:

     READ x(3)
     PRINT *, (x, i= 1,2)

write the elements of array x two times as follows:

     x(1)  x(2)  x(3)  x(1)  x(2)  x(3)

On output, the list can contain expressions that use the index value.
For example, the statements:

     READ a(10)
     PRINT *, (i*2, a(i*2), i= 1,5)

write the numbers 2, 4, 6, 8, 10, alternating with array elements a(2),
a(4), a(6), a(8), a(10).

Implied DO loops are useful for controlling the order in which arrays are
output.  You can output an array in column-major or row-major order.
Suppose you have the following program:

     PROGRAM implieddo
     INTEGER a1(2,3)
     DATA a1 /1, 2, 3, 4, 5, 6/
     WRITE (6, '(1X, 3I2)') a1
     WRITE (6, '(1X, 3I2)') ((a1(i,j), j = 1,3), i=1,2)
     END

The statement:

     WRITE (6, '(1X, 3I2)') a1

writes the array elements in column-major order, like this:

     1 2 3
     4 5 6

The statement:

     WRITE (6, '(1X, 3I2)') ((a1(i,j), j = 1,3), i=1,2)

writes the array in row-major order, like this:

     1 3 5
     2 4 6

Because FORTRAN stores arrays in column-major order, these two statements
produce the same result:

     WRITE (6, '(1X, 3I2)') ((array(i,j),i = 1,2), j = 1,3)

     WRITE (6, '(1X, 3I2)') array

The following program initializes a 10 by 10-element array as an identity
matrix.  An identity matrix has a diagonal of ones and the rest of the
array is filled with zeros.  The program uses a WRITE statment with an
implied DO loop to output the array in row-major order.

     PROGRAM array

     INTEGER id_array(10,10)
     DATA ((id_array(i,j), j = i+1,10), i=1,9) /45*0/ ! upper
     DATA (id_array(i,i), i=1,10)             /10*1/  ! diagonal
     DATA (id_array(i,j), i = j+1,10), j=1,9) /45*0/  ! lower
     WRITE(6,'(1X, 10I2)') ((id_array(i,j), j = 1,10), i=1,10)

     END

The program produces this output:

     1 0 0 0 0 0 0 0 0 0
     0 1 0 0 0 0 0 0 0 0
     0 0 1 0 0 0 0 0 0 0
     0 0 0 1 0 0 0 0 0 0
     0 0 0 0 1 0 0 0 0 0
     0 0 0 0 0 1 0 0 0 0
     0 0 0 0 0 0 1 0 0 0
     0 0 0 0 0 0 0 1 0 0
     0 0 0 0 0 0 0 0 1 0
     0 0 0 0 0 0 0 0 0 1

Implied DO loops for input or output are not just used with arrays.  The
following program prints a table of degrees and the sine of each, in
steps of 10 degrees.

           PROGRAM sine

           WRITE (6,100) (d, SIN(d*3.14159/180.), d=0,360,10)
      100  FORMAT (1X, F4.0, F9.5)

           END

The program produces the following output:

       0.     .00000
      10.     .17365
      20.     .34202
      30.     .50000
      40.     .64279
      50.     .76604
      60.     .86602
      70.     .93969
      80.     .98481
      90.    1.00000
     100.     .98481
     110.     .93969
     120.     .86603
     130.     .76605
     140.     .64279
     150.     .50000
     160.     .34202
     170.     .17365
     180.     .00000
     190.    -.17365
     200.    -.34202
     210.    -.50000
     220.    -.64279
     230.    -.76604
     240.    -.86602
     250.    -.93969
     260.    -.98481
     270.   -1.00000
     280.    -.98481
     290.    -.93969
     300.    -.86603
     310.    -.76605
     320.    -.64279
     330.    -.50000
     340.    -.34202
     350.    -.17365
     360.    -.00001



MPE/iX 5.0 Documentation