21
PIC 10A Lecture 11 The do-while loop

4.2 the Do-While Loop

  • Upload
    juan

  • View
    228

  • Download
    1

Embed Size (px)

DESCRIPTION

Lecture UCLA 2015

Citation preview

Page 1: 4.2 the Do-While Loop

PIC 10ALecture 11 The do-while loop

Page 2: 4.2 the Do-While Loop

The while loop• Sometimes it’s useful to repeat a block of code.• We call a block of code that is repeated a loop.• The while loop acts like a repeating if statement, repeating the code

below it as long as the boolean statement is true. Also called a stopping condition.

• The braces { } enclose the code to repeat.• Like with if, indenting makes it easier to read.

while ( statement is true ) {// Code to be executed

} Can put anything in here, as long as it evaluates to a Boolean (true or false)

Page 3: 4.2 the Do-While Loop

Example of a while loop

• What does this code do?• How many times will it repeat?• Why was it necessary to initialize x=-1?

Page 4: 4.2 the Do-While Loop

Looping Until the User Says Stop• Multiply two numbers entered by the user.• Ask the user if he/she wants to continue.• Stop when the user says so.

Page 5: 4.2 the Do-While Loop

Looping a Fixed # of Times•Suppose we want to say hello 10 times.

•What would happen if we deleted the counter++ line?

Page 6: 4.2 the Do-While Loop

Looping While Input is Valid• Add up a list of numbers typed by the user. Stop when they enter

something that is not an int.

Page 7: 4.2 the Do-While Loop

Infinite Loops• An infinite loop is a loop that never stops.• An infinite loop is very bad programming style.• Make sure your while loop terminates.

Page 8: 4.2 the Do-While Loop

Boolean Variables• Recall that a boolean (bool) takes only true/false values. (Could

also give it 1/0, or any positive # / zero.)• We can use a boolean as a loop condition.

Page 9: 4.2 the Do-While Loop

Ex: Vegas• The roulette wheel at Caesar’s is broken and always lands on red.

Winning a bet on red doubles your money. Starting with $1, how many bets will you have to make to earn $1,000,000?

Page 10: 4.2 the Do-While Loop

Ex: Factorial• The factorial N! of a number N is defined as

N! = N(N-1)(N-2)...(3)(2)(1)• For example: 4! = (4)(3)(2)(1) = 24• Not to be confused with the C++ negation !• For our purposes, the factorial operator ! is only defined for

positive integers.• Write a program that gets a number N from the user and

computes N!

Page 11: 4.2 the Do-While Loop

Ex: Factorial

•There’s a very serious error in the code above. What is it?• It would output factorial = 0 every time.• It iterates one too many times, so it multiplied by 0 in last step.

Page 12: 4.2 the Do-While Loop

Ex: Factorial•Small change in the stopping condition from (M>=1) to (M>1) makes a huge difference.

Page 13: 4.2 the Do-While Loop

do while•Sometimes you want to run the contents of a loop first, and then test for a condition at the end.

•For example, int x;cout<<“Please input a positive number\n”;cin>>x;// Get numbers from console and square themwhile(x > 0) {

cout<<“Your number squared is “<<x*x<<endl;

cout<<“Please input a positive number\n”;

cin>>x;}

Page 14: 4.2 the Do-While Loop

do while• In general we wish to avoid repetitive code. In this case, because we must test a condition first, we must have some valid input to start.int x;

cout<<“Please input a positive number\n”;cin>>x;// Get numbers from console and square themwhile(x > 0) {

cout<<“Your number squared is “<<x*x<<endl;

cout<<“Please input a positive number\n”;

cin>>x;}

Page 15: 4.2 the Do-While Loop

Just do it•The simple solution is the do-while loop, as follows

•Quite simply, this loop executes the code inside the brackets first, unconditionally, and then reruns as long as the condition returns true.

// Description of do-while loopdo {

// Code to be executed here} while(condition);

Page 16: 4.2 the Do-While Loop

do while•Now we can replace our example using while loops…

int x;cout<<“Please input a positive number\n”;cin>>x;// Get numbers from console and square themwhile(x > 0) {

cout<<“Your number squared is “<<x*x<<endl;

cout<<“Please input a positive number\n”;

cin>>x;}

Page 17: 4.2 the Do-While Loop

do while•…with this example that uses do-while loops

int x;// Get numbers from console and square themdo {

cout<<“Please input a positive number\n”;

cin>>x;cout<<“Your number squared is

“<<x*x<<endl;} while(x > 0);

Page 18: 4.2 the Do-While Loop

do while•With this example that uses do-while loops

•The only problem with this is that we will always take the square root of the last value. This will print a weird number if we input a negative value for x at the end.

int x;// Get numbers from console and take square rootdo {

cout<<“Please input a positive number\n”;cin>>x;cout<<“The square root is

“<<sqrt( static_cast<double>(x) )<<endl;} while(x > 0);

Page 19: 4.2 the Do-While Loop

while vs do while// Description of do-while loopdo {

// Code to be executed here} while(condition);

// Description of do-while loopwhile ( condition) {

// Code to be executed}•do while loops are not as common and can be more difficult to read because the condition is at the bottom

•Therefore, while loops are often considered preferable•How can a do while loop be converted to a while loop?

Page 20: 4.2 the Do-While Loop

while vs do while

bool flag = true;// Description of do-while loopwhile (flag) {

// Code to be executed

flag = condition;}

// Code to be executed// Description of do-while loopwhile (condition) {

// Code to be executed}

The Obvious Way The Preferred Way

This could be very long. With the second way, you don’t have to repeat all that code!

Page 21: 4.2 the Do-While Loop

while vs do while

int x;bool flag = true;// Get numbers from console and square themwhile(flag) {

cout<<“Please input a positive number\n”;

cin>>x;cout<<“Your number squared is

“<<x*x<<endl;flag = (x > 0);

}