Chapter 6
Looping
Dale/Weems
Chapter 6 Topics
while Statement Syntax
Using the End-of-File Condition to Control
Input Data
Using a while loop for Summing or Counting
for loop
Nested Loops
Loop Testing and Debugging
Loops
What is a loop?
A loop is a repetition control structure
that causes a single statement or
block to be executed repeatedly
Two Types of Loops
Count controlled loops
Repeat a statement or block a specified
number of times
Event-controlled loops
Repeat a statement or block until a
condition within the loop body changes
that causes the repetition to stop
4
While Statement
SYNTAX
while ( x-- > 0 )
{
.
.
// loop body
Loop body can be a single statement, a null
statement, or a block
5
When the expression is tested and found to be
false, the loop is exited and control passes to
the statement that follows the loop body
WHILE LOOP
FALSE
Expression
TRUE
body
statements
Count-Controlled Loops
Count-controlled loops contain
An
initialization of the loop control
variable
An expression to test if the proper
number of repetitions has been
completed
An update of the loop control variable to
be executed with each iteration of the
body
7
Count-Controlled Loop Example
int
count;
count = 4;
!
while( count > 0 )
{!
cout << count
!
count--;
!
}!
cout << Done <<
// Loop-control variable!
// Initialize loop variable!
! // Test expression!
<< endl; // Repeated action!
// Update loop variable!
endl;!
Count-controlled Loop
int
count;!
count = 4;
!
!
!
!!
!
while(count > 0) !
!
!!
{!
cout << count << endl;!
!!
count --;
!
!
!!
}!
cout << Done << endl;!
count
OUTPUT
Count-controlled Loop
int
count;!
!
count = 4;
!
!!
!
while(count > 0) !
{!
cout << count
!
count --;
!
}!
cout
count
!
!!
OUTPUT
<< endl; !!
!
!!
<< Done << endl;
10
Count-controlled Loop
int
!
count
count
count;!
=
4;
!!
while(count > 0) !TRUE !!
{!
cout << count << endl; !!
!
count --;
!
!
!!
}!
cout << Done << endl;!
OUTPUT
11
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !
!
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;!
count
!
!!
!!
OUTPUT
4
!!
12
Count-controlled Loop
int
count;!
!
count = 4;
!
!!
!
while(count > 0) !
{!
cout << count
!
count --;
!
count
!
!!
<< endl; !!
!
OUTPUT
4
!!
}!
cout
<< Done << endl;!
13
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !TRUE !
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;!
count
!
!!
!!
OUTPUT
4
!!
14
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !
!
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;!
count
!
!!
!!
OUTPUT
4
3
!!
15
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !
!
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;!
count
!
!!
!!
!!
OUTPUT
4
3
16
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!
!!
!
while(count > 0) !TRUE !!
{!
cout << count << endl; !!
!
count --;
!
!
!!
}!
cout << Done << endl;!
count
OUTPUT
4
3
17
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !
!
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;
count
!
!!
!!
!!
OUTPUT
4
3
2
18
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !
!
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;!
count
!
!!
!!
!!
OUTPUT
4
3
2
19
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !TRUE !!
count
!
{!
cout
<< count
<< endl; !!
!
count --;
}!
cout
!!
OUTPUT
4
3
2
<< Done << endl;
20
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!!
!
while(count > 0) !
!
{!
cout << count << endl;
!
count --;
!
!
}!
cout << Done << endl;!
count
!
!!
!!
!!
OUTPUT
4
3
2
1
21
Count-controlled Loop
int
count;!
!
count = 4;
!
!!
!
while(count > 0) !
{!
cout << count
!
count --;
!
count
!
!!
<< endl; !!
!
}!
cout
!!
OUTPUT
4
3
2
1
<< Done << endl;!
22
Count-controlled Loop
int
count;!
!
count = 4;
!
!
!
!!
!
while(count > 0) !FALSE!
{!
cout << count << endl; !!
!
count --;
!
!
!!
}!
cout << Done << endl;!
count
OUTPUT
4
3
2
1
23
Count-controlled Loop
int
count;!
!
count = 10;
!
!!
!
while(count >= 0)!
{!
cout << count
!
count --;
!
}!
cout
count
!
!!
<< endl; !!
!
<< Done << endl;!
!!
OUTPUT
4
3
2
1
Done
24
Types of Event-Controlled Loops
Sentinel controlled
Keep processing data until a special value that
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
25
25
Examples of Kinds of Loops
Count controlled loop
Read exactly 100
blood pressures
from a file
End-of-file controlled
loop
Read all the blood
pressures from a
file no matter how
many are there
26
26
Examples of Kinds of Loops
Sentinel controlled
loop
Read blood pressures
until a special value
selected by you (like -1)
is read
Flag controlled
loop
Read blood pressures
until a dangerously high
BP(200 or more) is read
27
27
A Sentinel-controlled Loop
Requires a priming read
reading until EndOfFile requires priming
read too!
A
priming read is the reading of
one set of data before the loop to
initialize the variables in the
expression
28
// Sentinel controlled loop!
const int END_VALUE = -1;!
total = 0;!
!
cout << Enter a blood pressure(-1 to stop) ;!
cin >> thisBP;!
!
while ( thisBP != END_VALUE) !// While not sentinel!
{!
total = total + thisBP;!
cout << Enter a blood pressure(-1 to stop);!
cin >> thisBP;!
}!
cout << total;!
29
End-of-File Controlled Loop
Uses
the fact that a file goes into the
fail state when you try to read a data
value beyond the end of the file to
control the loop
30
ifstream bpFile("infile.txt");!
double thisBP;!
int total = 0;!
!
bpFile >> thisBP;
// Priming read!
!
!
while(bpFile)
! // While last read successful!
{!
total += thisBP;!
bpFile >> thisBP; ! // Read another!
}!
!
cout << total;!
!
31
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!
}!
cout << total;!
32
Flag-controlled Loops
Initialize
a flag(to true or false)
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
33
!
countGoodReadings = 0;!
isSafe = true;
! // Initialize Boolean flag!
!
while(isSafe)!
{ !!
cin >> thisBP;!
if (thisBP >= 200)!
isSafe = false; // Change flag value!
!
else!
countGoodReadings++;!
}!
!
cout << countGoodReadings <<
endl;
34
Common Loop Uses
Count all data values
Count special data values
Sum data values
Keep track of current and previous values
35
Loop Design Considerations
- What is the condition that terminates the loop?
- How should the condition be initialized?
- How should the condition be updated?
- What guarantees this condition will eventually occur?
- What is the process to be repeated?
- How should the process be initialized?
- How should the process be updated?
- What is the state of the program on exiting the loop?
- Are "boundary conditions" handled correctly?
36
Nested Loops
initialize outer loop
while (outer loop condition)
...
initialize inner loop
while(inner loop condition)
{
inner loop processing and update
}
...
}
37 37
Designing Nested Loops
Begin with outer loop
When you get to where the inner loop
appears, make it a separate module and
come back to its design later
38
Loop Testing and Debugging
Test data should test all sections of program!
Beware of infinite loops -- program doesnt stop
Check loop termination condition, and watch for off-by-1
bugs(OBOBs)
Use get function for loops controlled by detection of \n
character
Use algorithm walk-through to verify pre- and postconditions
Trace execution of loop by hand with code walk-through
Use a debugger to run program in slow motion or use debug
output statements
39