Programming in C DaleWeemsHeadington Chapter 6 Looping 1
- Slides: 36
Programming in C++ Dale/Weems/Headington Chapter 6 Looping 1
What is a loop? l A loop is a repetition control structure. l It causes a single statement or block to be executed repeatedly. 2
Two types of loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop body changes and this causes the repeating to stop 3
While Statement SYNTAX while ( Expression ) {. . . } // loop body NOTE: Loop body can be a single a null statement, or a block. statement, 4
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 expression TRUE body statement 5
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 6
Count-controlled loop int count ; count = 4; // initialize loop variable while (count > 0) // test expression { cout << count << endl ; // repeated action count -- ; // update loop variable } cout << “Done” << endl ; 7
Trace and Output count 4 3 2 1 0 OUTPUT: 4 3 2 1 Done so loop terminates 8
Count-Controlled Loop Example my. Infile contains 100 blood pressures. Use a while loop to read the 100 blood pressures and find their total. 9
ifstream my. Infile ; int This. BP ; int Total ; int count ; count = 0 ; // initialize while ( count < 100 ) { my. Infile >> This. BP ; Total = Total + This. BP ; count++ ; // test expression // update } cout << “The total = “ << Total << endl ; 10
Event-controlled loops l l l 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 controlled keep 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. 11
Examples of kinds of loops Count controlled loop Read exactly 100 blood pressures from a file. End-of-file controlled loop Read all the blood pressures from a file no matter how many are there. 12
Examples of kinds of loops Sentinel controlled loop Read blood pressures until a special value (like -1) selected by you is read. Flag controlled loop Read blood pressures until a dangerously high BP (200 or more) is read. 13
A Sentinel-controlled loop l Requires a “priming read” l “Priming read” means you read one set of data before the while 14
// Sentinel controlled loop Total = 0; cout << “Enter a blood pressure (-1 to stop ) ”; cin >> This. BP; while (This. BP != -1) // while not sentinel { Total = Total + This. BP; cout << “Enter a blood pressure (-1 to stop ) ”; cin >> This. BP; } cout << Total; 15
End-of-File controlled loop l depends on fact that a file goes into fail state when you try to read a data value beyond the end of the file. 16
// End-of-file controlled loop Total = 0; my. Infile >> This. BP; // priming read while (my. Infile) // while last read successful { Total = Total + This. BP; my. Infile >> This. BP; // read another } cout << Total; 17
//End-of-file at keyboard Total = 0; cout << “Enter blood pressure (Ctrl-Z to stop)”; cin >> This. BP; // priming read while (cin) // while last read successful { Total = Total + This. BP; cout << “Enter blood pressure”; cin >> This. BP; // read another } cout << Total; 18
Flag-controlled loops You initialize a flag (usually to 1 or 0) l Use meaningful name for the flag l A condition in the loop body changes the value of the flag l Test for the flag in the loop test expression l 19
Count. Good. Readings = 0; Safe = 1; // initialize flag to 1 while (Safe) { cin >> This. BP; if ( This. BP >= 200 ) Safe = 0; // change flag to 0 else Count. Good. Readings++; } cout << Count. Good. Readings << endl; 20
Loops often used to Count all data values l Count special data values l Sum data values l Keep track of previous and current values l 21
Previous and current values l Write a program that counts the number of != operators in a program file l Read one character in the file at a time l Keep track of current and previous characters 22
Keeping track of values (x != 3) { cout << endl; } FILE CONTENTS previous current count ( x 0 x ‘‘ 0 ‘‘ ! 0 ! = 1 = ‘‘ 1 ‘‘ 3 1 3 ) 1 23
int count; char previous, current; count = 0 ; in. File. get (previous); in. File. get(current); // priming reads while (in. File) { if ( (current == ‘=‘) && (previous == ‘!’) ) count++; previous = current; in. File. get(current); } // update // read another 24
Patient Data l A file contains blood pressure data for different people. l Each line has a patient ID, the number of readings for that patient, followed by the actual readings ID 4567 2318 5232 Howmany 5 2 3 Readings 180 140 150 170 120 170 210 151 151 25
Read the data and display a chart Patient ID BP Average 4567 152 2318 190 5232 151. . . There were 432 patients in file. 26
Algorithm uses Nest of Loops l l initialize Patient. Count to 0 read first ID and Howmany from file while not end-of-file n increment Patient. Count n display ID n use a count-controlled loop to read and sum up this patient’s Howmany BP’s n calculate and display Average for patient n read next ID and Howmany from file display Patient. Count 27
To design a nested loop l Begin with outer loop. l When you get to where the inner loop appears, make it a separate module and come back to its design later. 28
#include <iostream. h> #include <fstream. h> int main (void) { int Patient. Count, This. ID, How. Many, This. BP, Total. For. Patient, Count; float Average; ifstream my. Infile; // declarations
my. Infile. open(“A: \BP. dat”); if (!my. Infile ) // opening failed { cout << “File opening error. Program terminated. ”; return 1; } cout << “ID Number Average BP” << endl; Patient. Count = 0; my. Infile >> This. ID >> How. Many; // priming read
while ( my. Infile ) // last read successful { Patient. Count++; cout << This. ID; Total. For. Patient = 0; // initialize inner loop count = 0; while ( count < How. Many) { my. Infile >> This. BP; count ++; Total. For. Patient = Total. For. Patient + This. BP; } Average = Total. For. Patient / float(How. Many); cout << int (Average +. 5) << endl; // round my. Infile >> This. ID >> How. Many; // another read }
cout << “There were “ << Patient. Count << “patients on file. ” << endl; cout << “Program terminated. n”; return 0; }
Information about 20 books in diskfile “A: \my. In. dat” Price of book Hardback or Paperback? 3. 98 P <eoln> 7. 41 H <eoln> 8. 79 P <eoln>. . . WRITE A PROGRAM TO FIND TOTAL VALUE OF ALL BOOKS 33
Program to read info about 20 books from a disk file #include <iostream. h> #include <fstream. h> int main (void) { float price ; char kind ; ifstream my. Infile ; float total = 0. 0 ; int count = 1; // for cout // for file I/O // declarations 34
Rest of Program my. Infile. open(“A: \my. In. dat”) ; // count-controlled processing loop while ( count <= 20 ) { my. Infile >> price >> kind ; total = total + price ; count ++ ; } cout << “Total is: “ << total << endl ; my. Infile. close( ) ; return 0 ; } 35
Trace of Program Variables count 1 2 3 4 20 21 price 3. 98 7. 41 8. 79 etc. kind ‘P’ ‘H’ ‘P’ total 0. 0 3. 98 11. 39 20. 18 so loop terminates 36
- Perbedaan linear programming dan integer programming
- Greedy algorithm vs dynamic programming
- System programming definition
- Linear vs integer programming
- Perbedaan linear programming dan integer programming
- Perulangan pada flowchart
- Struktur perulangan dalam pascal
- Looping in writing
- Bentuk umum statemen for adalah
- Qbasic loops
- For bersarang
- Perulangan bersarang java
- Disadvantages of looping in education
- What is decision making and looping in c
- Pert vs cpm
- Flowchart counter loop
- Loop aircraft motion
- Pengulangan merupakan struktur dasar
- Looping pemrograman
- Looping pascal
- Turtle small basic
- Looping algoritma
- Struktur branching
- Looping
- Colon looping
- While loop forever
- What is a workaround
- Perulangan looping
- Perulangan looping
- Perulangan looping
- Python chapter 5
- The zj row in a simplex table for maximization represents
- Chapter 16 programming language
- Chapter 16 programming language
- Qm for windows
- Computer programming chapter 1
- Chapter 1 introduction to computers and programming