You are on page 1of 20

Techniques of Programming

CSCI 131, Fall 2004


Lecture 11
Loops

1
Loops

 Sometimes we would like to execute


a task repeatedly.
 To do so, we use a loop.
 A loop is a repetition control
structure.
 It causes a single statement or block
to be executed repeatedly.

2
while statement
SYNTAX

while ( Expression ) {
.
. // loop body
.
}

NOTE: Loop body can be a single statement, a


null statement, or a block.
3
Execution of while loops
If the expression is true, the body of the loop is executed, then
the expression is tested again.
If the expression is false, control goes to the end of the loop, and
continues from there.

FALSE
expression

TRUE
body
statement

4
Two types of loops

count controlled loops


repeat a specified number of times

event-controlled loops
some condition within the loop body
changes and this causes the
repeating to stop
5
Count controlled loops

A count-controlled loop contains:

1) an intialization of the loop control variable

2) an expression to test for continuing the loop

3) an update of the loop control variable to be executed with


each iteration of the body

6
Example
int count ;

count = 4; // initialize loop variable

while (count > 0) { // test expression

cout << count << endl ; // repeated action

count -- ; // update loop variable


} // while count is positive
cout << “Done” << endl ;

7
Trace and output
count
4
OUTPUT: 4
3 3
2 2
1 1
0 Done

so loop terminates

8
Count controlled loop example

myInfile contains 100 blood pressures.

Use a while loop to read the 100 blood


pressures and find their total.

9
The program
ifstream myInfile ;
int ThisBP ;
int Total ;

int count ;

count = 0 ; // initialize

while ( count < 100 ) { // test expression

myInfile >> ThisBP ;


Total = Total + ThisBP ;
count++ ; // update

} //while still have BPs to read


10
cout << “The total = “ << Total << endl ;
Event Controlled loops
 Sentinel controlled
keep processing data until a special value
which is not a possible data value is entered
to indicate that processing should stop.
End-of-file controlled
keep processing data as long as there is more
data in the file.
 Flag controlled
keep processing data until the value of a flag
changes in the loop body.
11
Examples of types of loops

Count controlled loop Read exactly 100


blood pressures
from a file.

End-of-file controlled Read all the blood


loop pressures from a
file no matter how
many are there.
12
Examples of types of loops
Sentinel controlled Read blood pressures
loop until a special value
(like -1) selected by
you is read.
Flag controlled Read blood pressures
loop until a dangerously
high BP (200 or more)
is read.
13
A sentinal controlled loop

 Requires a “priming read”

 “Primingread” means you read one set


of data before the while

14
Example
// Sentinel controlled loop

Total = 0;

cout << “Enter a blood pressure (-1 to stop ) ”;


cin >> ThisBP; //priming read

while (ThisBP != -1) { // while not sentinel


Total = Total + ThisBP;
cout << “Enter a blood pressure (-1 to stop ) ”;
cin >> ThisBP;
} // while still have valid BPs to read
cout << Total;
15
End of file control

 depends on fact that a file goes into


fail state when you try to read a data
value beyond the end of the file.

16
End of file Example
Total = 0;

myInfile >> ThisBP; // priming read

while (myInfile) { // while last read successful


Total = Total + ThisBP;
myInfile >> ThisBP; // read another
} // while

cout << Total; 17


Ending input at the keyboard
Total = 0;
cout << “Enter blood pressure (Ctrl-Z to stop)”;
cin >> ThisBP; // priming read
while (cin) { // while last read successful
Total = Total + ThisBP;
cout << “Enter blood pressure”;
cin >> ThisBP; // read another
} // while
cout << Total;
18
flag controlled loops

 You initialize a flag (usually to 1 or 0)


 Use meaningful name for the flag
 A condition in the loop body changes the
value of the flag
 Test for the flag in the loop test
expression

19
flag controlled example
CountGoodReadings = 0;
Safe = 1; // initialize flag to 1
while (Safe) {
cin >> ThisBP;
if ( ThisBP >= 200 )
Safe = 0; // change flag to 0
else
CountGoodReadings++;
} // while
cout << CountGoodReadings << endl; 20

You might also like