Examples of the do...while Loop

An example of data validation:

int number;
do
{
  cout << "Enter a positive number" << endl;
  cin >> number;
}while (number <= 0);

Another example (assume our definitions for Boolean type):

Boolean done = FALSE;
int opt;
do
{
  cout << "Choose option 1, 2, or 3" << endl;
  cin >> opt;
  switch(opt)
  {
    case 1 :
    case 2 :
    case 3 :
      doOption(opt);
      done = TRUE;
      break;
    
    default:
       cout << "Invalid option" << endl;
      break;
  }
}while (!done); 
Previous slide

Back to Lesson 9 Index
Back to Outline