1. About Switch Case
Below are components of the switch case construct:
- switch: Comes with an expression which evaluates to a value. A single variable can also be an expression as it evaluates to a value. For example, the variable month number can hold a value of 1…12, Which can act as an expression.
- case: The value which we want to compare with evaluated expression will take part in case statement. The constant value here forms an expression like
Switch Expression == Case_Value. When the match returns true, the case block executes. In the above picture, it showed the case block in red colour.
- break: After executing the case block, if you want code flow to go out of the complete switch case structure, you can use the break statement. The break keyword will stay as the last statement inside the case block.
- default: The default block executes when no case statement match with the evaluated switch expression.
The below example takes a number from the user and forms the switch-case structure to print some numbers in a word.
UserNovariable contains the input number. Here, we form the switch expression using this variable.
- Shows case statements. Here, we provide the match case with a constant. Let us consider the statement
case 3:and this will form the conditional expression
UserNo == 3. When this condition evaluates to true, the code flow enters the case block.
- We can use a break statement to come out of the case block. So once a case match happens, the code flow enters the case block. The break statement makes the flow go out of the switch-case structure.
- Shows a complete case block. One can see a break statement at the end of this case block. This will avoid case flow trying for the case match for 5 after executing the case block for 4.
- When no case block matches with the switch expression, the execution flow enters the default: block. In our example, we print numbers 1-5 in words. The default block reports the number is more than five.
3. Case Groups
Sometimes, we may need to execute the same block of code for more than one match. In this case, we can group the case statement to execute a single block of statements. Have a look at the below example:
- Here, we use the same switch expression on the variable
UserNo. But we group three cases
- This is the second group based on the case match 4,5,6.
- Common case block for 4,5,6. Note, here we do not use any break statement as it is the last case block within the switch..case structure.
4. Code Reference
<title>Java Script Switch Case Example</title>
//Sample 01: Take a Number from User
var strTheNumber = prompt("Enter a Positive Number :", 0);
var UserNo = parseInt(strTheNumber, 10);
//Sample 02: Form Switch Case
document.writeln("The Number is One </br>");
document.writeln("The Number is Two </br>");
document.writeln("The Number is Three </br>");
document.writeln("The Number is Four </br>");
document.writeln("The Number is Five </br>");
document.writeln("More Than Five </br>")
//Sample 03: Case Group
document.writeln("Input in Range 1-3");
document.writeln("Input in Range 4-6");
5. Sample Output of the Example
Do you like this Example? Please comment about it for others!!