Easy Tutorial
❮ Data Swap File Copy ❯

Java switch case Statement

The switch case statement is used to determine if a variable matches any value in a series of values, with each value representing a branch.

Syntax

The syntax for the switch case statement is as follows:

switch(expression){
    case value :
       // Statements
       break; // Optional
    case value :
       // Statements
       break; // Optional
    // You can have any number of case statements
    default : // Optional
       // Statements
}

The switch case statement has the following rules:

When executing a switch case, it always performs a match first. If a match is successful, it returns the current case value, and then determines whether to continue output based on the presence of a break statement.

Example

Test.java File Code:

public class Test {
   public static void main(String args[]){
      // char grade = args[0].charAt(0);
      char grade = 'C';

      switch(grade)
      {
         case 'A' :
            System.out.println("Excellent"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Good");
            break;
         case 'D' :
            System.out.println("Pass");
            break;
         case 'F' :
            System.out.println("You need to work harder");
            break;
         default :
            System.out.println("Unknown grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

The above code compiles and runs with the following result:

Good
Your grade is C

If there is no break statement in the case block, the JVM does not sequentially output the return values corresponding to each case. Instead, it continues to match, and if no match is found, it returns the default case.

Test.java File Code:

public class Test {
   public static void main(String args[]){
      int i = 5;
      switch(i){
         case 0:
            System.out.println("0");
         case 1:
            System.out.println("1");
         case 2:
            System.out.println("2");
         default:
            System.out.println("default");
      }
   }
}

The above code compiles and runs with the following result:

default

If there is no break statement in the matched case block, after a successful match, the values of all subsequent cases starting from the current case will be output.

Test.java File Code:

public class Test {
   public static void main(String args[]){
      int i = 1;
      switch(i){
         case 0:
            System.out.println("0");
         case 1:
            System.out.println("1");
         case 2:
            System.out.println("2");
         default:
            System.out.println("default");
      }
   }
}

The compilation and execution result of the above code is as follows:

1
2
default

If the currently matched case block does not have a break statement, then starting from the current case, the values of all subsequent cases will be output. If a subsequent case block has a break statement, it will exit the switch statement.

Test.java File Code:

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

The compilation and execution result of the above code is as follows:

1
2
3
❮ Data Swap File Copy ❯