The switch statement is another decision-making statement. Logically, the switch statement is very similar to an if/else ladder. The switch statement can have several execution paths; however, it always only evaluates one path.
How to Use Switch in Java
The switch statement utilizes several keywords for its operation, and each one is explained below.
1. Case
Each possible execution path in a switch statement is denoted with the case keyword. Each case contains a value that is compared with the switch value, and if both values match, then the code in the respective case block will be run.
A switch statement can have as many case sections as you want.
2. Break
The case blocks almost always end with the ' break ' keyword inside each case (and default) section. This tells the program to exit the switch and continue with the rest of the program when the proper case has been identified. There is one exception to this rule, which will be covered shortly.
3. Default
Instead of ending a switch statement with a case, you can end it with the default keyword. The default block will execute if none of the case values are matched.
Example of Switch in Java
Here is an example switch statement:
class Main {
public static void main(String[] args) {
int hour = 4;
switch(hour) {
case 1:
System.out.println("It is one o\'clock");
break;
case 2:
System.out.println("It is two o\'clock");
break;
case 3:
System.out.println("It is three o\'clock");
break;
case 4:
// this case will run since hour is set to 4,
// and this matches the case value of 4
System.out.println("It is four o\'clock");
break;
default:
// if, for example, the hour variable was set
// to 5, then this would be run since no case
// value of 5 was created
System.out.println("We don\'t know the time");
break;
}
}
}
When to Use the Switch Statement
The switch statement is best used when there are a finite number of outcomes, such as hours in a day, months in a year, or floors an elevator can visit.
Experiment with Switch Statements
Please demonstrate the use of a switch statement below. Use a System.out.println() statement for each case so you can validate which case is triggered. Be sure to include break statements in each case. Once you've got that done, remove all the break statements from each case and run the program again. What happens when you don't use break statements? See the example below for additional insights.
class Main {
public static void main(String[] args) {
// write your code below this line
// keep your code above this line
}
}
As you may have noticed, there is an interesting, intentional behavior with switch statements when the break keyword is not used. Essentially, after a case is triggered, if there is no break statement, each subsequent case will be triggered as well. This is great for situations such as membership levels. For instance, imagine that a business has "bronze", "gold", and "platinum" membership levels. The "bronze" level is the base package. The "gold" level includes everything in "bronze" plus a few more features. And the "platinum" package includes everything in "gold" plus a few more features. Experiment with this scenario in the code editor below.
class Main {
public static void main(String[] args) {
// after running this once, change the membershipLevel
// from "platinum" to "gold". What happens?
String membershipLevel = "platinum";
switch(membershipLevel){
case "platinum":
System.out.println("Platinum features included");
case "gold":
System.out.println("Gold features included");
case "bronze":
System.out.println("Bronze features included");
}
}
}
Summary: Java Switch Statement
- The
switchstatement contains multiple execution paths, but only one will be executed - The
casekeyword defines the value to be matched to enter a possible execution path - The
breakkeyword ends eachcasesection to tell the program to exit theswitchstatement - The
defaultsection will be executed if nocasevalue was matched
Syntax
Here's the syntax for the switch statement, where you can substitute the variables starting with your_ with your values.
switch(your_variable){
case your_value1:
// code to be executed if your_variable matches your_value1
break;
case your_value2:
// code to be executed if your_variable matches your_value2
break;
default:
// code to be executed if no case value is matched
break;
}