Switch Statement in Java


The switch statement is a powerful control flow statement in Java that allows for multi-way branching. It executes one statement from multiple conditions, making it similar to an if-else-if ladder statement. The switch statement provides a convenient way to dispatch execution to different parts of code based on the value of the expression. The expression is usually a primitive data type such as byte, short, char, or int.

It is important to note that starting from JDK7, the switch statement also works with enumerated types (Enums in Java), the String class, and Wrapper classes. However, the Java switch expression must be of byte, short, int, long (with its Wrapper type), enums, or string.

There are a few important rules to follow when using switch statements in Java. First, there can be any number of cases that impose a condition check, but duplicate case values are not allowed. Second, the value for a case must be of the same data type as the variable in the switch. Third, the value for a case must be constant or literal, and variables are not allowed. Fourth, the break statement is used inside the switch to terminate a statement sequence, and it is optional. If omitted, execution will continue into the next case. Fifth, the default statement is optional and can appear anywhere inside the switch block. If it is not at the end, a break statement must be kept after the default statement to omit the execution of the next case statement.

Here's the syntax for the switch-case statement:

javascript
switch(expression) { case value1 : // Statements break; case value2 : // Statements break; default : // Statements }

It is important to note that the Java switch statement is a fall-through statement, which means that it executes all statements if the break keyword is not used. Therefore, it is highly essential to use the break keyword inside each case.

Consider the following example program that declares an integer named day whose value represents a day (1-7). The code displays the name of the day based on the value of the day using the switch statement:

java
public class GFG { public static void main(String[] args) { int day = 5; String dayString; switch (day) { case 1: dayString = "Monday"; break; case 2: dayString = "Tuesday"; break; case 3: dayString = "Wednesday"; break; case 4: dayString = "Thursday"; break; case 5: dayString = "Friday"; break; case 6: dayString = "Saturday"; break; case 7: dayString = "Sunday"; break; default: dayString = "Invalid day"; } System.out.println(dayString); } }

In this example, we use the switch statement with the int data type to set the value of dayString based on the value of day.

It is also possible to have multiple cases without break statements between them. In the updated version of the above program, the code also displays whether a day is a weekday or a weekend day:

typescript
public class GFG { public static void main(String[] args) { int day = 2; String dayType; String dayString; switch (day) { case 1: dayString = "Monday"; break; case 2: dayString = "Tuesday"; break; case 3: dayString = "Wednesday"; break; case 4: dayString = "Thursday";
            
break;
case 5: dayString = "Friday"; break; case 6: dayString = "Saturday"; dayType = "Weekend"; break; case 7: dayString = "Sunday"; dayType = "Weekend"; break; default: dayString = "Invalid day"; break; } if (dayType != null) { System.out.println(dayString + " is a " + dayType + " day."); } else { System.out.println(dayString + " is a weekday."); } } }


In this updated version of the code, we added two new variables: dayType and dayString. dayType is used to store whether a day is a weekday or a weekend day, and dayString is used to store the name of the day.


The switch statement is used to determine the value of dayString based on the value of day. If day is equal to 1, dayString is set to "Monday". If day is equal to 2, dayString is set to "Tuesday", and so on. If day is not equal to any of the values in the case statements, dayString is set to "Invalid day".


If day is equal to 6 or 7, dayType is set to "Weekend". If dayType is not null, meaning that day is either 6 or 7, the program prints out dayString + " is a " + dayType + " day.". Otherwise, the program prints out dayString + " is a weekday.".


For example, if day is equal to 2, the program will print out "Tuesday is a weekday.". If day is equal to 7, the program will print out "Sunday is a weekend day.".


Previous Post Next Post