Case Option 1:
//Execute these statements when the expression result match Option 1
Case Option 2:
//Execute these statements when the expression result match Option 2
Case Option 3:
//Execute these statements when the expression result match Option 3
Case Option N:
//Execute these statements when the result of expression match Option N
//Execute these statements when the result of expression Not matching with any Option
The expression value should be either integer or characters (We can write the expression as n/2…. also but the result should be integer). Option 1, Option 2…… are constants.
Switch Case allows us to add default statement. If the variable value is not matching with any of the case statements then the code present in the default will be executed.
Break statement is useful to come out from the switch statement otherwise, all the statements in the switch condition will be executed. Whenever a break statement is encountered, the execution flow would directly come out of the switch. Please refer Break Statement article to understand the Break statement perfectly
Let us see the flow chart for better understanding.
var opertor = '*';
var number1 = 10, number2 = 2;
document.write("Addition of two numbers is: " + (number1 + number2));
document.write("Subtraction of two numbers is: " + (number1 - number2));
document.write("Multiplication of two numbers is: " + (number1 * number2));
document.write("Division of two numbers is: " + (number1 / number2));
document.write("Module of two numbers is: " + (number1 % number2));
document.write("<b> You have entered Wrong operator </b>");
document.write("<br \> Please enter Correct operator such as +, -, *, /, %");
OUTPUT 1: Here we assigned * as operator variable.
OUTPUT 2: Let us chang the operator from valid to wrong operator to check the default value
Thank You for Visiting Our Blog