Java

Switch Case statement In Java

When there are several possibilities (or choices) and it may be necessary to carry out a distinct job for each option, the switch case statement is utilized.

Switch case statement syntax is as follows:

switch (variable or an integer expression)
{
     case constant:
     //Java code
     ;
     case constant:
     //Java code
     ;
     default:
     //Java code
     ;
}

A Simple Switch Case Example

public class SwitchCaseExample1 {

   public static void main(String args[]){
     int num=2;
     switch(num+2)
     {
        case 1:
	  System.out.println("Case1: Value is: "+num);
	case 2:
	  System.out.println("Case2: Value is: "+num);
	case 3:
	  System.out.println("Case3: Value is: "+num);
        default:
	  System.out.println("Default: Value is: "+num);
      }
   }
}

Output:

Default: Value is: 2

Explanation: I used an expression in the switch, but you may use a variable as well. I entered num+2, where num is 2, and the result of the addition was 4. The default case was used since there was no case declared with value 4. In order to ensure that the default block is performed if there is no catch that satisfies the condition, we should utilize default in switch cases.

Switch Case Flow Diagram

The case block that matches the result is then run after the variable, value, or expression included in the switch parenthesis has first been evaluated.

Break statement in Switch Case

Although it is not required in switch case, you nearly always employ a break statement when working with switch case. Let’s look at the example below where I am not utilizing the break statement before we talk about it:

public class SwitchCaseExample2 {

   public static void main(String args[]){
      int i=2;
      switch(i)
      {
	 case 1:
	   System.out.println("Case1 ");
	 case 2:
	   System.out.println("Case2 ");
	 case 3:
	   System.out.println("Case3 ");
	 case 4:
           System.out.println("Case4 ");
	 default:
	   System.out.println("Default ");
      }
   }
}

Output:

Case2 
Case3 
Case4 
Default

When you wish your program’s flow to leave the switch body, you use break statements. When a break statement appears in the switch body, the execution flow will bypass the remaining cases and exit the switch.

Take the same example, but add a break statement this time.

Example with break statement

public class SwitchCaseExample2 {

   public static void main(String args[]){
      int i=2;
      switch(i)
      {
	 case 1:
	   System.out.println("Case1 ");
	   break;
	 case 2:
	   System.out.println("Case2 ");
	   break;
	 case 3:
	   System.out.println("Case3 ");
	   break;
	 case 4:
           System.out.println("Case4 ");
           break;
	 default:
	   System.out.println("Default ");
      }
   }
}

Output:

Case2

You can now see that just case 2 had been carried out, with the other instances being disregarded.

Few points about Switch Case

  1. Orders 1, 2, and 3 are not necessarily necessary in a case. Following the keyword case, it can have any integer value. Additionally, you don’t necessarily have to list cases in ascending order; you can do so depending on the situation.
  2. Characters can also be used in switch case. for instance –
    public class SwitchCaseExample2 {
    
       public static void main(String args[]){
          char ch='b';
          switch(ch)
          {
    	 case 'd':
    	   System.out.println("Case1 ");
    	   break;
    	 case 'b':
    	   System.out.println("Case2 ");
    	   break;
    	 case 'x':
    	   System.out.println("Case3 ");
    	   break;
    	 case 'y':
               System.out.println("Case4 ");
               break;
    	 default:
    	   System.out.println("Default ");
          }
       }
    }
  3. The phrase provided inside the switch must provide a constant value; otherwise, it is invalid.
    For instance:

Valid expressions for switch:

switch(1+2+23)
switch(1*2+3%4)

Invalid switch expressions:

switch(ab+cd)
switch(a+b+c)

4. Switch statements may be nested inside of other switches, as long as that is permitted. However, nested switch statements need to be avoided since they add complexity and make programs harder to comprehend.

Related Articles

Leave a Reply

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

Back to top button