
D. Haugli Aer E 160 Aerospace Engineering
2/8/2005 DO-EXIT Loops, Page 1 Iowa State University
DO-EXIT Loops
The essential structure of a DO-EXIT loop is:
DO
<Insert executable statements here on one line after the other>
IF (<insert condition for exit here>) EXIT
<Insert additional executable statements here if needed>
END DO
The IF statement includes an executable statement (EXIT) on the same line, so does not require a
THEN or an END IF. Only one statement can be executed in this manner.
Example 1: This example would write out the even values of i from 2 to 10. Assume
that i and sum are both integers.
i=0
DO
i=i+2
WRITE (*,*) i
IF (i ==10) EXIT
END DO
Output:
2
4
6
8
10
Example 2: Add up all the even numbers from 2 to 10. Assume that i and sum are both
integers.
sum=0
i=0
DO
i=i+2
sum=sum+i
IF (i == 10) EXIT
END DO
WRITE (*,*) sum
The output in this case (the value of sum) would be 30.