Notes Fortran 3

Math 60 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics

[Return to Math 60 Homepage | Return to Notes Listing Page]

Contents


Input Format [EF 4.3, 4.4]

E.g.,
        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.

Logical Expressions and Operators [EF 5.1]

Card punch machines caused the initial restrictions of using two letter abbreviations, surrounded by periods, for relational operators. These machines did not have the special characters that are common-place today (e.g., the < symbol). Fortran-90/95/2003 permits symbols as well as the older abbreviations.
        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.

Simple Choice (IF) [EF 5.2]

The IF statement performs the same way in Fortran as in C++, but in Fortran, one uses an ENDIF to conclude the statement and the keyword THEN after the condition. For example,
        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

One Line IF and Antiques [EF 5.3]

In place of the THEN, one can put a simple statement to condense an IF statement to one line. For example,
        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,

The arithmetic IF has this form:
        IF ( arith_expression ) n1, n2, n3
The action is as follows: IN GENERAL, this statement should be AVOIDED since it uses "implicit GO TO's" which is considered to be poor programming style. GO TO's lead to unreadable "spaghetti" code.

One should know what this statement does, in order to read older code, but try to avoid it in writing new code.

What Happens If ...? [DCS 4.3]

Suppose this were written in a program code (using Fixed Source Code form):
        IF (2    ..GE..     1) THEN
What happens? (Is it an error?)



Counted Loop [DCS 5, 16.4]

The Fortran counted loop is often called the DO loop because of the keyword "DO." The Fortran-90/95/2003 style is:
        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

Sample Program [EF 6.10, cf. Math 10, Notes N4, sample program at end]

        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

4

Other Fortran-90/95 Loops [EF 6.11]

Using the following structure [EF 6.11.2],
        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.