CPP do .. while loop

Using do ... while loop statements

The do...while loop executes the body of the loop before its condition is tested, thus ensuring that the body always executes at least one time:

Source code Result
// Demonstrates do while
#include <iostream>

int main(){
  using namespace  std;
  int count;
  cout << "How many hotdogs do you want? ";
  cin >> count;
  int i=0;
  do  {
    cout << "This is hotdog " << ++i << "\n";
    count--;
  }  while (count >0 );
  cout << "count is: " << count << endl;
  return 0;
}
How many hotdogs do you want? 5
This is hotdog 1
This is hotdog 2
This is hotdog 3
This is hotdog 4
This is hotdog 5
count is: 0

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.