SlideShare a Scribd company logo
1 of 22
Download to read offline
© 2016 Cengage Learning®. May not be scanned, copied or
Introduction to Programming in C++
Eighth Edition
Lesson 7.1:
The Repetition Structure
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Differentiate between apretest loop and aposttestloop
• Include apretest loop in pseudocode
• Include apretest loop in aflowchart
• Codeapretest loop using the C++whilestatement
• Utilize counter and accumulator variables
• Codeapretest loop using the C++forstatement
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
2An Introduction to Programming with C++, Eighth Edition
Objectives
• Therepetition structure, or loop, processes one or
more instructions repeatedly
• Every loop contains aBoolean condition that controls
whether the instructions arerepeated
• Alooping condition sayswhether to continue looping
through instructions
• Aloop exit condition sayswhether to stop looping
through the instructions
• Every looping condition can be expressed asaloop exit
condition (its opposite)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
3An Introduction to Programming with C++, Eighth Edition
Repeating Program Instructions
• C++useslooping conditions in repetitionstructures
• Arepetition structure can be pretest orposttest
• In apretest loop, the condition is evaluated beforethe
instructions in the loop areprocessed
• In aposttest loop, the condition is evaluated after the
instructions in the loop areprocessed
• In both cases,the condition is evaluated witheach
repetition
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
4An Introduction to Programming with C++, Eighth Edition
Repeating Program Instructions
(cont’d.)
• Theinstructions in aposttest loop will alwaysbe
processed at least once
• Instructions in apretest loop may not beprocessed if
the condition initially evaluates tofalse
• Therepeatable instructions in aloop are called the loop
body
• Thecondition in aloop must evaluate to aBoolean
value
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
5An Introduction to Programming with C++, Eighth Edition
Repeating Program Instructions
(cont’d.)
Repeating Program Instructions
(cont’d.)
Figure 7-1 A problem that requires the sequence and
selection structures
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
6An Introduction to Programming with C++, Eighth Edition
Repeating Program Instructions
(cont’d.)
Figure 7-2 A problem that requires the sequence and repetition structures
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
7An Introduction to Programming with C++, Eighth Edition
• Most loops have acondition and aloop body
• Someloops require the user to enter aspecialsentinel
value to end theloop
• Sentinel values should be easily distinguishable from
the valid data recognized by theprogram
• When aloop’s condition evaluates to true,the
instructions in the loop bodyare processed
• Otherwise, the instructions are skipped andprocessing
continues with the first instruction after the loop
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
8An Introduction to Programming with C++, Eighth Edition
Using a Pretest Loop to Solve a Real-
World Problem
• After each processing of the loop body (iteration), the
loop’s condition is reevaluated to determine if the
instructions should be processedagain
• Aprimingread is an instruction that appears before a
loop and is used to set up the loop with an initial value
entered by the user
• An update read is an instruction that appears within a
loop that allows the user to enter a new value at each
iteration of theloop
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
9An Introduction to Programming with C++, Eighth Edition
Using a Pretest Loop to Solve a Real-
World Problem (cont.)
Using a Pretest Loop to Solve a Real-
World Problem (cont’d.)
Figure 7-3 Problem specification and IPO chart for the commission program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
10An Introduction to Programming with C++, Eighth Edition
Figure 7-4 Components of Algorithm 2 from Figure 7-3
Using a Pretest Loop to Solve a Real-
World Problem (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
11An Introduction to Programming with C++, Eighth Edition
• Thediamond symbol in aflowchart is the decision
symbol – represents repetition structures
• Adiamond representing arepetition structure contains
aBoolean condition
• Thecondition determines whether the instructionsin
the loop areprocessed
• Adiamond representing arepetition structure hasone
flowline leading into it and two leading out
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
12An Introduction to Programming with C++, Eighth Edition
Flowcharting a Pretest Loop
• Theflowlines leading out are marked “T” (true)and “F”
(false)
• The“T” line points to the loop body
• The “F” line points to the instructions to be processed if
the loop’s condition evaluates tofalse
• Theflowline entering the diamond and symbolsand
flowlines of the true path form a circle, or loop
• Thisdistinguishes arepetition structure from a
selection structure in aflowchart
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
13An Introduction to Programming with C++, Eighth Edition
Flowcharting a Pretest Loop (cont’d.)
Flowcharting a Pretest Loop (cont’d.)
Figure 7-5 Flowchart for algorithm shown in Figure 7-4
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
14An Introduction to Programming with C++, Eighth Edition
Flowcharting a Pretest Loop (cont’d.)
Figure 7-6 First sales entry recorded in the desk-check table
Figure 7-7 First salesperson’s entry in the desk-check table
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
15An Introduction to Programming with C++, Eighth Edition
Flowcharting a Pretest Loop (cont’d.)
Figure 7-8 Second salesperson’s entry in the desk-check table
Figure 7-9 Completed desk-check table
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
16An Introduction to Programming with C++, Eighth Edition
• Youcan usethe while statement to code apretest
loop in C++
• Syntax is:
while (condition)
one statement or a statement block to beprocessedas
long asthe condition istrue
• Must supply looping condition (Booleanexpression)
• condition can contain constants, variables, functions,
arithmetic operators, comparison operators, andlogical
operators
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
17An Introduction to Programming with C++, Eighth Edition
The while Statement
• Must also provide loop body statements, whichare
processed repeatedly aslong ascondition is true
• If more than one statement in loop body, must be
entered asastatement block (enclosed in braces)
• Caninclude braces even if there is only onestatement
in the statementblock
• Good programming practice to include acomment,
such as//end while, to mark the end of the while
statement
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
18An Introduction to Programming with C++, Eighth Edition
The while Statement (cont’d.)
• Aloop whose instructions are processed indefinitely is
called an infinite loopor endlessloop
• Youcan usually stop aprogram that hasentered an
infinite loop by pressingCtrl+c
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
19An Introduction to Programming with C++, Eighth Edition
The while Statement (cont’d.)
Figure 7-10 How to use the while statement
The while Statement (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
20An Introduction to Programming with C++, Eighth Edition
Figure 7-11 IPO chart information and C++ instructions for the
commission program
The while Statement (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
21An Introduction to Programming with C++, Eighth Edition
The while Statement (cont’d.)
Figure 7-12 A sample run of the commission program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
22An Introduction to Programming with C++, Eighth Edition

More Related Content

What's hot

Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Processmshellman
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structuremshellman
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structuremshellman
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constantsmshellman
 
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
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Processmshellman
 
[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 capellaObeo
 
Portfolio control version sn_v5
Portfolio control version sn_v5Portfolio control version sn_v5
Portfolio control version sn_v5Samuel Narcisse
 
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 SAPESUG
 
Puppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepoPuppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepoPuppet
 

What's hot (15)

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 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
 
Lesson 10
Lesson 10Lesson 10
Lesson 10
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
 
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 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
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
 
[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
 
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
 
Puppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepoPuppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepo
 
PathMATE
PathMATEPathMATE
PathMATE
 

Similar to Lesson 7.1 repitition 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
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.pptMalathyN6
 
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 2360|Conferences
 
Introduction to ActiveMQ Apollo
Introduction to ActiveMQ ApolloIntroduction to ActiveMQ Apollo
Introduction to ActiveMQ Apollodejanb
 
[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...
[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...
[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...Mingda Lu
 
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...Thomas Wuerthinger
 
Graph ql subscriptions on the jvm
Graph ql subscriptions on the jvmGraph ql subscriptions on the jvm
Graph ql subscriptions on the jvmGerard Klijs
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdfTanayKashid
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjditzvenkatesh21
 
Preparing your code for Java 9
Preparing your code for Java 9Preparing your code for Java 9
Preparing your code for Java 9Deepu Xavier
 
Agile Java Testing With Open Source Frameworks
Agile Java Testing With Open Source FrameworksAgile Java Testing With Open Source Frameworks
Agile Java Testing With Open Source FrameworksViraf Karai
 
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
 

Similar to Lesson 7.1 repitition structure (20)

Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
Sql9e ppt ch03
Sql9e ppt ch03 Sql9e ppt ch03
Sql9e ppt ch03
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.ppt
 
Sql9e ppt ch04
Sql9e ppt ch04 Sql9e ppt ch04
Sql9e ppt ch04
 
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
 
Introduction to ActiveMQ Apollo
Introduction to ActiveMQ ApolloIntroduction to ActiveMQ Apollo
Introduction to ActiveMQ Apollo
 
[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...
[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...
[HKOUG] Oracle RWP - Developing High-Performance Systems by Christine Qu and ...
 
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for ...
 
Graph ql subscriptions on the jvm
Graph ql subscriptions on the jvmGraph ql subscriptions on the jvm
Graph ql subscriptions on the jvm
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
 
Preparing your code for Java 9
Preparing your code for Java 9Preparing your code for Java 9
Preparing your code for Java 9
 
Agile Java Testing With Open Source Frameworks
Agile Java Testing With Open Source FrameworksAgile Java Testing With Open Source Frameworks
Agile Java Testing With Open Source Frameworks
 
Open mp
Open mpOpen mp
Open mp
 
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
 

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

OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MysoreMuleSoftMeetup
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsPallavi Parmar
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
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.pptxJisc
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell Tolls
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
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
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 

Lesson 7.1 repitition structure

  • 1. © 2016 Cengage Learning®. May not be scanned, copied or Introduction to Programming in C++ Eighth Edition Lesson 7.1: The Repetition Structure duplicated, or posted to a publicly accessible website, in whole or in part.
  • 2. • Differentiate between apretest loop and aposttestloop • Include apretest loop in pseudocode • Include apretest loop in aflowchart • Codeapretest loop using the C++whilestatement • Utilize counter and accumulator variables • Codeapretest loop using the C++forstatement © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2An Introduction to Programming with C++, Eighth Edition Objectives
  • 3. • Therepetition structure, or loop, processes one or more instructions repeatedly • Every loop contains aBoolean condition that controls whether the instructions arerepeated • Alooping condition sayswhether to continue looping through instructions • Aloop exit condition sayswhether to stop looping through the instructions • Every looping condition can be expressed asaloop exit condition (its opposite) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3An Introduction to Programming with C++, Eighth Edition Repeating Program Instructions
  • 4. • C++useslooping conditions in repetitionstructures • Arepetition structure can be pretest orposttest • In apretest loop, the condition is evaluated beforethe instructions in the loop areprocessed • In aposttest loop, the condition is evaluated after the instructions in the loop areprocessed • In both cases,the condition is evaluated witheach repetition © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4An Introduction to Programming with C++, Eighth Edition Repeating Program Instructions (cont’d.)
  • 5. • Theinstructions in aposttest loop will alwaysbe processed at least once • Instructions in apretest loop may not beprocessed if the condition initially evaluates tofalse • Therepeatable instructions in aloop are called the loop body • Thecondition in aloop must evaluate to aBoolean value © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5An Introduction to Programming with C++, Eighth Edition Repeating Program Instructions (cont’d.)
  • 6. Repeating Program Instructions (cont’d.) Figure 7-1 A problem that requires the sequence and selection structures © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6An Introduction to Programming with C++, Eighth Edition
  • 7. Repeating Program Instructions (cont’d.) Figure 7-2 A problem that requires the sequence and repetition structures © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7An Introduction to Programming with C++, Eighth Edition
  • 8. • Most loops have acondition and aloop body • Someloops require the user to enter aspecialsentinel value to end theloop • Sentinel values should be easily distinguishable from the valid data recognized by theprogram • When aloop’s condition evaluates to true,the instructions in the loop bodyare processed • Otherwise, the instructions are skipped andprocessing continues with the first instruction after the loop © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8An Introduction to Programming with C++, Eighth Edition Using a Pretest Loop to Solve a Real- World Problem
  • 9. • After each processing of the loop body (iteration), the loop’s condition is reevaluated to determine if the instructions should be processedagain • Aprimingread is an instruction that appears before a loop and is used to set up the loop with an initial value entered by the user • An update read is an instruction that appears within a loop that allows the user to enter a new value at each iteration of theloop © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9An Introduction to Programming with C++, Eighth Edition Using a Pretest Loop to Solve a Real- World Problem (cont.)
  • 10. Using a Pretest Loop to Solve a Real- World Problem (cont’d.) Figure 7-3 Problem specification and IPO chart for the commission program © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10An Introduction to Programming with C++, Eighth Edition
  • 11. Figure 7-4 Components of Algorithm 2 from Figure 7-3 Using a Pretest Loop to Solve a Real- World Problem (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11An Introduction to Programming with C++, Eighth Edition
  • 12. • Thediamond symbol in aflowchart is the decision symbol – represents repetition structures • Adiamond representing arepetition structure contains aBoolean condition • Thecondition determines whether the instructionsin the loop areprocessed • Adiamond representing arepetition structure hasone flowline leading into it and two leading out © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12An Introduction to Programming with C++, Eighth Edition Flowcharting a Pretest Loop
  • 13. • Theflowlines leading out are marked “T” (true)and “F” (false) • The“T” line points to the loop body • The “F” line points to the instructions to be processed if the loop’s condition evaluates tofalse • Theflowline entering the diamond and symbolsand flowlines of the true path form a circle, or loop • Thisdistinguishes arepetition structure from a selection structure in aflowchart © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13An Introduction to Programming with C++, Eighth Edition Flowcharting a Pretest Loop (cont’d.)
  • 14. Flowcharting a Pretest Loop (cont’d.) Figure 7-5 Flowchart for algorithm shown in Figure 7-4 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14An Introduction to Programming with C++, Eighth Edition
  • 15. Flowcharting a Pretest Loop (cont’d.) Figure 7-6 First sales entry recorded in the desk-check table Figure 7-7 First salesperson’s entry in the desk-check table © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15An Introduction to Programming with C++, Eighth Edition
  • 16. Flowcharting a Pretest Loop (cont’d.) Figure 7-8 Second salesperson’s entry in the desk-check table Figure 7-9 Completed desk-check table © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16An Introduction to Programming with C++, Eighth Edition
  • 17. • Youcan usethe while statement to code apretest loop in C++ • Syntax is: while (condition) one statement or a statement block to beprocessedas long asthe condition istrue • Must supply looping condition (Booleanexpression) • condition can contain constants, variables, functions, arithmetic operators, comparison operators, andlogical operators © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 17An Introduction to Programming with C++, Eighth Edition The while Statement
  • 18. • Must also provide loop body statements, whichare processed repeatedly aslong ascondition is true • If more than one statement in loop body, must be entered asastatement block (enclosed in braces) • Caninclude braces even if there is only onestatement in the statementblock • Good programming practice to include acomment, such as//end while, to mark the end of the while statement © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 18An Introduction to Programming with C++, Eighth Edition The while Statement (cont’d.)
  • 19. • Aloop whose instructions are processed indefinitely is called an infinite loopor endlessloop • Youcan usually stop aprogram that hasentered an infinite loop by pressingCtrl+c © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19An Introduction to Programming with C++, Eighth Edition The while Statement (cont’d.)
  • 20. Figure 7-10 How to use the while statement The while Statement (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20An Introduction to Programming with C++, Eighth Edition
  • 21. Figure 7-11 IPO chart information and C++ instructions for the commission program The while Statement (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21An Introduction to Programming with C++, Eighth Edition
  • 22. The while Statement (cont’d.) Figure 7-12 A sample run of the commission program © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22An Introduction to Programming with C++, Eighth Edition