Summary: in this tutorial, you will learn how to use the Java switch statement to compare a value with multiple values and execute different code based on matches.
Introduction to the Java switch statement
The switch
statement allows you to evaluate an expression
, compare its result with multiple possible values, and execute different code blocks based on the match.
Here’s the syntax of the switch statement:
switch (expression) {
case value1:
// Code to execute when expression equals value1
break;
case value2:
// Code to execute when expression equals value2
break;
// More cases...
default:
// Code to execute when none of the cases match the expression
}
Code language: Java (java)
In this syntax:
expression
: This is the expression whose value will be compared against thecase
values.value1
,value2
, etc.: These are the possible values that theexpression
can match. You can have multiplecase
labels.break
: Thebreak
statement is used to exit theswitch
block once a match is found. Withoutbreak
, the control will continue to the nextcase
, which may lead to unexpected behavior.
How the switch statement works:
- First, evaluate the
expression
. - Second, compare the results of the
expression
with eachcase
label. - Third, execute the code block associated with that
case
label if a match is found. - Finally, if no match is found, and there is a
default
case, execute the code block under thedefault
label.
expression
This is the expression that the switch statement will evaluate and use its result to compare against multiple values.
The switch works with the following primitive types:
- byte
- sort
- int
- char
Besides these primitive types, the switch also supports the wrapper types of these primitive types, the enum type, and the String class. You’ll learn more about these types later.
case label
The switch statement may contain one or more case labels:
case value:
Code language: Java (java)
In the case label, you specify the value that the expression is compared against. The value in the case label must be a compile-time constant. In other words, it must be a literal or final variable.
default case
The default is optional in a switch statement. The switch statement will execute the default case when the expression doesn’t match any values.
It’s a good practice to always include a default case.
break statement
The break
statement is used to exit the switch
block once a match is found.
Without break
, the control will “fall through” to the next case
, executing all subsequent code blocks until a break
is encountered or the switch
block ends.
Java switch statement examples
Let’s take some examples of using the Java switch statement.
1) Simple Java switch example
The following example illustrates how to use the switch statement to display the day name based on the day number:
public class App {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid day");
}
}
}
Code language: Java (java)
Output:
Tuesday
Code language: Java (java)
How it works.
First, declare the day variable and initialize its value to 3.
Second, compare the day with values from 1 to 7 and display the day’s names respectively. If the day is not between 1 and 7, display the message “Invalid day” in the default.
The program displays the message “Tuesday” because the day is 3 which matches case 3.
2) Using switch statement with fall through example
The following example illustrates how to use the switch statement to display quarter based on the provided month:
public class App {
public static void main(String[] args) {
byte month = 5;
switch (month) {
case 1:
case 2:
case 3:
System.out.println("Quarter 1");
break;
case 4:
case 5:
case 6:
System.out.println("Quarter 2");
break;
case 7:
case 8:
case 9:
System.out.println("Quarter 3");
break;
case 10:
case 11:
case 12:
System.out.println("Quarter 4");
break;
default:
System.out.println("Invalid month");
}
}
}
Code language: Java (java)
Output:
Quarter 2
Code language: Java (java)
The program will print:
- Quarter 1 if the month is 1, 2, and 3.
- Quarter 2 if the month is 4, 5, and 6.
- Quarter 3 if the month is 7, 8, and 9.
- Quarter 4 if the month is 10, 11, and 12.
In this program, you can see that case
blocks 1, 2, and 3, for example, do not have break
statements. This means that if month
is 1, the code under “Quarter 1” is executed, and then it continues to execute the code under “Quarter 2” and “Quarter 3” as well. This behavior continues until a break
statement is encountered or until the switch
block ends.
As you can see, we intentionally use “fall through” to group multiple months into the same quarter.
Summary
- Java switch statement to compare a value with multiple values and execute different blocks of code based on matches.