The Java switch statement is a multi-way decision-making statement that allows you to execute different blocks of code based on the value of a variable or expression. In this tutorial, we will cover the basics of the switch statement in Java, including its syntax, examples, and best practices.
Syntax of Switch Statement in Java
The syntax of the switch statement in Java is as follows:
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
...
default:
// code to be executed if expression does not equal any of the above values
break;
}
In the above syntax:
expression
is the value that you want to compare with the values in the case statements.value1
,value2
, etc. are the values that you want to compare with the expression.break
is used to exit the switch statement and continue executing the code after the switch statement.default
is an optional statement that is executed if the expression does not equal any of the values in the case statements.
Example of Switch Statement in Java
Here is an example of a switch statement in Java:
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;
}
}
}
In this example, the switch statement is used to print the day of the week based on the value of the day
variable.
Best Practices for Using Switch Statement in Java
Here are some best practices for using the switch statement in Java:
- Use the switch statement when you have a fixed number of possible values for the expression.
- Use the break statement to exit the switch statement and continue executing the code after the switch statement.
- Use the default statement to handle cases where the expression does not equal any of the values in the case statements.
- Avoid using the switch statement with complex expressions or variables that can have a large number of possible values.
Switch Expression in Java 12 and Later
In Java 12 and later, you can use the switch expression, which is a more concise and expressive way of writing switch statements. Here is an example of a switch expression:
public class SwitchExpressionExample {
public static void main(String[] args) {
int day = 3;
String dayOfWeek = 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(dayOfWeek);
}
}
In this example, the switch expression is used to assign a value to the dayOfWeek
variable based on the value of the day
variable.
Comments
Post a Comment