Skip to main content

//Switch Case (Character)

Switch Case (Character) 


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //Switch Char:- In switch char we use character for ex. + ,-, *, /, %, @

  10. var a = 30;
  11. var b = 20;
  12. var c;
  13. var ch = '*'; //which case you want to execute 'Enter' case value.

  14. switch(ch)
  15. {
  16. //Addition
  17. case "+":  //the case label must be end with colon(:)
  18. {
  19. c = a + b;
  20. document.write("the add of a + b is:"+c+"<br>");//"<br>" use for linebreak.
  21. break; //It's necessary to use break after each block.
  22. }

  23. //Subtract
  24. case "-": //case label must be unique.
  25. {
  26. c = a - b;
  27. document.write("the subtraction a - b is:"+c+"<br>"); // document.write() use for print line.
  28. break; //if you don't use it, then all cases executed.
  29. }

  30. //Division
  31. case "/":
  32. {
  33. c = a / b;
  34. document.write("the division a / b is:"+c+"<br>");
  35. break;
  36. }

  37. //Multiplicaton
  38. case "*":
  39. {
  40. c = a * b;
  41. document.write("the multiplication a * b is:"+c+"<br>");
  42. break;
  43. }

  44. //Modulus
  45. case "%":
  46. {
  47. c = a % b;
  48. document.write("the modulos a % b is:"+c+"<br>");
  49. break;
  50. }


  51. default: //If none of the case label values matches to the value of the expression, then default part statement will be executed.
  52. {
  53. document.write("plz ente valid value");
  54. break;

  55. }


  56. }
  57. </script>
  58. </body>
  59. </html>


//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