« previous | next» |
3.2 Loops
Often it is useful to have repeated execution of a particular set of Fortran expressions. This can often be accomplished using the DO command. There are various ways for loops to function, but in general they are either based on counters where loops are continued for a certain number of iterations, or logical expressions where the loop terminates based on the value of a logical expression.
Counter Loops
Counter loops perform repetitive execution a number of times based on a control-variable. The control-variable is initialized to a value and incremented a step-size every time a particular list of commands is executed. When the control variable surpasses a specified threshold the execution halts. The general form of the counter controlled DO loop is:
DO control-variable = initial value,
limit, step-size
execution
list
END DO
Initial-value, limit, and step-size are all integer values that indicate the number of loops to be performed. These values can also be variables indicated previous to execution of the loop. Step-size is not required. If not indicated, the default step-size is one. The values for initial-value, limit, and step-size are indicated at the beginning of the loop by either numerical values of variables. These values cannot be changed during execution of the loop.
The execution list within a DO loop should be indented. This makes your code more readable, and it is easier to follow the logic. Often you will have DO loops within DO loops (nested DO loops), and in these cases indenting each new loop within the other loops allows you to follow the sequence of loop execution. Here is an example of a counter DO LOOP.
INTEGER :: max = 5
DO n=1, max
PRINT *, n,
n*2
END DO
This output:
1 2
2 4
3 6
4 8
5 10
Step-size was not indicated; so, it is one. The control-variable cannot be modified in the execution section of the loop, but it can be used in the execution statements. The following DO loop shows a loop nested within another loop:
DO n = 5, 1, -1 !
From 5 to 1 with a step size of minus 1
DO m = 1,2
PRINT *, n, m
END DO
END DO
Notice how by indenting the loops the order of execution is indicated. The most indented loop is performed in completion first. The output would be:
5 1
5 2
4 1
4 2
3 1
3 2
2 1
2 2
1 1
1 2
Loops can be labeled as another way to organize your nested loops. The previous nested loops could have been written:
outer: DO n = 5, 1, -1
! From 5 to 1 with a step size of minus 1
inner: DO m = 1,2
PRINT *, n, m
END DO inner
END DO outer
If you have many nested loops this could assist in helping
you and others follow your code.
LOGICAL LOOPS
Logical loops are loops that require fulfilling a logical criteria for completion of the loop. These can also be called DO-EXIT loops. The general form is:
DO
execution
statements
IF (logical
criteria) EXIT
execution
statements
END DO
The execution statements can be before and/or after the EXIT statement. When the IF statement is TRUE repetition is halted. Be careful when determining the logical criteria for your IF statement. If it never becomes TRUE you will be left with an infinite loop.
The most common use for a logical loop involves halting execution once a calculated value reaches a threshold. For example:
n = 1
DO
IF (n > 10)
EXIT
n = n + 1
END DO
You can also use user input to halt repetition. For example:
DO
PRINT *, "Enter
the radius of your circle."
READ *, r
PRINT *, "Area
is ", pi*r**2
PRINT *, "Do
you want to calculate another area?"
READ *, response
IF (response
== "n") EXIT
END DO
Granted, the parameter pi must have been defined previously and response would need to be defined as a character variable of length = 1 in the specification section of the program, but you see how the user input was used as a decision criteria.
A more modern and structured way of realizing a logical loop is with use of the DO WHILE - END DO expression:
DO WHILE expression
execution statements
END DO
Here the execution statements are performed as long as expression returns TRUE. When expression becomes FALSE the loop is broken and execution continues with the following statements. The equivalence between DO-EXIT and DO WHILE loops is portrayed below:
n = 1 DO IF (n > 10) EXIT n = n + 1 END DO |
← these two command list are equivalent → | n = 1 DO WHILE (n <= 10) n = n + 1 END DO |