SlideShare une entreprise Scribd logo
1  sur  50
1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
Chapter 5
Control Structures II (Repetition)
2
Objectives (1 of 2)
• In this chapter, you will:
• Learn about repetition (looping) control structures
• Learn how to use a while loop in a program
• Explore how to construct and use counter-controlled, sentinel-controlled, flag-
controlled, and EOF-controlled repetition structures
• Learn how to use a for loop in a program
• Learn how to use a do…while loop in a program
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
3
Objectives (2 of 2)
• Examine break and continue statements
• Discover how to form and use nested control structures
• Learn how to avoid bugs by avoiding patches
• Learn how to debug loops
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
4
Why Is Repetition Needed?
• Repetition allows efficient use of variables
• It is possible to input, add, and average multiple numbers using a limited
number of variables
• Consider the code to determine the average number of calories burned each
day doing regular exercise
• Method 1: Declare a variable for each day and enter the number of calories burned,
add the values and store in a variable for the week’s total, and divide the total by 7 to
find the average
• Method 2: Create a loop that reads a number into a variable and adds it to a variable
that contains the sum of the numbers (only two variables needed)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
5
while Looping (Repetition) Structure (1 of 3)
• A while loop is one of three repetition, or looping structures in C++
• Syntax of the while statement
• The statement can be simple or compound
• The expression acts as a decision maker and is usually a logical expression
• The statement is called the body of the loop
• The parentheses are part of the syntax
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
6
while Looping (Repetition) Structure (2 of 3)
FIGURE 5-1 while loop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
• The expression provides an entry condition to the loop
• The statement (body of the loop) continues to execute until the expression
is no longer true
• An infinite loop continues to execute endlessly
7
while Looping (Repetition) Structure (3 of 3)
• The preceding while loop produces the following output:
0 5 10 15 20
• The variable i in Example 5-1 is called the loop control variable (LCV)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
8
while Looping (Repetition) Structure (cont’d.)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
9
Case 1: Counter-Controlled while Loops
• When you know exactly how many times the statements need to be executed
• Use a counter-controlled while loop
counter = 0; //initialize the loop control variable
while (counter < N) //test the loop control variable
{
.
.
.
counter++; //update the loop control variable
.
.
.
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
10
Case 2: Sentinel-Controlled while Loops
• A sentinel variable is tested in the condition
• The loop ends when the sentinel is encountered
• The following is an example of a sentinel-controlled while loop:
cin >> variable; //initialize the loop control variable
while (variable != sentinel) //test the loop control
variable
{
.
.
.
cin >> variable; //update the loop control variable
.
.
.
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
11
Example 5-5: Telephone Digits
• Example 5-5 provides an example of a sentinel-controlled loop
• The program converts uppercase letters to their corresponding telephone digit
• The sentinel value is #
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
12
Case 3: Flag-Controlled while Loops
• Flag-controlled while loop: uses a bool variable to control the loop
isFound = false; //initialize the loop control variable
while (!isFound) //test the loop control variable
{
.
.
.
if (expression)
isFound = true; //update the loop control variable
.
.
.
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
13
Number Guessing Game
• Example 5-6 implements a number guessing game using a flag-controlled
while loop
• Uses the function rand of the header file cstdlib to generate a random
number
• rand() returns an int value between 0 and 32767
• To convert to an integer >= 0 and < 100:
rand() % 100
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
14
Case 4: EOF-Controlled while Loops (1 of 2)
• An end-of-file (EOF)-controlled while loop is a good choice when it is difficult
to select a sentinel value
• The logical value returned by cin can determine if there is no more input
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
15
Case 4: EOF-Controlled while Loops (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
16
eof Function
• The function eof can determine the end of file status
• eof is a member of data type istream
• Syntax for the function eof
• istreamVar is an input stream variable, such as cin
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
17
More on Expressions in while Statements
• The expression in a while statement can be complex
• Example
while ((noOfGuesses < 5) && (!isGuessed))
{
. . .
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
18
Programming Example: Fibonacci Number (1 of 3)
• Consider the following sequence of numbers:
1, 1, 2, 3, 5, 8, 13, 21, 34, ....
• Called the Fibonacci sequence
• Given the first two numbers of the sequence (say, a1 and a2)
• nth number an, n >= 3, of this sequence is given by: an = an-1 + an-2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
19
Programming Example: Fibonacci Number (2 of 3)
• Fibonacci sequence
• nth Fibonacci number
• a2 = 1
• a1 = 1
• Determine the nth number an, n >= 3
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
20
Programming Example: Fibonacci Number (3 of 3)
• Suppose a2 = 6 and a1 = 3
• a3 = a2 + a1 = 6 + 3 = 9
• a4 = a3 + a2 = 9 + 6 = 15
• Write a program that determines the nth Fibonacci number, given the first two
numbers
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
21
Programming Example: Input and Output
• Input: first two Fibonacci numbers and the desired Fibonacci number
• Output: nth Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
22
Programming Example: Problem Analysis and Algorithm Design
• Algorithm
• Get the first two Fibonacci numbers
• Get the desired Fibonacci number
- Get the position, n, of the number in the sequence
• Calculate the next Fibonacci number
- Add the previous two elements of the sequence
• Repeat Step 3 until the nth Fibonacci number is found
• Output the nth Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
23
Programming Example: Variables
int previous1; //variable to store the first Fibonacci number
int previous2; //variable to store the second Fibonacci number
int current; //variable to store the current Fibonacci number
int counter; //loop control variable
int nthFibonacci; //variable to store the desired
//Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
24
Programming Example: Main Algorithm (1 of 4)
• Prompt the user for the first two numbers—that is, previous1 and
previous2
• Read (input) the first two numbers into previous1 and previous2
• Output the first two Fibonacci numbers
• Prompt the user for the position of the desired Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
25
Programming Example: Main Algorithm (2 of 4)
• Read the position of the desired Fibonacci number into nthFibonacci
• if (nthFibonacci == 1)
The desired Fibonacci number is the first Fibonacci number; copy the value of
previous1 into current
• else if (nthFibonacci == 2)
The desired Fibonacci number is the second Fibonacci number; copy the value of
previous2 into current
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
26
Programming Example: Main Algorithm (3 of 4)
• else calculate the desired Fibonacci number as follows:
- Start by determining the third Fibonacci number
- Initialize counter to 3 to keep track of the calculated Fibonacci numbers.
- Calculate the next Fibonacci number, as follows:
current = previous2 + previous1;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
27
Programming Example: Main Algorithm (4 of 4)
• Assign the value of previous2 to previous1
• Assign the value of current to previous2
• Increment counter
• Repeat until Fibonacci number is calculated:
while (counter <= nthFibonacci)
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;
}
• Output the nth Fibonacci number, which is current
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
28
for Looping (Repetition) Structure (1 of 7)
• for loop: called a counted or indexed for loop
• Syntax of the for statement
• The initial statement, loop condition, and update statement
are called for loop control statements
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
29
for Looping (Repetition) Structure (2 of 7)
FIGURE 5-2 for loop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
30
for Looping (Repetition) Structure (3 of 7)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
31
for Looping (Repetition) Structure (4 of 7)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
32
for Looping (Repetition) Structure (5 of 7)
• The following is a semantic error:
• The following is a legal (but infinite) for loop:
for (;;)
cout << "Hello" << endl;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
33
for Looping (Repetition) Structure (6 of 7)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
34
for Looping (Repetition) Structure (7 of 7)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
35
do…while Looping (Repetition) Structure (1 of 6)
• Syntax of a do...while loop
• The statement executes first, and then the expression is evaluated
• As long as expression is true, loop continues
• To avoid an infinite loop, body must contain a statement that makes the
expression false
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
36
do…while Looping (Repetition) Structure (2 of 6)
• The statement can be simple or compound
• Loop always iterates at least once
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
37
do…while Looping (Repetition) Structure (3 of 6)
FIGURE 5-3 do. . .while loop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
38
do…while Looping (Repetition) Structure (4 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
39
do…while Looping (Repetition) Structure (5 of 6)
• Note that while and for loops are pretest loops
• It is possible that these loops many never activate due to entry conditions
• In contrast, do. . .while loops are posttest loops
• These loops always execute at least once
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
40
do…while Looping (Repetition) Structure (6 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
41
Choosing the Right Looping Structure
• All three loops have their place in C++
• If you can determine in advance the number of repetitions needed, the for loop is
the correct choice
• If you do not know and cannot determine in advance the number of repetitions
needed, and it could be zero, use a while loop
• If you do not know and cannot determine in advance the number of repetitions
needed, and it is at least one, use a do...while loop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
42
break and continue Statements (1 of 2)
• break and continue alter the flow of control
• break statement is used for two purposes:
• To exit early from a loop
• To skip the remainder of a switch structure
• After break executes, the program continues with the first statement after the
structure
• A break statement in a loop can eliminate the use of certain (flag) variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
43
break and continue Statements (2 of 2)
• continue is used in while, for, and do…while structures
• When executed in a loop
• It skips remaining statements and proceeds with the next iteration of the loop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
44
Nested Control Structures (1 of 2)
• To create the following pattern:
*
**
***
****
*****
• We can use the following code:
for (i = 1; i <= 5; i++) //Line 1
{ //Line 2
for (j = 1; j <= i; j++) //Line 3
cout << "*"; //Line 4
cout << endl; //Line 5
} //Line 6
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
45
Nested Control Structures (2 of 2)
• What is the result if we replace the first for statement with this?
for (i = 5; i >= 1; i--)
• Answer:
*****
****
***
**
*
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
46
Avoiding Bugs by Avoiding Patches
• A software patch is a piece of code written on top of an existing piece of code
• Intended to fix a bug in the original code
• Some programmers address the symptom of the problem by adding a software
patch
• A programmer should instead resolve the underlying issue
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
47
Debugging Loops
• Loops are harder to debug than sequence and selection structures
• Use a loop invariant
• Set of statements that remains true each time the loop body is executed
• The most common error associated with loops is off-by-one
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
48
Quick Review (1 of 3)
• C++ has three looping (repetition) structures:
• while, for, and do…while
• while, for, and do are reserved words
• while and for loops are called pretest loops
• do...while loop is called a posttest loop
• while and for may not execute at all, but do...while always executes at
least once
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
49
Quick Review (2 of 3)
• In a while loop:
• The expression is the decision maker
• The statement is the body of the loop
• A while loop can be:
• Counter-controlled
• Sentinel-controlled
• EOF-controlled
• In the Windows console environment, the end-of-file marker is entered using
Ctrl+z
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom
50
Quick Review (3 of 3)
• A for loop simplifies the writing of a counter-controlled while loop
• Putting a semicolon at the end of the for loop is a semantic error
• Executing a break statement in the body of a loop immediately terminates the
loop
• Executing a continue statement in the body of a loop skips to the next
iteration
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom

Contenu connexe

Tendances

9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath CommunityRohit Radhakrishnan
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8IndicThreads
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
Cso gaddis java_chapter5
Cso gaddis java_chapter5Cso gaddis java_chapter5
Cso gaddis java_chapter5mlrbrown
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2Marut Singh
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04Terry Yoast
 
Object-oriented Modeling with OptimJ
Object-oriented Modeling with OptimJObject-oriented Modeling with OptimJ
Object-oriented Modeling with OptimJPatrick Viry
 

Tendances (14)

9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
Cso gaddis java_chapter5
Cso gaddis java_chapter5Cso gaddis java_chapter5
Cso gaddis java_chapter5
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
The Java Carputer
The Java CarputerThe Java Carputer
The Java Carputer
 
Object-oriented Modeling with OptimJ
Object-oriented Modeling with OptimJObject-oriented Modeling with OptimJ
Object-oriented Modeling with OptimJ
 

Similaire à 9781337102087 ppt ch05

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressionspullaravikumar
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementDr. Ahmed Al Zaidy
 
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docxsleeperharwell
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentationdgdotson
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdfTanayKashid
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsAmitaSuri
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyDr. Ahmed Al Zaidy
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application SecurityDr. Ahmed Al Zaidy
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationdgdotson
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentationdgdotson
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Steve Guinan
 
Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6Steve Guinan
 
TMF2014 Thinksoft Automation Presentation
TMF2014 Thinksoft Automation PresentationTMF2014 Thinksoft Automation Presentation
TMF2014 Thinksoft Automation PresentationKJR
 

Similaire à 9781337102087 ppt ch05 (20)

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentals
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
 
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
Chapter 3 Basic Cryptography
Chapter 3 Basic CryptographyChapter 3 Basic Cryptography
Chapter 3 Basic Cryptography
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentation
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and Technology
 
Chapter 9 Client and application Security
Chapter 9 Client and application SecurityChapter 9 Client and application Security
Chapter 9 Client and application Security
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7
 
Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6
 
JavaMicroBenchmarkpptm
JavaMicroBenchmarkpptmJavaMicroBenchmarkpptm
JavaMicroBenchmarkpptm
 
TMF2014 Thinksoft Automation Presentation
TMF2014 Thinksoft Automation PresentationTMF2014 Thinksoft Automation Presentation
TMF2014 Thinksoft Automation Presentation
 

Plus de Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry Yoast
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 

Plus de Terry Yoast (17)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 

Dernier

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Dernier (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 

9781337102087 ppt ch05

  • 1. 1 C++ Programming: From Problem Analysis to Program Design, Eighth Edition Chapter 5 Control Structures II (Repetition)
  • 2. 2 Objectives (1 of 2) • In this chapter, you will: • Learn about repetition (looping) control structures • Learn how to use a while loop in a program • Explore how to construct and use counter-controlled, sentinel-controlled, flag- controlled, and EOF-controlled repetition structures • Learn how to use a for loop in a program • Learn how to use a do…while loop in a program © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 3. 3 Objectives (2 of 2) • Examine break and continue statements • Discover how to form and use nested control structures • Learn how to avoid bugs by avoiding patches • Learn how to debug loops © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 4. 4 Why Is Repetition Needed? • Repetition allows efficient use of variables • It is possible to input, add, and average multiple numbers using a limited number of variables • Consider the code to determine the average number of calories burned each day doing regular exercise • Method 1: Declare a variable for each day and enter the number of calories burned, add the values and store in a variable for the week’s total, and divide the total by 7 to find the average • Method 2: Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers (only two variables needed) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 5. 5 while Looping (Repetition) Structure (1 of 3) • A while loop is one of three repetition, or looping structures in C++ • Syntax of the while statement • The statement can be simple or compound • The expression acts as a decision maker and is usually a logical expression • The statement is called the body of the loop • The parentheses are part of the syntax © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 6. 6 while Looping (Repetition) Structure (2 of 3) FIGURE 5-1 while loop © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom • The expression provides an entry condition to the loop • The statement (body of the loop) continues to execute until the expression is no longer true • An infinite loop continues to execute endlessly
  • 7. 7 while Looping (Repetition) Structure (3 of 3) • The preceding while loop produces the following output: 0 5 10 15 20 • The variable i in Example 5-1 is called the loop control variable (LCV) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 8. 8 while Looping (Repetition) Structure (cont’d.) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 9. 9 Case 1: Counter-Controlled while Loops • When you know exactly how many times the statements need to be executed • Use a counter-controlled while loop counter = 0; //initialize the loop control variable while (counter < N) //test the loop control variable { . . . counter++; //update the loop control variable . . . } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 10. 10 Case 2: Sentinel-Controlled while Loops • A sentinel variable is tested in the condition • The loop ends when the sentinel is encountered • The following is an example of a sentinel-controlled while loop: cin >> variable; //initialize the loop control variable while (variable != sentinel) //test the loop control variable { . . . cin >> variable; //update the loop control variable . . . } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 11. 11 Example 5-5: Telephone Digits • Example 5-5 provides an example of a sentinel-controlled loop • The program converts uppercase letters to their corresponding telephone digit • The sentinel value is # © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 12. 12 Case 3: Flag-Controlled while Loops • Flag-controlled while loop: uses a bool variable to control the loop isFound = false; //initialize the loop control variable while (!isFound) //test the loop control variable { . . . if (expression) isFound = true; //update the loop control variable . . . } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 13. 13 Number Guessing Game • Example 5-6 implements a number guessing game using a flag-controlled while loop • Uses the function rand of the header file cstdlib to generate a random number • rand() returns an int value between 0 and 32767 • To convert to an integer >= 0 and < 100: rand() % 100 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 14. 14 Case 4: EOF-Controlled while Loops (1 of 2) • An end-of-file (EOF)-controlled while loop is a good choice when it is difficult to select a sentinel value • The logical value returned by cin can determine if there is no more input © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 15. 15 Case 4: EOF-Controlled while Loops (2 of 2) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 16. 16 eof Function • The function eof can determine the end of file status • eof is a member of data type istream • Syntax for the function eof • istreamVar is an input stream variable, such as cin © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 17. 17 More on Expressions in while Statements • The expression in a while statement can be complex • Example while ((noOfGuesses < 5) && (!isGuessed)) { . . . } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 18. 18 Programming Example: Fibonacci Number (1 of 3) • Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, .... • Called the Fibonacci sequence • Given the first two numbers of the sequence (say, a1 and a2) • nth number an, n >= 3, of this sequence is given by: an = an-1 + an-2 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 19. 19 Programming Example: Fibonacci Number (2 of 3) • Fibonacci sequence • nth Fibonacci number • a2 = 1 • a1 = 1 • Determine the nth number an, n >= 3 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 20. 20 Programming Example: Fibonacci Number (3 of 3) • Suppose a2 = 6 and a1 = 3 • a3 = a2 + a1 = 6 + 3 = 9 • a4 = a3 + a2 = 9 + 6 = 15 • Write a program that determines the nth Fibonacci number, given the first two numbers © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 21. 21 Programming Example: Input and Output • Input: first two Fibonacci numbers and the desired Fibonacci number • Output: nth Fibonacci number © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 22. 22 Programming Example: Problem Analysis and Algorithm Design • Algorithm • Get the first two Fibonacci numbers • Get the desired Fibonacci number - Get the position, n, of the number in the sequence • Calculate the next Fibonacci number - Add the previous two elements of the sequence • Repeat Step 3 until the nth Fibonacci number is found • Output the nth Fibonacci number © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 23. 23 Programming Example: Variables int previous1; //variable to store the first Fibonacci number int previous2; //variable to store the second Fibonacci number int current; //variable to store the current Fibonacci number int counter; //loop control variable int nthFibonacci; //variable to store the desired //Fibonacci number © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 24. 24 Programming Example: Main Algorithm (1 of 4) • Prompt the user for the first two numbers—that is, previous1 and previous2 • Read (input) the first two numbers into previous1 and previous2 • Output the first two Fibonacci numbers • Prompt the user for the position of the desired Fibonacci number © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 25. 25 Programming Example: Main Algorithm (2 of 4) • Read the position of the desired Fibonacci number into nthFibonacci • if (nthFibonacci == 1) The desired Fibonacci number is the first Fibonacci number; copy the value of previous1 into current • else if (nthFibonacci == 2) The desired Fibonacci number is the second Fibonacci number; copy the value of previous2 into current © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 26. 26 Programming Example: Main Algorithm (3 of 4) • else calculate the desired Fibonacci number as follows: - Start by determining the third Fibonacci number - Initialize counter to 3 to keep track of the calculated Fibonacci numbers. - Calculate the next Fibonacci number, as follows: current = previous2 + previous1; © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 27. 27 Programming Example: Main Algorithm (4 of 4) • Assign the value of previous2 to previous1 • Assign the value of current to previous2 • Increment counter • Repeat until Fibonacci number is calculated: while (counter <= nthFibonacci) { current = previous2 + previous1; previous1 = previous2; previous2 = current; counter++; } • Output the nth Fibonacci number, which is current © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 28. 28 for Looping (Repetition) Structure (1 of 7) • for loop: called a counted or indexed for loop • Syntax of the for statement • The initial statement, loop condition, and update statement are called for loop control statements © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 29. 29 for Looping (Repetition) Structure (2 of 7) FIGURE 5-2 for loop © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 30. 30 for Looping (Repetition) Structure (3 of 7) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 31. 31 for Looping (Repetition) Structure (4 of 7) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 32. 32 for Looping (Repetition) Structure (5 of 7) • The following is a semantic error: • The following is a legal (but infinite) for loop: for (;;) cout << "Hello" << endl; © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 33. 33 for Looping (Repetition) Structure (6 of 7) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 34. 34 for Looping (Repetition) Structure (7 of 7) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 35. 35 do…while Looping (Repetition) Structure (1 of 6) • Syntax of a do...while loop • The statement executes first, and then the expression is evaluated • As long as expression is true, loop continues • To avoid an infinite loop, body must contain a statement that makes the expression false © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 36. 36 do…while Looping (Repetition) Structure (2 of 6) • The statement can be simple or compound • Loop always iterates at least once © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 37. 37 do…while Looping (Repetition) Structure (3 of 6) FIGURE 5-3 do. . .while loop © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 38. 38 do…while Looping (Repetition) Structure (4 of 6) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 39. 39 do…while Looping (Repetition) Structure (5 of 6) • Note that while and for loops are pretest loops • It is possible that these loops many never activate due to entry conditions • In contrast, do. . .while loops are posttest loops • These loops always execute at least once © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 40. 40 do…while Looping (Repetition) Structure (6 of 6) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 41. 41 Choosing the Right Looping Structure • All three loops have their place in C++ • If you can determine in advance the number of repetitions needed, the for loop is the correct choice • If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a while loop • If you do not know and cannot determine in advance the number of repetitions needed, and it is at least one, use a do...while loop © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 42. 42 break and continue Statements (1 of 2) • break and continue alter the flow of control • break statement is used for two purposes: • To exit early from a loop • To skip the remainder of a switch structure • After break executes, the program continues with the first statement after the structure • A break statement in a loop can eliminate the use of certain (flag) variables © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 43. 43 break and continue Statements (2 of 2) • continue is used in while, for, and do…while structures • When executed in a loop • It skips remaining statements and proceeds with the next iteration of the loop © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 44. 44 Nested Control Structures (1 of 2) • To create the following pattern: * ** *** **** ***** • We can use the following code: for (i = 1; i <= 5; i++) //Line 1 { //Line 2 for (j = 1; j <= i; j++) //Line 3 cout << "*"; //Line 4 cout << endl; //Line 5 } //Line 6 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 45. 45 Nested Control Structures (2 of 2) • What is the result if we replace the first for statement with this? for (i = 5; i >= 1; i--) • Answer: ***** **** *** ** * © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 46. 46 Avoiding Bugs by Avoiding Patches • A software patch is a piece of code written on top of an existing piece of code • Intended to fix a bug in the original code • Some programmers address the symptom of the problem by adding a software patch • A programmer should instead resolve the underlying issue © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 47. 47 Debugging Loops • Loops are harder to debug than sequence and selection structures • Use a loop invariant • Set of statements that remains true each time the loop body is executed • The most common error associated with loops is off-by-one © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 48. 48 Quick Review (1 of 3) • C++ has three looping (repetition) structures: • while, for, and do…while • while, for, and do are reserved words • while and for loops are called pretest loops • do...while loop is called a posttest loop • while and for may not execute at all, but do...while always executes at least once © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 49. 49 Quick Review (2 of 3) • In a while loop: • The expression is the decision maker • The statement is the body of the loop • A while loop can be: • Counter-controlled • Sentinel-controlled • EOF-controlled • In the Windows console environment, the end-of-file marker is entered using Ctrl+z © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 50. 50 Quick Review (3 of 3) • A for loop simplifies the writing of a counter-controlled while loop • Putting a semicolon at the end of the for loop is a semantic error • Executing a break statement in the body of a loop immediately terminates the loop • Executing a continue statement in the body of a loop skips to the next iteration © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom