SlideShare a Scribd company logo
1 of 27
Download to read offline
© 2016 Cengage Learning®. May not be scanned, copied or
Introduction to Programming in C++
Eighth Edition
Lesson 8:
More on the Repetition Structure
duplicated, or posted to a publicly accessible website, in
whole or in part.
• Include aposttest loop in pseudocode
• Include aposttest loop in aflowchart
• Codeaposttest loop using the C++do while
statement
• Nest repetition structures
© 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 2
Objectives
• Repetition structures can be either pretest orposttest
loops
• Pretest loop – condition evaluated before instructions
are processed
• Posttest loop – condition evaluated afterinstructions
are processed
• Posttest loop’s instructions are always processedat
least once
• Pretest loop’s instructions may never beprocessed
© 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 3
Posttest Loops
Figure 8-1 Problem specification, illustrations, and solutions
containing pretest and posttest loops
Posttest 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 4
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 5
• Decision symbol in aflowchart (a diamond)
representing arepetition structure contains the loop
condition
• Decision symbol appears at the top ofapretest loop,
but at the bottom of aposttest loop
© 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 6
Flowcharting a Posttest Loop
Figure 8-3 Commission Program’s problem specification and flowcharts
Flowcharting a Posttest Loop
(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 7
Figure 8-3 Commission Program’s problem specification and flowchart
Flowcharting a Posttest Loop
(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 8
• do whilestatement 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);
• Someprogrammers useacomment (such as:
//begin loop) to mark beginning ofloop
© 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 9
The do while Statement
• Programmer must provide loop condition
– Must evaluate to aBooleanvalue
– May contain variables, constants, functions, arithmetic
operators, comparison operators, and logicaloperators
• Programmer must also provide statements to be
executed when condition evaluates to true
• Bracesare required around statements if thereare
more than one
© 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 10
The do while Statement (cont’d.)
Figure 8-4 How to use the do while statement
The do 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 11
Figure 8-5 Commission Program containing a posttest loop
The do 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 12
• Like selection structures, repetition structures canbe
nested
• Youcan place one loop (the inner, or nestedloop)
inside another loop (the outerloop)
• Both loops can be pretest loops or posttest loops,or
the two loops may be differenttypes
• Programmer decides whether aproblem requires a
nested loop by analyzing the problemspecification
© 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 13
Nested Repetition Structures
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 14
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 15
• Simple program that simulates aclock with minutesand
seconds.
• Theouter loop representsminutes.
• Theinner loop (nested loop) representsseconds.
• 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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 16
The Clock Program
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 17
The Clock Program (cont’d.)
Figure 8-10 Completed desk-check table and output
© 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 18
• Calculate the depreciation of acar overtime.
• Program usesacounter-controlled loop to display the
value of a new car at the end of each of five years, using
a15%annual depreciation rate.
© 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 19
The Car Depreciation Program
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 20
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 21
• Modify the CarDepreciation Program to usedifferent
depreciation rates.
• Thismodified program displays the value of anewcar
at the end of each of five years, using annual
depreciation rates of 15%,20%,and25%.
© 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 22
Car Depreciation Program (cont’d.)
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
An Introduction to Programming with C++, Eighth Edition 23
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
24An Introduction to Programming with C++, Eighth Edition
Car Depreciation Program (cont’d.)
Figure 8-13 Flowchart for the modified Car Depreciation 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 25
• Arepetition structure can be either apretest loop ora
posttest loop
• In apretest loop, the loop condition is evaluatedbefore
the instructions in the loop are processed
• In aposttest loop, the evaluation occurs afterthe
instructions within the loop areprocessed
• Usethe do whilestatement to code aposttest loop
in C++
• Useeither the while statement or the for statement
to code apretest loop inC++
© 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 26
Summary
• Repetition structures can be nested, which meansone
loop (called the inner or nested loop) can be placed
inside another loop (called the outerloop)
• For nested repetition structures towork correctly, the
entire inner loop must be contained within the outer
loop
© 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 27
Summary (cont’d.)

More Related Content

What's hot

What's hot (15)

Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functions
 
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
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
 
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection StructureChapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structure
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
 
Lesson 10
Lesson 10Lesson 10
Lesson 10
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
[CapellaDay Toulouse] Designing a test mean alla capella
[CapellaDay Toulouse] Designing a test mean alla capella[CapellaDay Toulouse] Designing a test mean alla capella
[CapellaDay Toulouse] Designing a test mean alla capella
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
 
Portfolio control version sn_v5
Portfolio control version sn_v5Portfolio control version sn_v5
Portfolio control version sn_v5
 
Capture Accurate Solution Requirements with Exploratory Modeling at SAP
Capture Accurate Solution Requirements with Exploratory Modeling at SAPCapture Accurate Solution Requirements with Exploratory Modeling at SAP
Capture Accurate Solution Requirements with Exploratory Modeling at SAP
 
ARTEMIS Project MBAT: Advanced Validation & Verification of Embedded Systems ...
ARTEMIS Project MBAT: Advanced Validation & Verification of Embedded Systems ...ARTEMIS Project MBAT: Advanced Validation & Verification of Embedded Systems ...
ARTEMIS Project MBAT: Advanced Validation & Verification of Embedded Systems ...
 
Pitfalls of machine learning in production
Pitfalls of machine learning in productionPitfalls of machine learning in production
Pitfalls of machine learning in production
 
Sample instrument using lab view abhijeet agarwal-1
Sample instrument using lab view  abhijeet agarwal-1Sample instrument using lab view  abhijeet agarwal-1
Sample instrument using lab view abhijeet agarwal-1
 

Similar to Lesson 8 more on repitition structure

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
keturahhazelhurst
 
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
robertad6
 
Decoding Puppet & Jenkins via DevOps
Decoding Puppet & Jenkins via DevOpsDecoding Puppet & Jenkins via DevOps
Decoding Puppet & Jenkins via DevOps
Skillspeed
 

Similar to Lesson 8 more on repitition structure (20)

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 ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
 
Questions Log: Transitioning to Cognos Workspace Advanced
Questions Log: Transitioning to Cognos Workspace AdvancedQuestions Log: Transitioning to Cognos Workspace Advanced
Questions Log: Transitioning to Cognos Workspace Advanced
 
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
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
 
Sql9e ppt ch03
Sql9e ppt ch03 Sql9e ppt ch03
Sql9e ppt ch03
 
Continuous Integration Testing Techniques to Improve Chef Cookbook Quality
Continuous Integration Testing Techniques to Improve Chef Cookbook QualityContinuous Integration Testing Techniques to Improve Chef Cookbook Quality
Continuous Integration Testing Techniques to Improve Chef Cookbook Quality
 
DC16_Ch06 (1).pptx
DC16_Ch06 (1).pptxDC16_Ch06 (1).pptx
DC16_Ch06 (1).pptx
 
Samuel Asher Rivello - PureMVC Hands On Part 2
Samuel Asher Rivello - PureMVC Hands On Part 2Samuel Asher Rivello - PureMVC Hands On Part 2
Samuel Asher Rivello - PureMVC Hands On Part 2
 
Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"
 
Decoding Puppet & Jenkins via DevOps
Decoding Puppet & Jenkins via DevOpsDecoding Puppet & Jenkins via DevOps
Decoding Puppet & Jenkins via DevOps
 
Cwin16 tls-s2-implementing a dev ops pipeline
Cwin16 tls-s2-implementing a dev ops pipelineCwin16 tls-s2-implementing a dev ops pipeline
Cwin16 tls-s2-implementing a dev ops pipeline
 
Business analytics© 2021 cengage learning. all rights
Business analytics© 2021 cengage learning. all rights Business analytics© 2021 cengage learning. all rights
Business analytics© 2021 cengage learning. all rights
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Lesson 8 more on repitition structure

  • 1. © 2016 Cengage Learning®. May not be scanned, copied or Introduction to Programming in C++ Eighth Edition Lesson 8: More on the Repetition Structure duplicated, or posted to a publicly accessible website, in whole or in part.
  • 2. • Include aposttest loop in pseudocode • Include aposttest loop in aflowchart • Codeaposttest loop using the C++do while statement • Nest repetition structures © 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 2 Objectives
  • 3. • Repetition structures can be either pretest orposttest loops • Pretest loop – condition evaluated before instructions are processed • Posttest loop – condition evaluated afterinstructions are processed • Posttest loop’s instructions are always processedat least once • Pretest loop’s instructions may never beprocessed © 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 3 Posttest Loops
  • 4. Figure 8-1 Problem specification, illustrations, and solutions containing pretest and posttest loops Posttest 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 4
  • 5. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 5
  • 6. • Decision symbol in aflowchart (a diamond) representing arepetition structure contains the loop condition • Decision symbol appears at the top ofapretest loop, but at the bottom of aposttest loop © 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 6 Flowcharting a Posttest Loop
  • 7. Figure 8-3 Commission Program’s problem specification and flowcharts Flowcharting a Posttest Loop (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 7
  • 8. Figure 8-3 Commission Program’s problem specification and flowchart Flowcharting a Posttest Loop (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 8
  • 9. • do whilestatement 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); • Someprogrammers useacomment (such as: //begin loop) to mark beginning ofloop © 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 9 The do while Statement
  • 10. • Programmer must provide loop condition – Must evaluate to aBooleanvalue – May contain variables, constants, functions, arithmetic operators, comparison operators, and logicaloperators • Programmer must also provide statements to be executed when condition evaluates to true • Bracesare required around statements if thereare more than one © 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 10 The do while Statement (cont’d.)
  • 11. Figure 8-4 How to use the do while statement The do 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 11
  • 12. Figure 8-5 Commission Program containing a posttest loop The do 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 12
  • 13. • Like selection structures, repetition structures canbe nested • Youcan place one loop (the inner, or nestedloop) inside another loop (the outerloop) • Both loops can be pretest loops or posttest loops,or the two loops may be differenttypes • Programmer decides whether aproblem requires a nested loop by analyzing the problemspecification © 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 13 Nested Repetition Structures
  • 14. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 14
  • 15. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 15
  • 16. • Simple program that simulates aclock with minutesand seconds. • Theouter loop representsminutes. • Theinner loop (nested loop) representsseconds. • 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 16 The Clock Program
  • 17. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 17
  • 18. The Clock Program (cont’d.) Figure 8-10 Completed desk-check table and output © 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 18
  • 19. • Calculate the depreciation of acar overtime. • Program usesacounter-controlled loop to display the value of a new car at the end of each of five years, using a15%annual depreciation rate. © 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 19 The Car Depreciation Program
  • 20. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 20
  • 21. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 21
  • 22. • Modify the CarDepreciation Program to usedifferent depreciation rates. • Thismodified program displays the value of anewcar at the end of each of five years, using annual depreciation rates of 15%,20%,and25%. © 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 22 Car Depreciation Program (cont’d.)
  • 23. 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 duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 23
  • 24. 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 duplicated, or posted to a publicly accessible website, in whole or in part. 24An Introduction to Programming with C++, Eighth Edition
  • 25. Car Depreciation Program (cont’d.) Figure 8-13 Flowchart for the modified Car Depreciation 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 25
  • 26. • Arepetition structure can be either apretest loop ora posttest loop • In apretest loop, the loop condition is evaluatedbefore the instructions in the loop are processed • In aposttest loop, the evaluation occurs afterthe instructions within the loop areprocessed • Usethe do whilestatement to code aposttest loop in C++ • Useeither the while statement or the for statement to code apretest loop inC++ © 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 26 Summary
  • 27. • Repetition structures can be nested, which meansone loop (called the inner or nested loop) can be placed inside another loop (called the outerloop) • For nested repetition structures towork correctly, the entire inner loop must be contained within the outer loop © 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 27 Summary (cont’d.)