Welcome to The Coding College! In this tutorial, we will explore the switch statement in Java, a useful control flow tool for handling multiple conditions efficiently.
What is a Switch Statement?
The switch
statement is an alternative to using multiple if...else
conditions. It evaluates a single expression and matches its value against multiple case labels. When a match is found, the corresponding block of code executes.
Syntax
switch (expression) {
case value1:
// Code to execute if expression equals value1
break; // Exit the switch
case value2:
// Code to execute if expression equals value2
break;
// Add more cases as needed
default:
// Code to execute if no case matches
}
Key Points
- Expression Type: The
expression
in theswitch
statement must evaluate to a byte, short, int, char, String, or an enum type. - Break Statement: The
break
statement prevents fall-through to the next case. If omitted, execution continues to the subsequent cases. - Default Case: The
default
block is optional but runs when no cases match.
Example 1: Simple Switch Statement
public class SwitchExample {
public static void main(String[] args) {
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;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Output:
Wednesday
Example 2: Switch with Strings
public class SwitchStringExample {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("You chose Apple.");
break;
case "Banana":
System.out.println("You chose Banana.");
break;
case "Orange":
System.out.println("You chose Orange.");
break;
default:
System.out.println("Unknown fruit.");
}
}
}
Output:
You chose Apple.
Example 3: Switch Without Break
public class SwitchNoBreak {
public static void main(String[] args) {
int num = 2;
switch (num) {
case 1:
System.out.println("Case 1");
case 2:
System.out.println("Case 2");
case 3:
System.out.println("Case 3");
default:
System.out.println("Default case");
}
}
}
Output:
Case 2
Case 3
Default case
Note: Without break
, all subsequent cases execute after a match.
Example 4: Using Default
public class SwitchWithDefault {
public static void main(String[] args) {
int month = 13;
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
default:
System.out.println("Invalid month");
}
}
}
Output:
Invalid month
Example 5: Switch with Multiple Cases
public class SwitchMultipleCases {
public static void main(String[] args) {
char grade = 'A';
switch (grade) {
case 'A':
case 'B':
System.out.println("Good job!");
break;
case 'C':
case 'D':
System.out.println("Needs improvement.");
break;
default:
System.out.println("Invalid grade.");
}
}
}
Output:
Good job!
Example 6: Enhanced Switch (Java 12+)
Java introduced enhanced switch expressions for more concise syntax.
public class EnhancedSwitch {
public static void main(String[] args) {
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println("Day: " + dayName);
}
}
Output:
Day: Wednesday
Practice Problems
- Write a program to display the name of the month based on an integer input (1–12).
- Create a calculator program using the switch statement to perform basic arithmetic operations.
- Use a switch statement to categorize a given character as a vowel, consonant, or invalid.
Conclusion
The switch
statement is a powerful tool for handling multiple conditions cleanly and efficiently. It is especially helpful when there are many discrete values to check against. Start practicing today to master this essential feature of Java!
For more tutorials, visit The Coding College and keep learning! 🚀