Sie sind auf Seite 1von 4

Chapter 4(c) –

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

 Syntax  The do keyword starts the loop


 The statement or statements
belonging to the loop are enclosed in
do curly braces.
{  After the close curly brace, the while
statement(s); keyword appears, followed by the
} while (condition); condition in parentheses, terminated
by a semicolon.
3 4

Comparison of the Do While


and While Loop
 Example 1: Using while loop

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

Using while loop EXAMPLE 3: Using do while

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

 Another common example of this situation is  If the user chooses options 1, 2, or 3,


when a menu is displayed
the program performs the indicated
Menu operation (add, edit, or delete) and
==== then again displays the menu for the
1. Add an entry user’s next choice.
2. Edit an entry
 If the user chooses option 4, the
3. Delete an entry
4. Exit
program ends.

13 14

Scope

 In this menu example, the menu  With a do while loop, it is important


always displays at least once; that a variable used in the condition
 The user cannot choose to exit before following the while keyword not be
being given that choice. declared inside the loop.
 Accordingly, a do while loop normally  In the program that demonstrated the
is preferable to a while loop when do while loop, the variables num and
choosing a loop to display a menu. quit were declared before the loop:

15 16
Example 4

int num;  These variables could not be declared inside


the do while loop, as in the following code
char choice; segment, because the code would not
bool quit = false; compile.
 The parentheses following the while
do { keyword is highlighted, and the compiler
// statements error is that num and quit are undeclared
identifiers
} while (num <= 0 && quit == false);
 The reason why this alternative will not
compile concerns variable scope.
17 18

 A variable must be declared before it can be


char choice; referred to in code.
do {  Once a variable is declared, it may be referred to
wherever in the code it has scope.
int num;  Thus far, variables have been declared in main, just
after the open curly brace which begins the body of
bool quit = false; the main function.
 This gives these variables scope until the close
// more statements curly brace, which ends the body of the main
function.
} while (num <= 0 && quit == false);  Since thus far our programs have had only one
function, main, as a practical matter, the variables,
once declared, could be referred to throughout the
19
entire program. 20

 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

Das könnte Ihnen auch gefallen