CPP while loop

Using while loop statement

A while loop causes your program to repeat a sequence of statements as long as the starting condition remains true:

Source code Result
// Looping with while
#include <iostream>

int main() {
  using namespace  std;
  int count = 0;           // initialize the condition
  while(count < 5) {       // test condition still true
    count++;               // body of the loop starts
    cout << "count: " << count << endl;
  }
  cout << "At end Count: " << count << endl;
  return 0;
}
count: 1
count: 2
count: 3
count: 4
count: 5
At end Count: 5

You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.