Beruflich Dokumente
Kultur Dokumente
100
times
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
3
Introducing while Loops
int count = 0;
while (count < 100)
{
cout << "Welcome to C++!\n";
count++;
}
Loop
false false
Continuation (count < 100)?
Condition?
true true
Statement(s) cout << "Welcome to C++!\n";
(loop body) count++;
(a) (b)
SentinelValue Run
true Loop
Continuation
do 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-continuation- int i;
condition; action-after-each- for (i = 0; i < 100; i++)
iteration)
{ {
// loop body; cout << "Welcome to C++!\n";
Statement(s);
} }
Initial-Action i=0
Loop
false false
Continuation (i < 100)?
Condition?
true true
Statement(s) cout <<
(loop body) "Welcome to C++\n";
Action-After-Each-Iteration i++
(A) (B)
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
18
animation
}
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
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:
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
Example: Using for Loops
Problem: Write a program that sums a series that starts with
0.01 and ends with 1.0. The numbers in the series will
increment by 0.01, as follows: 0.01 + 0.02 + 0.03 and so on.
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.
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; initial-action;
loop-continuation-condition; Equivalent while (loop-continuation-condition)
action-after-each-iteration) {
{ // Loop body;
// Loop body; (a) action-after-each-iteration; (b)
} }
TestMultiplicationTable
Run
GreatestCommonDivisor Run
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
37
Using break and continue
Examples for using the break and continue
keywords:
TestBreak.cpp
TestBreak Run
TestContinue.cpp
TestContinue Run