SlideShare a Scribd company logo
1 of 39
© 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 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.
• 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
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.
• 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
Posttest Loops
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.
An Introduction to Programming with C++, Eighth Edition 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.
Posttest Loops (cont’d.)
An Introduction to Programming with C++, Eighth Edition 5
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.
• 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
Flowcharting a Posttest Loop
An Introduction to Programming with C++, Eighth Edition 6
© 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 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 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.
• 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
The do while Statement
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.
• 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
The do while Statement (cont’d.)
An Introduction to Programming with C++, Eighth Edition 10
© 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-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 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.
• 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
Nested Repetition Structures
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.
Nested Repetition Structures
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 14
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.
Nested Repetition Structures
(cont’d.)
An Introduction to Programming with C++, Eighth Edition 15
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.
• 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.
The Clock Program
An Introduction to Programming with C++, Eighth Edition 16
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
The Clock Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 17
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.
The Clock Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 18
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.
• 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.
The Car Depreciation Program
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.
Car Depreciation Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 20
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.
Car Depreciation Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 21
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.
• 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%.
Car Depreciation Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 22
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
Car Depreciation Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 23
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.
Car Depreciation Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 24
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.
Car Depreciation Program (cont’d.)
An Introduction to Programming with C++, Eighth Edition 25
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.
• 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++
Summary
An Introduction to Programming with C++, Eighth Edition 26
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
• 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
Summary (cont’d.)
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.
• Study the program below and answer the questions.
Lab 8-1: Stop and Analyze
An Introduction to Programming with C++, Eighth Edition 28
Figure 8-14 Code for Lab 8-1
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create
An Introduction to Programming with C++, Eighth Edition 29
Figure 8-15 Problem specification, IPO chart, and desk-
check table for the Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 30
Figure 8-15 Remainder of the desk-check table for the Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 31
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 32
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
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 33
Figure 8-17 Desk-check table for the Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 34
Figure 8-18 Beginning of the finalized Retirement program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
Lab 8-2: Plan and Create (cont’d.)
An Introduction to Programming with C++, Eighth Edition 35
Figure 8-18 Remainder of the finalized Retirement program
© 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 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.
Lab 8-3: Modify
An Introduction to Programming with C++, Eighth Edition 36
© 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 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.
Lab 8-4: What’s missing?
An Introduction to Programming with C++, Eighth Edition 37
Figure 8-19 Sample output for Lab 8-4
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in
whole or in part.
• Desk-check the code in Figure 8-20
• What will the code display on the computer screen?
Lab 8-5: Desk-Check
An Introduction to Programming with C++, Eighth Edition 38
Figure 8-20 Code for Lab 8-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
Lab8-6.cpp file.
• The program should display a store’s quarterly sales, but
it is not working properly.
• Debug the program.
Lab 8-6: Debug
An Introduction to Programming with C++, Eighth Edition 39

More Related Content

What's hot

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 Structuremshellman
 
While loop,Do While loop ,for loop
While loop,Do While loop ,for loop While loop,Do While loop ,for loop
While loop,Do While loop ,for loop MuhammadWaseem305
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
Grails 3.0先取り!? Spring Boot入門ハンズオン #jggug_boot
Grails 3.0先取り!? Spring Boot入門ハンズオン #jggug_bootGrails 3.0先取り!? Spring Boot入門ハンズオン #jggug_boot
Grails 3.0先取り!? Spring Boot入門ハンズオン #jggug_bootToshiaki Maki
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract ClassOUM SAOKOSAL
 
C/C++とWebAssemblyを利用したライブラリ開発
C/C++とWebAssemblyを利用したライブラリ開発C/C++とWebAssemblyを利用したライブラリ開発
C/C++とWebAssemblyを利用したライブラリ開発祐司 伊藤
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentationahmed sayed
 
golang.tokyo #6 (in Japanese)
golang.tokyo #6 (in Japanese)golang.tokyo #6 (in Japanese)
golang.tokyo #6 (in Japanese)Yuichi Murata
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingPurvik Rana
 
Basic for Loop in C
Basic for Loop in CBasic for Loop in C
Basic for Loop in CRohit Soni
 

What's hot (20)

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
 
History of c++
History of c++ History of c++
History of c++
 
While loop,Do While loop ,for loop
While loop,Do While loop ,for loop While loop,Do While loop ,for loop
While loop,Do While loop ,for loop
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java applets
Java appletsJava applets
Java applets
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
java Features
java Featuresjava Features
java Features
 
Grails 3.0先取り!? Spring Boot入門ハンズオン #jggug_boot
Grails 3.0先取り!? Spring Boot入門ハンズオン #jggug_bootGrails 3.0先取り!? Spring Boot入門ハンズオン #jggug_boot
Grails 3.0先取り!? Spring Boot入門ハンズオン #jggug_boot
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
C/C++とWebAssemblyを利用したライブラリ開発
C/C++とWebAssemblyを利用したライブラリ開発C/C++とWebAssemblyを利用したライブラリ開発
C/C++とWebAssemblyを利用したライブラリ開発
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
 
golang.tokyo #6 (in Japanese)
golang.tokyo #6 (in Japanese)golang.tokyo #6 (in Japanese)
golang.tokyo #6 (in Japanese)
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Java programming course for beginners
Java programming course for beginnersJava programming course for beginners
Java programming course for beginners
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
Basic for Loop in C
Basic for Loop in CBasic for Loop in C
Basic for Loop in C
 
C++
C++C++
C++
 

Similar to Chapter 8 - More on the Repetition Structure

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
 
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
 
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....at315244
 
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 pipelineCapgemini
 
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 AdvancedSenturus
 
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 QualityJosiah Renaudin
 

Similar to Chapter 8 - More on the Repetition Structure (20)

Lesson 8 more on repitition structure
Lesson 8 more on repitition structureLesson 8 more on repitition structure
Lesson 8 more on repitition structure
 
Lesson 7.1 repitition structure
Lesson 7.1 repitition structureLesson 7.1 repitition structure
Lesson 7.1 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
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
 
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
 
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
 
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
 
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....
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
 
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
 
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
 
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
 
Sql9e ppt ch03
Sql9e ppt ch03 Sql9e ppt ch03
Sql9e ppt ch03
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
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
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

Chapter 8 - More on 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 8: More on 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. • 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 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. • 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 Posttest Loops 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. An Introduction to Programming with C++, Eighth Edition 4 Figure 8-1 Problem specification, illustrations, and solutions containing pretest and posttest loops Posttest Loops (cont’d.)
  • 5. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Posttest Loops (cont’d.) An Introduction to Programming with C++, Eighth Edition 5 Figure 8-2 Selection structure added to Solution 2 from Figure 8-1
  • 6. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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 Flowcharting a Posttest Loop An Introduction to Programming with C++, Eighth Edition 6
  • 7. © 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 flowcharts Flowcharting a Posttest Loop (cont’d.)
  • 8. © 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 Figure 8-3 Commission Program’s problem specification and flowchart Flowcharting a Posttest Loop (cont’d.)
  • 9. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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 The do while Statement 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. • 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 The do while Statement (cont’d.) An Introduction to Programming with C++, Eighth Edition 10
  • 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 8-4 How to use the do while statement The do while Statement (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. An Introduction to Programming with C++, Eighth Edition 12 Figure 8-5 Commission Program containing a posttest loop The do while Statement (cont’d.)
  • 13. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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 Nested Repetition Structures 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. Nested Repetition Structures (cont’d.) An Introduction to Programming with C++, Eighth Edition 14 Figure 8-6 Problem specification and solution that requires a loop
  • 15. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Nested Repetition Structures (cont’d.) An Introduction to Programming with C++, Eighth Edition 15 Figure 8-7 Modified problem specification and solution that requires a nested loop
  • 16. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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. The Clock Program An Introduction to Programming with C++, Eighth Edition 16
  • 17. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Clock Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 17 Figure 8-8 Algorithm, code, and a sample run of the Clock Program
  • 18. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The Clock Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 18 Figure 8-10 Completed desk-check table and output
  • 19. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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. The Car Depreciation Program 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. Car Depreciation Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 20 Figure 8-11 Beginning of Car Depreciation Program with Problem Specification
  • 21. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Car Depreciation Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 21 Figure 8-11 Remainder of Car Depreciation Program with sample run
  • 22. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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%. Car Depreciation Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 22
  • 23. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Car Depreciation Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 23 Figure 8-12 Beginning of the modified Car Depreciation Program with problem specification
  • 24. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Car Depreciation Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 24 Figure 8-12 Remainder of the modified Car Depreciation Program with sample run
  • 25. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Car Depreciation Program (cont’d.) An Introduction to Programming with C++, Eighth Edition 25 Figure 8-13 Flowchart for the modified Car Depreciation Program
  • 26. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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++ Summary An Introduction to Programming with C++, Eighth Edition 26
  • 27. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • 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 Summary (cont’d.) 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. • Study the program below and answer the questions. Lab 8-1: Stop and Analyze An Introduction to Programming with C++, Eighth Edition 28 Figure 8-14 Code for Lab 8-1
  • 29. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create An Introduction to Programming with C++, Eighth Edition 29 Figure 8-15 Problem specification, IPO chart, and desk- check table for the Retirement program
  • 30. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 30 Figure 8-15 Remainder of the desk-check table for the Retirement program
  • 31. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 31 Figure 8-16 Beginning of the IPO chart information and C++ instructions for the Retirement program
  • 32. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 32 Figure 8-16 Remainder of the IPO chart information and C++ instructions for the Retirement program
  • 33. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 33 Figure 8-17 Desk-check table for the Retirement program
  • 34. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 34 Figure 8-18 Beginning of the finalized Retirement program
  • 35. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Lab 8-2: Plan and Create (cont’d.) An Introduction to Programming with C++, Eighth Edition 35 Figure 8-18 Remainder of the finalized Retirement program
  • 36. © 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 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. Lab 8-3: Modify An Introduction to Programming with C++, Eighth Edition 36
  • 37. © 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 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. Lab 8-4: What’s missing? An Introduction to Programming with C++, Eighth Edition 37 Figure 8-19 Sample output for Lab 8-4
  • 38. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Desk-check the code in Figure 8-20 • What will the code display on the computer screen? Lab 8-5: Desk-Check An Introduction to Programming with C++, Eighth Edition 38 Figure 8-20 Code for Lab 8-5
  • 39. © 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 Lab8-6.cpp file. • The program should display a store’s quarterly sales, but it is not working properly. • Debug the program. Lab 8-6: Debug An Introduction to Programming with C++, Eighth Edition 39