O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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!\n";
13:          counter--;
14:      }
15:      std::cout << "counter is OutPut: " << counter;
16:      return 0;
17:  }
 How many hellos?: 2 Hello! Hello! counter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required