Professional Documents
Culture Documents
Do While: Loop Outline Repetition
Do While: Loop Outline Repetition
Do While: Loop Outline Repetition
Repetition
Looping
A loop is a sequence of statements to be executed, in order, over and
over, until some condition is reached.
Fortran 90 has a loop construct known as the DO WHILE loop:
DO WHILE ( condition )
statement1
statement2
...
END DO
Here, the condition is a LOGICAL expression completely enclosed
in parentheses, just like in an IF block.
The sequence of statements between the DO WHILE statement and
the END DO statement is known as the loop body.
DO WHILE ( condition )
statement1
statement2
...
END DO
When a DO WHILE statement is encountered, the following sequence of events occurs, in order:
1. The condition (a LOGICAL expression completely enclosed in
parentheses) is evaluated, resulting in a value of either .TRUE.
or .FALSE.
2. If the condition evaluates to .FALSE., then the statements inside the loop body are skipped, and control is passed to the statement immediately after the END DO statement.
3. If the condition evaluates to .TRUE., then the statements inside
the loop body are executed in sequence.
4. When the END DO statement is encountered, the program jumps
back up to the associated DO WHILE statement and starts over
with Step 1.
statement_before
statement_before
DO WHILE ( condition )
statement_inside1
statement_inside2
...
statement_inside_last
END DO
statement_after
condition
False
True
statement_inside1
statement_inside2
.
.
.
statement_after
.
.
.
4
Input # chickens.
chickens < 0
True
Output error.
Prompt again.
Output valid.
5
False
An Infinite Loop
An infinite loop is a loop that never reaches its termination condition.
For example:
% cat infiniteloop.f90
PROGRAM infinite_loop
IMPLICIT NONE
INTEGER,PARAMETER :: my_number = 5
INTEGER :: your_number
PRINT *, "Enter an integer:"
READ *, your_number
PRINT *, "I had ", my_number, "."
DO WHILE (your_number < my_number)
PRINT *, "Your number is less than mine!"
END DO !! WHILE (your_number < my_number)
END PROGRAM infinite_loop
% f95 -o infiniteloop infiniteloop.f90
% infiniteloop
Enter an integer:
8
I had 5 .
% infiniteloop
Enter an integer:
3
I had 5 .
Your number is less than mine!
Your number is less than mine!
Your number is less than mine!
Your number is less than mine!
Your number is less than mine!
Your number is less than mine!
Your number is less than mine!
...
Ctrl - C
There are other kinds of executable statements that can occur inside
a DO WHILE loop, some of which well learn later in the semester.
10
11
12
1
What value would you like to stop counting at,
which must be greater than or equal to 1 .
0
ERROR: the final value 0
is less than the initial value
%
sum = 0
current_value = initial_value
DO WHILE (current_value <= final_value)
sum = sum + current_value
current_value = current_value + 1
END DO !! WHILE (current_value <= final_value)
1 .
dowhilecount
1
What value would you like to stop counting at,
which must be greater than or equal to 1 .
5
The sum of the integers from
13
through
is
15 .
14
sum = 0
current_value = initial_value
DO WHILE (current_value <= final_value)
sum = sum + current_value
current_value = current_value + 1
END DO !! WHILE (current_value <= final_value)
Snapshot of
Iteration After
#
Stmt #
N/A
1
N/A
2
1
3
1
4
1
5
1
6
2
3
2
4
2
5
2
6
3
3
3
4
3
5
3
6
4
3
4
4
4
5
4
6
5
3
5
4
5
5
5
6
6
3
Trace
Value of Value of
sum
count
0
garbage
0
1
0
1
1
1
1
2
1
2
1
2
3
2
3
3
3
3
3
3
6
3
6
4
6
4
6
4
10
4
10
5
10
5
10
5
15
5
15
6
15
6
15
6
Comment
15