Using the switch statement - C++ Statement

C++ examples for Statement:switch

Description

Using the switch statement

Demo Code

#include <iostream>

int main()/*w ww .  j  av  a 2s  .  c  o  m*/
{
  int choice {};             // Stores selection value

  std::cout << "You can choose from the following:\n"
            << "1 \n"
            << "2 \n"
            << "3 \n"
            << "4 \n\n"
            << "Enter your selection number: ";
  std::cin >> choice;

  switch(choice)
  {
  case 1:
    std::cout << "\n one." << std::endl;
    break;
  case 2:
    std::cout << "two." << std::endl;
    break;
  case 3:
    std::cout << "three." << std::endl;
    break;
  case 4:
    std::cout << "four." << std::endl;
      break;
  default:
    std::cout << "default." << std::endl;
  }
}

Result


Related Tutorials