Java Switch multiple cases

In java switch case is used to reduce the complexity of if else ladder. We can use multiple cases in switch statement. In this topic we will see how we can use switch case java with combined cases.

You can also read the related topics from here.
1. switch statement in Java?
2. Rules of the switch statement Java
3. Nested switch statements in Java?
4. Does Java switch multiple cases?
5. switch case without a break statement?
6. switch case with char?
7. switch case with wrapper classes?

8. Why does float values are not allowed in the switch statement?
9. Why the break statement is used in the switch statement?

As you already know about the Switch statement, it can contain multiple cases. You can write multiple cases in combined form in the Switch statement. It totally depends upon the user because some time the user wants to perform the same action on different inputs. So in that case user can combine the case. It means you can combine multiple cases of the Switch statement.

Syntax

switch(variable/expression)
 {
   case value1 :
   case value2 :
   case value3 :
   case value4 :
      // code inside the combined case
      break; // optional
   
   case value5 :
   case value6 :
   case value7 :
   case value8 :
      // code inside the combined case value 
      break; // optional
    .
    .
    .
    default : 
           // code inside the default case .
}

You can combine multiple cases as you can see in syntax.
Whenever you want to combine multiple cases you just need to write a case with case label and colons(:). You can’t provide the break statement in between of combined cases.

public class ExampleOfSwitch
  {  
     public static void main(String[] args)
     {  
          //Declaring a variable for switch expression  
          int noOfMonth = 5;  
          switch(noOfMonth) //Switch expression  
          {  
            //Case statements  
            case 1: 
            case 3:
            case 5:
            case 7:
            case 9:
            case 11:
                            System.out.println("It has 31 days");  
                    break;  
            case 2:
                            System.out.println("It has 28 days");  
                    break;  
            case 4:
            case 6:
            case 8:
            case 10:
            case 12: 
                    System.out.println("It has 30 days");  
                    break;  
            //Default case statement  
            default: System.out.println("Please enter valid input");  
           }  
       }  
 }  

Output: It has 31 days

Java Switch multiple cases

Explanation of Java Switch multiple cases

In the above example, We want to print the numbers of the day in the month. In a year, some months have equal numbers of days but some have different numbers of days. So, we decide to combine all the cases in which we want to perform same task. The example of months, As we know only February has 28 days the rest of the months contain either 30 or 31 days. So, we combined some cases because those months have the same days.

1. Quiz, Read the below code and do answer.

public class ExampleOfSwitch
  {  
     public static void main(String[] args)
     {  
          //Declaring a variable for switch expression  
          int noOfMonth = 5;  
          switch(noOfMonth) //Switch expression  
          {  
            //Case statements  
            case 1: 
            case 3:
            case 5:
            case 7:
            case 9:
            case 11:
                    System.out.println("It has 31 days");  
                    break;  
            case 2:
                    System.out.println("It has 28 days");  
                    break;  
            case 4:
            case 6:
            case 8:
            case 10:
            case 12: 
                    System.out.println("It has 30 days");  
                    break;  
            //Default case statement  
            default: System.out.println("Please enter valid input");  
           }  
       }  
 }

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class ExampleOfSwitch
  {  
     public static void main(String[] args)
     {  
          //Declaring a variable for switch expression  
          int noOfMonth = 4;  
          switch(noOfMonth) //Switch expression  
          {  
            //Case statements  
            case 1: 
            case 3:
            case 5:
            case 7:
            case 9:
            case 11:
                    System.out.println("It has 31 days");  
                    break;  
            case 2:
                    System.out.println("It has 28 days");  
                    break;  
            case 4:
            case 6:
            case 8:
            case 10:
            case 12:
            default:
                    System.out.println("It has 30 days");  
                    break;  
           }  
       }  
 }

Click on anyone to know the answer.

1 thought on “Java Switch multiple cases”

Leave a Comment