![Page 1: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/1.jpg)
Chapter 7:The Repetition Structure
Introduction to Programming with C++
Fourth Edition
![Page 2: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/2.jpg)
Introduction to Programming with C++, Fourth Edition 2
Objectives
• Include a repetition structure in pseudocode and in a flowchart
• Code a pretest loop using the C++ while statement
• Initialize and update counters and accumulators• Code a pretest loop using the C++ for statement
![Page 3: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/3.jpg)
Introduction to Programming with C++, Fourth Edition 3
Using the Repetition Structure
• Repetition structure or loop - allows repeated processing of one or more program instructions until some condition is met
• Repetition structure can be either a pretest loop or a posttest loop– Pretest loop: evaluation occurs before loop
instructions are processed– Posttest loop: evaluation occurs after loop
instructions are processed
![Page 4: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/4.jpg)
Introduction to Programming with C++, Fourth Edition 4
Pretest Loops
![Page 5: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/5.jpg)
Introduction to Programming with C++, Fourth Edition 5
Pretest Loops (continued)
![Page 6: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/6.jpg)
Introduction to Programming with C++, Fourth Edition 6
Pretest Loops (continued)
• Every loop has:– a loop condition– a loop body
• Loop body: – instructions within a loop
• Loop condition: – appears at the beginning of a pretest loop– determines the number of times instructions
within the loop are processed
![Page 7: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/7.jpg)
Introduction to Programming with C++, Fourth Edition 7
• Sentinel value – special value to end the loop
• Priming read:– Appears above the loop– Used to prepare or set up the loop
Pretest Loops (continued)
![Page 8: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/8.jpg)
Introduction to Programming with C++, Fourth Edition 8
Components of a Loop
![Page 9: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/9.jpg)
Introduction to Programming with C++, Fourth Edition 9
O’Donnell Incorporated Algorithm Shown in Flowchart Form
![Page 10: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/10.jpg)
Introduction to Programming with C++, Fourth Edition 10
Using the while Statement to Code the Pretest Loop
• While statement: programmer must supply the loop condition to be evaluated
• Loop condition:– must be a Boolean expression (true or false)– can contain variables, constants, functions,
methods, arithmetic operators, comparison operators, and logical operators
• Endless or infinite loop – one that processes its instructions indefinitely
![Page 11: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/11.jpg)
Introduction to Programming with C++, Fourth Edition 11
Syntax of the C++ while Statement
![Page 12: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/12.jpg)
Introduction to Programming with C++, Fourth Edition 12
Using Counters and Accumulators
• Counter - a numeric variable used for counting
• Accumulator - a numeric variable used for accumulating a value
• Initializing - assigning a beginning value to the counter or accumulator
• Updating or incrementing - adding a number to the value stored in the counter or the accumulator
![Page 13: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/13.jpg)
Introduction to Programming with C++, Fourth Edition 13
Counter-Controlled Pretest Loops
![Page 14: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/14.jpg)
Introduction to Programming with C++, Fourth Edition 14
Using the for Statement to Code a Pretest Loop
• Most common use for the for statement:– Code pretest loops whose processing is
controlled by a counter• For statement:
– Begins with the for clause– Followed by the body of the loop
• If the loop body contains more than one statement:– Statements must be entered as a statement block
i.e., enclosed in braces ({})
![Page 15: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/15.jpg)
Introduction to Programming with C++, Fourth Edition 15
Syntax of the C++ for statement
![Page 16: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/16.jpg)
Introduction to Programming with C++, Fourth Edition 16
for Statement Displays the Numbers 1 Through 3
![Page 17: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/17.jpg)
Introduction to Programming with C++, Fourth Edition 17
for Statement Calculates and Displays a Commission
![Page 18: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/18.jpg)
Introduction to Programming with C++, Fourth Edition 18
for Statement Calculates and Displays a Bonus
![Page 19: Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition](https://reader035.vdocument.in/reader035/viewer/2022081806/5a4d1b8d7f8b9ab0599bfc3f/html5/thumbnails/19.jpg)
Introduction to Programming with C++, Fourth Edition 19
Summary
• Repetition structure or loop - allows repeated processing of one or more program instructions until some condition is met – Repetition structures contain a condition and
a loop body