Using Multiple Values with Switch Case Statements

In programming, switch case statements are used to execute different blocks of code based on the value of a variable. However, sometimes we need to perform the same action for multiple values. In this tutorial, we will explore how to use multiple values with switch case statements.

Introduction to Switch Case Statements

A switch case statement is a type of control structure that allows us to execute different blocks of code based on the value of a variable. The general syntax of a switch case statement is as follows:

switch (variable) {
    case value1:
        // code to be executed for value1
        break;
    case value2:
        // code to be executed for value2
        break;
    default:
        // code to be executed if none of the above values match
        break;
}

Using Multiple Values with Switch Case Statements

To use multiple values with switch case statements, we can simply list the values separated by commas. However, this syntax is only available in Java 14 and later versions.

switch (variable) {
    case value1, value2:
        // code to be executed for both value1 and value2
        break;
}

Alternatively, we can use multiple case labels without any code between them. This will execute the same block of code for all the values listed.

switch (variable) {
    case value1:
    case value2:
        // code to be executed for both value1 and value2
        break;
}

Fallthrough in Switch Case Statements

In switch case statements, if we don’t use a break statement at the end of each case block, the program will continue executing the next case blocks until it encounters a break statement. This is known as fallthrough.

switch (variable) {
    case value1:
        // code to be executed for value1
    case value2:
        // code to be executed for both value1 and value2
        break;
}

In this example, if the variable matches value1, it will execute the code for value1 and then continue executing the code for value2.

Using Arrow Labels in Switch Case Statements

In Java 14 and later versions, we can use arrow labels to make our switch case statements more concise.

switch (variable) {
    case value1, value2 -> {
        // code to be executed for both value1 and value2
    }
}

This syntax eliminates the need for break statements and makes our code more readable.

Example Use Case

Let’s consider an example where we want to calculate the number of days in a month based on its name. We can use a switch case statement with multiple values to achieve this.

public class MonthDays {
    public static int getMonthDays(String month) {
        int days = 0;
        switch (month.toLowerCase()) {
            case "january", "march", "may", "july", "august", "october", "december":
                days = 31;
                break;
            case "april", "june", "september", "november":
                days = 30;
                break;
            case "february":
                // code to calculate days for February
                break;
            default:
                System.out.println("Invalid month");
                break;
        }
        return days;
    }

    public static void main(String[] args) {
        String month = "August";
        int days = getMonthDays(month);
        System.out.println("Number of days in " + month + ": " + days);
    }
}

In this example, we use a switch case statement with multiple values to calculate the number of days in each month.

Conclusion

In conclusion, using multiple values with switch case statements is a powerful feature that allows us to write more concise and readable code. By understanding how to use this feature effectively, we can simplify our code and make it easier to maintain.

Leave a Reply

Your email address will not be published. Required fields are marked *