Lecture 4a
This presentation is the property of its rightful owner.
Sponsored Links
1 / 112

Lecture 4A PowerPoint PPT Presentation


  • 32 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 4A. Repetition. Richard Gesick. Overview. Iteration 3 kinds of loops for while do while Infinite Loops. Example: Computing Velocity over a Range of Time. Sequential structures are not well suited for computing the same quantities repeatedly.

Download Presentation

Lecture 4A

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


Lecture 4a

Lecture 4A

Repetition

Richard Gesick


Overview

Overview

Iteration

3 kinds of loops

for

while

dowhile

Infinite Loops


Example computing velocity over a range of time

Example: Computing Velocity over a Range of Time.

  • Sequential structures are not well suited for computing the same quantities repeatedly.

  • Flow charts and pseudocode are useful to describe repetition.


Looping

Looping

In computing, we often need to perform the same operations on multiple items.

Typically, these tasks follow this pattern:

initialize values (set total to 0)

process items one at a time (add price to total)

report results (report total)

The flow of control that programmers use to complete jobs with this pattern is called looping, or repetition.


Iteration

Iteration

One thing that computers do well is repeat commands

Programmers use loops to accomplish this

3 kinds of loops in C++

for loop

while loop

do while loop


Criteria for loops

Criteria for loops

Usually have some initial condition

Starting a counter

Beginning in a certain state

Must have a test to continue

Must make progress towards finishing


Loops in everyday life

Loops in Everyday Life

Bad children are told to write sentences on the board

“I will not pour Clorox in the fish tank”

Have to write this sentence either

A certain number of times

Until the teacher is happy

As many as you can during break


The for loop

The for Loop

Ideal when you know the number of iterations to perform before the loop begins

Examples:

Find the sum of 5 numbers

Find the maximum of 20 numbers

Print the odd numbers from 1 to 10


The for loop1

The for loop

Good when you know exactly how many times you need to execute something

Has format:

for (<initialization>; <test to continue>; <increment>) {

// everything in here is what is repeated

// over and over again

}

Initialization is where the counter is given a starting value

The test determines whether or not to continue

The increment can be any amount, including negative, and occurs after the loop statements execute


More for loop syntax

More for Loop Syntax

for ( initialization; loop condition; loop update )

{

// loop body

}

Notes:

semicolons separate terms in the loop header

no semicolon follows the loop header

curly braces are required only if more than one statement is in the loop body


For loop flow of control

for Loop Flow of Control


For loop flow of control1

for Loop Flow of Control

The initialization statement is executed (once only).

The loop condition is evaluated. If the condition is true, the loop body is executed.

The loop update statement is executed, and the loop condition is reevaluated (#2).

And so on, until the condition is false.


Satisfying the teacher

Satisfying the Teacher

Example: 1000 sentences? No problem…

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<“I will not pour Clorox…”<<endl;

}

// Remember, counter++ is the same as

// counter = counter + 1


But i want them numbered

“But I want them numbered!”

No problem…

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}


Why this works

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

1


Why this works1

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

true

1


Why this works2

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

Output: 1 I will not pour Clorox in the Fish Tank

counter

1


Why this works3

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

2


Why this works4

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

true

2


Why this works5

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

Output: 2 I will not pour Clorox in the Fish Tank

counter

2


Why this works6

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

3


Why this works7

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

true

3


Why this works8

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

Output: 3 I will not pour Clorox in the Fish Tank

counter

3


Why this works9

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

4


When will it end

When will it end?

We see that this will go on for a while

It’s a little more interesting later around 1000


Why this works10

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

true

999


Why this works11

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

Output: 999 I will not pour Clorox in the Fish Tank

counter

999


Why this works12

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

1000


Why this works13

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

true for last time

1000


Why this works are we finished

Why this works(are we finished?)

intcounter;

for(counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

Output: 1000 I will not pour Clorox in the Fish Tank

counter

1000


Why this works14

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

}

counter

1001


Why this works15

Why this works

intcounter;

for (counter = 1; counter <= 1000; counter++) {

cout<<counter<<“ I will not pour…”<<endl;

} // Jump down here and continue

counter

false

1001


Final output

Final Output

1 I will not pour Clorox in the fish tank.

2 I will not pour Clorox in the fish tank.

3 I will not pour Clorox in the fish tank.

4 I will not pour Clorox in the fish tank.

.

.

.

999 I will not pour Clorox in the fish tank.

1000 I will not pour Clorox in the fish tank.


Another example of repetition

Another Example of Repetition

int num;

for ( num = 1 ; num <= 3; num++)

{

cout<<num << “ Potato”<<endl;

}


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

?

OUTPUT


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

1

OUTPUT


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

1

num

true

OUTPUT


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

1

OUTPUT

1Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

2

OUTPUT

1Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

2

num

true

OUTPUT

1Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

2

OUTPUT

1Potato

2Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

3

OUTPUT

1Potato

2Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

3

num

true

OUTPUT

1Potato

2Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

3

OUTPUT

1Potato

2Potato

3Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

num

4

OUTPUT

1Potato

2Potato

3Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

4

num

false

OUTPUT

1Potato

2Potato

3Potato


Lecture 4a

int num;

for ( num = 1 ; num <= 3; num++)

cout<<num << “ Potato”<<endl;

4

num

false

When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.


The output was

The output was:

1Potato

2Potato

3Potato


Lecture 4a

int count ;

for ( count = 4 ; count > 0 ; count-- )

{

cout<<count<<endl;

}

cout<<“Done”<<endl;

OUTPUT: 4

3

2

1

Done


The while loop

The while Loop

The while loop is designed for repeating a set of operations on data items when we don't know how many data items there will be.

We will get some signal when we have reached the end of the items to process.

The end of data items could be indicated by a special input value called a sentinel value or by reaching the end of a file

Receiving the signal is an event; we call this event-controlled looping


The while loop1

The while loop

Good for when you don’t know how many times to repeat

Teacher says “Write until I’m happy”

Has format:

while (<boolean value>) {

// stuff to repeat over and over

}


The while statement

The while Statement


Operation of the while loop

Operation of the while Loop

If the condition evaluates to true, the loop body is executed, then the condition is re-evaluated.

As long as the condition evaluates to true, we continue to repeat the loop body.

The loop body must "update the loop condition"; that is, it must perform some operation that eventually will cause the loop condition to evaluate to false

Typically, the loop update will be an attempt to read the next input value, in order to detect the sentinel value or the end of the file.


Loop characteristics

All loops have the following three characteristics:

initialization

test

update

Loop Characteristics


Count controlled loop

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


Count controlled loop1

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

OUTPUT


Count controlled loop2

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

4

OUTPUT


Count controlled loop3

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

4

TRUE

OUTPUT


Count controlled loop4

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

4

OUTPUT

4


Count controlled loop5

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

3

OUTPUT

4


Count controlled loop6

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

3

TRUE

OUTPUT

4


Count controlled loop7

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

3

OUTPUT

4

3


Count controlled loop8

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

2

OUTPUT

4

3


Count controlled loop9

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

2

TRUE

OUTPUT

4

3


Count controlled loop10

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

2

OUTPUT

4

3

2


Count controlled loop11

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

1

OUTPUT

4

3

2


Count controlled loop12

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

1

TRUE

OUTPUT

4

3

2


Count controlled loop13

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

1

OUTPUT

4

3

2

1


Count controlled loop14

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

0

OUTPUT

4

3

2

1


Count controlled loop15

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

0

FALSE

OUTPUT

4

3

2

1


Count controlled loop16

Count-controlled Loop

int count ;

count = 4;

while (count > 0)

{

cout<<count<<endl;

count -- ;

}

cout<<“Done”<<endl;

count

0

OUTPUT

4

3

2

1

Done


Example re writing 1 1000 using a while loop

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

counter

1


Example re writing 1 1000 using a while loop1

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

counter

1


Example re writing 1 1000 using a while loop2

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

Output: 1 I will not pour Clorox in the fish tank

counter

1


Example re writing 1 1000 using a while loop3

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

counter

1


Example re writing 1 1000 using a while loop4

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

counter

1


Example re writing 1 1000 using a while loop5

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

Output: 1 I will not pour Clorox in the fish tank

counter

1


Example re writing 1 1000 using a while loop6

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

counter

1


Example re writing 1 1000 using a while loop7

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

counter

1


Example re writing 1 1000 using a while loop8

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

}

Output: 1 I will not pour Clorox in the fish tank

counter

1


Infinite loops

Infinite Loops

This loop isn’t making a lot of progress!

Loops that repeat forever are called infinite loops

Apparently “lock up”

Output:

1 I will not pour Clorox in the fish tank

1 I will not pour Clorox in the fish tank

1 I will not pour Clorox in the fish tank

1 I will not pour Clorox in the fish tank

.

.

.

}

Continue

forever


Problem solved

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

1


Problem solved1

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

1


Problem solved2

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

Output: 1 I will not pour Clorox in the fish tank

counter

1


Problem solved3

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

2

// Remember, counter++ is the same as// counter = counter + 1


Example re writing 1 1000 using a while loop9

Example: Re-Writing 1-1000(using a while loop)

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

2


Problem solved4

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

2


Problem solved5

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

Output: 2 I will not pour Clorox in the fish tank

counter

2


Problem solved6

Problem Solved…

int counter = 1;

while(counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

3


How does it end

How does it end?

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

999


How does it end1

How does it end?

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

999


How does it end2

How does it end?

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

999


Problem solved7

Problem Solved…

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

Output: 999 I will not pour Clorox in the fish tank

counter

999


How does it end3

How does it end?

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

1000


How does it end4

How does it end?

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

counter

1000


How does it end5

How does it end?

int counter = 1;

while (counter < 1000) {

cout<<counter<< “ I will not…”<<endl;

counter++;

}

// So we never print out

// 1000 I will not pour Clorox in the fishtank

counter

now false

1000


Another problem solved

Another Problem Solved

int counter = 1;

while (counter <= 1000) {

cout<<counter<< “ I will not…”<<endl;counter++;

}

counter

now true

1000


Example

Example

bool teacherHappy = false;

int lineNumber = 1;

while (!teacherHappy) {

cout<<lineNumber << “ I will not…”<<endl;

lineNumber++;

teacherHappy = attitudeFunction ( );

}

// assume attitudeFunction can change

// teacherHappy


The do while loop

The do-while loop

Similar to while loop

Must execute at least one time (test is at bottom)

Has format:

do{

}while (<boolean value>);


Example count from 1 to 3

Example(count from 1 to 3)

intcounter = 0;

do{

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

0


Example count from 1 to 31

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

0


Example count from 1 to 32

Example(count from 1 to 3)

int counter = 0;

do{

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

1


Example count from 1 to 33

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

1

Output: 1


Example count from 1 to 34

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

1


Example count from 1 to 35

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

2


Example count from 1 to 36

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

2

Output: 2


Example count from 1 to 37

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

2


Example count from 1 to 38

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

// Note: counter is now 3, but we still have

// to finish out the loop – it doesn’t skip

counter

3


Example count from 1 to 39

Example(count from 1 to 3)

int counter = 0;

do {

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

3

Output: 3


Example count from 1 to 310

Example(count from 1 to 3)

int counter = 0;

do{

counter++;

cout<<counter<<endl;

} while (counter < 3);

counter

3

now false, so loop is finished


Summary

Summary

for loops good for when you know how many times you want to repeat

while and do-while good for when you don’t

All loops must finish, or they become infinite loops

All loops must have a test to continue, or they become infinite loops


  • Login