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

while Loops

Demonstrated in Listing 6.1, a while loop causes your program to repeat a sequence of statements as long as the starting condition remains true.

Listing 6.1. while Loops
 0:  // Listing 6.1
 1:  // Looping with while
 2:  #include <iostream>
 3:
 4:  int main()
 5:  {
 6:      int counter = 0;            // initialize the condition
 7:
 8:      while(counter < 5)          // test condition still true
 9:      {
10:          counter++;              // body of the loop
11:          std::cout << "counter: " << counter << "\n";
12:      }
13:
14:      std::cout << "Complete. counter: " << counter << ".\n";
15:      return 0;
16:  }
counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
Complete. counter: 5

This simple program ...

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