Switch case without break statement

As you already know the Switch statement in java, can contain any number of cases. Each case may or may not be followed by a break statement. Because the break statement is optional for each block. You can’t force the user to provide a break statement for each case. So in this article, we will read how behaves Switch case works without a break statement.

Youtube video available in the Hindi language

Youtube video available in the Hindi language

Why is the break statement used?

The break statement is used inside the switch to terminate a statement sequence and jumps the control after the switch expression. Whenever a break statement is found in the Switch statement the control flow directly jumps out of the Switch statement.

NOTE: If a break statement is not used in any case the execution will continue into the next case until a break statement is reached. If no break statement is found in any case then it executes all the cases including the default case. It is also known as fall-through.

Syntax

switch(variable/expression)
 {
   case value1 :
      // code inside the case value1
      // Break is not used because it is optional
   
   case value2 :
      // code inside the case value2 
      // Break is not used because it is optional
    .
    .
    .
    default : 
           // code inside the default case.
}

In Syntax, you can see the break statement is optional.

public class ExampleOfSwitch
{  
   public static void main(String[] args)
   {  
      //Declaring a variable for switch expression  
      int noOfDays = 28;  
      switch(noOfDays) //Switch expression  
      {  
   	 //Case statements  
   	 case 28: 
         System.out.println("Only February month has 28 days");  
         // NOTE: There is no break statement after the case 28
    	 case 30:
         System.out.println("April, June, August, October, December has 30 days");  
    	 break;  
   	
         case 31: System.out.println("January, March, May, July, September, November has  
                  31 days");  
    	 break;  
    	//Default case statement  
   	 default: System.out.println("Please enter valid no of days");  
      }  
   }  
}  

Output: Only February month has 28 days
April, June, August, October, December has 30 days

Switch case without break statement

You can see in the example: Compiler doesn’t find the break statement after case 28 and it executes the next case also.

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 noOfDays = 28;  
      switch(noOfDays) //Switch expression  
      {  
      	 //Case statements  
   	 	 case 28: 
         System.out.println("Only February month has 28 days");  
         // NOTE: There is no break statement after the case 28
    	
   	 	 case 30:
         System.out.println("April, June, August, October, December has 30 days");  
    	 break;  
   	
         case 31: System.out.println("January, March, May, July, September, November has 31 days");  
    	 break;  
    	 //Default case statement  
         default: System.out.println("Please enter valid no of days");  
      }  
   }  
}

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 noOfDays = 28;  
      switch(noOfDays) //Switch expression  
      {  
      	 //Case statements  
   	 	 case 28: 
         System.out.println("Only February month has 28 days");  
         // NOTE: There is no break statement after the case 28
    	 case 30:
         System.out.println("April, June, August, October, December has 30 days");  
   	
         case 31: System.out.println("January, March, May, July, September, November has 31 days");  
    	 //Default case statement  
         default: System.out.println("Please enter valid no of days");  
      }  
   }  
}

Click on anyone to know the answer.

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

public class ExampleOfSwitch
{  
   public static void main(String[] args)
   {  
      //Declaring a variable for switch expression  
      int noOfDays = 28;  
      switch(noOfDays) //Switch expression  
      {  
      	 //Case statements  
   	 	 case 28: 
         System.out.println("Only February month has 28 days");  
         // NOTE: There is no break statement after the case 28
    	 case 30:
         System.out.println("April, June, August, October, December has 30 days");  
   	
         case 31: System.out.println("January, March, May, July, September, November has 31 days");
         break;
    	 //Default case statement  
         default: System.out.println("Please enter valid no of days");  
      }  
   }  
}

Click on anyone to know the answer.

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

public class ExampleOfNestedSwitch
{
   public static void main(String[] args)
   {
      //Declaring a variable for switch expression
      int year = 2;
      int marks = 80;
      switch(year) //Switch expression
      {
         //Case statements
         case 1:
         System.out.println("First year students are not eligible for scholarship ");
         break;
         case 2:
         System.out.println("Second year students are not eligible for scholarship");
         case 3:
         switch(marks)
         {
            case 50:
            System.out.println("You are not eligible for scholarship");
            break;
            case 80:
            System.out.println("Congrats!!!!! you are eligible for scholarship");
            break;
         }
         break;
         //Default case statement
         default: System.out.println("Please enter valid year");
     }
   }
}

Click on anyone to know the answer.

3 thoughts on “Switch case without break statement”

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.