What is a loop
Download
1 / 23

What is a loop? - PowerPoint PPT Presentation


  • 114 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' What is a loop?' - molimo


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
What is a loop
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
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


While pretest loop
While (pretest loop)

SYNTAX

while ( Expression)

{ .

. // loop body

.

}

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


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


Count-controlled loop contains loop is exited and control passes to the statement which follows the loop body.

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


Count controlled loop
Count-controlled Loop loop is exited and control passes to the statement which follows the loop body.

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 loop1

count loop is exited and control passes to the statement which follows the loop body.

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count = count - 1;

}

cout << “Done” << endl ;

OUTPUT


Count controlled loop2

count loop is exited and control passes to the statement which follows the loop body.

4

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT


Count controlled loop3

count loop is exited and control passes to the statement which follows the loop body.

4

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT


Count controlled loop4

count loop is exited and control passes to the statement which follows the loop body.

4

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4


Count controlled loop5

count loop is exited and control passes to the statement which follows the loop body.

3

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4


Count controlled loop6

count loop is exited and control passes to the statement which follows the loop body.

3

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4


Count controlled loop7

count loop is exited and control passes to the statement which follows the loop body.

3

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3


Count controlled loop8

count loop is exited and control passes to the statement which follows the loop body.

2

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3


Count controlled loop9

count loop is exited and control passes to the statement which follows the loop body.

2

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3


Count controlled loop10

count loop is exited and control passes to the statement which follows the loop body.

2

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2


Count controlled loop11

count loop is exited and control passes to the statement which follows the loop body.

1

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2


Count controlled loop12

count loop is exited and control passes to the statement which follows the loop body.

1

Count-controlled Loop

int count ;

count = 4;

while (count > 0) TRUE

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2


Count controlled loop13

count loop is exited and control passes to the statement which follows the loop body.

1

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2

1


Count controlled loop14

count loop is exited and control passes to the statement which follows the loop body.

0

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2

1


Count controlled loop15

count loop is exited and control passes to the statement which follows the loop body.

0

Count-controlled Loop

int count ;

count = 4;

while (count > 0) FALSE

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2

1


Count controlled loop16

count loop is exited and control passes to the statement which follows the loop body.

0

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout << count << endl ;

count --;

}

cout << “Done” << endl ;

OUTPUT

4

3

2

1

Done


Example
Example loop is exited and control passes to the statement which follows the loop body.

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


ad