C++ – Do While Statement

Working sample:


// custom countdown using while
#include <iostream>
using namespace std;

int main ()
{
  int n = 10;

  while (n>0) {
    cout << n << ", ";
    --n;
  }

  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// echo machine
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  do {
    cout << "Enter text: ";
    getline (cin,str);
    cout << "You entered: " << str << '\n';
  } while (str != "goodbye");
}

The result:

Enter text: hello
You entered: hello
Enter text: who’s there?
You entered: who’s there?
Enter text: goodbye
You entered: goodbye
Process returned 0