The switch statement picks one of many code blocks to run. The following is how it works:
- The switch expression is only evaluated one time
- The expression’s value is compared against the values in each circumstance
- If a match is found, the relevant code block is run
- The keywords break and default are optional and will be discussed later
Switch Case Statement Java
The switch statement in Java has the following syntax:
switch (expression) { case value1: // code break; case value2: // code break; ... ... default: // default statements }
What is the purpose of the switch-case statement?
The expression is evaluated once, and the values of each instance are compared. If the expression matches value1, the case value1 code is triggered. Similarly, if the expression matches value2, case value2’s code is run. If no match is found, the default case’s code is run.
The switch-case statement works similarly to the Java if…else…if. The syntax of the switch statement, on the other hand, is significantly cleaner and easier to read and write. The weekday number is used to calculate the weekday name in the example below:
public class Main { public static void main(String[] args) { int chooseDay = 6; switch (chooseDay) { case 1: System.out.println(" It is a Monday"); break; case 2: System.out.println("It is a Tuesday"); break; case 3: System.out.println("It is a Wednesday"); break; case 4: System.out.println("It is a Thursday"); break; case 5: System.out.println("It is a Friday"); break; case 6: System.out.println("It is a Saturday"); break; case 7: System.out.println("It is a Sunday"); break; } } }
Break Keyword
The term “break” describes a period where Java exits the switch block when it reaches the break keyword. More code and case testing are not executed inside the block due to this. It’s time to take a break once you’ve discovered a match and finished the task. No more testing is required.
A break can save a lot of time because it “ignores” the execution of the rest of the code in the switch block.
What is the default Keyword?
On the off chance of no matching case, the default keyword defines some code to run. If used as the last statement in a switch block, the default statement does not require a break.
public class Main { public static void main(String[] args) { int chooseDay = 4; switch (chooseDay) { case 6: System.out.println("Today is Saturday"); break; case 7: System.out.println("Today is Sunday"); break; default: System.out.println("It is a rest day"); } } }
Not using the break statement
The use of the break statement is optional. If the break is skipped, execution will continue to the following case. However, multiple cases with no break statements between them are occasionally beneficial. For example, consider the revised version of the software above, showing whether a given day is a weekday or a weekend day.
// Java program demonstrating the switch case with multiple cases without break statements public class Test { public static void main(String[] args) { int day = 2; String typeOfDay; String daysActivity; switch (day) { // multiple cases without break statements case 1: case 2: case 3: case 4: case 5: typeOfDay = "On Duty"; break; case 6: case 7: typeOfDay = "Holiday"; break; default: typeOfDay = "Invalid Answer"; } System.out.println( daysActivity + " is a " + typeOfDay); } }
Switch Case statements that are nested
A switch can be used as part of an outer switch’s statement sequence. It is referred to as a nested switch. Because a switch statement creates its block, there are no conflicts between the case constants in the inner and outer switches. Consider the following scenario:
// Java program to demonstrate how to use a nested switch case statement public class Test { public static void main(String[] args) { String Branch = "electives"; int yearOfStudy = 3; switch (yearOfStudy) { case 1: System.out.println("main course : Computer Studies in General"); break; case 2: System.out.println("main course : Understanding Digital Mathematics"); break; case 3: switch (electives) // nested switch { case "BIG DATA": case "DATA SCIENCE": System.out.println("elective options : The diversity of data in the digital age"); break; case "DATABASE": System.out.println("elective options : Relational Databases"); break; default: System.out.println("elective options : Machine Learning in CS"); } case 4: System.out.println("main course : Advanced data structures and Algorithms"); break; } } }
The following are some critical switch statement rules:
- Identical case values are not permitted.
- A case’s value must be the same data type as the switch’s variable.
- A case’s value must be either a constant or a literal. Variables aren’t permitted.
- The break statement is used to end a statement sequence inside the switch.
- The break statement is not required. If this parameter is left blank, execution will go to the next case.
- The default statement can go anywhere within the switch block and is optional. If it is not at the end, a break statement is added after the default statement to prevent the following case statement from being executed.
Example 1: Switch Case Statement on Students Results
Example 1: Switch Case Statement on Students Results // Java Program for checking the size using the switch...case statement class Main { public static void main(String[] args) { int num = 20; String result; // switch statement to check student results switch (num) { case 20: result = "Below Average"; break; case 51: result = "Average"; break; // match the value of week result 71: size = "Good"; break; case 94: result = "Excellent"; break; default: result = "Unknown"; break; } System.out.println("Your Score is: " + result); } }
We utilized the switch statement in the previous example to find the size. We have a variable number here. Each case statement’s value is compared to the variable. For example, the code for case 20 is run since the value matches 20. The size variable is given the value ‘Below Average’ in this case.
Conclusion
With the help of examples, you’ve learned how to utilize the switch statement in Java to regulate the flow of your program’s execution. The switch statement lets us choose several different ways to run a code block. A multi-way branch statement is the switch statement. It makes it simple to route execution to other areas of code based on the expression’s value. The primitive data types byte, short, char, and int can all be used in the expression. It also works with enumerated types like Enums in Java, String, and Wrapper classes, starting with JDK7.