Switch statement Java

The switch statement Java is like the if-else-if ladder statement. To reduce the code complexity of the if-else-if ladder switch statement comes. Switch statement is the alternate the of if else if ladder statement.

Here is the table content of the article will we will cover this topic.
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?

The switch statement executes one block of the statement from multiple blocks of statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice.

switch(variable/expression)
 {
   case value1 :
      // code inside the case value1
      break; // optional
   
   case value2 :
      // code inside the case value2 
      break; // optional
    .
    .
    .
    default : 
           // code inside the default case .
}

Rules of the Switch statement Java

  1. An expression can be of byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. You can place a variable or an expression in between brackets of switch().
  2. You can create any number of cases in switch statement.
  3. The case value must be literal or constant. The case value type should be a type of expression.
  4. Each case should be unique. If you create duplicate case value, it will throw compile-time error.
  5. Each case has a break statement which is optional. It is used inside the switch to terminate a statement sequence and jumps the control after the switch expression.
  6. If a break statement is not used in any case the execution will continue into the next case until a break statement is reached.
  7. In the switch, we can have an optional default case that must be placed at the end of the switch statement. If none of the cases are true, then this default case will be executed. The default case doesn’t need break statement.
Flowchart of Switch statement Java
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");  
    	break;  
    	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

In this example, we want to print the name of months based on their days. Firstly, we declared a variable is using in switch expression. The switch expression variable is a type of int. So, we created three cases of type int. Each case has different codes according to our requirements. We also provide a default case at the end of the switch if the user enters any wrong input which doesn’t match with any case then the default case will execute.

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 = 29;  
     switch(noOfDays) //Switch expression  
    {  
        //Case statements  
       case 28: 
        System.out.println("Only February month has 28 days");  
        break;  
        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  
     String noOfDays = "28";  
     switch(noOfDays) //Switch expression  
    {  
        //Case statements  
       case 28: 
        System.out.println("Only February month has 28 days");  
        break;  
        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.

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");  
        break;  
        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.

4. 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");  
       }
       System.out.println("Outside the case 28");
        break;  
        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.

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

are the braces mandatory in-between cases?

switch(variable/expression)
 {
   case value1 :
   {
      // code inside the case value1
      break; // optional
   }
   case value2 :
   {
      // code inside the case value2 
      break; // optional
   }
    .
    .
    .
    default : 
           // code inside the default case .
}

 

Click on anyone to know the answer.

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

public class ExampleOfSwitch
{  
  public static void main(String[] args)
  {  
     //Declaring a variable for switch expression  
     Boolean check = null;  
     switch(check) //Switch expression  
    {  
        //Case statements  
       case true:
        System.out.println("Value is true");  
        break;  
        case false:
        System.out.println("Value is false");  
        break;  
        //Default case statement  
       default: System.out.println("Please enter valid input");  
    }  
   }  
}

Click on anyone to know the answer.

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

public class ExampleOfSwitch
{  
  public static void main(String[] args)
  {  
     //Declaring a variable for switch expression  
    Integer a = 5; 
    switch(a) //Switch expression  
    {  
        //Case statements  
       case 1:
        System.out.println("Value is 1");  
        break;  
        case 2:
        System.out.println("Value is 2");  
        break;  
        case 5:
        System.out.println("Value is 5");  
        break;  
        //Default case statement  
       default: System.out.println("Please enter valid input");  
    }  
   }  
}

Click on anyone to know the answer.

Leave a Comment