Switch Statement
The switch statement is conditional statement like if statement. Switch statement is useful us when we want to execute the one of the multiple code block based on the expression.
Switch statement have the multiple case statement with the some block of code. Every case statement have the break statement to terminate the switch statement.
In Switch statement we have to pass the expression or value to check with the every case of the switch statement. Switch statement check that value with the every case statement and if the switch statement find the matching case statement than it will execute that block of code.
Switch statement also come with the default statement or default case, default case will be execute when no matching case found. default statement is an optional statement. There is no thumb rule for the default statement to put it always last in the switch case. We can put the default statement anywhere in the switch statement. It executes only when no matching case found.
Syntax:
switch (searching_value or expression) { case value1: // Block of code executes when // seraching_value matched to the value1 break; case value2: // Block of code executes when // seraching_value matched to the value2 break; case value3: // Block of code executes when // seraching_value matched to the value3 break; default: // This is default statement. // This is the optional, no need to write. // This block of code executes when // no searching_value found in the all cases break; }
We must be have to use the break statement to terminate the switch execution. Every case statement have the break statement.
Switch statement can take any type of expression like integer or string.
Example 1:
var no = 2; switch (no) { case 1: alert("Number is 1"); break; case 2: alert("Number is 2"); break; case 3: alert("Number is 3"); break; default: alert("Sorry number is out of range!!!"); break; } // Number is 2
In above example, We are checking the value of the no. Here the value of the no is 2. So it will display the popup with the message “Number is 2”
Example 2:
var no = 4; switch (no) { case 1: alert("Number is 1"); break; case 2: alert("Number is 2"); break; case 3: alert("Number is 3"); break; default: alert("Sorry number is out of range!!!"); break; } // Sorry number is out of range!!!
In above example, We are also checking the value of the no. But the value of the no is 4. But there is no case statement witch match to the 4, So here default statement will be executed and it display the popup with the message “Sorry number is out of range!!!”.
We have to put the constant value for the case statement. Yes it is compulsory to give the constant or some variable name to the case statement. Because switch statement have to check the value with the every case statement’s value.
Execution flow of switch statement
- Switch statement except some value for the checking with the every case statement. So first we have to pass the checking value to switch statement.
- After that we have to write the case statement with the constant value or give the value to the case statement by giving variable name. We also have to write the some block of code inside the case statement. Never forget to the put the break statement after every block of code of the case statement. Because only break statement can terminate the execution of the switch statement.
- Write the default statement. It is an optional statement. No need to write if you don’t want to write. You can write it anywhere in the switch statement. It can executes only when there is no matched case found.
- Now switch statement will start to check the value of the case statement with the searching_value one by one. It check case statement one by one till the searching_value found.
- If the searching_value found than it will start to executes the block of code of the that case statement. And It will stop the execution when it found the break statement.
- Once break statement executes, switch statement will be terminated and remaining case statement will be not checked.
- If no matched case found than the default statement will be execute, if we written the default statement.
Combined case statement
we can also put the combined case statement to execute the same block of code on the multiple case statement.
Example:
var day = "Sunday"; //var day = 7; switch (day) { case 1: case "Monday": alert("Monday"); break; case 2: case "Tuesday": alert("Tuesday"); break; case 3: case "Wednesday": alert("Wednesday"); break; case 4: case "Thursday": alert("Thursday"); break; case 5: case "Friday": alert("Friday"); break; case 6: case "Saturday": alert("Saturday"); break; case 7: case "Sunday": alert("Sunday"); break; default: alert("Sorry, You entered wrong day!!!"); break; }
In above examples I am displaying the day on the based of the value passed to the switch statement. I am using the two case statement combined for the day checking. For examples I want to print the Monday when user pass the “1” or the “Monday” to the switch statement.