1 lecture 14 chapter 6 looping dale/weems/headington

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

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

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

1

Lecture 14

Chapter 6

Looping

Dale/Weems/Headington

Page 2: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

2

Chapter 6 Topics

While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control

Input Data Using a While Statement for Summing and

Counting Nested While Loops Loop Testing and Debugging

Page 3: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

3

Revision of Selection

What construct is used in C++ to narrow down our range of values?

What is the rule for matching else constructs to if constructs?

What is the danger in using an if followed by another if?

For testing selection control structures, should we use white box testing or black box testing?

Page 4: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

4

A loop is a repetition control structure.

it causes a single statement or block to be executed repeatedly

What is a loop?

Page 5: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

5

Two Types of Loops

count controlled loops

repeat a specified number of times

event-controlled loopssome condition within the loop body changes and this causes the repeating to stop

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

6

While Statement

SYNTAX

while ( Expression )

{ .

. // loop body

.

}

NOTE: Loop body can be a single statement, a null statement, or a block.

Page 7: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

bodystatement

Expression

Page 8: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 9: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 10: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

10

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

count

Page 11: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

11

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

count

4

Page 12: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

12

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

count

4

Page 13: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

13

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4

count

4

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

14

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4

count

3

Page 15: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

15

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4

count

3

Page 16: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

16

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3

count

3

Page 17: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

17

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3

count

2

Page 18: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

18

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3

count

2

Page 19: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

19

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3 2

count

2

Page 20: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

20

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3 2

count

1

Page 21: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 22: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 23: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 24: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 25: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 26: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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

Page 27: 1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington

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 ;