Introduction
The switch statement in Java provides a way to execute one block of code among many options based on the value of an expression. It is a powerful control flow statement that can replace multiple if-else-if conditions, making the code more readable and easier to manage. In this chapter, we will explore the switch statement in detail, including its syntax, usage, and examples.
Syntax
The basic syntax of the switch statement is as follows:
switch (expression) {
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
// you can have any number of case statements
default:
// code to be executed if none of the cases match
}
Key Points:
- The
expressioncan be of typebyte,short,char,int,String, or an enumeration. - Each
casekeyword is followed by a value and a colon. - The
breakstatement is used to exit theswitchstatement. If omitted, the program will continue to the next case (fall-through). - The
defaultcase is optional and executed if none of thecasevalues match theexpression.
Example
Let’s consider an example where we use the switch statement to determine the day of the week based on an integer input.
Example Code:
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");
break;
}
}
}
Output:
Wednesday
Fall-Through in switch Statement
In a switch statement, if the break statement is omitted, the program will continue executing the following case statements until a break is encountered or the switch statement ends. This behavior is known as "fall-through."
Example Code:
public class FallThroughExample {
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
default:
System.out.println("Default");
}
}
}
Output:
Two
Three
Default
In the above example, since the break statements are omitted, the execution continues from case 2 to case 3 and then to the default case.
Using Strings in switch Statement
Starting from Java 7, you can use String objects in the switch statement.
Example Code:
public class StringSwitchExample {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("It's an apple.");
break;
case "Banana":
System.out.println("It's a banana.");
break;
case "Cherry":
System.out.println("It's a cherry.");
break;
default:
System.out.println("Unknown fruit.");
break;
}
}
}
Output:
It's an apple.
Diagram: Flow Chart of switch Statement
Start
|
[expression]
|
v
[switch]
|
/|\
/ | \
case1 case2 ... caseN default
| | | |
v v v v
Code1 Code2 CodeN CodeD
| | | |
break break break (optional)
\____|_____/ /
| /
End ________/
Conclusion
The switch statement is a versatile control flow statement that simplifies the process of selecting one of many code paths based on the value of an expression. It improves code readability and maintainability compared to multiple if-else-if statements. Understanding how to use the switch statement, including fall-through behavior and string support, can help you write cleaner and more efficient Java code.