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:
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,DO statements 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.REAL :: x, y, z DO READ(*,*) x y = x*x z = x*x*x WRITE(*,*) x, ' square = ', y, ' cube = ', z 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.DO statements-1 EXIT statements-2 END DO
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.
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.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
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
INTEGER :: x, Sum
Sum = 0
DO
READ(*,*) x
IF (x < 0) EXIT
Sum = Sum + x
END DO
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
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
The following is another example:
INTEGER :: i
i = 5
DO
IF (i < -2) EXIT
WRITE(*,*) i
END DO
INTEGER :: i = 1, j = 5
DO
IF (j < 0) EXIT
WRITE(*,*) i
i = i + 1
END DO
INTEGER :: i
DO
IF (i <= 3) EXIT
WRITE(*,*) i
i = i - 1
END DO