Repetitive Instructions and DO Loops
Repetitive instructions: the DO loop
What is a DO loop?
When doing numerical calculations it is often necessary to do
the same calculation many times. An example might be
computing the Coriolis parameter at every latitude as in Exercise 5,
or finding 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. You could use a DO loop to write
a simple program that computes 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
How does it work?
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. Your DO loop indices should always be integer variables.
It's possible to use real variables for DO loop indices but this
can produce unexpected results, for reasons we will explain later.
- The amount that the index is increased with each
repetition of the loop is called the
increment or stride 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 and including 24.
- The CONTINUE statement is
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.
They are not required to appear in numerical order, though it's
best if they do in order to avoid confusion.
DO ... END DO
Almost all modern compilers (even for Fortran 77) allow DO loops to
be terminated with the END DO statement instead of using a numbered
statement. Using this approach the loop in the short program at the top of this page would be written
as follows:
DO I = 0, 24
SECOND = 3600 * I
PRINT *, 'HOUR = ', I, ' SECONDS =', SECOND
END DO
Notice that since there is no numbered termination statement, the DO at the
start of the loop is not followed by a statement number.
The DO...END DO construction is slightly simpler. However some DO loops can include
hundreds or thousands of statements with multiple levels of nested DO loops.
In such cases it is confusing to sort out where each loop starts and ends. A good practice is:
- For short loops, use the DO...END DO form for simplicity.
- For longer loops, especially complex ones, use loops that terminate with a numbered statement so that you can more
easily see where the loop ends.
Assignment: Construct a DO loop for the Coriolis parameter
computations in the previous exercise.
Next: Progrmming style and readability
Back to the main page for the programming tutorial.