0% found this document useful (0 votes)
80 views21 pages

4.2 The Do-While Loop

This document discusses do-while loops in C++. It begins by reviewing while loops and their syntax. It then provides examples of using while loops, including looping a fixed number of times, looping until user input is invalid, and infinite loops. It introduces do-while loops, which execute the code block first before checking the condition, unlike regular while loops which check the condition first. The key difference between while and do-while loops is discussed. The document also provides ways to convert a do-while loop into a regular while loop.

Uploaded by

Juan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views21 pages

4.2 The Do-While Loop

This document discusses do-while loops in C++. It begins by reviewing while loops and their syntax. It then provides examples of using while loops, including looping a fixed number of times, looping until user input is invalid, and infinite loops. It introduces do-while loops, which execute the code block first before checking the condition, unlike regular while loops which check the condition first. The key difference between while and do-while loops is discussed. The document also provides ways to convert a do-while loop into a regular while loop.

Uploaded by

Juan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PIC 10A

Lecture 11 The do-while loop

The while loop


Sometimes

its 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.

while ( statement is true ) {


// Code to be executed
}

The

braces { } enclose the code to repeat.

Like with if, indenting makes it easier to read.

Can put anything in


here, as long as it
evaluates to a
Boolean (true or
false)

Example of a while loop

What

does this code do?

How many times will it repeat?

Why was it necessary to initialize x=-1?

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.

Looping a Fixed # of Times


Suppose

What

we want to say hello 10 times.

would happen if we deleted the counter++ line?

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.

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.

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.

Ex: Vegas
The

roulette wheel at Caesars 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?

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!

Ex: Factorial

Theres

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.

Ex: Factorial
Small

change in the stopping condition from (M>=1)


to (M>1) makes a huge difference.

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
them
while(x > 0) {
cout<<Your number squared is
<<x*x<<endl;
cout<<Please input a positive
number\n;
cin>>x;

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
them
while(x > 0) {
cout<<Your number squared is
<<x*x<<endl;
cout<<Please input a positive
number\n;
cin>>x;
}

Just do it
The

simple solution is the do-while loop, as follows


// Description of do-while loop
do {
// Code to be executed here
} while(condition);

Quite

simply, this loop executes the code inside the brackets


first, unconditionally, and then reruns as long as the
condition returns true.

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
them
while(x > 0) {
cout<<Your number squared is
<<x*x<<endl;
cout<<Please input a positive
number\n;
cin>>x;
}

do while
with

this example that uses do-while loops

int x;
// Get numbers from console and square
them
do {
cout<<Please input a positive
number\n;
cin>>x;
cout<<Your number squared is
<<x*x<<endl;
} while(x > 0);

do while
With

this example that uses do-while loops

int x;
// Get numbers from console and take square root
do {
cout<<Please input a positive number\n;
cin>>x;
cout<<The square root is
<<sqrt( static_cast<double>(x) )<<endl;
} while(x > 0);

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.

while vs do while
// Description of do-while
// Description of do-while
loop
loop
do {
while ( condition) {
// Code to be executed
// Code to be executed
here
}
} while(condition);
do while loops are not as common
and can be more difficult
to read because the condition is at the bottom
Therefore,
How

while loops are often considered preferable

can a do while loop be converted to a while loop?

while vs do while
The Obvious Way

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

The Preferred Way

bool flag = true;


// Description of do-while
loop
while (flag) {
// Code to be
executed
flag = condition;
}

This could be very long.


With the second way, you dont have to repeat all that code!

while vs do while

int x;
bool flag = true;
// Get numbers from console and square
them
while(flag) {
cout<<Please input a positive
number\n;
cin>>x;
cout<<Your number squared is
<<x*x<<endl;
flag = (x > 0);
}

You might also like