READ(20,101) A,I
101 FORMAT(F10.5,I5)
Assume input file FOR020.DAT (version 1) contains:
bbbb12bbbbbb3bb
(where b indicates an invisible blank).
OUTPUT:
A <== _______
I <== _______
NOTE: The field descriptor F10.5 specifies that the first 5
input characters are to the LEFT of the decimal point and the next
5 (for a total of 10) are to the RIGHT. Thus, a decimal point will
be "assumed" to exist between 1 and 2 in the input stream.
Assume input file FOR020.DAT (version 2) contains:
bbbbbbb1.23b3b23
(where b indicates an invisible blank).
OUTPUT:
A <== _______
I <== _______
If there are multiple READ statements, each statement
will begin reading data on a NEW INPUT LINE, ignoring
whatever has been unread on the previous line.
F77 F90/95 C++
.EQ. ==
.LT. <
.LE. <=
.GT. >
.GE. >=
.NE. /= !=
Logical Constants (surrounding periods are necessary!)
.TRUE. .FALSE.
Other Operators
Fortran C++
.AND. &&
.OR. ||
.NOT. !
As in C++, logical expressions MUST be surrounded by
parentheses.
IF (A < B) THEN
...
ELSE
...
END IF
NOTE that the concluding END IF can be spelled as either one word or two
on most systems.
The ELSE clause may be omitted, for example,
IF (A == B) THEN
...
END IF
One can include several ELSE clauses in one IF if one uses the
following form in which an ELSE IF is placed on a single line:
IF (A >= B) THEN
...
ELSE IF (A > C) THEN
...
ELSE
...
END IF
IF (A <= 0 ) A = -A
This can be used in Fortran-90/95/2003 to EXIT out of an infinite loop (see
later).
There are two "antique" IFs still permitted,
IF ( arith_expression ) n1, n2, n3
The action is as follows:
One should know what this statement does, in order to read older code, but try to avoid it in writing new code.
IF (2 ..GE.. 1) THEN
What happens? (Is it an error?)
DO I = 1, 20
...
END DO
In this example, the loop variable I starts at the value of 1
and finishes with the value of 20. To use a "stride" or "step," one adds
another comma and number, for example:
DO I = 1, 23, 2
...
END DO
I starts at the value of 1 and increases by increments of 2 to 23.
(To count "down," one uses a stride of -1.)
The older FORTRAN-77 style explicitly includes the statement number of the final statement of the DO loop (cf. EF 6.5). The final statement was often the CONTINUE statement (cf. EF 6.4), a statement which does absolutely nothing (similar to an END DO statement). As an example (cf. EF 6.7):
DO 20 I = 1, 20
...
20 CONTINUE
Any legal statement number could be used to indicate the last
statement of the loop. Do not use the older style when a
compiler accepts the Fortran-90/95/2003 style of DO loops!
If you need to nest DO loops, and if you need to use the FORTRAN-77 style, be aware that the loops MUST be properly nested (cf. EF 6.9). The following example is WRONG and would lead to a compiler error.
DO 10 I=1,20
DO 20 J=1,10
...
10 CONTINUE
20 CONTINUE
PROGRAM TEST2
IMPLICIT NONE
INTEGER NUM
REAL MILES, KILOS
WRITE(6,100)
100 FORMAT(7X, 'Miles', 3X, 'Kilometers')
DO NUM = 5,60,5
MILES = NUM
KILOS = MILES*8.0/5.0
WRITE(6,'(2F10.1)') MILES, KILOS
END DO
STOP
END
DO
...
END DO
forces an infinite loop. This is not too useful in itself, but can be
helpful if used with an EXIT clause [EF 6.11.2.1].
The EXIT statement is used with a one-line IF and forces a termination of a loop. Its form is exemplified as follows:
IF ( .NOT. (TOT <= 100) ) EXIT
This would end the looping and continue the program with the statement
after the END DO.
We can use CYCLE in a similar way to skip the rest of the statements in the loop but continue the loop process itself.
Fortran-90/95/2003 also includes the DO WHILE loop exemplified as follows [EF 6.11.1]:
DO WHILE ( condition )
...
END DO
As long as the condition is true, the loop will continue its operation,
but when the condition turns false, the loop is exited.
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1998, 1999, 2006 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 18 January 2006.