Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Looping PowerPoint Presentation

Looping

106 Vues Download Presentation
Télécharger la présentation

Looping

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Looping

  2. Chapter 6 Topics • While Statement Syntax • Phases of Loop Execution • Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops • Using the End-of-File Condition to Control Input Data • Using a While Statement for Summing and Counting • How to Design Loops • Nested While Loops • Loop Testing and Debugging

  3. What is a loop? • A loop is a repetition control structure. • it causes a single statement or block to be executed repeatedly

  4. While Statement SYNTAX while ( Expression ) { . . // loop body . } NOTE: Loop body can be a single statement, a null statement, or a block.

  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 FALSE Expression TRUE body statement

  6. WHILE STATEMENT IF-THEN SRATEMENT While (Expression) If (Expression) false true false true Statement1 Statement1 Statement2 Statement2 A Comparison of If and While

  7. Phases of Loop Execution • Loop entry • Iteration • Loop test • Loop exit • Termination

  8. 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

  9. Count-controlled loop contains • an initialization of the loop control variable • an expression to test for continuing the loop • an update of the loop controlvariable to be executed with each iteration of the body

  10. 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 ;

  11. count Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT

  12. count 4 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT

  13. count 4 Count-controlled Loop int count ; count = 4; while (count > 0) TRUE { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT

  14. count 4 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4

  15. count 3 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4

  16. count 3 Count-controlled Loop int count ; count = 4; while (count > 0) TRUE { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4

  17. count 3 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3

  18. count 2 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3

  19. count 2 Count-controlled Loop int count ; count = 4; while (count > 0) TRUE { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3

  20. count 2 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2

  21. count 1 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2

  22. count 1 Count-controlled Loop int count ; count = 4; while (count > 0) TRUE { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2

  23. count 1 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2 1

  24. count 0 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2 1

  25. count 0 Count-controlled Loop int count ; count = 4; while (count > 0) FALSE { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2 1

  26. count 0 Count-controlled Loop int count ; count = 4; while (count > 0) { cout << count << endl ; count --; } cout << “Done” << endl ; OUTPUT 4 3 2 1 Done

  27. Count-Controlled Loop Example myInfile contains 100 blood pressures Use a while loop to read the 100 blood pressures and find their total

  28. 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 ;

  29. 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 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

  30. Examples of Kinds of Loops Read exactly 100 blood pressures from a file. Count controlled loop Read all the blood pressures from a file no matter how many are there. End-of-file controlled loop

  31. Examples of Kinds of Loops Read blood pressures until a special value (like -1) selected by you is read. Sentinel controlled loop Read blood pressures until a dangerously high BP (200 or more) is read. Flag controlled loop

  32. A Sentinel-controlled Loop • requires a “priming read” • “priming read” means you read one set of data before the while

  33. // 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;

  34. 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

  35. // End-of-file controlled loop total = 0; myInfile >> thisBP; // priming read while (myInfile) // while last read successful { total = total + thisBP; myInfile >> thisBP; // read another } cout << total;

  36. //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;

  37. Flag-controlled Loops • you initialize a flag (to true or false) • 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

  38. countGoodReadings = 0; isSafe = true; // initialize Boolean flag while (isSafe) { cin >> thisBP; if ( thisBP >= 200 ) isSafe = false;// change flag value else countGoodReadings++; } cout << countGoodReadings << endl;

  39. Looping Subtasks Loops often used to • count all data values • count special data values • sum data values • keep track of previous and current values

  40. 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

  41. Keeping Track of Values previous current count (x != 3) { cout << endl; } ( x 0 x ‘ ‘ 0 ‘ ‘ ! 0 FILE CONTENTS ! = 1 = ‘ ‘ 1 ‘ ‘ 3 1 3 ) 1

  42. int count; char previous; char current; count = 0 ; inFile.get (previous);// priming reads inFile.get(current); while (inFile) { if ( (current == ‘=‘) && (previous == ‘!’) ) count++; previous = current;// update inFile.get(current);// read another }

  43. How to Design Loops • Designing the Flow of Control • Designing the Process Within the Loop • The Loop Exit

  44. Pattern of a Nested Loop initialize outer loop while (outer loop condition) { . . . initialize inner loop while (inner loop condition) { inner loop processing and update } . . . }

  45. 4567 5 180 140 150 170 120 2318 2 170 210 5232 3 150 151 151 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

  46. 4567 152 2318 190 5232 151 . . . . . . There were 432 patients in file. Read the data and display a chart Patient ID BP Average

  47. Algorithm Uses Nested 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

  48. 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

  49. #include <iostream>#include <fstream>using namespace std;int main ( ){ int patientCount; // declarations int thisID; int howMany; int thisBP; int totalForPatient; int count; float average; ifstream myInfile;

  50. 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