Chapter 4 (C) - Repetition - Do While Loop
Chapter 4 (C) - Repetition - Do While Loop
Do while loop
Repetition (do while loop)
Iteration / loop Similar to the while loop.
There are 3 types of repetition The primary difference is that with a do
while loop the condition is tested at the
structure:
bottom of the loop, unlike a while loop
1. for loop where the condition is tested at the top
2. while loop This means that a do while loop will always
3. do while loop
execute at least once, whereas a while loop
may never execute at all if its condition is
false at the outset.
1 2
int num = 1;
The following program is a modification of
while (num <= 10) the one earlier (Example 11, Chapter 4(b) -
{
cout << num << " "; Repetition - while loop) that used a while
num++; loop to continue to prompt the user to enter
}
a positive number until the user either did
Example 2: Using do while loop so or quit, and then either outputted the
int num = 1; positive number or a message that the user
do did not enter a positive number.
{
cout << num << " ";
num++;
} while (num <= 10) ; 5 6
7 8
This modification uses a do while loop instead of a As a general rule, use a do while loop
while loop.
The preceding program, which used the do while over a while loop in those situations in
loop, did not need to prompt the user both (no which the loop must execute at least
repetition of code) before and inside the loop to
enter a number as did the corresponding program once before a condition may be tested,
that used the while loop. because under these circumstances it
However, this program using the do while loop
repeats the num <= 0 condition inside the loop, seems illogical to test the condition
whereas the corresponding program that used the prematurely on the first iteration of
while loop did not need to do that. the loop.
9 10
As you may recall, in the program variation The preceding program, in which the
that used the while loop, the value of quit
could be true in the loop condition under
user had to enter a number, whether
either of two possibilities: that number is positive or not, is an
1. the user’s first attempt to enter data so the example of the situation in which the
user has not yet been asked whether they want loop must execute at least once before
to quit,
2. the user’s second or later attempt to enter data a condition may be tested.
and the user answered that they wanted to quit.
By contrast, using the do while loop
eliminates the first possibility.
11 12
13 14
Scope
15 16
Example 4
In this example, the variables num and quit The while keyword and the parentheses that follow
it are outside the body of the do while loop, or put
are declared after the open curly brace that another way, after the close curly brace that ends
begins the body of the do while loop. the body of the do while loop.
That means their scope is limited to the Since the variables num and quit were declared
within the body of the do while loop, they do not
area between that open curly brace and the have scope outside the body of the loop where the
close curly brace that ends the body of the while parentheses are located.
do while loop. Therefore, these variables are regarded as
This area between an open and close curly undeclared when referred to within those
parentheses.
brace also is referred to as a block.
21 22
Summary
This issue arises far more often with the do while The for loop works well in situations where the loop
loop than with the for or while loops. will iterate a fixed number of times.
With for or while loops, the condition precedes the Often, however, the number of times a loop will
body of the loop, so any variables used in the iterate is unpredictable since the number of
condition necessarily would be declared before the iterations depends on user input during runtime.
loop or, in the case of the for loop, within the A while loop works better than a for loop when the
parentheses following the for keyword. number of times a loop will execute is unpredictable.
By contrast, since the condition of a do while loop There also are situations in which, while the
comes after the body of the loop, it is an easy number of times this loop may execute is
mistake to declare the variables used in the unpredictable, the loop will execute at least once.
condition before it, in the body of the loop.
23 24
An example discussed in this chapter is a
loop that displays a menu with various
choices, including exiting the program.
In this menu example, the menu always
displays at least once; the user cannot
choose to exit before being given that
choice. In such situations, a do while loop is
a better choice than a while loop.
This chapter showed you how to use a do
while loop, and introduced the issue of
variable scope.
25