Programming in C++ Chapter 6
Programming in C++ Chapter 6
Looping
Dale/Weems
Chapter 6 Topics
Loops
What is a loop?
A loop is a repetition control structure
that causes a single statement or
block to be executed repeatedly
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
WHILE LOOP
FALSE
Expression
TRUE
body
statements
Count-Controlled Loops
Count-controlled loops contain
An
// 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; !!
!
!!
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
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
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
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; !!
!
!!
OUTPUT
4
3
2
1
Done
24
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
End-of-file controlled
loop
Flag controlled
loop
27
27
A Sentinel-controlled Loop
Requires a priming read
reading until EndOfFile requires priming
read too!
A
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
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
35
Nested Loops
initialize outer loop
while (outer loop condition)
...
initialize inner loop
while(inner loop condition)
{
inner loop processing and update
}
...
}
37 37
38