SlideShare a Scribd company logo
1 of 75
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Introduction to Programming in C++
Eighth Edition
Chapter 7:
The Repetition Structure
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Differentiate between a pretest loop and a posttest loop
• Include a pretest loop in pseudocode
• Include a pretest loop in a flowchart
• Code a pretest loop using the C++ while statement
• Utilize counter and accumulator variables
• Code a pretest loop using the C++ for statement
Objectives
An Introduction to Programming with C++, Eighth Edition 2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The repetition structure, or loop, processes one or
more instructions repeatedly
• Every loop contains a Boolean condition that controls
whether the instructions are repeated
• A looping condition says whether to continue looping
through instructions
• A loop exit condition says whether to stop looping
through the instructions
• Every looping condition can be expressed as a loop exit
condition (its opposite)
Repeating Program Instructions
An Introduction to Programming with C++, Eighth Edition 3
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• C++ uses looping conditions in repetition structures
• A repetition structure can be pretest or posttest
• In a pretest loop, the condition is evaluated before the
instructions in the loop are processed
• In a posttest loop, the condition is evaluated after the
instructions in the loop are processed
• In both cases, the condition is evaluated with each
repetition
Repeating Program Instructions
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 4
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The instructions in a posttest loop will always be
processed at least once
• Instructions in a pretest loop may not be processed if
the condition initially evaluates to false
• The repeatable instructions in a loop are called the loop
body
• The condition in a loop must evaluate to a Boolean
value
Repeating Program Instructions
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 5
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Repeating Program Instructions
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 6
Figure 7-1 A problem that requires the sequence and
selection structures
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Repeating Program Instructions
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 7
Figure 7-2 A problem that requires the sequence and repetition structures
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Most loops have a condition and a loop body
• Some loops require the user to enter a special sentinel
value to end the loop
• Sentinel values should be easily distinguishable from
the valid data recognized by the program
• When a loop’s condition evaluates to true, the
instructions in the loop body are processed
• Otherwise, the instructions are skipped and processing
continues with the first instruction after the loop
Using a Pretest Loop to Solve a Real-
World Problem
An Introduction to Programming with C++, Eighth Edition 8
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• After each processing of the loop body (iteration), the
loop’s condition is reevaluated to determine if the
instructions should be processed again
• A priming read is an instruction that appears before a
loop and is used to set up the loop with an initial value
entered by the user
• An update read is an instruction that appears within a
loop that allows the user to enter a new value at each
iteration of the loop
Using a Pretest Loop to Solve a Real-
World Problem (cont.)
An Introduction to Programming with C++, Eighth Edition 9
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Using a Pretest Loop to Solve a Real-
World Problem (cont’d.)
An Introduction to Programming with C++, Eighth Edition 10
Figure 7-3 Problem specification and IPO chart for the commission program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 11
Figure 7-4 Components of Algorithm 2 from Figure 7-3
Using a Pretest Loop to Solve a Real-
World Problem (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The diamond symbol in a flowchart is the decision
symbol – represents repetition structures
• A diamond representing a repetition structure contains
a Boolean condition
• The condition determines whether the instructions in
the loop are processed
• A diamond representing a repetition structure has one
flowline leading into it and two leading out
Flowcharting a Pretest Loop
An Introduction to Programming with C++, Eighth Edition 12
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The flowlines leading out are marked “T” (true) and “F”
(false)
• The “T” line points to the loop body
• The “F” line points to the instructions to be processed if
the loop’s condition evaluates to false
• The flowline entering the diamond and symbols and
flowlines of the true path form a circle, or loop
• This distinguishes a repetition structure from a
selection structure in a flowchart
Flowcharting a Pretest Loop (cont’d.)
An Introduction to Programming with C++, Eighth Edition 13
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Flowcharting a Pretest Loop (cont’d.)
An Introduction to Programming with C++, Eighth Edition 14
Figure 7-5 Flowchart for algorithm shown in Figure 7-4
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Flowcharting a Pretest Loop (cont’d.)
An Introduction to Programming with C++, Eighth Edition 15
Figure 7-6 First sales entry recorded in the desk-check table
Figure 7-7 First salesperson’s entry in the desk-check table
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Flowcharting a Pretest Loop (cont’d.)
An Introduction to Programming with C++, Eighth Edition 16
Figure 7-8 Second salesperson’s entry in the desk-check table
Figure 7-9 Completed desk-check table
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• You can use the while statement to code a pretest
loop in C++
• Syntax is:
while (condition)
one statement or a statement block to be processed as
long as the condition is true
• Must supply looping condition (Boolean expression)
• condition can contain constants, variables, functions,
arithmetic operators, comparison operators, and logical
operators
The while Statement
An Introduction to Programming with C++, Eighth Edition 17
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Must also provide loop body statements, which are
processed repeatedly as long as condition is true
• If more than one statement in loop body, must be
entered as a statement block (enclosed in braces)
• Can include braces even if there is only one statement
in the statement block
• Good programming practice to include a comment,
such as //end while, to mark the end of the while
statement
The while Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 18
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• A loop whose instructions are processed indefinitely is
called an infinite loop or endless loop
• You can usually stop a program that has entered an
infinite loop by pressing Ctrl+c
The while Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 19
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 20
Figure 7-10 How to use the while statement
The while Statement (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 21
Figure 7-11 IPO chart information and C++ instructions for the
commission program
The while Statement (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The while Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 22
Figure 7-12 A sample run of the commission program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Some problems require you to calculate a total or
average
• To do this, you use a counter, accumulator, or both
• A counter is a numeric variable used for counting
something
• An accumulator is a numeric variable used for
accumulating (adding together) multiple values
• Two tasks are associated with counters and
accumulators: initializing and updating
Using Counters and Accumulators
An Introduction to Programming with C++, Eighth Edition 23
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Initializing means assigning a beginning value to a
counter or accumulator (usually 0) – happens once,
before the loop is processed
• Updating (or incrementing) means adding a number to
the value of a counter or accumulator
• A counter is updated by a constant value (usually 1)
• An accumulator is updated by a value that varies
• Update statements are placed in the body of a loop
since they must be performed at each iteration
Using Counters and Accumulators
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 24
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Using Counters and Accumulators
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 25
Figure 7-13 Example of a partial game program that uses a counter
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Using Counters and Accumulators
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 26
Figure 7-14 Syntax and examples of update statements for counters
and accumulators
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Example problem and program solution (following
slides)
– Program takes in a sequence of stock prices from the
keyboard and outputs their average
– Uses a counter to keep track of the number of prices
entered and an accumulator to keep track of the total
– Both are initialized to 0
– The loop ends when the user enters a sentinel value (-1)
The Stock Price Program
An Introduction to Programming with C++, Eighth Edition 27
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Stock Price Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 28
Figure 7-15 Problem specification for the Sales Express program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 29
Figure 7-15 IPO chart information and C++ instructions for
the Stock Price program
The Stock Price Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Stock Price Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 30
Figure 7-16 Flowchart for the Stock Price program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Stock Price Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 31
Figure 7-17 Desk-check table after the first update to the counter
and accumulator variables
Figure 7-18 Desk-check table after the second update to the
counter and accumulator variables
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Stock Price Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 32
Figure 7-19 Completed desk-check table and sample runs for the
Stock Price program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Loops can be controlled using a counter rather than a
sentinel value
• These are called counter-controlled loops
• Example problem and program solution (following
slides)
– Counter-controlled loop is used that totals the quarterly
sales from three regions
– Loop repeats three times, once for each region, using a
counter to keep track
Counter-Controlled Pretest Loops
An Introduction to Programming with C++, Eighth Edition 33
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Counter-Controlled Pretest Loops
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 34
Figure 7-20 Problem specification, IPO chart information, and C++
instructions for the modified Stock Price program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Counter-Controlled Pretest Loops
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 35
Figure 7-20 Problem specification, IPO chart information, and C++
instructions for the modified Stock Price program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 36
Figure 7-21 Flowchart for the modified Stock Price program
Counter-Controlled Pretest Loops
(cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 37
Figure 7-22 Completed desk-check table and sample run for the
modified Stock Price program
Counter-Controlled Pretest Loops
(cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The for statement can also be used to code any
pretest loop in C++
• Commonly used to code counter-controlled pretest
loops (more compact than while in this case)
• Syntax:
for ([initialization]; condition; [update])
one statement or a statement block to be
processed as long as condition is true
• initialization and update arguments are optional
The for Statement
An Introduction to Programming with C++, Eighth Edition 38
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• initialization argument usually creates and initializes a
counter variable
• Counter variable is local to for statement (can only be
used inside the loop)
• condition argument specifies condition that must be
true for the loop body to be processed
• condition must be a Boolean expression
– May contain variables, constants, functions, arithmetic
operators, comparison operators, and logical operators
The for Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 39
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Loop ends when condition evaluates to false
• update argument usually contains an expression that
updates the counter variable
• Loop body follows the for clause
– Must be placed in braces if it contains more than one
statement
– Braces can be used even if the loop body contains only
one statement
• Good programming practice to place a comment, such
as //end for, to mark the end of a for loop
The for Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 40
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The for Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 41
Figure 7-23 How to use the for statement
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 42
Figure 7-24 Processing steps for Example 1’s code
The for Statement (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Extended example of a problem and program solution
(following slides)
– Program totals up the payroll from three stores using a
for loop
The Total Payroll Program
An Introduction to Programming with C++, Eighth Edition 43
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 44
Figure 7-25 Program specification, IPO chart information, and C++
instructions for the Total Payroll program
The Total Payroll Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Total Payroll Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 45
Figure 7-26 Results of processing the declaration statements
and initialization argument
Figure 7-27 Desk-check table after update argument is processed first time
Figure 7-28 Desk-check table after update argument is processed second time
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Total Payroll Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 46
Figure 7-29 Desk-check table after update argument is processed third time
Figure 7-30 Sample run of the Total Payroll program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Extended example of a problem and program solution
(following slides)
– Calculates the ti for a given sales amount using three
different rates
– A for loop keeps track of each of the three rates
The Tip Program
An Introduction to Programming with C++, Eighth Edition 47
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 48
Figure 7-31 Problem specification, IPO chart information, and C++
instructions for the Tip program
The Tip Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Tip Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 49
Figure 7-32 Desk-check table and sample run of the
Tip program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Tip Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 50
Figure 7-33 Tip program’s algorithm shown in flowchart form
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Alternative version of the Commission program
(following slides)
– Uses a for loop instead of a while loop
Another Version of the Commission
Program
An Introduction to Programming with C++, Eighth Edition 51
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 52
Figure 7-34 IPO chart information and modified C++ instructions for
the Commission program
Another Version of the Commission
Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 53
Figure 7-35 Processing steps and sample run for the Commission program shown
in Figure 7-34
Another Version of the Commission
Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
An Introduction to Programming with C++, Eighth Edition 54
Figure 7-35 Processing steps and sample run for the Commission program shown
in Figure 7-34 (cont’d.)
Another Version of the Commission
Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Program to calculate and display the sum of three
integers entered by the user (following slides)
– Shows incorrect solution via a for loop
– Shows eventual correct solution via while loop
The Even Integers Program
An Introduction to Programming with C++, Eighth Edition 55
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Even Integers Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 56
Figure 7-36 Problem specification and code of the incorrect Even Integers
Program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Even Integers Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 57
Figure 7-36 Sample runs of the incorrect Even Integers Program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Even Integers Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 58
Figure 7-37 Incorrect and correct ways to fix the Even Integers Program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
The Even Integers Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 59
Figure 7-37 Sample runs of the correct solution to the Even Integers Program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Use the repetition structure (or loop) to repeatedly
process one or more instructions
• Loop repeats as long as looping condition is true (or
until loop exit condition has been met)
• A repetition structure can be pretest or posttest
• In a pretest loop, the loop condition is evaluated before
instructions in loop body are processed
• In a posttest loop, the evaluation occurs after
instructions in loop body are processed
Summary
An Introduction to Programming with C++, Eighth Edition 60
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Condition appears at the beginning of a pretest loop –
must be a Boolean expression
• If condition evaluates to true, the instructions in the
loop body are processed; otherwise, the loop body
instructions are skipped
• Some loops require the user to enter a special sentinel
value to end the loop
• Sentinel values should be easily distinguishable from
valid data recognized by the program
• Other loops are terminated by using a counter
Summary (cont’d.)
An Introduction to Programming with C++, Eighth Edition 61
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Input instruction that appears above a pretest loop’s
condition is the priming read
– Sets up the loop by getting first value from user
• Input instruction that appears within the loop is the
update read
– Gets the remaining values (if any) from user
• In most flowcharts, diamond (decision symbol) is used
to represent a repetition structure’s condition
Summary (cont’d.)
An Introduction to Programming with C++, Eighth Edition 62
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Counters and accumulators are used in repetition
structures to calculate totals and averages
• All counters and accumulators must be initialized and
updated
• Counters are updated by a constant value
• Accumulators are updated by a variable amount
• You can use either the while statement or the for
statement to code a pretest loop in C++
Summary (cont’d.)
An Introduction to Programming with C++, Eighth Edition 63
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Study the program in Figure 7-38 on page 229 and
answer the questions
• The program calculates the average number of text
messages sent each day for 7 days
Lab 7-1: Stop and Analyze
An Introduction to Programming with C++, Eighth Edition 64
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-1: Stop and Analyze (cont’d.)
An Introduction to Programming with C++, Eighth Edition 65
Figure 7-38 Code for Lab 7-1
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-2: Plan and Create
An Introduction to Programming with C++, Eighth Edition 66
Figure 7-39 Problem specification for Lab 7-2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 67
Figure 7-40 Completed IPO chart for Lab 7-2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 68
Figure 7-41 Completed desk-check table for Lab 7-2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 69
Figure 7-42 IPO chart and C++ instructions for Lab 7-2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 70
Figure 7-43 Completed desk-check table for Lab 7-2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
Lab 7-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 71
Figure 7-44 Finalized program for Lab 7-2
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Modify the program in Lab7-1.cpp to utilize a while loop
instead of a for loop. Save the modified program as Lab7-
3.cpp
• Test the program using the following data: 76, 80, 100, 43,
68, 70, and 79.
Lab 7-3: Modify
An Introduction to Programming with C++, Eighth Edition 72
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The program is this lab should display the average electric
bill.
• Follow the instructions for starting C++ and opening the
Lab7-4.cpp file.
• Put the C++ instructions in the proper order, and then
determine the one or more missing instructions.
• Test the program using the following monthly electric bills:
124.89, 110.65, 99.43, 100.35, and -1 (the sentinel value)
Lab 7-4: What’s Missing?
An Introduction to Programming with C++, Eighth Edition 73
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• The code in Figure 7-45 should display the numbers 2, 4, 6,
8, 10, and 12.
• Desk-check the code; did your desk-check reveal any errors?
• If so, correct the code and then desk-check it again
Lab 7-5: Desk-Check
An Introduction to Programming with C++, Eighth Edition 74
Figure 7-45 Code for Lab 7-5
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Follow the instructions for starting C++ and opening the
Lab7-6.cpp file
• Run the program and enter 15.45 when prompted
• The program goes into an infinite loop
• Type Ctrl+c to end the program
• Debug the program
Lab 7-6: Debug
An Introduction to Programming with C++, Eighth Edition 75

More Related Content

What's hot

Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
CCNA 1 Routing and Switching v5.0 Chapter 3
CCNA 1 Routing and Switching v5.0 Chapter 3CCNA 1 Routing and Switching v5.0 Chapter 3
CCNA 1 Routing and Switching v5.0 Chapter 3Nil Menon
 
Loopback address
Loopback addressLoopback address
Loopback addressCEC Landran
 
Route Redistribution
Route RedistributionRoute Redistribution
Route RedistributionNetwax Lab
 
CCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdf
CCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdfCCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdf
CCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdfpoojaswami31
 
CCNA 1 Routing and Switching v5.0 Chapter 2
CCNA 1 Routing and Switching v5.0 Chapter 2CCNA 1 Routing and Switching v5.0 Chapter 2
CCNA 1 Routing and Switching v5.0 Chapter 2Nil Menon
 
CCNAv5 - S1: Chapter 2 - Configuring a network operating system
CCNAv5 - S1: Chapter 2 - Configuring a network operating systemCCNAv5 - S1: Chapter 2 - Configuring a network operating system
CCNAv5 - S1: Chapter 2 - Configuring a network operating systemVuz Dở Hơi
 
C++ goto statement tutorialspoint
C++ goto statement   tutorialspointC++ goto statement   tutorialspoint
C++ goto statement tutorialspointNAMITHNAVAKRISHNAN
 
CCNA RS_NB - Chapter 5
CCNA RS_NB - Chapter 5CCNA RS_NB - Chapter 5
CCNA RS_NB - Chapter 5Irsandi Hasan
 
Chapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition StructureChapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition Structuremshellman
 
Dynamic Routing IGRP
Dynamic Routing IGRPDynamic Routing IGRP
Dynamic Routing IGRPKishore Kumar
 
Features of tcp (part 2) .68
Features of tcp  (part 2) .68Features of tcp  (part 2) .68
Features of tcp (part 2) .68myrajendra
 

What's hot (20)

Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
CCNA 1 Routing and Switching v5.0 Chapter 3
CCNA 1 Routing and Switching v5.0 Chapter 3CCNA 1 Routing and Switching v5.0 Chapter 3
CCNA 1 Routing and Switching v5.0 Chapter 3
 
OSI Network Layer
OSI Network LayerOSI Network Layer
OSI Network Layer
 
Loopback address
Loopback addressLoopback address
Loopback address
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Route Redistribution
Route RedistributionRoute Redistribution
Route Redistribution
 
Tokens_C
Tokens_CTokens_C
Tokens_C
 
CCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdf
CCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdfCCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdf
CCNA-LAB-GUIDE-V3_LAST-ADDITION (4).pdf
 
CCNA 1 Routing and Switching v5.0 Chapter 2
CCNA 1 Routing and Switching v5.0 Chapter 2CCNA 1 Routing and Switching v5.0 Chapter 2
CCNA 1 Routing and Switching v5.0 Chapter 2
 
Unit4
Unit4Unit4
Unit4
 
CCNAv5 - S1: Chapter 2 - Configuring a network operating system
CCNAv5 - S1: Chapter 2 - Configuring a network operating systemCCNAv5 - S1: Chapter 2 - Configuring a network operating system
CCNAv5 - S1: Chapter 2 - Configuring a network operating system
 
CCNA Lab Guide
CCNA Lab GuideCCNA Lab Guide
CCNA Lab Guide
 
C++ goto statement tutorialspoint
C++ goto statement   tutorialspointC++ goto statement   tutorialspoint
C++ goto statement tutorialspoint
 
CCNA RS_NB - Chapter 5
CCNA RS_NB - Chapter 5CCNA RS_NB - Chapter 5
CCNA RS_NB - Chapter 5
 
Spring cloud config
Spring cloud configSpring cloud config
Spring cloud config
 
Chapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition StructureChapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition Structure
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Dynamic Routing IGRP
Dynamic Routing IGRPDynamic Routing IGRP
Dynamic Routing IGRP
 
Features of tcp (part 2) .68
Features of tcp  (part 2) .68Features of tcp  (part 2) .68
Features of tcp (part 2) .68
 
Transport Layer
Transport LayerTransport Layer
Transport Layer
 

Viewers also liked

Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structuremshellman
 
5 problem solving with the sequential logic structure
5 problem solving with the sequential logic structure5 problem solving with the sequential logic structure
5 problem solving with the sequential logic structureRheigh Henley Calderon
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop typesRj Baculo
 
Quiz1 tonghop
 Quiz1 tonghop Quiz1 tonghop
Quiz1 tonghopDaewoo Han
 
While loop
While loopWhile loop
While loopFeras_83
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statementJunyoung Jung
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambalajatin batra
 
Instruction Set of 8086 Microprocessor
Instruction Set of 8086 MicroprocessorInstruction Set of 8086 Microprocessor
Instruction Set of 8086 MicroprocessorAshita Agrawal
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structuresindra Kishor
 
Creating HTML Pages
Creating HTML PagesCreating HTML Pages
Creating HTML PagesMike Crabb
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Viewers also liked (19)

Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
 
5 problem solving with the sequential logic structure
5 problem solving with the sequential logic structure5 problem solving with the sequential logic structure
5 problem solving with the sequential logic structure
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Quiz1 tonghop
 Quiz1 tonghop Quiz1 tonghop
Quiz1 tonghop
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
While loop
While loopWhile loop
While loop
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala
 
Instruction Set of 8086 Microprocessor
Instruction Set of 8086 MicroprocessorInstruction Set of 8086 Microprocessor
Instruction Set of 8086 Microprocessor
 
Loop c++
Loop c++Loop c++
Loop c++
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
Creating HTML Pages
Creating HTML PagesCreating HTML Pages
Creating HTML Pages
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar to Chapter 7 - The Repetition Structure

Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functionsmshellman
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Processmshellman
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Processmshellman
 
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Kamran Zafar
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programmingmshellman
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05Terry Yoast
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdfTanayKashid
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constantsmshellman
 
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docxCHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docxketurahhazelhurst
 
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docxCHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docxrobertad6
 
Omni channel network design
Omni channel network designOmni channel network design
Omni channel network designfaiza20202880
 

Similar to Chapter 7 - The Repetition Structure (20)

Lesson 7.1 repitition structure
Lesson 7.1 repitition structureLesson 7.1 repitition structure
Lesson 7.1 repitition structure
 
Lesson 8 more on repitition structure
Lesson 8 more on repitition structureLesson 8 more on repitition structure
Lesson 8 more on repitition structure
 
Lesson 6.2 logic error
Lesson  6.2 logic errorLesson  6.2 logic error
Lesson 6.2 logic error
 
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game programLesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
 
Lesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulatorsLesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulators
 
Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functions
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
 
Lesson 6.1 more on selection structure
Lesson 6.1 more on selection structureLesson 6.1 more on selection structure
Lesson 6.1 more on selection structure
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
 
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
 
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docxCHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
 
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docxCHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
CHAPTER 4Distribution and Omni-Channel Network DesignSu.docx
 
Omni channel network design
Omni channel network designOmni channel network design
Omni channel network design
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 

Chapter 7 - The Repetition Structure

  • 1. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Introduction to Programming in C++ Eighth Edition Chapter 7: The Repetition Structure
  • 2. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Differentiate between a pretest loop and a posttest loop • Include a pretest loop in pseudocode • Include a pretest loop in a flowchart • Code a pretest loop using the C++ while statement • Utilize counter and accumulator variables • Code a pretest loop using the C++ for statement Objectives An Introduction to Programming with C++, Eighth Edition 2
  • 3. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The repetition structure, or loop, processes one or more instructions repeatedly • Every loop contains a Boolean condition that controls whether the instructions are repeated • A looping condition says whether to continue looping through instructions • A loop exit condition says whether to stop looping through the instructions • Every looping condition can be expressed as a loop exit condition (its opposite) Repeating Program Instructions An Introduction to Programming with C++, Eighth Edition 3
  • 4. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • C++ uses looping conditions in repetition structures • A repetition structure can be pretest or posttest • In a pretest loop, the condition is evaluated before the instructions in the loop are processed • In a posttest loop, the condition is evaluated after the instructions in the loop are processed • In both cases, the condition is evaluated with each repetition Repeating Program Instructions (cont’d.) An Introduction to Programming with C++, Eighth Edition 4
  • 5. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The instructions in a posttest loop will always be processed at least once • Instructions in a pretest loop may not be processed if the condition initially evaluates to false • The repeatable instructions in a loop are called the loop body • The condition in a loop must evaluate to a Boolean value Repeating Program Instructions (cont’d.) An Introduction to Programming with C++, Eighth Edition 5
  • 6. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Repeating Program Instructions (cont’d.) An Introduction to Programming with C++, Eighth Edition 6 Figure 7-1 A problem that requires the sequence and selection structures
  • 7. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Repeating Program Instructions (cont’d.) An Introduction to Programming with C++, Eighth Edition 7 Figure 7-2 A problem that requires the sequence and repetition structures
  • 8. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Most loops have a condition and a loop body • Some loops require the user to enter a special sentinel value to end the loop • Sentinel values should be easily distinguishable from the valid data recognized by the program • When a loop’s condition evaluates to true, the instructions in the loop body are processed • Otherwise, the instructions are skipped and processing continues with the first instruction after the loop Using a Pretest Loop to Solve a Real- World Problem An Introduction to Programming with C++, Eighth Edition 8
  • 9. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • After each processing of the loop body (iteration), the loop’s condition is reevaluated to determine if the instructions should be processed again • A priming read is an instruction that appears before a loop and is used to set up the loop with an initial value entered by the user • An update read is an instruction that appears within a loop that allows the user to enter a new value at each iteration of the loop Using a Pretest Loop to Solve a Real- World Problem (cont.) An Introduction to Programming with C++, Eighth Edition 9
  • 10. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Using a Pretest Loop to Solve a Real- World Problem (cont’d.) An Introduction to Programming with C++, Eighth Edition 10 Figure 7-3 Problem specification and IPO chart for the commission program
  • 11. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 11 Figure 7-4 Components of Algorithm 2 from Figure 7-3 Using a Pretest Loop to Solve a Real- World Problem (cont’d.)
  • 12. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The diamond symbol in a flowchart is the decision symbol – represents repetition structures • A diamond representing a repetition structure contains a Boolean condition • The condition determines whether the instructions in the loop are processed • A diamond representing a repetition structure has one flowline leading into it and two leading out Flowcharting a Pretest Loop An Introduction to Programming with C++, Eighth Edition 12
  • 13. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The flowlines leading out are marked “T” (true) and “F” (false) • The “T” line points to the loop body • The “F” line points to the instructions to be processed if the loop’s condition evaluates to false • The flowline entering the diamond and symbols and flowlines of the true path form a circle, or loop • This distinguishes a repetition structure from a selection structure in a flowchart Flowcharting a Pretest Loop (cont’d.) An Introduction to Programming with C++, Eighth Edition 13
  • 14. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Flowcharting a Pretest Loop (cont’d.) An Introduction to Programming with C++, Eighth Edition 14 Figure 7-5 Flowchart for algorithm shown in Figure 7-4
  • 15. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Flowcharting a Pretest Loop (cont’d.) An Introduction to Programming with C++, Eighth Edition 15 Figure 7-6 First sales entry recorded in the desk-check table Figure 7-7 First salesperson’s entry in the desk-check table
  • 16. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Flowcharting a Pretest Loop (cont’d.) An Introduction to Programming with C++, Eighth Edition 16 Figure 7-8 Second salesperson’s entry in the desk-check table Figure 7-9 Completed desk-check table
  • 17. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • You can use the while statement to code a pretest loop in C++ • Syntax is: while (condition) one statement or a statement block to be processed as long as the condition is true • Must supply looping condition (Boolean expression) • condition can contain constants, variables, functions, arithmetic operators, comparison operators, and logical operators The while Statement An Introduction to Programming with C++, Eighth Edition 17
  • 18. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Must also provide loop body statements, which are processed repeatedly as long as condition is true • If more than one statement in loop body, must be entered as a statement block (enclosed in braces) • Can include braces even if there is only one statement in the statement block • Good programming practice to include a comment, such as //end while, to mark the end of the while statement The while Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 18
  • 19. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A loop whose instructions are processed indefinitely is called an infinite loop or endless loop • You can usually stop a program that has entered an infinite loop by pressing Ctrl+c The while Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 19
  • 20. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 20 Figure 7-10 How to use the while statement The while Statement (cont’d.)
  • 21. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 21 Figure 7-11 IPO chart information and C++ instructions for the commission program The while Statement (cont’d.)
  • 22. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The while Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 22 Figure 7-12 A sample run of the commission program
  • 23. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Some problems require you to calculate a total or average • To do this, you use a counter, accumulator, or both • A counter is a numeric variable used for counting something • An accumulator is a numeric variable used for accumulating (adding together) multiple values • Two tasks are associated with counters and accumulators: initializing and updating Using Counters and Accumulators An Introduction to Programming with C++, Eighth Edition 23
  • 24. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Initializing means assigning a beginning value to a counter or accumulator (usually 0) – happens once, before the loop is processed • Updating (or incrementing) means adding a number to the value of a counter or accumulator • A counter is updated by a constant value (usually 1) • An accumulator is updated by a value that varies • Update statements are placed in the body of a loop since they must be performed at each iteration Using Counters and Accumulators (cont’d.) An Introduction to Programming with C++, Eighth Edition 24
  • 25. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Using Counters and Accumulators (cont’d.) An Introduction to Programming with C++, Eighth Edition 25 Figure 7-13 Example of a partial game program that uses a counter
  • 26. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Using Counters and Accumulators (cont’d.) An Introduction to Programming with C++, Eighth Edition 26 Figure 7-14 Syntax and examples of update statements for counters and accumulators
  • 27. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Example problem and program solution (following slides) – Program takes in a sequence of stock prices from the keyboard and outputs their average – Uses a counter to keep track of the number of prices entered and an accumulator to keep track of the total – Both are initialized to 0 – The loop ends when the user enters a sentinel value (-1) The Stock Price Program An Introduction to Programming with C++, Eighth Edition 27
  • 28. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Stock Price Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 28 Figure 7-15 Problem specification for the Sales Express program
  • 29. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 29 Figure 7-15 IPO chart information and C++ instructions for the Stock Price program The Stock Price Program (cont’d.)
  • 30. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Stock Price Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 30 Figure 7-16 Flowchart for the Stock Price program
  • 31. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Stock Price Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 31 Figure 7-17 Desk-check table after the first update to the counter and accumulator variables Figure 7-18 Desk-check table after the second update to the counter and accumulator variables
  • 32. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Stock Price Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 32 Figure 7-19 Completed desk-check table and sample runs for the Stock Price program
  • 33. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Loops can be controlled using a counter rather than a sentinel value • These are called counter-controlled loops • Example problem and program solution (following slides) – Counter-controlled loop is used that totals the quarterly sales from three regions – Loop repeats three times, once for each region, using a counter to keep track Counter-Controlled Pretest Loops An Introduction to Programming with C++, Eighth Edition 33
  • 34. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Counter-Controlled Pretest Loops (cont’d.) An Introduction to Programming with C++, Eighth Edition 34 Figure 7-20 Problem specification, IPO chart information, and C++ instructions for the modified Stock Price program
  • 35. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Counter-Controlled Pretest Loops (cont’d.) An Introduction to Programming with C++, Eighth Edition 35 Figure 7-20 Problem specification, IPO chart information, and C++ instructions for the modified Stock Price program (cont’d.)
  • 36. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 36 Figure 7-21 Flowchart for the modified Stock Price program Counter-Controlled Pretest Loops (cont’d.)
  • 37. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 37 Figure 7-22 Completed desk-check table and sample run for the modified Stock Price program Counter-Controlled Pretest Loops (cont’d.)
  • 38. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The for statement can also be used to code any pretest loop in C++ • Commonly used to code counter-controlled pretest loops (more compact than while in this case) • Syntax: for ([initialization]; condition; [update]) one statement or a statement block to be processed as long as condition is true • initialization and update arguments are optional The for Statement An Introduction to Programming with C++, Eighth Edition 38
  • 39. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • initialization argument usually creates and initializes a counter variable • Counter variable is local to for statement (can only be used inside the loop) • condition argument specifies condition that must be true for the loop body to be processed • condition must be a Boolean expression – May contain variables, constants, functions, arithmetic operators, comparison operators, and logical operators The for Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 39
  • 40. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Loop ends when condition evaluates to false • update argument usually contains an expression that updates the counter variable • Loop body follows the for clause – Must be placed in braces if it contains more than one statement – Braces can be used even if the loop body contains only one statement • Good programming practice to place a comment, such as //end for, to mark the end of a for loop The for Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 40
  • 41. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The for Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 41 Figure 7-23 How to use the for statement
  • 42. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 42 Figure 7-24 Processing steps for Example 1’s code The for Statement (cont’d.)
  • 43. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Extended example of a problem and program solution (following slides) – Program totals up the payroll from three stores using a for loop The Total Payroll Program An Introduction to Programming with C++, Eighth Edition 43
  • 44. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 44 Figure 7-25 Program specification, IPO chart information, and C++ instructions for the Total Payroll program The Total Payroll Program (cont’d.)
  • 45. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Total Payroll Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 45 Figure 7-26 Results of processing the declaration statements and initialization argument Figure 7-27 Desk-check table after update argument is processed first time Figure 7-28 Desk-check table after update argument is processed second time
  • 46. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Total Payroll Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 46 Figure 7-29 Desk-check table after update argument is processed third time Figure 7-30 Sample run of the Total Payroll program
  • 47. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Extended example of a problem and program solution (following slides) – Calculates the ti for a given sales amount using three different rates – A for loop keeps track of each of the three rates The Tip Program An Introduction to Programming with C++, Eighth Edition 47
  • 48. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 48 Figure 7-31 Problem specification, IPO chart information, and C++ instructions for the Tip program The Tip Program (cont’d.)
  • 49. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Tip Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 49 Figure 7-32 Desk-check table and sample run of the Tip program
  • 50. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Tip Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 50 Figure 7-33 Tip program’s algorithm shown in flowchart form
  • 51. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Alternative version of the Commission program (following slides) – Uses a for loop instead of a while loop Another Version of the Commission Program An Introduction to Programming with C++, Eighth Edition 51
  • 52. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 52 Figure 7-34 IPO chart information and modified C++ instructions for the Commission program Another Version of the Commission Program (cont’d.)
  • 53. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 53 Figure 7-35 Processing steps and sample run for the Commission program shown in Figure 7-34 Another Version of the Commission Program (cont’d.)
  • 54. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 54 Figure 7-35 Processing steps and sample run for the Commission program shown in Figure 7-34 (cont’d.) Another Version of the Commission Program (cont’d.)
  • 55. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Program to calculate and display the sum of three integers entered by the user (following slides) – Shows incorrect solution via a for loop – Shows eventual correct solution via while loop The Even Integers Program An Introduction to Programming with C++, Eighth Edition 55
  • 56. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Even Integers Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 56 Figure 7-36 Problem specification and code of the incorrect Even Integers Program
  • 57. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Even Integers Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 57 Figure 7-36 Sample runs of the incorrect Even Integers Program
  • 58. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Even Integers Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 58 Figure 7-37 Incorrect and correct ways to fix the Even Integers Program
  • 59. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Even Integers Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 59 Figure 7-37 Sample runs of the correct solution to the Even Integers Program
  • 60. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Use the repetition structure (or loop) to repeatedly process one or more instructions • Loop repeats as long as looping condition is true (or until loop exit condition has been met) • A repetition structure can be pretest or posttest • In a pretest loop, the loop condition is evaluated before instructions in loop body are processed • In a posttest loop, the evaluation occurs after instructions in loop body are processed Summary An Introduction to Programming with C++, Eighth Edition 60
  • 61. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Condition appears at the beginning of a pretest loop – must be a Boolean expression • If condition evaluates to true, the instructions in the loop body are processed; otherwise, the loop body instructions are skipped • Some loops require the user to enter a special sentinel value to end the loop • Sentinel values should be easily distinguishable from valid data recognized by the program • Other loops are terminated by using a counter Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 61
  • 62. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Input instruction that appears above a pretest loop’s condition is the priming read – Sets up the loop by getting first value from user • Input instruction that appears within the loop is the update read – Gets the remaining values (if any) from user • In most flowcharts, diamond (decision symbol) is used to represent a repetition structure’s condition Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 62
  • 63. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Counters and accumulators are used in repetition structures to calculate totals and averages • All counters and accumulators must be initialized and updated • Counters are updated by a constant value • Accumulators are updated by a variable amount • You can use either the while statement or the for statement to code a pretest loop in C++ Summary (cont’d.) An Introduction to Programming with C++, Eighth Edition 63
  • 64. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Study the program in Figure 7-38 on page 229 and answer the questions • The program calculates the average number of text messages sent each day for 7 days Lab 7-1: Stop and Analyze An Introduction to Programming with C++, Eighth Edition 64
  • 65. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-1: Stop and Analyze (cont’d.) An Introduction to Programming with C++, Eighth Edition 65 Figure 7-38 Code for Lab 7-1
  • 66. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-2: Plan and Create An Introduction to Programming with C++, Eighth Edition 66 Figure 7-39 Problem specification for Lab 7-2
  • 67. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 67 Figure 7-40 Completed IPO chart for Lab 7-2
  • 68. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 68 Figure 7-41 Completed desk-check table for Lab 7-2
  • 69. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 69 Figure 7-42 IPO chart and C++ instructions for Lab 7-2
  • 70. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 70 Figure 7-43 Completed desk-check table for Lab 7-2
  • 71. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 7-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 71 Figure 7-44 Finalized program for Lab 7-2
  • 72. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Modify the program in Lab7-1.cpp to utilize a while loop instead of a for loop. Save the modified program as Lab7- 3.cpp • Test the program using the following data: 76, 80, 100, 43, 68, 70, and 79. Lab 7-3: Modify An Introduction to Programming with C++, Eighth Edition 72
  • 73. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The program is this lab should display the average electric bill. • Follow the instructions for starting C++ and opening the Lab7-4.cpp file. • Put the C++ instructions in the proper order, and then determine the one or more missing instructions. • Test the program using the following monthly electric bills: 124.89, 110.65, 99.43, 100.35, and -1 (the sentinel value) Lab 7-4: What’s Missing? An Introduction to Programming with C++, Eighth Edition 73
  • 74. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The code in Figure 7-45 should display the numbers 2, 4, 6, 8, 10, and 12. • Desk-check the code; did your desk-check reveal any errors? • If so, correct the code and then desk-check it again Lab 7-5: Desk-Check An Introduction to Programming with C++, Eighth Edition 74 Figure 7-45 Code for Lab 7-5
  • 75. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Follow the instructions for starting C++ and opening the Lab7-6.cpp file • Run the program and enter 15.45 when prompted • The program goes into an infinite loop • Type Ctrl+c to end the program • Debug the program Lab 7-6: Debug An Introduction to Programming with C++, Eighth Edition 75