Fortran 77 For Beginners
Fortran 77 For Beginners
Table of contents
Computer Constants and Arithmetic A simple Iteration, DO
Control structures Arrays
basics variables expressions program loop
DOUBLE
Input/output, Functions and The CHARACTER PRECISION EQUIVALENCE COMMON Writing and ASCII
FORMAT subroutines type COMPLEX
BLOCKDATA testing table
1 Computer basics
1.1 The memory
1.2 Programming languages
1.2.1 High level languages
4 A simple program
4.1 The PRINT statement
4.2 The PROGRAM statement
4.3 The END and STOP statements
4.4 Program layout (fixed format)
4.5 Comments
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 2 of 80
7 Arrays
7.1 Array declarations
7.2 Use of arrays and array elements
7.3 Initialising an array
7.3.1 The DATA statement
7.3.2 The implied DO list
7.4 Input and output of arrays
7.5 Multi-dimensional arrays
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 3 of 80
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 4 of 80
15 Copyright
A computer (often called simply a machine) is a device for the fast, accurate processing of symbolic
information under the control of a stored sequence of instructions called a program.
The control unit controls the operation of the whole machine. It:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 5 of 80
The memory stores programs and information. It consists of an array of storage units called words, all of
equal length and numbered in sequence. The number of each word is called its address.
Each word contains a row of storage elements, which can individually be set to either of two states,
conventionally represented as 0 and 1. Thus information is represented by binary codes.
When a program is run, each instruction in sequence is fetched from memory and executed. An instruction
can order the control unit to fetch its next instruction from an address other than the next in memory. In
this way, different sequences of instructions can be executed. This makes it possible to execute instruction
sequences conditionally and iteratively.
Today, most programs are written in high level languages, which resemble English and are therefore
easier to use than machine code, but which have a limited, specialised vocabulary and a simple syntax free
from ambiguity.
FORTRAN (FORmula TRANslation), introduced in 1956, was the first high level language. It has since
been revised several times. Fortran 77, though not the latest version, is widely available and is compatible
with later versions.
Note: although compatible with Fortran 90 standard, mind not to use old "obsolescent features" which
will be progressively eliminated from future standards (see "obsolescent features" in Fortran 90/95
manuals).
High level language instructions are not executable. Instead, a high level language source program is read
as input by a program called a compiler, which checks its syntax and, if it is free from errors, compiles an
equivalent machine code object program. (If the source program contains syntax errors, the compiler
outputs a number of messages indicating the nature of the errors and where they occur.)
Although it is in machine code, the object program is incomplete because it includes references to
subprograms which it requires for such common tasks as reading input, producing output and computing
mathematical functions. These subprograms are grouped together in libraries which are available for use
by all object programs. To create an executable program, the object program must be linked to the
subprogram libraries it requires. The executable program may then be loaded into memory and run. The
steps required to compile, link and run a Fortran program are illustrated by Figure 3.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 6 of 80
First page
We have seen that all information is represented in the computer in binary form. The type of information
determines the way in which it is represented and the operations which may be performed on it.
+-*/=(),.'$:
When included in a Fortran statement, a string must be delimited by single quotes ('). A single quote may
be included in a string by writing two consecutively. Only one is retained.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 7 of 80
Constants of type INTEGER are integer numbers. An INTEGER constant is written as a sequence of decimal
digits, optionally preceded by a sign (unary + or -).
Examples:
123 +1 0 4356 -4
INTEGER constants are represented in exact form. Their magnitude has a limit which depends on the word
length of the computer.
1. An integer part written as an INTEGER constant defined as above, followed by a decimal point,
followed by a fractional part written as a sequence of decimal digits. Either the integer or the
fractional part, but not both, may be omitted.
2. An INTEGER constant or a REAL constant, followed by a decimal exponent written as the letter 'E'
followed by an INTEGER constant. The constant is a power of 10 by which the preceding part is
multiplied.
Examples:
REAL constants are represented in approximate form. Their magnitude has a limit which depends on the
word length of the computer.
Note: the explicit type length specification in bytes (8 bits) is often specified after an "*". Examples:
REAL*8 or INTEGER*4 It was an extension to Fortran 77, now replaced by the KIND type parameter in
Fortran 90.
2.4 Variables
A variable is a unique name which a Fortran program applies to a word of memory and uses to refer to it.
A variable consists of one to six upper case alphabetic characters and decimal digits, beginning with an
alphabetic character.
Examples:
Spaces are ignored by Fortran 77, e.g. 'COL UMN' is equivalent to 'COLUMN'.
Clarity can be improved by choosing variables which suggest their usage, e.g.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 8 of 80
The value of a variable is the constant stored in the word to which it refers. Each variable has a type,
which stipulates the type of value it may have. The type of a variable may be specified explicitly, or
assigned implicitly (by default).
The type of a variable may be assigned explicitly by a type specification statement. This has the form:
type variable_list
The statement assigns the given type to all the variables in the list.
Examples:
INTEGER WIDTH
REAL NUM, K
Type specification statements are not compiled into executable machine code instructions. Instead the
compiler records the names and types of the variables and reserves storage for them. Such non-executable
statements must be placed at the beginning of a program, before the first executable statement.
If a variable is used without being included in a type specification, its type is assigned implicitly (by
default) according to the following rule:
If the variable begins with a character from I to N, its type is INTEGER. Otherwise, it is REAL.
Note: because a variable can be used without first being declared in a type specification, a misspelled
variable is not in general detected as an error by the compiler. The program may compile and run, but
produce incorrect results. Care should therefore be taken to get variable names right, and if unexpected
results are obtained, variable names are one of the first things to check.
Note: it is strongly recommended to force explicit typing by placing an IMPLICIT NONE statement before
type specifications. It was an extension to the Fortran 77 standard, but it is now integral part of Fortran
90 standard.
Before a variable can be used in computation, it must be assigned an initial value. This may be done by
reading a value from input or by using an assignment statement.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 9 of 80
The READ statement is used to assign values to variables by reading data from input. The simplest form of
the READ statement is:
READ *, variable_list
where variable_list is a single variable or a list of variables separated by commas. (The asterisk will be
explained later).
This statement reads constants from the terminal, separated by spaces, commas, or new lines, and assigns
them in sequence to the variables in the list. Execution of the program pauses until the right number of
constants has been entered.
Example:
waits for three constants to be entered and assigns them in sequence to the variables VAR1, VAR2 and VAR3.
variable = constant
This means that the constant is assigned as a value to the variable on the left-hand-side. Note that the '='
sign has a different meaning than in algebra. It does not indicate equality, but is an assignment operator.
Examples:
TEMP = 74.5
ITEMP = 100
Type rules
Whichever method is used to assign a value to a variable, the type of the value must be consistent with that
of the variable. The rules are:
3. A REAL value can be assigned to an INTEGER variable. The value assigned is truncated by
discarding the fractional part.:
Examples:
Value
Assigned
N = 0.9999 0
M = -1.9999 -1
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 10 of 80
First page
Operator Operation
+ Addition or unary +
- Subtraction or unary -
* Multiplication
/ Division
** Exponentiation
a numeric constant
a numeric variable, which may be preceded by a unary + or -.
an arithmetic expression in parentheses, i.e. (arithmetic_expression)
The square brackets indicate that the items operator operand are optional and the ellipsis (...) that they
may be repeated indefinitely.
Spaces may be used to improve readability, but are ignored by the Fortran 77 compiler.
Examples:
3.14159
K
(A+B)*(C+D)
-1.0/X + Y/Z**2
2.0 * 3.14159*RADIUS
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 11 of 80
Restrictions:
2. The operands must be such that the operations are mathematically defined, e.g. dividing by zero,
raising zero to a negative or zero power, or raising a negative number to a real power are all illegal.
4.0+6.0*2.0
which evaluates to 20.0 if the addition is performed first, or 16.0 if the multiplication is performed first.
The sequence of operations is determined by the following precedence order, in which operators on any
line have equal precedence and precedence decreases downwards.
**
* /
+ - (binary and unary)
Using this precedence order, the rules for the evaluation of an arithmetic expression may be stated as
follows:
Subject to the restrictions noted under 'Restrictions:' above, REAL and INTEGER operands may be freely
mixed in an arithmetic expression. The type of the expression's value is determined by applying the
following rules to each operation performed in its evaluation:
1. If both operands are of the same type, the resulting value is also of that type.
2. If one operand is INTEGER and the other REAL, the INTEGER operand is converted to its REAL
equivalent before the operation is performed, and the resulting value is REAL.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 12 of 80
This rule is inconsistent with the rules of arithmetic, in which dividing one integer by another (e.g.
7/5) or raising an integer to an integer power (e.g. 2-1) does not always result in an integer. Fortran
deals with such cases by truncating, as described under "Type rules" (in the above paragraph
"Assigning a value"), to obtain an INTEGER value.
Examples:
Value
99/100 0
7/3 2
-7/3 -2
N**(-1) 0
N**(1/2) 1
100*9/5 180
9/5*100 100
The last two examples show that the ordering of * and / operators with INTEGER operands is
significant. It is usually best to avoid dividing one integer by another unless there are special reasons
for doing so.
numeric_variable = arithmetic_expression
If the type of the expression differs from that of the variable, the rules listed under 'Type rules' (in the
above paragraph "Assigning a value"), are applied, i.e.
First page
We are now ready to write a simple Fortran program. All that is required is some information on printing
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 13 of 80
PRINT *, output_list
where output_list is a single constant, variable or expression or a list of such items, separated by commas.
The PRINT statement prints the output list on the terminal screen in a standard format. Later, we shall
consider more flexible output statements which give us greater control over the appearance of the output
and the device where it is printed.
PROGRAM program_name
The statement STOP stops execution of the program. In Fortran 77/90, but not in previous versions, END
also has this effect. Therefore, if execution is simply to stop at the end of the program, STOP is optional.
However, one or more STOP statements may be written earlier, to stop execution conditionally at points
other than the end.
All Fortran statements must be written in columns 7 to 72. A statement ends with the last character on the
line, unless the next line has any character other than 0 in column 6. Any such character indicates that
columns 7 to 72 are a continuation of the previous line.
Columns 73 to 80 are ignored by the compiler. Originally, these columns were used to print sequence
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 14 of 80
Columns 1 to 5 are reserved for statement labels. These are optional unique unsigned non-zero integers
used to provide a reference to statements.
Columns Usage
1-5 Statement labels
6 Continuation character or blank
7-72 Fortran statements
73-80 Unused
Note: the fixed format source of Fortran 77 is an obsolescent feature of Fortran 95. It must be repaced by
the new free format.
4.5 Comments
The letter 'C' or an asterisk in column one causes the compiler to ignore the rest of the line, which may
therefore be used as a comment to provide information for anyone reading the program.
Example 1: a driver fills his tank with petrol before setting out on a journey. Each time he stops for petrol
he puts in 40 litres. At his destination, he fills the tank again and notes the distance he has travelled in
kilometres. Write a program which reads the distance travelled, the number of stops and the amount of
petrol put in at the end of the journey, and prints the average petrol consumption in kilometres per litre,
rounded to the nearest litre.
1| PROGRAM PETROL
2| INTEGER STOPS, FILLUP
3| C
4| C THESE VARIABLES WOULD OTHERWISE BE TYPED REAL BY DEFAULT
5| C ANY TYPE SPECIFICATIONS MUST PRECEDE THE FIRST EXECUTABLE STATEMENT
6| C
7| READ *, KM,STOPS,FILLUP
8| USED = 40*STOPS + FILLUP
9| C COMPUTES THE PETROL USED AND CONVERTS IT TO REAL
10| KPL = KM/USED + 0.5
11| C 0.5 IS ADDED TO ENSURE THAT THE RESULT IS ROUNDED
12| PRINT *, 'AVERAGE KPL WAS',KPL
13| END
This program illustrates some of the points about type conversion made in the previous chapter. In line 8,
the number of litres of petrol used is computed. The computed value is of type INTEGER, but is converted
to REAL when assigned to the REAL variable USED.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 15 of 80
In line 10, the expression KM/USED is evaluated as REAL, but would be truncated, not rounded, when
assigned to the INTEGER variable KPL. Adding 0.5 before truncating has the effect of rounding up or down.
This is a useful rounding method. It is illustrated further below.
12.0 12.5 12
12.4 12.9 12
12.5 13.0 13
12.9 13.4 13
First page
The Fortran statements covered so far are enough to allow us to read information, evaluate arithmetic
expressions and print results. It is hardly necessary to write a program to perform such tasks, which can
usually be more easily done using a calculator.
This chapter deals with conditional execution while iteration is covered in Chapter 6.
Example 1: write a program to read the coefficients of a quadratic equation and print its roots.
Solution: the roots of the quadratic equation ax² + bx +c are given by the formula
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 16 of 80
2. Evaluate b² - 4ac
3. If b² - 4ac exceeds zero then
Compute and print two distinct real roots.
Otherwise, if b² - 4ac is equal to zero then
Compute and print two coincident real roots.
Otherwise
Print message: 'No real roots'.
In step 3, the program must test conditions such as "b² -4ac exceeds zero".
To express such conditions, Fortran uses another type, the LOGICAL type.
A LOGICAL variable can be assigned either of these values. It may not be assigned a value of any other
type. Each LOGICAL variable must be declared in a LOGICAL type specification statement, which must
occur, like all other type specifications, before the first executable statement.
Example: the LOGICAL variable ERROR could be declared and initialised by the statements:
LOGICAL ERROR
ERROR = .FALSE.
A relational expression is a logical expression which states a relationship between two expressions,
evaluating to .TRUE. if the relationship applies or .FALSE. otherwise. For the present, we shall consider
only relationships between arithmetic expressions. (As we shall see later, Fortran can also deal with
relationships between CHARACTER expressions.)
Meaning
.LT. Less than
.LE. Less than or equal to
.EQ. Equal to
.NE. Not equal to
.GE. Greater than or equal to
.GT. Greater than
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 17 of 80
N.GE.0
X.LT.Y
B**2 - 4*A*C .GT. 0.
Notes:
1. Relational operators have lower precedence than arithmetic operators. Therefore, in evaluating a
relational expression, the arithmetic expressions are evaluated before the comparison indicated by
the relational operator is made.
2. The two arithmetic expressions may be of different type (i.e. one INTEGER and one REAL). In this
case, the INTEGER expression is converted to REAL form before the comparison is made.
It is often necessary to express a condition which combines two or more logical expressions. For example,
to check that the value of a variable lies within a given range, we should have to check that it is greater
than the lower limit AND less than the upper limit. Such conditions are expressed in Fortran by composite
logical expressions, which have the form:
L1 logical_operator L2
where L1 and L2 are logical expressions (relational or composite). The logical operators and their
meanings are shown below. The second column indicates the conditions under which a composite logical
expression as above evaluates to .TRUE..
Meaning
.AND. Both L1 and L2 are .TRUE.
.OR. Either L1 or L2 or both are .TRUE.
.EQV. Both L1 and L2 have the same value (.TRUE. or .FALSE.)
.NEQV. L1 and L2 have different values (one .TRUE. and one .FALSE.)
Thus the following composite logical expression would evaluate to .TRUE. if the value of the variable X
lay within a range with non-inclusive limits MIN and MAX:
There is one further logical operator .NOT., which unlike the others, takes only one operand, which it
precedes. The expression .NOT.L is .TRUE. if the logical expression L is .FALSE. and vice versa.
As with arithmetic operators, precedence rules are required to define the interpretation of expressions like:
.NOT. L1 .OR. L2
which could evaluate to .TRUE. under either of the following conditions, depending on the order of
evaluation:
1. L1 is .FALSE. or L2 is .TRUE.
2. L1 and L2 are both .FALSE.
The precedence order is shown by the following list, in which precedence decreases downwards.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 18 of 80
Arithmetic operators
Relational operators
.NOT.
.AND.
.OR.
.EQV. and .NEQV.
As in arithmetic expressions, parentheses can be used to group partial logical expressions and change the
order of evaluation. Thus
.NOT.(L1.OR.L2)
Parentheses can also be used to improve clarity, even when not logically required, e.g.
The value of a logical expression can be assigned to a variable of type LOGICAL, e.g.
LOGICAL VALID
VALID = X.GT.MIN .AND. X.LT.MAX
Logical expressions are more commonly used in logical IF statements and structures.
IF (logical_expression) executable_statement
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 19 of 80
execution of one of a number of alternative sequences of instructions. It may be described informally as:
IF (Lo) THEN
So
ELSE IF (Li) THEN
Si
.....
...
ELSE
Sn
END IF
where:
Lo is evaluated. If it evaluates to .TRUE., the sequence So is executed and execution continues with the
statement following END IF.
Otherwise:
An Li evaluates to .TRUE. The sequence Si is executed and execution continues with the statement
following END IF.
or
The last Li evaluates to .FALSE. Execution continues.
If there is an ELSE clause, the sequence Sn is executed.
Execution continues with the statement following END IF.
IF (A.LT.B) THEN
SUM = SUM + A
PRINT *, SUM
END IF
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 20 of 80
Example: an employee is paid at the standard rate for the first 40 hours of work, at time and a half for the
next 10, and at double time for any hours in excess of 50. If the variable HRS represents the hours worked
and RATE the standard rate then the employee's salary is computed by the block IF structure:
IF (HRS.LE.40) THEN
SALARY = HRS*RATE
ELSE IF (HRS.LE.50) THEN
SALARY = 40.0*RATE + (HRS-40.0)*RATE*1.5
ELSE
SALARY = 40.0*RATE + 10.0*RATE*1.5 + (HRS-50.0)*RATE*2.0
END IF
We are now in a position to complete the quadratic roots program of Example 1, but first the outline
should be altered as follows:
Because REAL values are approximations, exact comparisons involving them are unreliable. The
relational expressions in our program should therefore be reformulated using a variable e (for error)
to which a small positive value (1.0E-9) has previously been assigned.
The expression "b² - 4ac exceeds zero" in step 3 should be replaced by:
b² - 4ac > e
Similarly, the expression "b² - 4ac is equal to zero" in step 3 should be replaced by:
-e <= b² -4 ac <= e
However, the expression is evaluated only if b² -4 ac > e has previously been evaluated as false,
which of course implies b² -4 ac <= e. Therefore, all that is required is:
-e <= b² -4 ac
The expression for the roots includes a divisor of 2a. Therefore if a has a value of zero, the
evaluation of this expression will cause the program to fail with an arithmetic error. The program
should prevent this by testing and printing a suitable message if it is zero. Again, the test should be
expressed using the error variable e.
In general, programs should be designed to be robust, i.e. they should take account of any exceptional
data values which may cause the program to fail, and take steps to prevent this.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 21 of 80
Now that the outline is complete, the program can be easily written:
PROGRAM QUAD
E = 1E-9
READ *, A,B,C
IF (A.GE. -E .AND. A.LE.E) THEN
PRINT *, 'FIRST COEFFICIENT MUST BE NON-ZERO.'
ELSE
S = B**2 - 4*A*C
IF (S.GT.E) THEN
D = S**0.5
X1 = (-B+D)/(2*A)
X2 = (-B-D)/(2*A)
PRINT *, 'TWO DISTINCT ROOTS:' X1 'AND' X2
ELSE IF (S.GT. -E) THEN
X = -B/(2*A)
PRINT *, 'TWO COINCIDENT ROOTS',X
ELSE
PRINT *, 'NO REAL ROOTS.'
END IF
END IF
END
Note that most of the program consists of a block IF structure, with a second block IF included in its ELSE
clause. The embedding of one structure within another in this way is called nesting.
First page
The last chapter showed how a sequence of instructions can be executed once, if a condition is true. The
need also frequently arises to execute a sequence of instructions repeatedly, while a condition is true, or
until a condition becomes true. Such repetitive execution is called iteration.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 22 of 80
Write a program to read the marks (notes in french) of a class of students in an exam, print the number of
marks and compute and print the average mark. The marks are to be read one at a time, with a 'dummy'
mark of 999 marking the end.
Unlike Fortran 90 and other more modern programming languages, Fortran 77 lacks a while structure as
such, but the effect can be obtained using an IF structure and a new statement, the GOTO.
GOTO label
where label is the label of an executable statement, with certain restrictions which will be considered later.
A GOTO statement causes the flow of execution to 'jump' to the labelled statement and resume from there.
PROGRAM AVMARK
INTEGER TOTAL,COUNT
TOTAL = 0
COUNT = 0
READ *, MARK
10 IF (MARK.NE.999) THEN
COUNT = COUNT+1
TOTAL = TOTAL+MARK
READ *, MARK
GOTO 10
END IF
IF (COUNT.GT.0) THEN
AVER = 1.0*TOTAL/COUNT
C MULTIPLY BY 1.0 TO CONVERT TO REAL AND AVOID TRUNCATION
PRINT *, COUNT, 'MARKS WERE READ.'
PRINT *, 'AVERAGE MARK IS', AVER
ELSE
PRINT *, 'NO MARKS WERE READ.'
END IF
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 23 of 80
END
Exercise: the average mark program provides for the possibility that the data consists only of the
terminator 999, but does no other checking. If the range of valid marks is 0 to 100, alter the program to
check the validity of each mark, printing a suitable message if it is invalid, and print a count of any invalid
marks with the results.
Often a loop is controlled by a variable which is incremented or decremented on each iteration until a
limiting value is reached, so that the number of iterations is predetermined. Such a loop is shown in
Example 2.
Example 2: write a program to print a table of angles in degrees and their equivalents in radians, from 0 to
360 degrees, in steps of 10 degrees.
PROGRAM CONVRT
INTEGER DEGREE
CONFAC = 3.141593/180.0
C CONVERSION FACTOR FROM DEGREES TO RADIANS
DEGREE = 0
10 IF (DEGREE .LE. 360) THEN
RADIAN = DEGREE*CONFAC
PRINT *, DEGREE,RADIAN
DEGREE = DEGREE + 10
GOTO 10
END IF
END
A loop control variable (DEGREE in the above example) is assigned an initial value before the first
iteration.
On each iteration, the control variable is incremented or decremented by a constant.
Iteration stops when the value of the control variable passes a predefined limit.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 24 of 80
Fortran provides for such loops with a structure called a DO loop, which is more concise and readable than
a construction using IF and GOTO.
label is the label of an executable statement sequentially following the DO statement called the terminal
statement of the DO loop.
var is an INTEGER or REAL (obsolete Fortran 90 feature) variable called the loop control variable.
e1, e2 and e3 are arithmetic expressions (i.e. INTEGER or REAL constants, variables or more complex
expressions).
The sequence of statements beginning with the statement immediately following the DO statement and
ending with the terminal statement is called the range of the DO loop.
6.3.1 Execution
1. The expressions e1, e2 and e3 are evaluated and if necessary converted to the type of var. If e3 is
omitted, a value of 1 is used. The resulting values are called the parameters of the loop. We shall
call them initial, limit and increment respectively.
3. var is compared with limit, the test depending on the value of increment as follows:
Condition tested
increment > 0 ---> var <= limit
increment < 0 ---> var >= limit
Examples:
DO 10, I = 1,5
causes the range of statements beginning with the next and ending with the statement labelled 10 to be
executed 5 times.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 25 of 80
DO 10, I = 0,100,5
DO 10, I = 100,0,-5
DO 10, I = 0,100,-5
In this case, the range is not executed at all, as the test in step 3 fails for the initial value of I.
DO 10, J = I,4*N**2-1,K
We can now rewrite the program of Example 2 using a DO loop. The outline becomes:
PROGRAM CONVRT
INTEGER DEGREE
CONFAC = 3.141593/180.0
C CONVERSION FACTOR FROM DEGREES TO RADIANS
DO 10, DEGREE = 0,360,10
RADIAN = DEGREE*CONFAC
PRINT *, DEGREE,RADIAN
10 CONTINUE
END
This is clearer and more concise than version 1. Note the use of indentation to clarify the loop structure.
To protect the integrity of the loop structure, there are various restrictions affecting DO loops.
2. The terminal statement must be one which is self-contained and allows execution to continue at the
next statement. This rules out STOP, END and another DO statement. It is often convenient to end a DO
loop with a CONTINUE statement, which has no effect whatever, serving only to mark the end of the
loop.
3. The range of a DO loop can be entered only via the initial DO statement. Thus a GOTO cannot cause a
jump into the range of a DO loop. However, GOTOs can be included in the range to jump to statements
either inside or outside it. In the latter case, this can cause iteration to stop before the control
variable reaches the limiting value.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 26 of 80
Examples:
GOTO 10
. . .
DO 20, I = 1,5
. . .
10 . . .
. . .
20 CONTINUE
is wrong, but
DO 20, I = 1,5
. . .
10 . . .
. . .
IF (...) GOTO 10
.
IF (...) GOTO 30
. . .
20 CONTINUE
. . .
30 . . .
is all right.
4. The control variable can be freely used in expressions in the range of the loop (as in Figure 10) but it
cannot be assigned a value.
5. The loop parameters are the values of the expressions e1, e2 and e3 on entry to the loop. The
expressions themselves are not used. Therefore if any of e1, e2 and e3 are variables, they can be
assigned values within the loop without disrupting its execution.
As explained under 'Execution' the control variable is incremented and tested at the end of each
iteration. Thus, unless iteration is interrupted by a GOTO, the value of the control variable after
execution of the loop will be the value which it was assigned at the end of the final iteration. For
example, in a loop controlled by the statement:
DO 10, I = 0,100,5
the control variable I is incremented to exactly 100 at the end of the 20th iteration. This does not
exceed limit, so another iteration is performed. I is then incremented to 105 and iteration stops, with
I retaining this value.
If the control variable is REAL, inconsistent results may be obtained unless allowance is made for
approximation. For example, in a loop controlled by:
DO 10, C = 0,100,5
the control variable C is incremented at the end of the 20th iteration to a value of approximately 100.
If it is less, execution continues for a further iteration, but if it is greater, iteration stops.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 27 of 80
DO 10, C = 0,101,5
Example:
Valid Invalid
DO 20 ... DO 20 ...
... ...
DO 10 ... DO 10 ...
... ...
10 CONTINUE 20 CONTINUE
... ...
20 CONTINUE 10 CONTINUE
The following provides a simple, if not very useful example of a nested loop structure.
Example 3:
PROGRAM TABLES
DO 20, I = 2,12
PRINT *,I,' TIMES TABLE'
DO 10, J = 1,12
10 PRINT *,I,' TIMES',J,' IS',I*J
20 CONTINUE
END
There is no logical need for the CONTINUE statement in this program as nested loops can share a
common terminal statement. Thus the program could be rewritten as:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 28 of 80
PROGRAM TABLES
DO 10, I = 2,12
PRINT *,I,' TIMES TABLE'
DO 10, J = 1,12
10 PRINT *,I,' TIMES',J,' IS',I*J
END
However, to clarify the structure, it is better to use separate terminal statements and indentation as in
the first version.
Note: sharing terminal statement or eliminating the terminal CONTINUE is an obsolescent feature of
Fortran 90.
First page
-7- Arrays
7.1 Array declarations
7.2 Use of arrays and array elements
7.3 Initialising an array
7.3.1 The DATA statement
7.3.2 The implied DO list
7.4 Input and output of arrays
7.5 Multi-dimensional arrays
All our programs so far have required the storage of only a few values, and could therefore be
written using only a few variables. For example, the average mark program of Figure 8 required
only variables for a mark, the total mark, the count of the marks and the average. When large
numbers of values have to be stored, it becomes impracticaI or impossible to use different variables
for them all. If the average mark program were rewritten to compute average marks for five
subjects, we should require five variables, say MARK1 ... MARK5 for the marks, five variables for
the totals, and five for the averages. This could be done, but the program would be rather repetitive.
The situation is even worse if, after computing the averages, the program is required to print a list
showing, for each student and subject, the student's mark and the difference between the mark and
the average. This could conceivably be done if the number of students were given in advance, but
the program would be extremely cumbersome. If, as in the example, the number of students is not
given but determined by counting, the task is impossible, as there is no way of knowing how many
variables will be required.
We need to store all the marks in order in a list or other structure to which we can apply a name, and
refer to individual marks by a combination of the name and a number or numbers indicating the
position of a mark in the list or structure.
In mathematics, an ordered list of n items is called a vector of dimension n. If the vector is denoted
by v, the items, usually called the components or elements of the vector, are denoted by v1, v2, v3,
..., vn.
Fortran uses a structure similar to a vector called an array. An array A of dimension N is an ordered
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 29 of 80
list of N variables of a given type, called the elements of the array. In Fortran, the subscript notation
used for the components of a vector is not available. Instead the elements are denoted by the name
of the array followed by an integer expression in parentheses. Thus, the elements of A are denoted
by A(1), A(2),... A(N). The parenthesised expressions are called array subscripts even
though not written as such.
A subscript can be any arithmetic expression which evaluates to an integer. Thus, if A, B, and C are
arrays, the following are valid ways of writing an array element:
A(10)
B(I+4)
C(3*I+K)
INTEGER AGE(100),NUM(25),DEG
This reserves 100 words of storage for array AGE, 25 for array NUM, and one word for the variable
DEG. All three items are of type INTEGER.
Space can also be reserved for arrays by the DIMENSION statement, which reserves storage using a
similar syntax, but includes no information about type. Thus, if this method is used, the type is either
determined by the initial letter of the array or assigned by a separate type specification. Therefore,
the equivalent to the above using a DIMENSION statement is:
INTEGER AGE,DEG
DIMENSION AGE(100),NUM(25)
DIMENSION statements, like type specifications, are non-executable and must be placed before the
first executable statement.
When this form of declaration is used in a type or DIMENSION statement the upper and lower
bounds for the subscript are 1 and the dimension respectively. Thus, AGE in the above example may
have any subscript from 1 to 100. Arrays can also be declared to have subscripts with a lower bound
other than 1 by using a second form of declaration in which the lower and upper bounds are given,
separated by a colon. For example:
REAL C(0:20)
INTEGER ERROR(-10:10)
reserves 21 words of storage for each of the arrays C and ERROR and stipulates that the subscripts of
C range from 0 to 20 inclusive, while those of ERROR range from -10 to 10.
Although the declaration stipulates bounds for the subscript, not all compilers check that a subscript
actually lies within the bounds. For example, if NUM is declared as above to have a subscript from 1
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 30 of 80
to 25, a reference to NUM(30)may not cause an error. The compiler may simply use the 30th word of
storage starting from the address of NUM(1) even though this is outside the bounds of the array. This
can cause unpredictable results. Care should therefore be taken to make sure that your subscripts are
within their bounds.
The array name without a subscript refers to the entire array and can be used only in a number of
specific ways.
NUM(1) = 0
NUM(2) = 5
If all the elements are to have equal values, or if their values form a regular sequence, a DO loop can
be used. Thus, if NUM and DIST are arrays of dimension 5:
DO 10, I = 1,5
NUM(I) = 0
10 CONTINUE
DO 10, I = 1,5
DIST(I) = 1.5*I
10 CONTINUE
assigns the values 1.5, 3.0, 4.5, 6.0 and 7.5 to DIST(1),DIST(2),DIST(3),DIST(4) and DIST(5)
respectively.
Each variable_list is a list of variables, and each constant_list a list of constants, separated by
commas in each case. Each constant_list must contain the same number of items as the preceding
variable_list and corresponding items in sequence in the two lists must be of the same type.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 31 of 80
The DATA statement assigns to each variable in each variable_list a value equal to the corresponding
constant in the corresponding constant_list. For example:
DATA A,B,N/1.0,2.0,17/
assigns the values 1. and 2. respectively to the REAL variables A and B, and 17 to the INTEGER
variable N.
A constant may be repeated by preceding it by the number of repetitions required (an integer) and an
asterisk. Thus:
DATA N1,N2,N3,N4/4*0/
Items in a variable_list may be array elements. Thus, if A is an array of dimension 20, the DATA
statement:
DATA A(1),A(2),A(3),A(4)/4*0.0/,A(20)/-1.0/
assigns a value of zero to the first four elements, -1.0 to the last element, and leaves the remaining
elements undefined.
When a large number of array elements have to be initialised, we can avoid writing them all
individually by using an implied DO list.
An implied DO list is used in a DATA statement or an input/output statement to generate a list of array
elements. The simplest form of implied DO list is:
(dlist, int=c1,c2[,c3])
where dlist is a list of array elements separated by commas. The expresssion: int=c1,c2[,c3] has a
similar effect to the expression: var=e1,e2,[,e3] in a DO loop, but int must be a variable of type
INTEGER, and c1,c2 and c3 must be constants or expressions with constant operands. The implied DO
variable int is defined only in the implied DO list, and is distinct from any variable of the same name
used elsewhere.
The implied DO list expands dlist by repeating the list for each value of int generated by the loop,
evaluating the array subscripts each time. Thus:
DATA (A(I),I=1,4)/4*0.0/,A(20)/-1.0/
DATA (A(I),A(I+1),I=1,19,3)/14*0.0/,(A(I),I=3,18,3)/6*1.0/
which assigns a value of zero to A(1),A(2), A(4),A(5), ... A(19),A(20) and a value of 1.0 to
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 32 of 80
Finally, an entire array can be initialised by including its name, without a subscript, in variable_list
in a DATA statement. This is equivalent to a list of all its elements in sequence. Thus, if A has
dimension 20, all the elements of A are initialised to zero by:
DATA A/20*0.0/
DATA statements can be placed anywhere in a program after any specifications. In the interests of
clarity, it is probably best to put them immediately before the first executable statement. Wherever
they may be, they cause initialisation when the program is loaded (before execution begins).
Therefore they can only be used to initialise variables and not to re-assign values to them throughout
execution of the program. For this purpose, assignment statements or READ statements must be used.
Note: not placing DATA statements before the first executable statement is an obsolescent feature of
Fortran 90.
Implied DO lists in input/output statements differ in two respects from those in DATA statements:
1. In output statements, dlist can include any output list item. For example:
will print the values of A(1)...A(4) followed in each case by 'ABC' and the value of K.
2. The loop parameters need not be constants or constant expressions, but can include variables
(INTEGER or REAL) provided that these have been assigned values, e.g.
N = 5
. . .
PRINT *,(A(I),I=1,N)
In an input statement, the loop parameters can depend on values read before by the same statement,
e.g.
READ *, N, (A(I),I=1,N)
If variables are used in this way, care should be taken to ensure that they lie within the subscript
bounds of the array, as in the following example:
REAL A(20)
. . .
READ *, N
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 33 of 80
We can now return to the exam marks problem mentioned at the beginning of the chapter.
Example 1: write a program to read the marks of a class of students in five papers, and print, for
each paper, the number of students sitting it and the average mark. The marks are to be read as a list
of five marks in the same order for each student, with a negative mark if the student did not sit a
paper. The end of the data is indicated by a dummy mark of 999.
We shall use arrays MARK, COUNT and TOTAL to store the five marks for a student, a count of students
sitting each paper and the total mark for each paper respectively. The program follows.
PROGRAM EXAM
INTEGER MARK(5),TOTAL(5),COUNT(5)
DATA COUNT/5*0/,TOTAL/5*0/
READ *,(MARK(I),I=1,5)
10 IF (MARK(1).NE.999) THEN
DO 20, I=1,5
IF (MARK(I).GE.0) THEN
COUNT(I) = COUNT(I)+1
TOTAL(I) = TOTAL(I)+MARK(I)
END IF
20 CONTINUE
READ *,(MARK(I),I=1,5)
GOTO 10
END IF
DO 30, I=1,5
IF (COUNT(I).GT.0) THEN
AVMARK = 1.0*TOTAL(I)/COUNT(I)
C MULTIPLY BY 1.0 TO CONVERT TO REAL AND AVOID TRUNCATION
PRINT *,COUNT(I),' STUDENTS SAT PAPER NUMBER',I
PRINT *,'THE AVERAGE MARK WAS', AVMARK
ELSE
PRINT *,'NO STUDENTS SAT PAPER NUMBER',I
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 34 of 80
END IF
30 CONTINUE
END
One problem with this program is that if the last line of input consists of the single terminating value
of 999, the statement: READ *,(MARK(I),I=1,5) will wait for another four values to be entered.
This can be avoided by following 999 by a '/' character, which is a terminator causing the READ
statement to ignore the rest of the input list.
The problem could be dealt with more easily if we could add a second subscript to the MARK array to
represent the number of each student in sequence. Our array could then be declared either by:
INTEGER MARK(5,100)
or by:
INTEGER MARK(100,5)
and would reserve enough space to store the marks of up to 100 students in 5 subjects.
In fact, Fortran arrays can have up to seven dimensions, so the above declarations are valid. The
subscript bounds are specified in the same way as for one-dimensional arrays. For example:
REAL THREED(5,0:5,-10:10)
declares a three-dimensional array of type REAL, with subscript bounds of 1...5, 0...5 and -10...10 in
that order.
An array element must always be written with the number of subscripts indicated by the declaration.
When multi-dimensional array elements are used in an implied DO list, multiple subscripts can be
dealt with by including nested implied DO lists in dlist, for example:
READ *, (A(J),(MARK(I,J),I=1,5),J=1,100)
Here, dlist contains two items, A(J) and the implied DO list (MARK(I,J),I=,5) .This inner implied
DO list is expanded once for each value of J in the outer implied DO list. Thus the above READ
statement reads values into the elements of A and MARK in the order:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 35 of 80
The unsubscripted name of a multi-dimensional array can be used, like that of a one-dimensional
array, in input/output and DATA statements to refer to all its elements, but it is essential to know their
order. The elements are referenced in the order of their positions in the computer's memory. For a
one-dimensional array, the elements occur, as we might expect, in increasing order of their
subscripts, but for multi-dimensional arrays, the ordering is less obvious. The rule is that the
elements are ordered with the first subscript increasing most rapidly, then the next and so on, the last
subscript increasing most slowly. Thus if MARK is declared as:
INTEGER MARK(5,100)
its elements are ordered in memory as shown above, and the statement:
READ *,MARK
is equivalent to:
READ *, ((MARK(I,J),I=1,5),J=1,100)
Of course, the order could be altered by swapping the control variables in the inner and outer
implied DO loops thus:
READ *, ((MARK(I,J),J=1,100),I=1,5)
Note: if we consider a two dimensional array as a matrix, we should say that in memory its elements
are stored column after colum. With the C language it should be row after row !
We can use a two-dimensional array to solve the problem posed at the beginning of this section.
Example 2: write a program to read the marks of up to 100 students in five papers, and print, for
each paper, the number of students sitting it, the average mark, and a list of the marks and their
differences from the average. The marks are to be read as a list of five marks in the same order for
each student, with a negative mark if the student did not sit a paper. The end of the data is indicated
by a dummy mark of 999.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 36 of 80
Since the marks are read five subjects at a time for each student, it is convenient to store them in an
array MARK(5,100). The program follows:
PROGRAM EXAM2
IMPLICIT NONE
REAL AVMARK
INTEGER MARK(5,100),TOTAL(5),COUNT(5),ALL,I,J,LAST
DATA COUNT/5*0/,TOTAL/5*0/,ALL/0/
DO 20, J=1,100
READ *,(MARK(I,J),I=1,5)
IF (MARK(1,J).EQ.999) GOTO 30
ALL = ALL+1
DO 10, I=1,5
IF (MARK(I,J).GE.0) THEN
COUNT(I) = COUNT(I)+1
TOTAL(I) = TOTAL(I)+MARK(I,J)
END IF
10 CONTINUE
20 CONTINUE
READ *,LAST
IF (LAST.NE.999) THEN
PRINT *,'MARKS ENTERED FOR MORE THAN 100 STUDENTS.'
STOP
END IF
30 DO 50, I=1,5
IF (COUNT(I).GT.0) THEN
AVMARK = 1.0*TOTAL(I)/COUNT(I)
C MULTIPLY BY 1.0 TO CONVERT TO REAL AND AVOID TRUNCATION
PRINT *,COUNT(I),' STUDENTS SAT PAPER NUMBER',I
PRINT *,'THE AVERAGE MARK WAS', AVMARK
PRINT *,'MARKS AND THEIR DIFFERENCES FROM THE AVERAGE:'
DO 40, J=1,ALL
IF (MARK(I,J).GE.0)PRINT *,MARK(I,J),MARK(I,J)-AVMARK
40 CONTINUE
ELSE
PRINT *,'NO STUDENTS SAT PAPER NUMBER',I
END IF
50 CONTINUE
END
First page
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 37 of 80
This chapter introduces input, output and format statements which give us greater flexibility than the
simple READ and PRINT statements used so far.
READ *, variable_list
reads a line (or record ) of information from the standard input (defined as the keyboard for
programs run from a terminal) and stores it in the variables in variable_list. The asterisk refers to a
list-directed format used to split the information into separate items using spaces and/or commas as
separators and convert each item to the appropriate internal representation, which is determined by
the type of the corresponding variable in variable_list.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 38 of 80
PRINT *, output_list
uses a list-directed format to convert each constant, and the value of each variable, in output_list to a
suitable form for output on standard output (defined for a program run from a terminal as the
screen) and prints the list as a line of output, with spaces between the items.
READ 10,A,B,C
10 FORMAT(...)
The FORMAT statement describes the layout of each item to be read or printed, and how it is to be
converted from external to internal form or vice versa. It also describes the movements of an
imaginary cursor which can be envisaged as scanning the input list. Its general form is:
label is a statement label. A FORMAT statement must always be labelled to provide a reference for use
in input/output statements.
FORMAT statements can be placed anywhere in a program. It is often convenient to place them all at
the end (immediately before END), especially if some of them are used by more than one
input/output statement.
Descriptor Meaning
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 39 of 80
This is used to read a value into an INTEGER variable. Its form is Iw, where w is an unsigned integer
indicating the number of characters to be read (the width of the field). These characters must consist
of decimal digits and/or spaces, which are interpreted as zeroes, with an optional + or - sign
anywhere before the first digit. Any other characters will cause an input error.
Example:
READ 10,MEAN,INC
10 FORMAT(I4,I4)
Input: b123b-5b
(b represents a blank). This assigns a value of 123 to MEAN and -50 to INC.
This is used to read a value into a REAL variable. It has the form Fw.d, where w is an unsigned
integer representing the width of the field and d is an unsigned integer representing the number of
digits in the fractional part.
The corresponding input item must consist of decimal digits and/or spaces, with an optional sign
anywhere before the first digit and an optional decimal point. As with the I format descriptor,
spaces are interpreted as zeroes. If there is no decimal point in the item, the number of fractional
digits is indicated by d. If the item includes a decimal point, d is ignored, and the number of
fractional digits is as indicated.
Example:
READ 10,X,A,B,C,D
10 FORMAT(F4.5,F4.1,F2.2,F3.5,F3.0)
Input: b1.5b123456789bb
This is used to read a value into a REAL variable. It has a similar form to the F format descriptor, but
is more versatile, as it can be used to read input in exponential notation.
We saw in Chapter 2 that a REAL constant can be written in exponential notation as a REAL or
INTEGER constant followed by an exponent in the form of the letter 'E' followed by the power of 10
by which the number is to be multiplied. For input, the exponent can also be a signed integer
without the letter 'E'.
Example:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 40 of 80
With a format descriptor of E9.2, all the following will be read as 1.26
0.126Eb01
1.26bEb00
1.26bbbbb
12.60E-01
bbb.126E1
bbbbbb126
126bbbbbb
bbb12.6-1
The I, F and E format descriptors may be repeated by preceding them by a number indicating the
number of repetitions. For example:
10 FORMAT(3I4)
is equivalent to:
10 FORMAT(I4,I4,I4)
Example:
READ 10,I,J
10 FORMAT(I4,3X,I3)
Input: 123456789b
The T (for tab), TL and TR format descriptors are used to move the cursor to a given position. This
is defined absolutely by the T format descriptor or relative to the current position by the TL and TR
descriptors.
Example:
READ 10,I,J,K
10 FORMAT(T4,I2,TR2,I2,TL5,I3)
Input: 123456789b
Results: I: 45 J: 89 K: 567
Notes:
1. TRn is equivalent to nX.
2. As illustrated by the example, tabs can be used not only to skip over parts of the input, but to
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 41 of 80
Descriptor Meaning
Iw Output an INTEGER value in the next w character positions
Output a REAL value in the next w character positions, with d digits
Fw.d
in the fractional part.
Output a REAL value in exponential notation in the next w character positions,
Ew.d
with d digits in the fractional part.
nX Skip the next n character positions.
Tc Skip to character absolute position c.
TLn Skip to the character which is n characters to the left of the current character.
TRn Skip to the character which is n characters to the right of the current character.
'c1c2...cn' Output the string of n characters c1c2...cn starting at the next character position.
nHc1c2...cn Output the string of n characters c1c2...cn starting at the next character position.
As well as defining the layout of a line of output via an associated FORMAT statement, an output
statement must define the vertical placement of the line on the screen or page of printed output. The
method of doing this is described before the use of the format descriptors of Figure 15.
The computer uses the output list and the corresponding format specification list to build each line
of output in a storage unit called an output buffer before displaying or printing it. When the
contents of the buffer are displayed on the screen or printed on paper, the first character is not
shown, but is interpreted as a control character, defining the vertical placement of the line. Four
control characters are recognised, as shown in Figure 16.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 42 of 80
The effect of any other character is not defined, but is usually the same as a space, i.e. output is on
the next line.
Note: these vertical control characters are generally called "Fortran ASA carriage control
characters". They are ineffective on modern displays and printers. To take them in account you must
activate a filter which is contructor dependent. It converts them to equivalent ASCII control
characters to simulate the vertical action.
The overprint is simulated by as many backspaces as necessary to override the previous line: it's not
always working very well!
On IBM, the available filter is called asa: for more details consult its man.
Incorrect output may be obtained if the control character is not taken into account. It is therefore
best to use the format specification to insert a control character as the first character in a line, rather
than to provide it via the output list. For example:
N = 15
PRINT 10,N
10 FORMAT(1X,I2)
Output: 15
The initial blank in the buffer is interpreted as a control character, and '15' is printed on the next line.
However, if the FORMAT statement were:
10 FORMAT(I2)
the buffer contents would be '15'. On printing, the initial '1' would be interpreted as a control
character, and '5' would be printed at the start of the next page.
The following sections describe in more detail the effect of the format descriptors in output
statements.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 43 of 80
The format descriptor Iw is used to print an INTEGER value right-justified in a field of width w
character positions, filling unused positions on the left with blanks and beginning with a '-' sign if
the value is negative. If the value cannot be printed in a field of width w, the field is filled with
asterisks and an output error is reported.
Example:
I = 15
J = 709
K = -12
PRINT 10,I,J,K,
10 FORMAT(1X,I4,I4,I4)
Output: bb15b709b-12
Notes:
1. The first format descriptor 1X provides a space as a control character to begin output on a new
line. The next descriptor I4 then prints the value 15 in a field of width 4. The same effect
could be obtained by using I5 as the first descriptor, but it is clearer to use a separate
descriptor for the control character.
10 FORMAT(1X,3I4)
The format descriptor Fw.d (F for floating point) is used to print a REAL value right-justified in a
field of width w, with the fractional part rounded (not truncated) to d places of decimals. The field is
filled on the left with blanks and the first non-blank character is '-' if the value is negative. If the
value cannot be printed according to the descriptor, the field is filled with asterisks and an error is
reported.
Example:
X = 3.14159
Y = -275.3024
Z = 12.9999
PRINT 10,X,Y,Z,
10 FORMAT(1X,3F10.3)
Output: bbbbb3.142bb-275.302bbbb13.000
The value of X is rounded up, that of Y is rounded down, and that of Z is rounded up, the 3 decimal
places being filled with zeroes.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 44 of 80
The format descriptor Ew.d is used to print a REAL value in exponential notation right-justified in a
field of width w, with the fractional part rounded to d places of decimals. Thus the layout for a
format descriptor of E10.3 is:
S0.XXXESXX
<d>
<---w---->
S indicates a position for a sign. The initial sign is printed only if negative, but the sign of the
exponent is always printed. X indicates a digit.
Example:
The value 0.0000231436 is printed as shown with the various format descriptors:
E10.4 0.2314E-04
E12.3 bbb0.231E-04
E12.5 b0.23144E-04
The literal format descriptors 'c1c2...cn' and nHc1c2...cn place the string of n characters c1c2...cn
directly into the buffer. Thus a PRINT statement using either of the following FORMAT statements will
print the header: 'RESULTS' at the top of a new page:
10 FORMAT('1','RESULTS')
10 FORMAT(1H1,7HRESULTS)
The quoted form is generally easier to use, but the 'H' form is convenient for providing control
characters.
Note: the nHc1c2...cn Hollerith form is an obsolescent feature of Fortran 90 and deleted from
Fortran 95.
A repetition count may be used with a literal format descriptor if the descriptor is enclosed in
parentheses, e.g.
10 FORMAT(1X,3('RESULTS'))
Each Fortran input/output statement reads information from, or writes it to, a file. The file must be
connected to an external unit, i.e. a physical device such as the keyboard or screen, or a magnetic
disk. An external unit is referred to by a unit identifier, which may be:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 45 of 80
an asterisk ('*'), which normally refers to the keyboard for input, or the screen for output.
The READ and PRINT statements we have considered so far read from the file 'standard input',
normally connected to the keyboard, and print on the file 'standard output', normally connected to
the screen. To use different files and devices and to obtain various other options, we require a more
general form of the READ statement for input, and a new statement, the WRITE statement for output.
These statements have the form:
where cilist is a list of input-output specifiers, separated by commas. Each specifier takes the form:
keyword = value
The specifiers may be in any order. In special cases noted below, only the value is required. Some of
the keywords are:
UNIT
FMT
ERR
END
The unit specifier must always be included. Its value must be a unit identifier, as defined above.
If the unit specifier is the first item in cilist, it may be denoted by its value only (without 'UNIT=').
Unit identifiers 5 and 6 are preconnected to the files 'standard input' and 'standard output'
respectively.
The value of the format specifier FMT is the label of a FORMAT statement to be used for input/output
conversion, or an asterisk to indicate list-directed formatting. A format specifier may be denoted by
its value only (without 'FMT=') if it is the second item in cilist and follows a unit specifier also
denoted by its value only.
Examples: if unit identifier 5 corresponds to standard input, the following are all equivalent:
READ(*,*) A,B,C
READ(5,*) A,B,C
READ *, A,B,C
The last two specifiers deal with special conditions. If an error occurs in input or output execution
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 46 of 80
ERR = label
is included in cilist, execution continues from the statement labelled label. This makes it possible to
include statements in the program to take special actions to deal with such errors.
If a READ statement tries to read more data than is available, an input error normally occurs.
However, if a file ends with a special end-of-file record, a specifier of the form:
END = label
Note: the format specification list can be put inside the READ or WRITE statement. By example:
OPEN(openlist)
keyword = value
Specifiers may occur in any order. Two of the more important keywords are:
UNIT
FILE
The unit specifier must be included. Its value must be a unit identifier.
If the unit specifier is the first item in openlist, it may be denoted by its value only (without
'UNIT=').
The value of the file specifier is a character expression naming a file to be opened, i.e. connected to
the external unit specified by the unit specifier. If the file does not exist, a new file is created.
Example:
OPEN(8, FILE='MYFILE.DAT')
connects the file MYFILE.DAT to unit 8. If the file does not exist, it is created. READ and WRITE
statements referring to this unit identifier will then read from or write to this file.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 47 of 80
Example:
READ(5,10) A,B,C,P,Q,R,X,Y,Z
10 FORMAT(3F12.3)
This reads three values from the first line of input into the variables A,B and C, from the second line
into P,Q and R, and from the third line into X,Y and Z. Similarly:
WRITE(6,10) A,B,C,P,Q,R,X,Y,Z
10 FORMAT(1X,3F12.3)
The format specification list may also be re-used partially if it includes nested parentheses. The rules
are:
If there are no nested parentheses, the specification list is re-used from the beginning.
If the list includes nested parentheses, the list is re-used from the left parenthesis
corresponding to the last nested right parenthesis.
If the left parenthesis so defined is preceded by a repeat count the list is re-used from
immediately before the repeat count.
This is illustrated by the following examples, in which a vertical bar indicates the point from which
repetition, if required, begins:
On input, / causes the rest of the current record to be skipped, and the next value to be read from the
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 48 of 80
READ(5,100) A,B,C,I,J,K
100 FORMAT(2F10.2,F12.3/I6,2I10)
reads three REAL values into A,B and C from a line, ignores anything more on that line, and reads
three INTEGER values into I,J and K from the next line.
Consecutive slashes cause records to be skipped. Thus if the FORMAT statement in the above example
were changed to:
100 FORMAT(2F10.2,F12.3//I6,2I10)
a complete line would be skipped before the values were read into I,J and K.
On output, a / marks the end of a record, and starts a new one. Consecutive slashes cause blank
records to be output. For example:
WRITE(6,200) A,B,A+B,A*B
200 FORMAT(1H1////T10,'MULTIPLE LINES EXAMPLE'///
* 1X,'THE SUM OF',F5.2,' AND',F5.2,' IS',F5.2/
* 1X,'AND THEIR PRODUCT IS',F8.2////)
prints four blank lines and a header at the top of a new page, followed by two blank lines, then the
sum and product on consecutive lines followed by four blank lines.
The following example illustrates the use of formatting to produce output in tabular form with
headers and regular spacing.
Example 1: rewrite the degrees to radians conversion program (Chapter 6, Example 2) to print
angles from 1 to 360 degrees in 1 degree intervals and their equivalents in radians. The results
should be printed 40 lines to a page, with the values suitably formatted, blank lines separating
groups of 10 consecutive lines, headers for the 'Degrees' and 'Radians' columns, and a header and
page number at the start of each page.
PROGRAM ANGLES
IMPLICIT NONE
REAL RADIAN,CONFAC
INTEGER DEGREE,PAGENO,OUT,N
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 49 of 80
DATA OUT/6/
C UNIT NUMBER FOR OUTPUT. A DIFFERENT DEVICE COULD BE USED BY
C CHANGING THIS VALUE
DATA PAGENO/0/
CONFAC = 3.141593/180.0
C CONVERSION FACTOR FROM DEGREES TO RADIANS
DO 10, DEGREE = 1,360
N = DEGREE-1
IF (N/40*40 .EQ. N) THEN
C PRINT PAGE HEADER, NUMBER AND COLUMN HEADERS
PAGENO = PAGENO+1
WRITE(OUT,100)PAGENO
ELSE IF (N/10*10 .EQ.N) THEN
WRITE(OUT,110)
END IF
RADIAN = DEGREE*CONFAC
WRITE(OUT,120)DEGREE,RADIAN
10 CONTINUE
100 FORMAT(1H1//1X,'DEGREES TO RADIANS CONVERSION TABLE',
* T74,'PAGE',I2//1X,'DEGREES RADIANS'/)
110 FORMAT(1X)
120 FORMAT(1X,I5,T10,F7.5)
END
Example:
REAL T(100)
DO 10 I=1, 100
T(I) = SIN(REAL(100)/123.)
10 CONTINUE
OPEN(UNIT=10, FILE='MYFIC', ACCESS='SEQUENTIAL',
1 FORM='UNFORMATTED', STATUS='NEW')
WRITE(UNIT=10) T
REWIND 10
READ(UNIT=10) T
CLOSE(UNIT=10)
END
ACCESS='SEQUENTIAL'
FORM='UNFORMATTED'
STATUS='UNKNOWN'
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 50 of 80
This program writes five records, each of then containing a part of the array T. The data are written
without any format specification (often said "binary" mode).Then it reads the third record and stocks
it in array TR.
Example:
Notes:
REC is the record number: that's the number of the record to be read or written.
RECL is the record length: it's value specifies the length (in bytes) of each record. Here
REC=20*4 bytes.
First page
Very often, a program has to perform a computation several times using different values, producing
a single value each time. An example is the conversion of an angle in degrees to an equivalent in
radians in Example 1 of the previous chapter.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 51 of 80
In Fortran, such a computation can be defined as a function and referred to by a name followed by a
list of the values (called arguments) which it uses, in parentheses, i.e.
name([argument_list])
where argument_list is an optional list of arguments separated by commas. Note that the parentheses
must be included even if argument_list is omitted, i.e.
name()
Such a function reference can be used in the same way as a variable or array element, except that it
cannot be the object of an assignment. Like a variable or array element, a function reference is
evaluated and the value obtained is substituted for it in the expression in which it appears. The type
of a function is the type of the value so obtained.
Thus, in the above example, a REAL function DGTORD might be defined to convert an angle in degrees
to an equivalent in radians. The function would have a single argument, of type INTEGER,
representing the value of the angle in degrees, and would be evaluated to obtain the equivalent in
radians. The function might be used in an assignment statement like:
RADIAN = DGTORD(DEGREE)
The definition of a function must include a definition of its type and the number and types of its
arguments. In a function reference the number and type of the arguments must be as defined. Thus,
for example:
RADIAN = DGTORD(DEGREE,X)
would be an error.
As the above example illustrates, a function reference has an identical form to an array element, and
may be used in a similar context. Fortran distinguishes between the two by checking whether the
name has been declared as an array, and assuming that it is a function if it has not. Thus, for
example, if DGTORD were declared as:
REAL DGTORD(100)
ABS(X)
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 52 of 80
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 53 of 80
9.2.1.1 Type
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 54 of 80
Example:
FUNCTION FUN1(A,B,N)
REAL A(100)
INTEGER B
name(argument_list)
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 55 of 80
with respect to the number of arguments and the type of each argument.
For example:
REAL X(100)
. . .
RESULT = FUN1(X,J,10)
Example:
REAL X(5,10)
. . .
Y = FUN(X,5,10)
. . .
END
FUNCTION FUN(A,M,N)
REAL A(M,N)
. . .
Arrays as arguments
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 56 of 80
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 57 of 80
9.2.4 Examples
Note that, since the type of this function differs from that implied
by the first letter of its name, any program referring to it must
declare the name in a type specification, e.g.
REAL MEAN
name(argument_list) = expression
where:
name is the name of the statement function.
argument_list is a list of dummy arguments.
expression is an expression which may include constants, variables
and array elements defined in the same program unit, and
function references.
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 58 of 80
9.3.1 Rules
9.4 Subroutines
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 59 of 80
SUBROUTINE name[(argument_list)]
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 60 of 80
DATA PAGENO/0/
DO 10, DEGREE = 1,360
N = DEGREE-1
IF (N/40*40 .EQ. N) THEN
CALL HEADER(PAGENO,OUT)
ELSE IF (N/10*10 .EQ.N) THEN
WRITE(OUT,110)
END IF
WRITE(OUT,120)DEGREE,DGTORD(DEGREE)
10 CONTINUE
110 FORMAT(1X)
120 FORMAT(1X,I5,T10,F7.5)
END
Example 1
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 61 of 80
PROGRAM MAIN
EXTERNAL ANALYS
INTRINSIC SQRT
. . .
CALL SUB1(ANALYS,SQRT,A,B)
. . . .
END
SUBROUTINE SUB1(SUB,FUN,X,Y)
EXTERNAL FUN
. . .
CALL SUB(...)
. . .
CALL SUB2(FUN,X,Y)
. . .
END
SUBROUTINE SUB2(F,P,Q)
. . .
Q = F(P)
. . .
END
Example 2
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 62 of 80
Example:
each time the following function is executed, it prints a message
indicating how many times it has been referenced.
FUNCTION AVE(X,Y)
INTEGER COUNT
SAVE COUNT
DATA COUNT/0/
COUNT = COUNT+1
WRITE(6,10)COUNT
. . .
10 FORMAT(1X,'FUNCTION AVE REFERENCED',I3,' TIMES.')
. . .
END
First page
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 63 of 80
10.2.1 Arrays
10.2.2 Assignment
10.3.1 Concatenation
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 64 of 80
The form:
CHARACTER*len var [,var]...
The form:
CHARACTER var[*vlen] [,var[*vlen]]...
Example:
the following specification assigns a length of 4 characters to
the CHARACTER variables A and C, and 6 characters to B.
CHARACTER*4 A,B*6,C
Example:
CHARACTER*4 A(3,4),B(10,20)*6
10.2.2 CHARACTERAssignment
Example:
PROGRAM CHAREX
CHARACTER*4 A*3,B,C
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 65 of 80
A = 'END'
B = A
C = 'FINAL'
STOP
END
Results:
Value
A 'END'
B 'END '
C 'FINA'
10.3.1 Concatenation
Example:
Example:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 66 of 80
LANG(1:1) 'F'
LANG(1:7) 'FORTRAN'
LANG(2:3) 'OR'
LANG(7:7) 'N'
LANG(:4) 'FORT'
LANG(5:) 'RAN'
Example:
For input:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 67 of 80
For output:
Output:
bbWHATbbbFOR
PROGRAM CHRIN
CHARACTER*4 A,B
READ(1,200)A,B
200 FORMAT(A6,3X,A3)
STOP
END
Result:
(b represents a blank.)
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 68 of 80
their binary codes. Two coding schemes, ASCII and EBCDIC, are
in common use. The two collating sequences are different but have
the following rules in common:
or:
Examples:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 69 of 80
DOUBLE PRECISION
11.1.4 Expressions
11.1.5 Functions
11.2 COMPLEX
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 70 of 80
Dw.d
11.1.4 Expressions
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 71 of 80
11.1.5 Functions
11.2 COMPLEX
Fortran provides for the representation of complex numbers using
the type COMPLEX.
Example:
(3.0,-1.5).
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 72 of 80
Results:
A = ( 12.500, 8.400)
B = ( 6.500 9.600)
C = 0.610 + I 174.600
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 73 of 80
12.1 EQUIVALENCE
12.2 COMMON
12.3 BLOCKDATA
12.4.1 Arithmetic IF
12.1 EQUIVALENCE
An EQUIVALENCE statement is used to specify the sharing of
the same storage units by two or more variables or arrays.
Example:
PROGRAM EQUIV
COMPLEX*16 CMPLX(2)
REAL*8 TAMPON(4)
CHARACTER*8 STR
CHARACTER*1 TC(8)
EQUIVALENCE (TAMPON(1), CMPLX(1))
EQUIVALENCE (STR, TC(1))
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 74 of 80
STR = 'ABCDEFGH'
DO 10 I=1,4
TAMPON(I)=I
10 CONTINUE
PRINT *, 'TC(3)=', TC(3), ' TC(4)=', TC(4)
PRINT *, 'CMPLX(1)=', CMPLX(1), ' CMPLX(2)=', CMPLX(2)
END
Result:
TC(3)=C TC(4)=D
CMPLX(1)=(1.0,2.0) CMPLX(2)=(3.0,4.0)
12.2 COMMON
The COMMON statement specifies blocks of physical storage, called
common blocks that may be accessed by any of the functions or subroutines
of a program. Thus, the COMMON provides a global facility
based on storage association.
The common blocks may be named and are called named common blocks,
or may be unnamed and are called blank common.
Example:
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 75 of 80
12.3 BLOCKDATA
A block data program unit is used to provide values for data objects
in named common blocks.
Example:
12.4.1 Arithmetic IF
Its' a conditional GOTO. According to the value of an
INTEGER expression, this statement allows branching to one
of three specified labels.
Example:
IF(I+2) 10, 20 ,30
. . .
10 . . .
. . .
20 . . .
. . .
30 . . .
. . .
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 76 of 80
Example:
IF(10, 20 ,30, 40), I+2
. . .
10 . . .
. . .
20 . . .
. . .
30 . . .
. . .
40 . . .
. . .
First page
2. Develop in stages
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 77 of 80
While developing your program as above, think about the main variables
and arrays you will require to represent the information. Choose
names which suggest their usage and write down each name with
its type and dimensions (if an array) and a note of what it represents.
4. Modularise
6. Clarity
7. Testing
Once you have eliminated the syntax errors from your program and
subroutines, try running them using suitable test data. Calculate
what the results should be, and check that the actual results
correspond. If they do not, you will have to revise some of the
steps above to correct the errors in your logic. To determine
the cause of an error, you may have to insert extra WRITE
statements to print out the values of variables etc. at various stages.
8. Optimizing
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 78 of 80
First page
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 79 of 80
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010
Fortran 77 for beginners Page 80 of 80
-15- Copyright
We'd appreciate a request before you use these notes, partly to justify
distributing them, but also so we can distribute news of any updates.
First page
http://www.idris.fr/data/cours/lang/fortran/f90/F77.html 9/7/2010