Repetitive Instructions and DO Loops
When doing numerical calculations it is often necessary to do
the same calculation numerous times. An example might be
computing the solar elevation angle for each hour of the day.
Fortran provides a standardized set of instructions - called a
"DO
loop" - for performing these repetitive
calculations. An example of a repeated calculation using a DO
loop could be a simple program to compute the number of elapsed
seconds at each hour of the day, beginning at midnight:
PROGRAM MAIN
INTEGER I, SECOND
DO 10 I = 0, 24
SECOND = 3600 * I
PRINT *, 'HOUR = ', I, ' SECONDS =', SECOND
10 CONTINUE
STOP
END
The statement beginning with DO
controls the instructions that are to be repeated and the number
of times they are repeated. We need to look closely at each part
of the DO statement, from left to
right:
DO 10 I = 0, 24
- The word DO means that
this line is the first of the statements that will be
repeated.
- The number 10 is the statement number
of the last instruction to be repeated. Essentially, this
tells the system "we will repeat all statements from
here up to and including the statement that is numbered 10." This final statement
is sometimes referred to as the terminal
statement of the loop.
- The letter I is called
the index
of the DO loop. By default, the index increases by 1 each
time the loop is repeated.
- The number following the equals sign is the initial value of
the DO loop index. This is the value that is assigned to
the index the first time the loop is executed.
- The number following the comma is the limit of the DO
loop. Once the index exceeds the limit, the loop is no
longer repeated.
A verbal description what happens in a DO loop might be as follows:
Set the index
to its initial value.
Then, repeat all the instructions from the DO line down to the statement with
the given statement number.
After these instructions are completed, add 1 to the index.
Keep doing this until the index exceeds the limit.
Some details:
- The index does not have to be called I. It is a variable that is
treated the same as any other variable in Fortran. It is very strongly recommended
that your DO loop indices always be integer variables.
- The amount that the index is increased with each
repetition of the loop is called the increment of the
loop. The default increment is 1, as in the example
above, but you can specify the increment to be some other
value. You do this by adding a comma and the desired
increment after the limit. That is, the DO statement
above is equivalent to:
DO 10 I = 0, 24, 1
For example, if we wanted to do some calculations every
three hours, we would construct a DO
statement like:
DO 10 I = 0, 24, 3
This would perform the calculations in the loop for values
of I equal to 0, 3, 6 ... up to 24.
- The CONTINUE statement is
essentially a "do-nothing" statement. It is
recommended that the terminal statement of your DO loop always be a CONTINUE statement.
- Recall from our very first lesson that all statements had
to begin from the seventh (or later) position of a line.
The first five spaces are reserved for the statement
number of a numbered statement such as the
terminal statement of our DO
loop. The statement number must appear completely within
these first five spaces; that is, the number cannot spill
over into the sixth or following spaces. (It follows that
the largest possible number for a statement number is
99999.)
- Statement numbers must be unique; that is, a program
cannot include two statements with the same number.
Assignment: Construct a DO loop around the solar elevation
computations in the previous exercise,
so that you compute the solar elevation angle at each hour of the
day.