0% found this document useful (0 votes)
85 views39 pages

Chapter 8 More On Repetition Structure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views39 pages

Chapter 8 More On Repetition Structure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Introduction to Programming in C++

Eighth Edition

Chapter 8: More on 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.
Objectives

• Include a posttest loop in pseudocode


• Include a posttest loop in a flowchart
• Code a posttest loop using the C++ do while
statement
• Nest repetition structures

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 2
whole or in part.
Posttest Loops

• Repetition structures can be either pretest or posttest


loops
• Pretest loop – condition evaluated before instructions
are processed
• Posttest loop – condition evaluated after instructions
are processed
• Posttest loop’s instructions are always processed at
least once
• Pretest loop’s instructions may never be processed

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 3
whole or in part.
Posttest Loops (cont’d.)

Figure 8-1 Problem specification, illustrations, and solutions


containing pretest and posttest loops
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 4
whole or in part.
Posttest Loops (cont’d.)

Figure 8-2 Selection structure added to Solution 2 from Figure 8-1

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 5
whole or in part.
Flowcharting a Posttest Loop

• Decision symbol in a flowchart (a diamond)


representing a repetition structure contains the loop
condition
• Decision symbol appears at the top of a pretest loop,
but at the bottom of a posttest loop

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 6
whole or in part.
Flowcharting a Posttest Loop
(cont’d.)

Figure 8-3 Commission Program’s problem specification and flowcharts

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 7
whole or in part.
Flowcharting a Posttest Loop
(cont’d.)

Figure 8-3 Commission Program’s problem specification and flowchart

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 8
whole or in part.
The do while Statement

• do while statement is used to code posttest loops in


C++
• Syntax:
do {
one or more statements to be processed one time,
and thereafter as long as the condition is true
} while (condition);
• Some programmers use a comment (such as:
//begin loop) to mark beginning of loop

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 9
whole or in part.
The do while Statement (cont’d.)

• Programmer must provide loop condition


– Must evaluate to a Boolean value
– May contain variables, constants, functions, arithmetic
operators, comparison operators, and logical operators
• Programmer must also provide statements to be
executed when condition evaluates to true
• Braces are required around statements if there are
more than one

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 10
whole or in part.
The do while Statement (cont’d.)

Figure 8-4 How to use the do while statement


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 11
whole or in part.
The do while Statement (cont’d.)

Figure 8-5 Commission Program containing a posttest loop

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 12
whole or in part.
Nested Repetition Structures

• Like selection structures, repetition structures can be


nested
• You can place one loop (the inner, or nested loop)
inside another loop (the outer loop)
• Both loops can be pretest loops or posttest loops, or
the two loops may be different types
• Programmer decides whether a problem requires a
nested loop by analyzing the problem specification

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 13
whole or in part.
Nested Repetition Structures
(cont’d.)

Figure 8-6 Problem specification and solution that requires a loop

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 14
whole or in part.
Nested Repetition Structures
(cont’d.)

Figure 8-7 Modified problem specification and solution that


requires a nested loop
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 15
whole or in part.
The Clock Program

• Simple program that simulates a clock with minutes and


seconds.
• The outer loop represents minutes.
• The inner loop (nested loop) represents seconds.
• To make it easier to desk-check the instructions, the nested
loop uses only three seconds per minute and the outer loop
stops after two minutes.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 16
whole or in part.
The Clock Program (cont’d.)

Figure 8-8 Algorithm, code, and a sample run of the Clock Program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 17
whole or in part.
The Clock Program (cont’d.)

Figure 8-10 Completed desk-check table and output

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 18
whole or in part.
The Car Depreciation Program

• Calculate the depreciation of a car over time.


• Program uses a counter-controlled loop to display the
value of a new car at the end of each of five years, using
a 15% annual depreciation rate.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 19
whole or in part.
Car Depreciation Program (cont’d.)

Figure 8-11 Beginning of Car Depreciation Program with Problem


Specification
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 20
whole or in part.
Car Depreciation Program (cont’d.)

Figure 8-11 Remainder of Car Depreciation Program with sample


run
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 21
whole or in part.
Car Depreciation Program (cont’d.)

• Modify the Car Depreciation Program to use different


depreciation rates.
• This modified program displays the value of a new car
at the end of each of five years, using annual
depreciation rates of 15%, 20%, and 25%.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 22
whole or in part.
Car Depreciation Program (cont’d.)

Figure 8-12 Beginning of the modified Car Depreciation Program


with problem specification
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 23
whole or in part.
Car Depreciation Program (cont’d.)

Figure 8-12 Remainder of the modified Car Depreciation Program


with sample run
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 24
whole or in part.
Car Depreciation Program (cont’d.)

Figure 8-13 Flowchart for the modified Car Depreciation Program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 25
whole or in part.
Summary

• A repetition structure can be either a pretest loop or a


posttest loop
• In a pretest loop, the loop condition is evaluated before
the instructions in the loop are processed
• In a posttest loop, the evaluation occurs after the
instructions within the loop are processed
• Use the do while statement to code a posttest loop
in C++
• Use either the while statement or the for statement
to code a pretest loop in C++

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 26
whole or in part.
Summary (cont’d.)

• Repetition structures can be nested, which means one


loop (called the inner or nested loop) can be placed
inside another loop (called the outer loop)
• For nested repetition structures to work correctly, the
entire inner loop must be contained within the outer
loop

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 27
whole or in part.
Lab 8-1: Stop and Analyze

• Study the program below and answer the questions.

Figure 8-14 Code for Lab 8-1

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 28
whole or in part.
Lab 8-2: Plan and Create

Figure 8-15 Problem specification, IPO chart, and desk-


check table for the Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 29
whole or in part.
Lab 8-2: Plan and Create (cont’d.)

Figure 8-15 Remainder of the desk-check table for the Retirement program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 30
whole or in part.
Lab 8-2: Plan and Create (cont’d.)

Figure 8-16 Beginning of the IPO chart information and C++


instructions for the Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 31
whole or in part.
Lab 8-2: Plan and Create (cont’d.)

Figure 8-16 Remainder of the IPO chart information and C++


instructions for the Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 32
whole or in part.
Lab 8-2: Plan and Create (cont’d.)

Figure 8-17 Desk-check table for the Retirement program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 33
whole or in part.
Lab 8-2: Plan and Create (cont’d.)

Figure 8-18 Beginning of the finalized Retirement program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 34
whole or in part.
Lab 8-2: Plan and Create (cont’d.)

Figure 8-18 Remainder of the finalized Retirement program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 35
whole or in part.
Lab 8-3: Modify

• Modify the program in Lab8-2.cpp. Change the outer for


loop to a posttest loop. Save the modified program as Lab8-
3.cpp
• Save, run, and test the program.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 36
whole or in part.
Lab 8-4: What’s missing?

• The program is this lab should display the pattern of


numbers shown in Figure 8-19 below.
• Follow the instructions for starting C++ and opening the
Lab8-4.cpp file. Put the C++ instructions in the proper order,
and then determine the one or more missing instructions.
• Test the program appropriately.

Figure 8-19 Sample output for Lab 8-4


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 37
whole or in part.
Lab 8-5: Desk-Check

• Desk-check the code in Figure 8-20


• What will the code display on the computer screen?

Figure 8-20 Code for Lab 8-5


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 38
whole or in part.
Lab 8-6: Debug

• Follow the instructions for starting C++ and opening the


Lab8-6.cpp file.
• The program should display a store’s quarterly sales, but
it is not working properly.
• Debug the program.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 39
whole or in part.

You might also like