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

Looping

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

Looping

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

    1. Looping Lecture 4

    2. Structured-Programming Summary All programs broken down into Sequence Selection if, if/else, or switch Any selection can be rewritten as an if statement Repetition while, do/while, or for Any repetition structure can be rewritten as a while statement

    3. while Repetition Structure Repetition control structure Action repeated while some condition remains true Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false Example int product = 2; while ( product <= 1000 ) product = 2 * product; Two varieties: event-controlled and count-controlled

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

    5. Repetition Structure Elements Repetition structure has three required elements: Statement to be repeated Condition to be evaluated Determines loop termination Initial value for the condition Repetition statements include while for do while

    6. Basic Loop Structures The condition can be tested At the beginning: pretest or entrance-controlled loop The condition can be tested At the end: posttest or exit-controlled loop Something in the loop body must cause the condition to change in order to avoid an infinite loop (one that never terminates)

    7. Pre-test Symbology Pretest loop: condition is tested first; if false, statements in the loop body are never executed while and for loops are pretest loops

    8. Post-test Symbology

    9. When the expression is tested and found to be false, the loop exits and control passes to the statement which follows the loop body. WHILE LOOP

    10. Number of Loop Iterations There are essentially two kinds of loop iterations: Fixed-count loop: loop is processed for a fixed number of repetitions Variable-condition loop: number of repetitions depends on the value of a variable

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    28. Count-controlled Loop int count ; count = 4; while (count > 0) FALSE { cout << count << endl ; count -- ; } cout << Done << endl ;

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

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

    31. Examples of Count-controlled and EOF Loops

    32. Examples of Sentinel and Flag Loops

    33. A Sentinel-controlled Loop requires a priming read priming read means you read one set of data before the while

    34. // Sentinel controlled loop total = 0; cout << Enter a blood pressure (-1 to stop ) ; cin >> thisBP; // The priming read while (thisBP != -1) // while not sentinel { total = total + thisBP; cout << Enter a blood pressure (-1 to stop ) ; cin >> thisBP; } cout << total;

    35. End-of-File Controlled Loop depends on the fact that a file goes into fail state when you try to read a data value beyond the end of the file

    36. // End-of-file controlled loop

    37. //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; or while (!cin.eof()) { total = total + thisBP; cout << Enter blood pressure; cin >> thisBP; // read another } cout << total;

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

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

    40. Loops are often used to: count all data values count special data values sum data values keep track of previous and current values, etc.

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

    45. Trace of Program Variables count price kind total 0.0 1 3.98 P 3.98 2 7.41 H 11.39 3 8.79 P 20.18 4 etc. 20 21 so loop terminates

    46. for Loops

    47. for Loops (continued) Altering list: provides the increment value that is added or subtracted from the counter in each iteration of the loop If altering list is missing, the counter must be altered in the loop body If initializing list is missing, the counter initial value must be provided prior to entering the for loop Omitting the expression will result in an infinite loop

    48. for Loop Example

    49. for Loop Symbology

    50. In-Class Exercise (follows) Based on following two programs

    51. In-Class Exercise: count.cpp // Program Count prompts for, reads, echo prints, and sums a // fixed number of integer values. The sum is printed. #include <iostream> using namespace std; const int LIMIT = 10; int main () { int counter; // loop-control variable int sum; // summing variable int dataValue; // input value counter = 1; sum = 0; // Input and sum integer data values. while (counter <= LIMIT) { cout << "Enter an integer value. Press return." << endl; cin >> dataValue; sum = sum + dataValue; counter++; } cout << "Sum is " << sum << endl; return 0; }

    52. In-Class Exercise: count2.cpp // Program Count2 prompts for, reads, and sums integer // values until a negative number is read. The input // values and the sum are printed. #include <iostream> using namespace std; int main () { int sum; // summing variable int dataValue; // input value sum = 0; cout << "To stop processing, enter a negative value." << endl; cout << " Enter an integer value; press return." << endl; cin >> dataValue; // Read first data value to prepare for loop. // Input and sum integer data values while (dataValue > 0) { sum = sum + dataValue; cout << "Enter an integer value; press return." << endl; cin >> dataValue; } cout << "Sum is " << sum << endl; return 0; }

    53. Exercises: What is printed if the following data values are entered as prompted for count.cpp? 8 5 3 -2 0 9 1 7 3 10 Is the loop in program count.cpp a count-controlled loop or an event-controlled loop? Explain. What is printed if the following data values are entered one per line for count2.cpp? 8 5 3 -2 0 9 1 7 3 10 Is the loop in program count2.cpp a count-controlled loop or an event-controlled loop? Explain. Change count.cpp so that the number of values to be read (LIMIT) is input from the keyboard rather than being set as a named constant. Please dont forget to prompt for limit.