General DO-Loop in Fortran

General DO-Loop with EXIT

The general DO-loop is actually very simple. But, to use it properly, you need to be very careful, since it may never stop. The general DO-loop has a form as follows:

DO
statements
END DO

Between DO and END DO, there are statements. These statements are executed over and over without any chance to get out of the DO-loop. Here is an example,

REAL  :: x, y, z
DO
READ(*,*) x
y = x*x
z = x*x*x
WRITE(*,*) x, ' square = ', y, ' cube = ', z
END DO

One iteration of this loop consists of reading a value for x, computing its square and cube to y and z, respectively, and displaying the results. Then, the execution goes back to the top and executes the four statements again. Consequently, this loop is executed over and over and has no chance to stop at all. A loop that never stops is usually referred to as an infinite loop. To stop the iteration of a DO-loop, we need something else.

The EXIT Statement

The EXIT is as simple as writing down the word EXIT. It is used to bail out the containing loop.

DO
statements-1
EXIT
statements-2
END DO

In the above, statements-1 is executed followed by the EXIT statement. Once the EXIT statement is reached, the control leaves the inner-most DO-loop that contains the EXIT statement. Therefore, in the above case,statements-2 will never be executed.

Since it must be some reason for bailing out a DO-loop, the EXIT statement is usually used with an IF or even an IF-THEN-ELSE-END IF statement in one of the following forms. Note that these are not the only cases in which you can use EXIT.

DO
statements-1
IF (logical-expression) EXIT
statements-2
END DO


DO
statements-1
IF (logical-expression) THEN
statements-THEN
EXIT
END IF
statements-2
END DO

For each iteration, statements in statements-1 are executed, followed the evaluation of the logical-expression. If the result is .FALSE., statements in statements-2 are executed. This completes one iteration and the control goes back to the top and executes statements-1 again for next iteration.

If the result of evaluating logical-expression is .TRUE., the first form will executes EXIT, which immediately stops the execution of the DO-loop. The next statement to be executed is the one following END DO.
For the second form, if the result of evaluating logical-expression is .TRUE., statements in statements-THEN are executed followed by the EXIT statement, which brings the execution to the statement following END DO. Therefore, statements in statements-THEN will do some “house-keeping” work before leaving the DO-loop. If there is no “house-keeping” work, the first form will suffice.

Examples

  •  The following code reads in values into variable x until the input value becomes negative. All input values are added to Sum. Note that the negative one is not added to Sum, since once the code sees such a negative value, EXIT is executed.
    INTEGER :: x, Sum

    Sum = 0
    DO
    READ(*,*) x
    IF (x < 0) EXIT
    Sum = Sum + x
    END DO

  • The following is an example that “simulates” a counting DO-loop using REAL variables. Variable x is initialized to the initial value Lower and serves as a control variable. Before any statement of the DO-loop is executed, the value of x is checked to see if it is greater than the final value Upper. If it is, EXIT is executed, leaving the loop. Otherwise, the loop body is executed and before goes back to the top, the control variable x must be increased by the step size Step. This loop will display -1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75 and 1.0, each of them is on a separate line.
    REAL, PARAMETER :: Lower = -1.0, Upper = 1.0, Step = 0.25
    REAL :: x

    x = Lower ! initialize the control variable (DON'T FORGET)
    DO
    IF (x > Upper) EXIT ! is it > final-value?
    WRITE(*,*) x ! no, do the loop body
    x = x + Step ! an increase by step-size
    END DO

  • In many cases, your program may expect an input satisfying certain conditions. DO-loop can help a lot. The following code keeps asking and checking if the input integer value is in the range of 0 and 10 inclusive. If it is not, the program warns the user and reads again until the input is in the stated range.
    INTEGER :: Input

    DO
    WRITE(*,*) 'Type an integer in the range of 0 and 10 please --> '
    READ(*,*) Input
    IF (0 <= Input .AND. Input <= 10) EXIT
    WRITE(*,*) 'Your input is out of range. Try again'
    END DO


Some Helpful Notes

  • One of the most commonly seen problem is forgetting to change the logical-expression that may cause an EXIT. The following DO-loop never stops and keeps displaying 5, 5, 5, 5, …, forever. The reason? The value of i is never changed.
    INTEGER  :: i

    i = 5
    DO
    IF (i < -2) EXIT
    WRITE(*,*) i
    END DO

    The following is another example:

    INTEGER :: i = 1, j = 5

    DO
    IF (j < 0) EXIT
    WRITE(*,*) i
    i = i + 1
    END DO

  • Sometimes we just forget to initialize the control-var . We really do not know what would be displayed since the value of i is unknown at the beginning of the DO and is certainly unknown after executing i = i – 1.
    INTEGER :: i

    DO
    IF (i <= 3) EXIT
    WRITE(*,*) i
    i = i - 1
    END DO

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s