Skip to main content

//Switch Case (String)

Switch Case (String)


  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 String:- In Switch String we use words and numbers in "duble quotes".
  10. var a = 30;
  11. var b = 20;
  12. var c;
  13. var string = "six"; //which case you want to execute 'Enter' case.

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

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

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

  34. case "four":
  35. {
  36. c = a * b;
  37. document.write("the multiplication a * b is:"+c+"<br>");
  38. break;
  39. }

  40. case "five":
  41. {
  42. c = a % b;
  43. document.write("the modulos a % b is:"+c+"<br>");
  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. document.write("plz enter valid value");
  49. break;
  50. }
  51. }
  52. </script>
  53. </body>
  54. </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