next up previous contents index
Next: REWIND and BACKSPACE Statements Up: READ and WRITE Statements Previous: Error and End-of-file Identifiers

Implied DO-loops

  The simplest and most efficient way to read or write all the elements of an array is to put its name, unsubscripted, in the data-transfer list. In the case of a multi-dimensional array the elements will be transferred in the normal storage sequence, with the first subscript varying most rapidly.

An implied-DO loop allows the elements to be transferred selectively or in some non-standard order. The rules for an implied-DO are similar to that of an ordinary DO-loop but the loop forms a single item in the data-transfer list and is enclosed by a pair of parentheses rather than by DO and CONTINUE statements. For example:

 
      READ(UNIT=*, FMT=*) (ARRAY(I), I= IMIN, IMAX) 
      WRITE(UNIT=*, FMT=15) (M, X(M), Y(M), M=1,101,5) 
15    FORMAT(1X, I6, 2F12.3)

A multi-dimensional array can be printed out in a transposed form. The next example outputs an array X(100,5) but with 5 elements across and 100 lines vertically:

 
      WRITE(UNIT=*, FMT=5) (I,I=1,5), 
     $                     ((L,X(L,I),I=1,5),L=1,100) 
5     FORMAT(1X,'LINE', 5I10, 100(/,1X,I4, 5F10.2))

The first loop writes headings for the five columns, then the double loop writes a line-number for each line followed by five array elements. Note that the parentheses have to be matched and that a comma is needed after the inner right-parenthesis since the inner loop is just an item in the list contained in the outer loop.

The implied DO-loop has the general form:
( data-list, loop-variable = start, limit, step )
where the rules for the start, limit, and step values are exactly as in an ordinary DO statement. The loop-variable (normally an integer) may be used within the data-list and this list may, in turn, include further complete implied-DO lists.

If an error or end-of-file condition occurs in an implied DO-loop then the loop-control variable will be undefined on exit; this means that an explicit DO-loop is required to read an indefinite list of data records and exit with knowledge of how many items were actually input.


next up previous contents index
Next: REWIND and BACKSPACE Statements Up: READ and WRITE Statements Previous: Error and End-of-file Identifiers
Helen Rowlands
8/27/1998