



Statements and Lines READ, DO, ELSE
IF, GO TO. Since blanks are ignored, compound keywords can be written
either as one word or two: ELSEIF or ELSE IF (but the latter seems easier to
read).
The rules for statement layout are an unfortunate relic of punched-card days. Every statement must start on a new line and each line is divided into three fixed fields:
Any characters in column 73 or beyond are likely to be ignored (columns 73 to 80 were once used to hold card sequence numbers). This invisible boundary after column 72 demands careful attention as it can have very pernicious effects: it is possible for a statement to be truncated at the boundary but still be syntactically correct, so that the compiler will not detect anything wrong.
Statements do not have to fit on a single line. The initial line of each statement should have a blank in column 6, and all subsequent lines, called continuation lines, must have some character other than blank (or the digit zero) in column 6. Up to 19 continuation lines are allowed, i.e. 20 in total. The column layout needed with continuation lines is illustrated here:
columns 123456789... IF(REPLY .EQ. 'Y' .OR. REPLY .EQ. 'y' .OR. $ REPLY .EQ. 'T' .OR. REPLY .EQ. 't') THEN |
The END statement is an exception to the continuation rule: it may not be
followed by continuation lines and no other statement may have an initial line
which just contains the letters "END". Neither rule causes many problems in
practice.
Programs which make excessive use of continuation lines can be hard to read and to modify: it is generally better, if possible, to divide a long statement into several shorter ones.
Comments form an important part of any computer program even though they are completely ignored by the compiler: their purpose is to help any human who has to read and understand the program (such as the original programmer six months later).
Comments in Fortran always occupy a separate line of text; they are marked by an asterisk in the first column. For example:
*Calculate the atmospheric refraction at PRESS mbar. REF = PRESS * (0.1594 + 1.96E-2 * A + 2E-5 * A**2) *Correct for the temperature T (Celsius) TCOR = (273.0 + T) * (1.0 + 0.505 * A + 8.45E-2 * A**2) |
END
statement (unless another program unit follows, in which case it will form the first
line of the next unit). A completely blank line is also allowed and is treated as a
blank comment. This means that a blank line is not actually permitted after the last
END statement of a program.
There is no limit to the number of consecutive comment lines which may be used; comments may also appear in the middle of a sequence of continuation lines. To conform to the Fortran Standard, comment lines should not be over 72 characters long, but this rule is rarely enforced.
Comments may include characters which are not in the Fortran character set. It helps to distinguish comments from code if they are mainly written in lower-case letters (where available). It is also good practice for comments to precede the statements they describe rather than follow them.
Some systems allow end-of-line comments, usually prefaced by an exclamation mark: this is not permitted by the Fortran standard. For compatibility with Fortran66 comments can also be denoted by the letter C in column 1.
A label can be attached to any statement. There are three reasons for using labels:
DO-loop is specified by a label given in the DO statement;
FORMAT statement must have a label attached as that is how READ
and WRITE statements refer to it;
GO TO statement.
*Read numbers from input file until it ends, add them up. SUM = 0.0 100 READ(UNIT=IN, FMT=200, END=9999) VALUE 200 FORMAT(F20.0) SUM = SUM + VALUE GO TO 100 9999 WRITE(UNIT=*, FMT=*)'SUM of values is', SUM |
A label must be unique within a program unit but labels in different program
units are quite independent. Although any statement may be labelled, it only makes
sense to attach a label to a FORMAT statement or an executable statement, since there
is no way of using a label on any other type of statement.
Statement labels are unsatisfactory because nearly all of them mark a point to which control could be transferred from elsewhere in the program unit. This makes it much harder to understand a program with many labelled statements. Unfortunately at present one cannot avoid using labels altogether in Fortran. If labels are used at all they should appear in ascending order and preferably in steps of 10 or 100 to allow for changes. Labels do not have to be right-justified in the label field.