Professional Documents
Culture Documents
الفصل الخامس للطباعة PDF
الفصل الخامس للطباعة PDF
Chapter 5: Looping
Topics
5.1 Introduction to Loops: The while Loop
5.2 Using the while loop for Input Validation
5.3 The Increment and Decrement Operators
5.4 Counters
5.5 The do-while loop
5.6 The for loop
5.7 Keeping a Running Total
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-2
1
11/21/2018
Topics (continued)
5.8 Sentinels
5.9 Deciding Which Loop to Use
5.10 Nested Loops
5.11 Breaking Out of a Loop
5.12 Using Files for Data Storage
5.13 Creating Good Test Data
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-3
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-4
2
11/21/2018
false
condition
true
statement(s)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-6
3
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-7
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-8
4
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-10
5
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-11
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-12
6
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-13
7
11/21/2018
Prefix Mode
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-15
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 © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-16
8
11/21/2018
Postfix Mode
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-17
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-18
9
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-19
5.4 Counters
• Counter: variable that is incremented or
decremented each time a loop repeats
• Can be used to control execution of the
loop (loop control variable)
• Must be initialized before entering loop
• May be incremented/decremented either
inside the loop or in the loop test
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-20
10
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-21
11
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-23
statement(s)
true
condition
false
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-24
12
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-25
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-26
13
11/21/2018
do {
// code to display menu
// and perform actions
cout << "Another choice? (Y/N) ";
} while (choice =='Y'||choice =='y');
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-27
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-28
14
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-29
initialization
code
update
code
false
test
true
statement(s)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-30
15
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-31
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-32
16
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-33
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-34
17
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-35
5.8 Sentinels
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-36
18
11/21/2018
Sentinel Example
int total = 0;
cout << "Enter points earned "
<< "(or -1 to quit): ";
cin >> points;
while (points != -1) // -1 is the sentinel
{
total += points;
cout << "Enter points earned: ";
cin >> points;
}
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-37
19
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-40
20
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-41
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-42
21
11/21/2018
File Types
• Text file – contains information encoded as
text, such as letters, digits, and punctuation.
Can be viewed with a text editor such as
Notepad.
• Binary file – contains binary (0s and 1s)
information that has not been encoded as
text. It cannot be viewed with a text editor.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-44
22
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-45
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-46
23
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-47
24
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-50
25
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-51
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-52
26
11/21/2018
User-Specified Filenames
• Program can prompt user to enter the names
of input and/or output files. This makes the
program more versatile.
• Filenames can be read into string objects. The
C-string representation of the string object can
then be passed to the open function:
cout << "Which input file? ";
cin >> inputFileName;
inFile.open(inputFileName.c_str());
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-54
27
11/21/2018
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-55
Chapter 5: Looping
28