Switch Statement in Java
When comparing a single condition in a chain of if/else(if)
statements, the switch
statement is an alternative. It consists of an expression that evaluates to a single value. This value is then compared through different case statements that will execute if there is a match.
Switch Statement
Here's an example of program using the if/else(if)
statement:
String activityToDo = "tennis";if (activityToDo == "shopping") {System.out.println("Head over to the mall");} else if (activityToDo == "tennis") {System.out.println("Grab your racket");} else if (activityToDo == "running") {System.out.println("Put on your running shoes");} else if (activityToDo == "picnic") {System.out.println("Pack up food, drinks, and a blanket to sit on");} else if (activityToDo == "movies") {System.out.println("Buy popcorn and a drink");} else {System.out.println("Nothing to do");}
It can get repetitive to constantly be typing activityToDo == "SOME_SORT_OF_ACTIVITY"
. This is where the switch statement is useful. It can help make the code more readable, but that is more of a personal or team preference.
Instead, we can write the program to be like this:
String activityToDo = "tennis";switch (activityToDo) {case "shopping":System.out.println("Head over to the mall");break;case "tennis":System.out.println("Grab your racket");break;case "running":System.out.println("Put on your running shoes");break;case "picnic":System.out.println("Pack up food, drinks, and a blanket to sit on");break;case "movies":System.out.println("Buy popcorn and a drink");break;default:System.out.println("Nothing to do");break;}
Using the switch
statement, we can achieve the same functionality as the previous if/else(if)
program. We start a switch statement with the switch
keyword followed by the expression we are evaluating. Then in the body of the switch
statement, we have different cases that will execute if the case matches the expression.
Notice that for each case, there is a break
keyword. This means that once there has been a match, execute any code before hitting a break
keyword and exit the switch
statement. This ensures that no other code gets run.
The default
case acts like an else
condition. If none of the cases match the expression, then the default
case gets executed. The break
is optional here since it is the last case being evaluated. You may want to write it for code consistency.
Trailing Switch Cases
If we don't include a break
statement, then any code below the matching case will be executed until a break
statement is encountered. Consider the following if/else(if)
statement:
String sportToPlay = "tennis";if (sportToPlay == "tennis") {System.out.println("Bring your racket");} else if (sportToPlay == "badminton") {System.out.println("Bring your racket");} else if (sportToPlay == "racquetball") {System.out.println("Bring your racket");} else if (sportToPlay == "running") {System.out.println("Put on your shoes");} else if (sportToPlay == "swimming") {System.out.println("Bring your goggles");} else if (sportToPlay == "basketball") {System.out.println("Bring a basketball");}
Here, we see that there are three places with the exact same message being printed out. Using the switch
statement, we don't have to write out the same statement multiple times by taking advantage of the trailing case:
String sportToPlay = "tennis";switch (sportToPlay) {case "tennis":case "badminton":case "racquetball":System.out.println("Bring your racket");break;case "running":System.out.println("Pit on your shoes");break;case "swimming":System.out.println("Bring your goggles");break;case "basketball":System.out.println("Bring a basketball");break;}
Conclusion
The switch
statement is an alternative to using if/else(if)
statements. Using it is more of a stylistic choice on what makes the code more readable and maintainable.