Chapter 05
Chapter 05
Tenth Edition
Chapter 5
Looping
5-2
Topics 1 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-3
Topics 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-5
while (condition)
{ statement(s);
}
condition is evaluated
–if it is true, the statement(s) are executed,
and then condition is evaluated again
–if it is false, the loop is exited
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-7
int val = 5;
while (val >= 0)
{ cout << val << " ";
val = val - 1;
}
• produces output:
5 4 3 2 1 0
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-8
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-13
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-14
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-15
Prefix Mode
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-16
x = ++y; // y is incremented to 2,
// then 2 is assigned to x
cout << x
<< " " << y; // Displays 2 2
x = --y; // y is decremented to 1,
// then 1 is assigned to x
cout << x
<< " " << y; // Displays 1 1
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-17
Postfix Mode
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-18
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-19
5.4 Counters
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-21
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-22
5.6 Sentinels
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-25
Sentinel Example
int total = 0;
cout << "Enter points earned "
<< "(or -1 to finish): ";
cin >> points;
while (points != -1) // -1 is the sentinel
{
total += points;
cout << "Enter points earned: ";
cin >> points;
}
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-26
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-27
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-28
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-29
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-30
do {
// code to display menu
// and perform actions
cout << "Another choice? (Y/N) ";
} while ((choice =='Y')||(choice=='y'));
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-31
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-33
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-34
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-35
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-36
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-37
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-38
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-39
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-41
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-42
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-43
File Types
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-45
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-46
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-47
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-49
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-51
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-52
User-Specified Filenames
• A program can prompt the user to enter the names
of input and/or output files. This makes the
program more versatile.
• Filenames can be read into string objects. In C++
prior to C++ 11, the C-string representation of the
string object can be passed to the open function:
cout << "Which input file? ";
cin >> inputFileName;
inFile.open(inputFileName.c_str());
• In C++ 11, the string object can be passed to the
open() function directly.
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-53
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-54
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
5-56
Copyright
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved