Loops/Continue: Difference between revisions

→‎{{header|Fortran}}: Thanks to I0 format...
(→‎{{header|Fortran}}: Thanks to I0 format...)
Line 594:
C5001 FORMAT (I3, ',', ADVANCE='NO')
END</lang>
 
===Relying instead upon the looping features of FORMAT===
For historical reasons, 6 is often the default unit number for standard output.
<lang Fortran>
WRITE (6,1) (I,I = 1,10)
1 FORMAT (4(1X,I0,","),1X,I0)
END
</lang>
Here the break and continuation comes through the workings of the FORMAT interpreter. The feature 4(etc) means four repetitions of the format items within the brackets, and as each datum from the WRITE statement arrives, it is aligned with the next format item that can receive a datum, the I-format specifier (here I0, which means an integer of only as many digits as are needed for the value) and until such a reciever is encountered, intervening format items are acted upon - 1X means "one space", and the quotes surround a text literal. Accordingly, the first datum generates a space, a one-digit value, and a comma, as does the second and so on. When the sixth datum is received, the end of the format statement has been reached, and the convention is to write the current line and start a new line of output, and further, go back in the FORMAT specification to the first-encountered open-bracket symbol (the rightmost) which in this case is not the beginning of the FORMAT statement but the one that has a repetition count of four in front of it, and, resume interpretation. When the last datum has been accepted, naturally, the line is printed.
 
An alternative might be FORMAT (4(I2,","),I2) but that would generate
1, 2, 3, 4, 5
6, 7, 8, 9,10
 
This sort of scheme facilitates a compact way of printing a table with a heading, where the WRITE statement simply pours forth the data and relies on something like FORMAT("heading",/,(complex details for one line)) - thus printing the table line-by-line with only the first line having the heading.
 
=={{header|GAP}}==
1,220

edits