C++ Loops
C++ Loops
Motivations
Suppose that you need to print a string (e.g.,
"Welcome to C++!") a hundred times. It would be
tedious to have to write the following statement a
hundred times:
cout << "Welcome to C++!" << endl;
So, how do you solve this problem?
Opening Problem
Problem:
100
times
cout
cout
cout
cout
cout
<<
<<
<<
<<
<<
"Welcome
"Welcome
"Welcome
"Welcome
"Welcome
to
to
to
to
to
C++!"
C++!"
C++!"
C++!"
C++!"
<<
<<
<<
<<
<<
endl;
endl;
endl;
endl;
endl;
cout
cout
cout
cout
cout
<<
<<
<<
<<
<<
"Welcome
"Welcome
"Welcome
"Welcome
"Welcome
to
to
to
to
to
C++!"
C++!"
C++!"
C++!"
C++!"
<<
<<
<<
<<
<<
endl;
endl;
endl;
endl;
endl;
int count = 0;
while (count < 100)
{
cout << "Welcome to C++!\n";
count++;
}
count = 0;
Loop
Continuation
Condition?
true
false
true
Statement(s)
(loop body)
(a)
false
(b)
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
10
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
11
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
12
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
13
animation
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
14
15
SentinelValue
Run
16
do-while Loop
SentinelValue
Run
Statement(s)
(loop body)
true
do
Loop
Continuation
Condition?
false
// Loop body;
Statement(s);
} while (loop-continuation-condition);
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
17
for Loops
for (initial-action; loop-continuationcondition; action-after-eachiteration)
{
// loop body;
Statement(s);
}
Initial-Action
Loop
Continuation
Condition?
int i;
for (i = 0; i < 100; i++)
{
cout << "Welcome to C++!\n";
}
i=0
false
(i < 100)?
false
true
Statement(s)
(loop body)
true
cout <<
"Welcome to C++\n";
Action-After-Each-Iteration
i++
(A)
(B)
18
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
Declare i
19
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
Execute initializer
i is now 0
20
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
(i < 2) is true
since i is 0
21
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
22
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
23
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
24
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
25
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
26
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
(i < 2) is false
since i is 2
27
animation
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
28
Note
The initial-action in a for loop can be a list of zero or more
comma-separated expressions. The action-after-eachiteration in a for loop can be a list of zero or more commaseparated statements. Therefore, the following two for
loops are correct. They are rarely used in practice,
however.
for (int i = 1; i < 100; cout << (i++));
29
Note
If the loop-continuation-condition in a for loop is omitted,
it is implicitly true. Thus the statement given below in (a),
which is an infinite loop, is correct. Nevertheless, it is
better to use the equivalent loop in (b) to avoid confusion:
for ( ; ; )
{
// Do something
}
(a)
Equivalent
This is better
while (true)
{
// Do something
}
(b)
30
Caution
Dont use floating-point values for equality checking in a loop control. Since floating-point
values are approximations for some values, using them could result in imprecise counter
values and inaccurate results. Consider the following code for computing 1 + 0.9 + 0.8 + ... +
0.1:
double item = 1;
double sum = 0;
while (item != 0) { // No guarantee item will be 0
sum += item;
item -= 0.1;
}
cout<<sum;
Variable item starts with 1 and is reduced by 0.1 every time the loop body is executed. The
loop should terminate when item becomes 0. However, there is no guarantee that item will be
exactly 0, because the floating-point arithmetic is approximated. This loop seems OK on the
surface, but it is actually an infinite loop.
See the following link for the internal representation of floating point numbers based on IEEE
754:
http://www.h-schmidt.net/FloatConverter/IEEE754.html
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
31
32
Run
Note: The small floating point insignificant fractions will actually affect the loop only in
the case of equality check. This is demonstrated in the example as the value 1.0 is
unreachable and therefore excluded from the summation. However, this loop is not infinite
as the condition is based on less than or equal condition. The loop will terminate as soon as
the value of the counter exceeds the value 1.0.
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
33
Equivalent
for ( ; loop-continuation-condition; )
{
// Loop body
(b)
}
A for loop in (a) in the following figure can generally be converted into the
following while loop in (b) except in certain special cases (see Review Question
3.19 for one of them):
for (initial-action;
loop-continuation-condition;
action-after-each-iteration)
{
// Loop body;
(a)
}
Equivalent
initial-action;
while (loop-continuation-condition)
{
// Loop body;
action-after-each-iteration;
(b)
}
34
Recommendations
Use the one that is most intuitive and comfortable for
you. In general, a for loop may be used if the number of
repetitions is counter-controlled, as, for example, when you
need to print a message 100 times.
A while loop may be used if the number of repetitions is
sentinel-controlled, as in the case of reading the numbers
until the input is 0. A do-while loop can be used to
replace a while loop if the loop body has to be executed
before testing the continuation condition.
35
Nested Loops
Problem: Write a program that uses nested for loops to print a
multiplication table.
TestMultiplicationTable
Run
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
36
Case Study:
Run
37
TestBreak.cpp
TestBreak
Run
TestContinue.cpp
TestContinue
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
38