do...while Loops

It is possible that the body of a while loop will never execute. The while statement checks its condition before executing any of its statements, and if the condition evaluates false, the entire body of the while loop is skipped. Listing 6.5 illustrates this.

Listing 6.5. Skipping the Body of the while Loop
 0:  // Listing 6.5
 1:   // Demonstrates skipping the body of
 2:   // the while loop when the condition is false.
 3:  #include <iostream>
 4:
 5:  int main()
 6:  {
 7:      int counter;
 8:      std::cout << "How many hellos?: ";
 9:      std::cin >> counter;
10:      while (counter > 0)
11:      {
12:          std::cout << "Hello!
";
13:          counter--;
14:      }
15:      std::cout << "counter is OutPut: " << counter;
16:      return 0;
17:  }


How many hellos?: 2
Hello!
Hello!
counter is OutPut:0

How many hellos?: 0
counter is 0
					

The user is prompted for a starting value on line 8, which is stored in the integer variable counter. The value of counter is tested on line 10 and decremented in the body of the while loop. The first time through counter was set to 2, so the body of the while loop ran twice. The second time through, however, the user typed in 0. The value of counter was tested on line 10 and the condition was false; counter was not greater than 0. The entire body of the while loop was skipped, and hello was never printed.


What if you want to ensure that hello is always printed at least once? The while loop can't accomplish this because the if condition is tested before any printing is done. You can force the issue with an if statement just before entering the while:

if (counter < 1)  // force a minimum value
counter = 1;

but that is what programmers call a kludge, an ugly and inelegant solution.

The do...while loop executes the body of the loop before its condition is tested, and ensures that the body always executes at least one time. Listing 6.6 demonstrates this program rewritten with a do...while loop.

Listing 6.6. Demonstrating a do...while Loop
 0:  // Listing 6.6
 1:  // Demonstrates do while
 2:  #include <iostream>
 3:
 4:  int main()
 5:  {
 6:      int counter;
 7:      std::cout << "How many hellos? ";
 8:      std::cin >> counter;
 9:      do
10:      {
11:          std::cout << "Hello
";
12:          counter--;
13:      } while (counter >0 );
14:      std::cout << "counter is: " << counter << std::endl;
15:      return 0;
16:  }


How many hellos? 2
Hello
Hello
counter is: 0
					

The user is prompted for a starting value on line 7, which is stored in the integer variable counter. In the do...while loop, the body of the loop is entered before the condition is tested, and therefore is guaranteed to be executed at least once. On line 11 the message is printed, on line 12 the counter is decremented, and on line 13 the condition is tested. If the condition evaluates true, execution jumps to the top of the loop on line 11; otherwise it falls through to line 14.


The continue and break statements work in the do...while loop exactly as they do in the while loop. The only difference between a while loop and a do...while loop is when the condition is tested.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.79.121