1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
-
Upload
hope-warren -
Category
Documents
-
view
235 -
download
0
description
Transcript of 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
![Page 1: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/1.jpg)
1
Programming in C++
Dale/Weems/HeadingtonChapter 6Looping
![Page 2: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/2.jpg)
2
A loop is a repetition control structure.
It causes a single statement or block to be executed repeatedly.
What is a loop?
![Page 3: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/3.jpg)
3
Two types of loops
count controlled loopsrepeat a specified number of times
event-controlled loopssome condition within the loop body changes and this causes the repeating to stop
![Page 4: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/4.jpg)
4
While Statement
SYNTAX
while ( Expression ){ .
. // loop body .}
NOTE: Loop body can be a single statement, a null statement, or a block.
![Page 5: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/5.jpg)
5
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
expressionFALSE
TRUE bodystatement
![Page 6: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/6.jpg)
6
Count-controlled loop contains
an intialization 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
![Page 7: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/7.jpg)
7
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 8: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/8.jpg)
8
count
43210
so loop terminates
Trace and Output
OUTPUT: 4 3 2 1 Done
![Page 9: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/9.jpg)
9
myInfile contains 100 blood pressures.
Use a while loop to read the 100 blood pressures and find their total.
Count-Controlled Loop Example
![Page 10: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/10.jpg)
10
ifstream myInfile ;int ThisBP ;int Total ;
int count ;
count = 0 ; // initialize
while ( count < 100 ) // test expression{ myInfile >> ThisBP ;
Total = Total + ThisBP ; count++ ; // update
}
cout << “The total = “ << Total << endl ;
![Page 11: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/11.jpg)
11
Event-controlled loops Sentinel controlled
keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop.
End-of-file controlledkeep processing data as long as there is more
data in the file. Flag controlled
keep processing data until the value of a flag changes in the loop body.
![Page 12: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/12.jpg)
12
Examples of kinds of loops
Count controlled loop Read exactly 100 blood pressures from a file.
End-of-file controlledloop
Read all the blood pressures from a file no matter how many are there.
![Page 13: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/13.jpg)
13
Examples of kinds of loops
Sentinel controlled loop
Read blood pressures until a special value (like -1) selected by you is read.
Flag controlledloop
Read blood pressures until a dangerously high BP (200 or more) is read.
![Page 14: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/14.jpg)
14
A Sentinel-controlled loop
Requires a “priming read”
“Priming read” means you read one set of data before the while
![Page 15: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/15.jpg)
15
// Sentinel controlled loop
Total = 0;
cout << “Enter a blood pressure (-1 to stop ) ”;cin >> ThisBP;
while (ThisBP != -1) // while not sentinel{
Total = Total + ThisBP;cout << “Enter a blood pressure (-1 to stop ) ”;cin >> ThisBP;
}cout << Total;
![Page 16: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/16.jpg)
16
End-of-File controlled loop
depends on fact that a file goes into fail state when you try to read a data value beyond the end of the file.
![Page 17: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/17.jpg)
17
Total = 0;
myInfile >> ThisBP; // priming read
while (myInfile) // while last read successful
{Total = Total + ThisBP;myInfile >> ThisBP; // read another
}
cout << Total;
// End-of-file controlled loop
![Page 18: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/18.jpg)
18
//End-of-file at keyboard
Total = 0;
cout << “Enter blood pressure (Ctrl-Z to stop)”;cin >> ThisBP; // priming read
while (cin) // while last read successful{
Total = Total + ThisBP;cout << “Enter blood pressure”;cin >> ThisBP; // read another
}cout << Total;
![Page 19: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/19.jpg)
19
Flag-controlled loops
You initialize a flag (usually to 1 or 0) Use meaningful name for the flag A condition in the loop body changes the
value of the flag Test for the flag in the loop test
expression
![Page 20: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/20.jpg)
20
CountGoodReadings = 0;Safe = 1; // initialize flag to 1
while (Safe){
cin >> ThisBP;if ( ThisBP >= 200 )Safe = 0; // change flag to 0
elseCountGoodReadings++;
}
cout << CountGoodReadings << endl;
![Page 21: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/21.jpg)
21
Loops often used to
Count all data values Count special data values Sum data values Keep track of previous and current values
![Page 22: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/22.jpg)
22
Previous and current values
Write a program that counts the number of != operators in a program file
Read one character in the file at a time
Keep track of current and previous characters
![Page 23: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/23.jpg)
23
Keeping track of values
(x != 3) { cout << endl;}
FILE CONTENTS
previous current count( x 0
! = 1= ‘ ‘ 1
x ‘ ‘ 0
3 ) 1‘ ‘ 3 1
‘ ‘ ! 0
![Page 24: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/24.jpg)
24
int count;char previous, current;
count = 0 ;inFile.get (previous); // priming readsinFile.get(current);
while (inFile){ if ( (current == ‘=‘) && (previous == ‘!’) )
count++; previous = current; // update
inFile.get(current); // read another}
![Page 25: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/25.jpg)
25
Patient Data A file contains blood pressure data for
different people.
Each line has a patient ID, the number of readings for that patient, followed by the actual readings
ID Howmany Readings
4567 5 180 140 150 170 1202318 2 170 2105232 3 150 151 151
![Page 26: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/26.jpg)
26
4567 1522318 1905232 151 . . . . . .There were 432 patients in file.
Read the data and display a chart
Patient ID BP Average
![Page 27: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/27.jpg)
27
Algorithm uses Nest of Loops
initialize PatientCount to 0 read first ID and Howmany from file while not end-of-file
increment PatientCount display ID use a count-controlled loop to read
and sum up this patient’s Howmany BP’s calculate and display Average for
patient read next ID and Howmany from file
display PatientCount
![Page 28: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/28.jpg)
28
To design a nested loop
Begin with outer loop.
When you get to where the inner loop appears, make it a separate module and come back to its design later.
![Page 29: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/29.jpg)
#include <iostream.h>#include <fstream.h>
int main (void){ int PatientCount, // declarations ThisID,
HowMany, ThisBP, TotalForPatient, Count;
float Average;
ifstream myInfile;
![Page 30: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/30.jpg)
myInfile.open(“A:\\BP.dat”);
if (!myInfile ) // opening failed{ cout << “File opening error. Program terminated.”; return 1;}
cout << “ID Number Average BP” << endl;
PatientCount = 0;
myInfile >> ThisID >> HowMany; // priming read
![Page 31: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/31.jpg)
while ( myInfile ) // last read successful{
PatientCount++; cout << ThisID; TotalForPatient = 0; // initialize inner loop
count = 0; while ( count < HowMany) { myInfile >> ThisBP; count ++; TotalForPatient = TotalForPatient + ThisBP; }
Average = TotalForPatient / float(HowMany); cout << int (Average + .5) << endl; // round myInfile >> ThisID >> HowMany; // another read}
![Page 32: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/32.jpg)
cout << “There were “ << PatientCount << “patients on file.” << endl;
cout << “Program terminated.\n”;
return 0;
}
![Page 33: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/33.jpg)
33
Information about 20 books in diskfile
“A:\\myIn.dat”
3.98 P <eoln>7.41 H <eoln>8.79 P <eoln>
.
.
.
Price of bookHardback orPaperback?
WRITE A PROGRAM TO FIND TOTAL VALUE OF ALL BOOKS
![Page 34: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/34.jpg)
34
#include <iostream.h> // for cout#include <fstream.h> // for file I/O
int main (void){ float price ; // declarations char kind ; ifstream myInfile ; float total = 0.0 ; int count = 1;
Program to read info about 20 books from a disk file
![Page 35: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/35.jpg)
35
Rest of Program
myInfile.open(“A:\\myIn.dat”) ;
// count-controlled processing loop while ( count <= 20 ) { myInfile >> price >> kind ;
total = total + price ; count ++ ;
} cout << “Total is: “ << total << endl ; myInfile.close( ) ; return 0 ;}
![Page 36: 1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b8c7f8b9ab0599bf3ea/html5/thumbnails/36.jpg)
36
Trace of Program Variables
count price kind total 0.0
1 3.98 ‘P’ 3.982 7.41 ‘H’ 11.393 8.79 ‘P’ 20.184 etc.
20 21 so loop terminates