⬅ Previous Topic
Java if-else-if StatementNext Topic ⮕
Java Ternary Operator⬅ Previous Topic
Java if-else-if StatementNext Topic ⮕
Java Ternary OperatorWhen you're faced with multiple paths of logic based on the value of a single variable, the switch
statement in Java shines as a clean, readable alternative to a long series of if-else-if blocks. It helps you route your program's behavior based on specific values—like menu choices, user input, or predefined constants.
Imagine choosing what to eat at a food court. Based on the stall number, you want to print out the food item. You can write:
if (stall == 1) {
System.out.println("Pizza");
} else if (stall == 2) {
System.out.println("Burger");
} else if (stall == 3) {
System.out.println("Sushi");
} else {
System.out.println("Option not available");
}
Or you could write this more cleanly using switch
:
switch (stall) {
case 1:
System.out.println("Pizza");
break;
case 2:
System.out.println("Burger");
break;
case 3:
System.out.println("Sushi");
break;
default:
System.out.println("Option not available");
}
Pizza
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
...
default:
// default code block
}
Key Points:
expression
must evaluate to int
, char
, byte
, short
, enum
, or from Java 7 onwards, String
.break
statement is used to terminate a case. Without it, execution "falls through" to the next case.default
case is optional and executes when none of the cases match.int day = 3;
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");
}
Wednesday
If you omit break
, Java continues to execute the next statements—even if the case matched!
int choice = 2;
switch (choice) {
case 1:
System.out.println("First");
case 2:
System.out.println("Second");
case 3:
System.out.println("Third");
}
Second
Third
This is known as fall-through, and while sometimes useful, it can also lead to bugs if not handled carefully.
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("Red fruit");
break;
case "Banana":
System.out.println("Yellow fruit");
break;
default:
System.out.println("Unknown fruit");
}
Red fruit
Modern Java versions allow switch expressions that return values:
String result = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid";
};
System.out.println(result);
Wednesday
int section = 1;
int shelf = 2;
switch (section) {
case 1:
switch (shelf) {
case 1:
System.out.println("Section 1 - Shelf 1");
break;
case 2:
System.out.println("Section 1 - Shelf 2");
break;
}
break;
case 2:
System.out.println("Section 2");
break;
}
Section 1 - Shelf 2
break
unless you're intentionally falling through.default
to handle unexpected inputs.switch
for fixed values; use if-else
for range checks.Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice: 1. Start 2. Stop 3. Exit");
int input = sc.nextInt();
switch (input) {
case 1 -> System.out.println("System Started...");
case 2 -> System.out.println("System Stopped.");
case 3 -> System.out.println("Exiting System.");
default -> System.out.println("Invalid choice.");
}
System Started...
int choice = 2;
switch (choice) {
case 1:
System.out.println("First");
case 2:
System.out.println("Second");
case 3:
System.out.println("Third");
}
fruit
will trigger the default case?
String fruit = "Mango";
switch (fruit) {
case "Apple":
System.out.println("Red fruit");
break;
case "Banana":
System.out.println("Yellow fruit");
break;
default:
System.out.println("Unknown fruit");
}
int section = 1;
int shelf = 2;
switch (section) {
case 1:
switch (shelf) {
case 1:
System.out.println("Section 1 - Shelf 1");
break;
case 2:
System.out.println("Section 1 - Shelf 2");
break;
}
break;
case 2:
System.out.println("Section 2");
break;
}
⬅ Previous Topic
Java if-else-if StatementNext Topic ⮕
Java Ternary OperatorYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.