SlideShare a Scribd company logo
© 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 Process
mshellman
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
mshellman
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
mshellman
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
mshellman
 
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
mshellman
 
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
mshellman
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
MLG College of Learning, Inc
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
Samuel Narcisse
 
Mechatronics engineer
Mechatronics engineerMechatronics engineer
Mechatronics engineer
Samuel Narcisse
 
[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
Obeo
 
Portfolio control version sn_v5
Portfolio control version sn_v5Portfolio control version sn_v5
Portfolio control version sn_v5
Samuel 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 SAP
ESUG
 
Puppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepoPuppet camp2021 testing modules and controlrepo
Puppet camp2021 testing modules and controlrepo
Puppet
 
PathMATE
PathMATEPathMATE
PathMATE
CS, NcState
 

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
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
MLG College of Learning, Inc
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
mshellman
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
Terry Yoast
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.ppt
MalathyN6
 
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
360|Conferences
 
Introduction to ActiveMQ Apollo
Introduction to ActiveMQ ApolloIntroduction to ActiveMQ Apollo
Introduction to ActiveMQ Apollo
dejanb
 
[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 jvm
Gerard Klijs
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
TanayKashid
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
itzvenkatesh21
 
Preparing your code for Java 9
Preparing your code for Java 9Preparing your code for Java 9
Preparing your code for Java 9
Deepu 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 Frameworks
Viraf Karai
 
Open mp
Open mpOpen mp
Open mp
Gopi Saiteja
 
Omni channel network design
Omni channel network designOmni channel network design
Omni channel network design
faiza20202880
 

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
 
Omni channel network design
Omni channel network designOmni channel network design
Omni channel network design
 

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

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 

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