what is a loop?

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

Upload: molimo

Post on 19-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

What is a loop?. A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly. Two Types of Loops. count controlled loops repeat a specified number of times event-controlled loops - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: What is a loop?

A loop is a repetition control structure.

it causes a single statement or block to be executed repeatedly

What is a loop?

Page 2: What is a loop?

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 3: What is a loop?

While (pretest loop)

SYNTAX

while ( Expression )

{ .

. // loop body

.

}

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

Page 4: What is a loop?

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 5: What is a loop?

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 6: What is a loop?

int count ;

count = 4; // initialize loop variable

while (count > 0) // test expression

{

cout << count << endl ; // repeated action

count = count – 1; // update loop variable

}

cout << “Done” << endl ;

Count-controlled Loop

Page 7: What is a loop?

7

Count-controlled Loop

int count ;

count = 4;

while (count > 0){

cout << count << endl ;

count = count - 1;

}cout << “Done” << endl ;

OUTPUT

count

Page 8: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

count

4

Page 9: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

count

4

Page 10: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4

count

4

Page 11: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4

count

3

Page 12: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4

count

3

Page 13: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3

count

3

Page 14: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3

count

2

Page 15: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3

count

2

Page 16: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3 2

count

2

Page 17: What is a loop?

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count -- ;}

cout << “Done” << endl ;

OUTPUT

4 3 2

count

1

Page 18: What is a loop?

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 19: What is a loop?

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 20: What is a loop?

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 21: What is a loop?

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 22: What is a loop?

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 23: What is a loop?

Example

Use a while loop to read the 100 blood pressures and find their total int thisBP ; int total ; int count ;

count = 0 ; // initialize

while ( count < 100 ) // test expression { cin >> thisBP ; total = total + thisBP ; count++ ; // update

}

cout << “The total = “ << total<<endl;