Skip to main content

//Switch Case (Character)

 //Switch Case(Character):-

                                             In switch char we use character for ex. + ,-, *, /, %, @


CODE

👇

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char ch = '+'; //which case you want to execute 'Enter' case value.
  6. int a = 30; //variable def
  7. int b = 20; 
  8. int c;
  9. switch(ch)
  10. {
  11. case '+': //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 '-': //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 '/':
  26.         {
  27.         //Divition
  28.                 c = a / b;
  29.                 cout<<"the value of a/b is"<<c<<endl;
  30.         }

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

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

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


👉Execute👈


//Output

/*

a = 30; b = 20;


case +: c = a + b;

the value of a+b is:50


case -: c = a - b;

the value of a-b is:10


case /: c = a / b;

the value of a/b is:1


case *: c = a * b;

the value of a*b is:600


case %: c = a % b;

the value of a%b is:10

*/






                              //ThE ProFessoR