Sometimes we need to make a decision based on the value of a single variable. For example, imagine we have a menu where 1 means "Start", 2 means "Settings", and 3 means "Exit". We could use several if-else statements for this, but Java provides a cleaner option called the switch statement.
A switch statement allows us to compare one value with multiple possible values and execute the code that matches.
Basic Syntax
The basic structure of a switch statement looks like this:
switch (value) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
The switch expression is the value we want to check. Each case represents a possible value. When Java finds a matching case, it executes the code inside it.
The break statement is used to stop the switch after the matching case has been executed. The default block is used when none of the cases match.
A Simple Example
Let's say we have a number representing a day:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
The value of day is 2, so Java finds case 2 and prints:
Tuesday
Why Do We Need break?
The break statement is important because it tells Java to exit the switch statement after executing a matching case.
For example:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
}
When Java finds case 2, it prints Tuesday and then encounters break, so the switch ends.
If we don't use break in a traditional switch, Java can continue executing the following cases even though they don't match. This behavior is called fall-through.
For example:
int number = 1;
switch (number) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
}
Output:
One
Two
Three
This happens because there are no break statements, so Java continues into the following cases.
The default Case
The default case is executed when none of the cases match.
For example:
int day = 8;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
Since there is no case for 8, Java executes default:
Invalid day
The default case is optional, but it is often useful for handling unexpected values.
Multiple Cases with the Same Code
Sometimes multiple values should perform the same action.
For example, suppose we want Saturday and Sunday to both display "Weekend".
We can write:
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}
If day is 6 or 7, the program prints:
Weekend
This is useful when several choices should lead to the same result.
Switch with Strings
A switch isn't limited to numbers. We can also use it with String values.
For example:
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("You selected Apple.");
break;
case "Banana":
System.out.println("You selected Banana.");
break;
case "Orange":
System.out.println("You selected Orange.");
break;
default:
System.out.println("Unknown fruit.");
}
Output:
You selected Apple.
This can be useful when a program needs to respond to different text choices.
Switch vs if-else
Both switch and if-else can be used to make decisions, but they are useful in slightly different situations.
If you are checking specific values, switch can make the code cleaner:
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
}
But if you need to check ranges or complex conditions, if-else is usually more suitable:
if (marks >= 90) {
System.out.println("A+");
} else if (marks >= 80) {
System.out.println("A");
}
So, a simple rule is:
Specific values →
switch
Conditions and ranges →if-else
A Real-World Example
Let's create a simple menu:
int choice = 2;
switch (choice) {
case 1:
System.out.println("Starting the game...");
break;
case 2:
System.out.println("Opening settings...");
break;
case 3:
System.out.println("Exiting the game...");
break;
default:
System.out.println("Invalid choice.");
}
Since choice is 2, the output is:
Opening settings...
This is the basic idea behind a switch statement. It is especially useful when you have one value and several specific choices that need different actions.