Skip to main content

//Switch Case (Integer)

 //Switch Integers:- 

                                            In Switch Integer we use Numbers(integers)

CODE

👇

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int _int = '5'; //which case you want to execute 'Enter' case value.
  6. int a = 30; //variable def
  7. int b = 20; 
  8. int c; 
  9. switch(_int) 
  10. {
  11. case '1': //the case label must end with colon(:)
  12. {
  13. //Addition
  14. c = a + b;
  15. cout<<"the value of a+b is:"<<c<<endl; //"cout" use for print line.
  16. break; //It's necessary to use break after each block.
  17. }

  18. case '2': //case label must be unique.
  19.         {
  20.         //Subtract
  21.                 c = a - b;
  22.                 cout<<"the value of a-b is:"<<c<<endl; //"endl" use for linebreak. 
  23.         break; //if you don't use it, then all cases executed.
  24. }

  25. case '3': //integer write in 'single quotes'.
  26.         {
  27.         //Division
  28.                 c = a / b;
  29.                 cout<<"the value of a/b is:"<<c<<endl;
  30.                 break;
  31.         }

  32. case '4':
  33.         {
  34.         //Multiply
  35.                 c = a * b;
  36.                 cout<<"the value of a*b is:"<<c<<endl;
  37.         break;
  38. }

  39. case '5':
  40.         {
  41.         //Modulus
  42.                 c = a % b;
  43.                 cout<<"the value of a%b is:"<<c<<endl;
  44.         break;
  45. }

  46. default: //If none of the case label values matches to the value of the expression, then default part statement will be executed.
  47. {
  48. cout<<"Enter the value valid"<<endl;
  49. }
  50. }
  51. return 0;
  52. }


👉Execute👈


//Output

/*

a = 30; b = 20;


case 1: c = a + b;

the value of a+b is:50


case 2: c = a - b;

the value of a-b is:10


case 3: c = a / b;

the value of a/b is:1


case 4: c = a * b;

the value of a*b is:600


case 5: c = a % b;

the value of a%b is:10

*/






                              //ThE ProFessoR