C++ switch Statements :

The switch Statement :

C++ provides a multiple branch selection statement know as switch. This selection statement successively tested the value of an expression against a list of integer or character constants. When match is found, the statements associated with that constant are executed.

Syntax :

  1. switch(variable)
  2. {
  3. case 1:
  4. //execute your code
  5. break;
  6. case n:
  7. //execute your code
  8. break;
  9. default:
  10. {
  11. //execute your code
  12. }

Example :

  1. /* Description: Simple switch statement program in C++ language */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int a;
  7. cout << " Please enter a no between 1 and 5: " << endl;
  8. cin >> a;
  9. switch(a)
  10. {
  11. case 1:
  12. cout << " You chose One" << endl;
  13. break;
  14. case 2:
  15. cout << " You chose Two" << endl;
  16. break;
  17. case 3:
  18. cout << " You chose Three" << endl;
  19. break;
  20. case 4:
  21. cout << " You chose Four" << endl;
  22. break;
  23. case 5:
  24. cout << " You chose Five" << endl;
  25. break;
  26. default :
  27. cout << "Invalid Choice. Enter a no between 1 and 5" << endl;
  28. break;
  29. }
  30. }

Output :

C++ language Simple switch statement program in C++ language

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels