Use Multiple Values for One Switch Case Statement in Java

Shubham Vora Nov 15, 2022
  1. Use the switch-case Statement
  2. Use Two or More Values for One switch-case Statement
  3. Use the Arrow Syntax to Use Multiple Values for One switch-case Statement
Use Multiple Values for One Switch Case Statement in Java

In this article, we will learn to use multiple values for one switch-case statement.

Use the switch-case Statement

Java allows programmers to overcome too many if-else conditional statements like other programming languages by using the switch case statement. Also, what if users want to perform the same operation for multiple cases while using the switch-case statement?

Usually, users can do like below.

class Test {
    public static void main(String[] args) {
    int num = 9;
    switch (num%4) {
        case 1:
            System.out.println("num is not divisible by 4.");
            break;
        case 2:
            System.out.println("num is not divisible by 4.");
            break;
        case 3:
            System.out.println("num is not divisible by 4.");
            break;
        case 0:
            System.out.println("num is divisible by 4.");
            break;
        default:
            System.out.println("num is not divisible by 4.");
    }
    }
}

In the above example, we use the switch-case statement to check whether the number is divisible by 4. We use the num%4 condition in the switch-case statement and print the response based on the modulo operation.

Users can see that we are printing the same sentence for modulo 1, 2, and 3: num is not divisible by 4.

Output:

num is not divisible by 4.

Use Two or More Values for One switch-case Statement

To determine if a number is divisible by four or not, using a switch-case statement, we have written the 10s of lines of code in the above example. We can make that code clearer and more readable by using multiple values for a single switch-case statement.

In the example below, we used a single switch-case statement for cases 1, 2, and 3. Users can see that the code looks clearer and more readable and still gives the same output.

Example Code:

class Test {
    public static void main(String[] args) {
    int num = 9;
    switch (num%4) {
        case 1:
        case 2:
        case 3:
            System.out.println("num is not divisible by 4.");
            break;
        case 0:
            System.out.println("num is divisible by 4.");
            break;
        default:
            System.out.println("num is not divisible by 4.");
    }
    }
}

Output:

num is not divisible by 4.

Use the Arrow Syntax to Use Multiple Values for One switch-case Statement

Java 14 introduces a new syntax for the switch-case statement. Users can add multiple values for a single case by separating the comma, and users have to put the executable code in the curly braces.

The arrow syntax for the switch-case statement is:

case firstCase,secondCase,thirdCase ->{
           // executable code
    }

Example Code:

public class Test {
    public static void main(String[] args) {
    int num = 9;
    switch (num%4) {
        case 1,2,3 -> {
            System.out.println("num is not divisible by 4.");
        }
        case 0->{
            System.out.println("num is divisible by 4.");
        }
        default->{
            System.out.println("num is not divisible by 4.");
        }
    }
    }
}

In the above example, we’ve used the new syntax of the switch-case statement for the 1, 2, and 3 cases while checking if the number is divisible by 4.

Output:

num is not divisible by 4.

Example Code: Return the Number of Days of the Month of a Particular Year Using switch-case

In the following example, we used the switch-case arrow syntax to get the current month’s number of days. Users can see that we are returning the 31 days for 1, 3, 5, 7, 8, 10, and 12 cases using the yield and 30 days for 4, 6, 9, and 11 cases.

public class Test {
    public static void main(String[] args) {
        int monthToCheck = 2;
        int yearToCheck = 1968;
        int days = switch (monthToCheck) {
            case 4, 6, 9, 11 ->{
               yield 30;
            }
            case 1, 3, 5, 7, 8, 10, 12 ->{
               yield 31;
            }
            case 2 ->{
                if ((( monthToCheck % 4 == 0) &&
                        !( monthToCheck % 100 == 0))
                        || ( monthToCheck % 400 == 0))
                    yield 29;
                else
                   yield 28;
            }
            default ->{
                System.out.println("Month is not valid.");
                yield -1;
            }
        };
        System.out.println("Number of Days in month " + monthToCheck + " is = " + days);
    }
}

Output:

Number of Days in month 2 is = 28

This article taught us to use multiple values for a single switch-case statement via different examples. Also, we have seen the real-life example of getting the number of days using the switch-case statements with multiple values.

We can clarify our code by using multiple values for a single switch-case statement.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - Java Switch