1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
-
date post
21-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
![Page 1: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/1.jpg)
1
Lecture 14
Chapter 6
Looping
Dale/Weems/Headington
![Page 2: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/2.jpg)
2
Chapter 6 Topics
While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control
Input Data Using a While Statement for Summing and
Counting Nested While Loops Loop Testing and Debugging
![Page 3: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/3.jpg)
3
Revision of Selection
What construct is used in C++ to narrow down our range of values?
What is the rule for matching else constructs to if constructs?
What is the danger in using an if followed by another if?
For testing selection control structures, should we use white box testing or black box testing?
![Page 4: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/4.jpg)
4
A loop is a repetition control structure.
it causes a single statement or block to be executed repeatedly
What is a loop?
![Page 5: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/5.jpg)
5
Two Types of Loops
count controlled loops
repeat a specified number of times
event-controlled loopssome condition within the loop body changes and this causes the repeating to stop
![Page 6: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/6.jpg)
6
While Statement
SYNTAX
while ( Expression )
{ .
. // loop body
.
}
NOTE: Loop body can be a single statement, a null statement, or a block.
![Page 7: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/7.jpg)
7
When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body.
WHILE LOOP
FALSE
TRUE
bodystatement
Expression
![Page 8: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/8.jpg)
8
an initialization of the loop control variable
an expression to test for continuing the loop
an update of the loop control variable to be executed with each iteration of the body
Count-controlled loop contains
![Page 9: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/9.jpg)
9
int count ;
count = 4; // initialize loop variable
while (count > 0) // test expression
{
cout << count << endl ; // repeated action
count -- ; // update loop variable
}
cout << “Done” << endl ;
Count-controlled Loop
![Page 10: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/10.jpg)
10
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
count
![Page 11: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/11.jpg)
11
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
count
4
![Page 12: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/12.jpg)
12
Count-controlled Loop
int count ;
count = 4;
while (count > 0) TRUE
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
count
4
![Page 13: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/13.jpg)
13
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4
count
4
![Page 14: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/14.jpg)
14
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4
count
3
![Page 15: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/15.jpg)
15
Count-controlled Loop
int count ;
count = 4;
while (count > 0) TRUE
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4
count
3
![Page 16: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/16.jpg)
16
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3
count
3
![Page 17: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/17.jpg)
17
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3
count
2
![Page 18: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/18.jpg)
18
Count-controlled Loop
int count ;
count = 4;
while (count > 0) TRUE
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3
count
2
![Page 19: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/19.jpg)
19
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2
count
2
![Page 20: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/20.jpg)
20
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2
count
1
![Page 21: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/21.jpg)
21
Count-controlled Loop
int count ;
count = 4;
while (count > 0) TRUE
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2
count
1
![Page 22: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/22.jpg)
22
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2 1
count
1
![Page 23: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/23.jpg)
23
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2 1
count
0
![Page 24: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/24.jpg)
24
Count-controlled Loop
int count ;
count = 4;
while (count > 0) FALSE
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2 1
count
0
![Page 25: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/25.jpg)
25
Count-controlled Loop
int count ;
count = 4;
while (count > 0)
{
cout << count << endl ;
count -- ;}
cout << “Done” << endl ;
OUTPUT
4 3 2 1 Done
count
0
![Page 26: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/26.jpg)
26
User has to give 100 blood pressure values observed for a patient
Use a while loop to read the 100 blood pressure values and find their average
Count-Controlled Loop Example
![Page 27: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.](https://reader038.fdocuments.in/reader038/viewer/2022103123/56649d5a5503460f94a3a4ab/html5/thumbnails/27.jpg)
27
int thisBP ;int total ;int count ;
count = 0 ; // initialize
while ( count < 100 ) // test expression{ cin >> thisBP ;
total = total + thisBP ; count++ ; // update
}
cout << “The average = “ << ((float) total)/100.0 << endl ;