SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
© 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.)

Contenu connexe

Tendances

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
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
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

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.)