An Exit-Condition Loop: do while

I l @ ve RuBoard

An Exit-Condition Loop: do while

The while loop and the for loop are both entry-condition loops . The test condition is checked before each iteration of the loop, so it is possible for the statements in the loop to never execute. C also has an exit-condition loop in which the condition is checked after each iteration of the loop, guaranteeing that statements are executed at least once. This variety is called a do while loop. Listing 6.13 shows an example.

Listing 6.13 The dowhile.c program.
 /* dowhile.c -- exit condition loop */ #include <stdio.h> int main(void) {    char ch;   do   {        scanf("%c", &ch);        printf("%c", ch);   } while (ch != '#');   return 0; } 

The program in Listing 6.13 reads input characters and reprints them until the # character shows up. Here is a sample run:

 I choose door #3! I choose door # 

How is this program different from a while loop version such as that in Listing 6.14?

Listing 6.14 The entry.c program.
 /* entry.c -- entry condition loop */ #include <stdio.h> int main(void) {    char ch;   scanf("%c", &ch);   while (ch != '#')   {        printf("%c", ch);        scanf("%c", &ch);   }   return 0; } 

The behavioral difference appears when the # character is read. The while loop prints all the characters up to the first # character:

 I choose door #3! I choose door 

The do while loop, however, printed all the characters up to and including the # character. Only after it has printed the # character does the do while loop check to see if a # character has shown up. In a do while loop, that action comes before the test condition.

This is the general form of the do while loop:

 do     statement while ( expression ); 

The statement can be simple or compound. Note that the do while loop itself counts as a statement and therefore requires a terminating semicolon. Also see Figure 6.5.

Figure 6.5. Structure of a do while loop.
graphics/06fig05.jpg

A do while loop is always executed at least once because the test is made after the body of the loop has been executed. A for loop or a while loop, on the other hand, may be executed zero times because the test is made before execution. You should restrict the use of do while loops to cases that require at least one iteration. For example, a password program could include a loop along these pseudocode lines:

 do {    prompt for password    read user input } while (input not equal to password) ; 

Avoid a do while structure of the type shown in the following pseudocode:

 ask user if he or she wants to continue do some clever stuff while (answer is yes) 

Here, after the user answers "no," some clever stuff gets done anyway because the test comes too late.

Summary: The do while Statement

Keywords:

 do, while 

General Comments:

The do while statement creates a loop that repeats until the test expression becomes false or zero. The do while statement is an exit-condition loop ”the decision to go through one more pass of the loop is made after the loop has been traversed. Therefore, the loop must be executed at least once. The statement part of the form can be a simple statement or a compound statement.

Form:

 do     statement while (expression); 

The statement portion is repeated until the expression becomes false or zero.

Example:

 do     scanf("%d", &number); while (number != 20); 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net