Switch Case (Character)
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- </head>
- <body>
- <script type="text/javascript">
- //Switch Char:- In switch char we use character for ex. + ,-, *, /, %, @
- var a = 30;
- var b = 20;
- var c;
- var ch = '*'; //which case you want to execute 'Enter' case value.
- switch(ch)
- {
- //Addition
- case "+": //the case label must be end with colon(:)
- {
- c = a + b;
- document.write("the add of a + b is:"+c+"<br>");//"<br>" use for linebreak.
- break; //It's necessary to use break after each block.
- }
- //Subtract
- case "-": //case label must be unique.
- {
- c = a - b;
- document.write("the subtraction a - b is:"+c+"<br>"); // document.write() use for print line.
- break; //if you don't use it, then all cases executed.
- }
- //Division
- case "/":
- {
- c = a / b;
- document.write("the division a / b is:"+c+"<br>");
- break;
- }
- //Multiplicaton
- case "*":
- {
- c = a * b;
- document.write("the multiplication a * b is:"+c+"<br>");
- break;
- }
- //Modulus
- case "%":
- {
- c = a % b;
- document.write("the modulos a % b is:"+c+"<br>");
- break;
- }
- default: //If none of the case label values matches to the value of the expression, then default part statement will be executed.
- {
- document.write("plz ente valid value");
- break;
- }
- }
- </script>
- </body>
- </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