The switch statement in Java is a control flow statement used to execute one block of code from multiple options, based on the value of a single variable or expression. It is an alternative to the if-else if ladder when you are comparing a variable against multiple possible values.
Java switch statement often provides a better alternative than a large series of if-else-if statements.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
}
Important Points
1. Expression: The value inside the switch must be of type byte, short, int, char, String, or an enum.
2. case Values: Each case represents a potential match for the expression. The case values must be constant and unique.
3. break Statement: It prevents the execution from “falling through” to the next case. Without break, all subsequent cases are executed.
4. default Case: This is optional and executed if none of the case values match the expression.
Example: Using switch with int
public class SwitchExample {
public static void main(String[] args) {
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;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
}
}
}
Output: Tuesday
Example: Using switch with String
public class SwitchWithString {
public static void main(String[] args) {
String color = "green";
switch (color.toLowerCase()) {
case "red":
System.out.println("Stop!");
break;
case "yellow":
System.out.println("Get ready.");
break;
case "green":
System.out.println("Go!");
break;
default:
System.out.println("Invalid color.");
}
}
}
Output: Go!
Example: Omitting break statement
If you omit the break statement, execution continues to the next case until a break or the end of the switch is reached.
Example:
public class SwitchFallthrough {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
default:
System.out.println("Weekend");
}
}
}
Output:
WednesdayWeekend
When Default Case is executed
The default block is executed if no other case matches the expression. It is like the else in if-else.
Example:
public class ExampleSwitchDefault {
public static void main(String[] args) {
int number = 10;
switch (number) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("No match found.");
}
}
}
Output: No match found
Using switch with enum
You can also use enum types in a switch statement.
public class SwitchWithEnum {
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public static void main(String[] args) {
Day today = Day.TUESDAY;
switch (today) {
case MONDAY:
System.out.println("Start of the week!");
break;
case FRIDAY:
System.out.println("Almost weekend!");
break;
case SATURDAY:
case SUNDAY:
System.out.println("It's weekend!");
break;
default:
System.out.println("It's a weekday.");
}
}
}
Output: It’s a weekday.
Limitations of switch
1. switch cannot handle range-based conditions (<, >, etc.).
Example: switch cannot check if (age > 21) or if (age < 21).
2. It works only with primitive types (byte, short, int, char), String, or enum.
Java switch Statement – Questions and Answers
Q 1: What is the switch statement in Java?
Ans: The switch statement executes one case from multiple conditions based on an expression.
Q 2: Which data types are supported in switch?
Ans: int, char, String, and enum.
Q 3: What is the role of break in switch?
Ans: It terminates the switch block to prevent fall-through.
Q 4: What happens if break is not used?
Ans: Execution continues to the next case.
Q 5: Is default case mandatory?
Ans: No, but it is recommended.
Java Switch Statement – Objective Questions (MCQs)
Q1. Which of the following is the correct syntax of a switch statement in Java?
Q2. Which data type cannot be used in a switch statement in Java?
Q3. What will be the output of the following code?
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");
}
Q4. What happens if the break statement is omitted in a switch case?
Q5. What will be the output of the following code?
int x = 2;
switch (x) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
break;
default:
System.out.println("Other");
}