Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.

Similar presentations


Presentation on theme: "1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington."— Presentation transcript:

1 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

2 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to Control Input Data l Using a While Statement for Summing and Counting l Nested While Loops l Loop Testing and Debugging

3 3 Revision of Selection l What construct is used in C++ to narrow down our range of values? l What is the rule for matching else constructs to if constructs? l What is the danger in using an if followed by another if? l For testing selection control structures, should we use white box testing or black box testing?

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

5 5 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

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

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

8 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

9 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

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

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

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

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

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

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

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

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

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

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

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

21 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

22 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

23 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

24 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

25 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

26 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

27 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 ;


Download ppt "1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington."

Similar presentations


Ads by Google