Skip to main content

//Switch Case (Integer)

Switch Case (Integer)


  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 Integer:- In Switch Integer we use Numbers(integers)

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

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

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

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

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

  44. case 5:
  45. {
  46. //Modulus
  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 enter 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