65
Lecture 4 Looping

Lecture 4 Looping. Building on the foundation Now that we know a little about cout cin math operators boolean operators making decisions using

Embed Size (px)

Citation preview

Page 1: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Lecture 4

Looping

Page 2: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Building on the foundation

Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Page 3: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Advantages of Computers

Computers are really quick

Computers don't get bored

They can do the same thing over and over and be “happy”

Page 4: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Loops

What is the effect of a loop?

{ some C++ code; some C++ code; some C++ code; some C++ code; some C++ code; some C++ code; some C++ code;}

as long as some condition is true

Page 5: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

While loopswhile ( num < 10 ) { cout << “num = ” << num << endl; num = num + 1;}

False

BoolExp

True

Code Block

Page 6: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

While loopswhile ( num < 10 ) cout << “Hello there \n” ;

As before if there is only one line in the body of the loop, the { } are not needed.

But....what is the problem?

False

BoolExp

Code Block

True

Page 7: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

While loopswhile ( num < 10 ) cout << “Hello there \n” ;

As before if there is only one line in the body of the loop, the { } are not needed.

But....what is the problem?

Infinite Loop:a loop that doesn't end

False

BoolExp

Code Block

True

Page 8: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

What is a difference between if and while

Which of the following is the if (left or right)?

False

BoolExp

Code Block

True False

BoolExp

Code Block

True

Page 9: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

What is a difference between if and while

Which of the following is the if (left or right)?

False

BoolExp

Code Block

True

FalseBoolExp

Code Block

True

if while

Page 10: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Class Exercise

ask the user for 10 integers

return to the user the total of the 10 integers and their average

Please enter 10 numbersnum 1: 23num 2: 56.....num 10: -34

The total of your 10 numbers is: 345The average for the 10 numbers is: 34

Things to think about:

What part needs to be repeated?

This will go in the loop body

How many variables do you need?

Page 11: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Answer

int num = 1, total = 0, temp;

cout << “Please enter 10 numbers \n”

while ( num <= 10 ) {cout << “num “ << num << “:” ;cin >> temp ;total = total + temp;num = num + 1;

}

cout << “The total of your 10 numbers is: “ << total << endl;cout << “The average of your 10 number is: << ( total / 10 ) << endl;

Page 12: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Counter Controled Loop Counter Controled Loop uses a variable (num) to count

and control when the loop stops

int num = 1, total = 0, temp;

cout << “Please enter 10 numbers \n”

while ( num <=10 ) {cout << “num “ << num << “:” ;cin >> temp ;total = total + temp;num = num + 1;

}

cout << “The total of your 10 numbers is: “ << total << endl;

Page 13: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Another Class Exercise

Most of the time our DOS screen is 80 characters wide

To help with making a “pretty” display, make a loop that will print 80 numbers across the screen.....but always print from 1 to 10 ( 0 will represent a 10)

123456789012345...................................67890

Page 14: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Answer

int count = 1, output = 1;

while ( count <= 80 ) {cout << output ;count = count + 1;output = output + 1;

if (output > 9 ) // reset output to 0 if over 9output = 0;

}

Page 15: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Another Answer

int count = 1, output = 1;

while ( count <= 80 ) {cout << output ;count = count + 1;

// reset output to 0 if over 9output = ( output > 9 ? 0 : output + 1 );

}

Page 16: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Another Answer

int num = 1 ;

while ( num <= 80 ) {cout << ( num % 10 ) ;num = num + 1;

}

Page 17: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

While loops You can put any kind

of code in the code block.....

False

BoolExp

Code Block

True

Page 18: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

While loops You can put any kind

of code in the code block.....

...even other loops

False

BoolExp

Code Block

True

False

BoolExp

Code Block

True

Page 19: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

False

BoolExp

Code Block

True

False

BoolExp

Code Block

True

In everyday life we have an embedded loop: ????

Embedded loops(a loop inside of another loop)

Page 20: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

False

BoolExp

Code Block

True

False

BoolExp

Code Block

True

hours

minutes

In everyday life we have an embedded loop: Time

Embedded loops(a loop inside of another loop)

Page 21: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Creating Some Time Output

int hour = 0, min;

while ( hour < 24 ) { min = 0; while ( min < 60 ) { cout << hour << ':' << min << endl;

min = min + 1; } // end of minute loop hour = hour + 1; } // end of hour loop

Page 22: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Now What is the Output?int hour = 0, min;

while ( hour < 24 ) { min = 0; while ( min < 60 ) { cout << (hour < 10 ? '0' : “” ) << hour; cout << ':' ; cout << (min < 10 ? '0' : “” ) << min << endl; min = min + 1; } // end of minute loop hour = hour + 1; } // end of hour loop

Page 23: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Infinite loops

Common Infinite Loops

while ( ch = ‘y’ ) {........

}

Remember: non-zero numbers true zero false

Page 24: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Infinite loops )

Common Infinite Loops

while ( ch = ‘y’ ) {........

}

Remember: non-zero numbers true zero false

or

int i = 0while ( i < 10 ) {

....

.... // forgot i++;}

Page 25: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Looping Control Structures

Three different commands

while statement

do-while statement

......

Page 26: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

The Do-While Statement

Syntax

do code block

while (bool expr);

BoolExp

Code Block

True

False

Page 27: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Using a do while loop

char reply;

do { ......... // some code cout << "Do you want to continue?(y):";

cin >> reply;

} while(reply == 'y');

Page 28: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Why use a .......?

What is the advantage of a

while:

do...while:

Page 29: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Why use a .......?

What is the advantage of a

while: body will execute 0 – N times

do...while: body will execute 1 – N times

Page 30: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Looping Control Structures

Three different commands

while statement

do-while statement

for statement

Page 31: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Common use of loop

int cnt = 0; // initialize

while ( cnt < 10 ) { // check ......;// do something.......body of loop......;cnt++; // update

}

Page 32: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

The for loop

Syntaxfor (initialization ; bool expression ; update

action)

body of the loop

Examplefor (int i = 0; i < 3; i++) { cout << "i is " << i << endl;}

Page 33: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Bool Exp

Body

truefalse

Initialization

Update Action

Executed onceat the beginning

of the forloop'sexecution The Bool Exp is

evaluated at thestart of each

iteration of theloop

Page 34: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 0

Page 35: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 0

Page 36: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 0

i is 0

Page 37: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 0

i is 0

Page 38: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 1

i is 0

Page 39: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 1

i is 0

Page 40: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 1

i is 0i is 1

Page 41: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 1

i is 0i is 1

Page 42: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 2

i is 0i is 1

Page 43: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 2

i is 0i is 1

Page 44: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 2

i is 0i is 1i is 2

Page 45: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 2

i is 0i is 1i is 2

Page 46: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 3

i is 0i is 1i is 2

Page 47: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 3

i is 0i is 1i is 2

Page 48: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Execution Example

for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl;

}cout << "all done" << endl;

i 3

i is 0i is 1i is 2all done

Page 49: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Embedding for loops

Just like while loops, for loops can also be embedded inside other loops

in fact, due to there compactness (information being easier to read) many embedded loops will be for loops

Page 50: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

int cntr1 =0, cntr2 =0, cntr3 =0,cntr4 =0, cntr5 =0;cntr1++;

for (int i = 1; i <= 10; ++i) {cntr2++;

for (int j = 1; j <= 20; ++j) { cntr3++;

}

cntr4++;}

cntr5++;

cout << “cntr1: “ << cntr1 << endl << “cntr2: “ << cntr2 << endl << “cntr3: “ << cntr3 << endl << “cntr4: “ << cntr4 << endl

<< “cntr5: “ << cntr5 << endl;

What is the output?

Page 51: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Answer:

cntr1: 1cntr2: 10cntr3: 200cntr4: 10cntr5: 1Press any key to continue

Page 52: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Class Exercise Write code to display a grid.

For each position, display the location’s row and column number. Put two blanks between each location.

Ex:1,1 1,2 1,3 .... 1,92,1 2,2 .... 2,9 ....8,1 8,2 .... 8,99,1 9,2 9,3 .....9,9

Page 53: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

One solution to grid exercise

for(int row = 1; row < 10; row++ ){

for(int col = 1; col < 10; col++) {cout << row << ‘,’ << col << “ “;

} // end of col loop

cout << endl;

} // end of row loop

Page 54: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Another use of break break can be used to exit a loop

// in some game

while ( life > 0 ) {........ // play game

cout << “Do you want to quit(q):”;cin >> userResponse;if (userResponse == ‘q’ )

break;

} // end while loop not the best example

Page 55: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

A better solution

// in some game

userResponse = ‘c’;

while ( life > 0 && userResponse != ‘q’ ) {// play game

....cin >> userResponse;

} // end while loop

Page 56: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

The best solution

// in some game

do {// play game

....cin >> userResponse;

} while ( life > 0 && userResponse != ‘q’ );

Page 57: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Another use of break break will only exit out of one loop (if embedded)

for ( int row = 1; row < 10; row++ ) {

for ( int col = 1; col < 10; col++) {// do something

if ( ???? ) break; // this will stop the col loop} // col loop

} // row loop

Page 58: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Continue, the brother of break

the key word, continue, will cause execution to skip to the boolean expression

while( something == true ) {// do somethingif ( x > 0 )

continue; // skip to end of block// do some more....

} // end of while

Page 59: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Continue, the brother of break

the key word, continue, will cause execution to skip to the boolean expression

while( something == true ) {// do somethingif ( x > 0 )

continue; // skip to end of block// do some more....

} // end of while

Page 60: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Using break and continue in loops

First look for a solution that does not use break or continue

Only use break and continue

if it is the only solution or

it makes the code easier to understand

Note: always avoid using goto.... there is almost always a better way

Page 61: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Sentinel Controled Loops Continue doing the loop until a special value (the sentinel) is encounter

Page 62: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Sentinel Controled Loops Continue doing the loop until a special value (the sentinel) is encounter

Example: Before we asked the user for 10 numbers

and then we told the user total of the 10 numbers average of the 10 numbers

Page 63: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

Sentinel Controled Loops Continue doing the loop until a special value (the sentinel) is encounter

Example: Before we asked the user for 10 numbers

and then we told the user total of the 10 numbers average of the 10 numbers

Now we want the user to enter as many numbers as they want a negative number is the signal that they are

done ( the sentinel )

Page 64: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using

quiz

int count = 1, total = 0, input = 0;

cout << “Please enter some positive numbers that I will average \n”;cout << “Enter a negative number to signal that you are done \n”;

while ( input >= 0 ) {cout << “# “ << count << “:” ;cin >> input;total = total + input;count = count + 1;

}

count = count – 1;

if ( count > 0 ) { cout << “The total of your “ << count ; cout << “ numbers is: “ << total << endl; cout << “The average of your “ << count cout << “ number is: << ( total / count ) << endl;}

try it with:57 -1

try it with:57 -1

Will this work?Will this work?

Page 65: Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using