Skip to content

Java Switch Statement | Switch Case Multiple Values example

  • by

Java switch statement is matched case(condition) and execute statement for that. In the Switch statement, using passing value and then this value will go down the list of the case to find matched one. If There is not matched then it will return the default statement.

When you are using an if-else condition statement, it will become longer, So you can use a switch statement instead of it.

Java Switch Statement | Switch Case Multiple Values example

This Tutorial you will learn about What is and How to use switch case in java.

Syntax

Here is java switch statement syntax:

switch (variable/expression) {
case value1:
   // statements
   break; //optional
case value2:
   // statements
   break;
default:
   // statements
}

Switch Statement Flow Diagram:

Look at the flow diagram, how switch cases work.

Here is java switch statement syntax

Java switch Example

A simple example of the switch statement, where declaring variables with values and pass in switch express. If anyone of the case is matched then it will print the matched statement otherwise default value. A break keyword is required to stop code on the case.

package eye.hunts;

public class A {
    public static void main(String[] args) {
        //Declaring a variable 
        int number = 2;
        //Switch expression
        switch (number) {
            //Case statements
            case 1:
                System.out.println("Value 1");
                break;
            case 2:
                System.out.println("Value 2");
                break;
            case 3:
                System.out.println("Value 3");
                break;
                //Default case statement
            default:
                System.out.println("Not in 1, 2 or 3");
        }
    }
}

Output: Value 2

Java Switch Statement with String

You have seems upper example has based used int values, now look at how to use a Switch statement with string.

package eye.hunts;

public class A {
    public static void main(String[] args) {
        // char grade = args[0].charAt(0);
        char grade = 'C';

        switch(grade) {
            case 'A' :
                System.out.println("Excellent!");
                break;
            case 'B' :
                System.out.println("Good!");
            case 'C' :
                System.out.println("Well done");
                break;
            case 'D' :
                System.out.println("You passed");
            case 'F' :
                System.out.println("Better try again");
                break;
            default :
                System.out.println("Invalid grade");
        }
    }
}

Output: Well done

Switch case multiple values

What if you want a check multiple value check for single statement? Here is Java switch case multiple values example.

case text1: case text4: 
     do stuff;
     break;

Complete example:

package eye.hunts;

public class A {
    public static void main(String[] args) {
        //Declaring a variable
        int number = 2;
        //Switch expression
        switch (number) {
            //Case statements
            case 1: case 2:
                System.out.println("Value 1");
                System.out.println("Value 2");
                break;
            case 3:
                System.out.println("Value 3");
                break;
                //Default case statement
            default:
                System.out.println("Not in 1, 2 or 3");
        }
    }
}

Output: Value 1
Value 2

QA: What if you don’t use a break keyword in a switch case statement?

In this situation not using break keyword then Java switch statement is fall-through. It means it will execute all statements after the first case match. See this example after matched case 2: all the statement is executed and printed.

package eye.hunts;

public class A {
    public static void main(String[] args) {
        //Declaring a variable
        int number = 2;
        //Switch expression
        switch (number) {
            //Case statements
            case 1:
                System.out.println("Value 1");
            case 2:
                System.out.println("Value 2");
            case 3:
                System.out.println("Value 3");
                //Default case statement
            default:
                System.out.println("Not in 1, 2 or 3");
        }
    }
}

Output: Value 2
Value 3
Not in 1, 2 or 3

QA: What if the 2 case unit value is the same?

In this case, the compiler will throw an Error:(14, 13) java: duplicate case label

Here is an example for the same.

package eye.hunts;

public class A {
    public static void main(String[] args) {
        //Declaring a variable
        int number = 2;
        //Switch expression
        switch (number) {
            //Case statements
            case 1:
                System.out.println("Value 1");
            case 2:
                System.out.println("Value 2");
            case 2:
                System.out.println("Value 3");
                //Default case statement
            default:
                System.out.println("Not in 1, 2 or 3");
        }
    }
}

Output Screenshot: 

Java Switch Statement error

Some Notes :

Here is some important point of the switch statement in Java, have to follow.

  • You can use N number of case values for a switch expression.
  • The Case unit must be unique, otherwise, a compile-time error occurred.
  • Variables are not allowed in case unit
  • The default case is optional.

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Switch statement Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

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