A for loop running at the user's request. exit for loop based on user input - C++ Statement

C++ examples for Statement:for

Description

A for loop running at the user's request. exit for loop based on user input

Demo Code

#include <iostream>
using namespace std;
int main()/* w w w  .  j  a v a 2s . c o m*/
{
   int num;   // Loop counter variable
   char ans;
   cout << "Here are the numbers from 1 to 20\n";
   for (num=1; num<=20; num++)
   {
      cout << num << "\n";
      cout << "Do you want to see another (Y/N)? ";
      cin >> ans;
      if ((ans == 'N') || (ans == 'n'))
      {
         break;
      }    // Will exit the for loop if user wants to.
   }
   cout << "\nThat's all, folks!\n";
   return 0;
}

Result


Related Tutorials