Docsity

DO-EXIT Loops: Aerospace Engineering Example from Iowa State University - Prof. Dana G. Ha, Study notes of Aerospace Engineering

Toggle sidebar
bg1
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.
pf2

Partial preview of the text

Download DO-EXIT Loops: Aerospace Engineering Example from Iowa State University - Prof. Dana G. Ha and more Study notes Aerospace Engineering in PDF only on Docsity!

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

IF () EXIT 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:

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.

D. Haugli Aer E 160 Aerospace Engineering

2/8/2005 DO-EXIT Loops, Page 2 Iowa State University

Example 3: Compute the product of all the even numbers from 2 to 10. Assume that i

and product are both integer.

product = 1 i = 0 DO i = i + 2 product = producti IF (i == 10) EXIT END DO WRITE (,*) product

The output in this case (the value of product) would be 3840.