Java – Switch

In Java, the Switch statement is used to execute one of the multiple code statements. It may be thought of as a collection of If-else expressions. Let us start Java Switch Case.

Syntax

switch (expression){
  case 1:
     statement 1;
     break;
  case 2:
     statement 2;
     break;
     ...
     ...
     ...
  case N:
     statement N;
     break;
  default:
     default statement;
}

The switch expression is compared to the cases and a match is made. When the case matches, the code in the following block is run.

Flow Diagram:

Java Switch Case

Example:

The switch expression in the example below is a variable named i with the value 2 that is matched against case values. The following piece of code is performed when the case value matches the expression value.

public class MyClass {
  public static void main(String[] args) {
    int i = 2;
    switch(i){
      case 1: 
         System.out.println("Red");
         break; 
      case 2: 
         System.out.println("Blue");
         break;
      case 3: 
         System.out.println("Green");
    }  
  }
}

The output of the above code will be:

Blue

default and break statements

  • Default Case: When the switch expression and test cases don’t match, the Default Statement is used.
  • Break Statement: After a match is discovered, the Break statement is used to exit the Switch statement.

Example:

The switch expression in the example below is a variable named I with a value of 10 that is tested against case values. Because there is no case that matches the number 10, the default code block is run.

public class MyClass {
  public static void main(String[] args) {
    int i = 10;
    switch(i){
      case 1: 
         System.out.println("Red");
         break; 
      case 2: 
         System.out.println("Blue");
         break;
      case 3: 
         System.out.println("Green");
         break;
      default:
         System.out.println("No match found.");
    }  
  }
}

The output of the above code will be:

No match found.

Please keep in mind that the default statement might be anywhere in the switch statement. In such cases, use the break statement in conjunction with the default statement.

Example:

Consider the following scenario, in which the default statement is placed first in a switch statement.

public class MyClass {
  public static void main(String[] args) {
    int i = 10;
    switch(i){
      default:
         System.out.println("No match found."); 
         break;
      case 1: 
         System.out.println("Red");
         break; 
      case 2: 
         System.out.println("Blue");
         break;
      case 3: 
         System.out.println("Green");
    }  
  }
}

The output of the above code will be:

No match found.

Common code blocks

On certain occasions, the same code block is required in many scenarios.

Example:

In the following example, the same code block is used for many switch scenarios.

public class MyClass {
  public static void main(String[] args) {
    int i = 10;
    switch(i){
      case 1: 
         System.out.println("Red");
         break; 
      case 2:
      case 10: 
         System.out.println("Blue");
         break;
      case 3:
      case 4:
      case 5: 
         System.out.println("Green");
    }  
  }
}

The output of the above code will be:

Leave a Reply

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