Switch Statement in Java
A switch statement in Java is used to simplify the selection among multiple possible paths.
It allows your program to branch execution to different parts of code based on the value of a variable or expression.
This control flow structure allows your program to execute one block of code out of many based on the value of a variable or expression.
The switch statement can be a cleaner and more efficient alternative to the if-else if ladder, especially when dealing with multiple possible values for a single variable.
Syntax of a switch Statement
Here is the basic syntax of a switch statement in Java:
switch (variable) {
case value1:
// code to be executed if variable == value1
break;
case value2:
// code to be executed if variable == value2
break;
case value3:
// code to be executed if variable == value3
break;
// you can have any number of case statements
default:
// code to be executed if variable doesn't match any case
}
variable: The variable or expression whose value is compared with the values in each case.
case value: A possible value for the variable. If the variable matches this value, the corresponding code block is executed.
break: A statement that terminates the switch statement, preventing the execution from falling through to the next case.
default: An optional case that runs if none of the specified case values match the variable.
How It Works
When a switch statement is encountered, the value of the variable is compared with each case value:
If a match is found, the code block associated with that case is executed.
The break statement is used to exit the switch statement after executing the matched case's code block.
If no match is found, the default block (if present) is executed.
Example of a switch Statement
Let's look at a simple example to understand how a switch statement works. Consider the following code:
public class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Explanation:
Declaration of the variable: We declare an integer variable day and assign it a value of 3.
The switch statement: We compare the value of day with the values in each case.
Matching case: When day is equal to 3, the code block for case 3 is executed, printing "Tuesday."
Break statement: The break statement terminates the switch statement after executing the matched case's code block.
Since day is 3, the output will be:
Tuesday
More Detailed Breakdown
- Condition: day
- This is the variable whose value we are comparing with each case value.
- Case Values:
case 1:, case 2:, case 3:, etc., are the possible values for the day variable.
The code block associated with case 3: is executed because day is 3.
- Code Block:
- The statement System.out.println("Tuesday"); runs, printing the message to the console.
- Break Statement:
The break statement prevents the execution from falling through to the subsequent case blocks.
Without the break statement, the code for all subsequent cases would execute until a break is encountered or the end of the switch statement is reached.
Using Strings in switch Statements
Java also supports using strings in switch statements. Here is an example:
public class Main {
public static void main(String[] args) {
String day = "Tuesday";
switch (day) {
case "Sunday":
System.out.println("1st day of the week");
break;
case "Monday":
System.out.println("2nd day of the week");
break;
case "Tuesday":
System.out.println("3rd day of the week");
break;
case "Wednesday":
System.out.println("4th day of the week");
break;
case "Thursday":
System.out.println("5th day of the week");
break;
case "Friday":
System.out.println("6th day of the week");
break;
case "Saturday":
System.out.println("7th day of the week");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Explanation:
String Variable: The switch statement works similarly with a string variable day.
Case Values: The cases now compare against string values.
Output: The output will be "3rd day of the week" since day is "Tuesday."
Summary
switch statement: A control flow structure that simplifies the selection among multiple possible paths based on the value of a variable or expression.
Variable: The switch statement evaluates the value of a single variable or expression.
Case values: Specific values that the variable is compared against. If the variable matches a case value, the corresponding code block is executed.
Break statement: Terminates the switch statement after executing the matched case's code block, preventing fall-through to subsequent cases.
Default block: An optional block that executes if none of the case values match the variable. It acts as a fallback for unmatched cases.
The switch statement is a powerful tool for making decisions in your Java programs when there are multiple possible values for a single variable.