Java Switch Case

Switch case are used to check multiple test case of single value and pattern. Inside the body of switch define test condition. If condition is match by given expression then executed case block statements will work and terminate by break statement.

switch(expression){

    case condition1:
        break;
    case condition2:
        break:
    case condition3:
        break;
    //:
    //:
    //:
    default:
        //if no finding any valid case
}

Pattern is based on an expression or values. We are know about that every value is related to data type. This expression can be form of String, Enums, byte, short, char, and int primitive data types. And inside switch case statement are working this similar data values. For example.

public class SwitchCase{

    public static void main(String[] args){

        //Case 1 Work of Integer values
        int dataOne = 2;
        switch(dataOne){ //pattern are integer value

            //Case expression result in form of integers
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            default:
                //Default statement of this switch
                System.out.println("Default Statement");
                
        }

        //Case 2 Work of String value

        String name = "C++";
        switch(name){ //pattern are string value
            
            case "C++":
                System.out.printf("C++ Programming");
                break;
            case "Java":
                System.out.printf("Java Programming");
                break;
            //Remove default statement  
        }
    }
}

Output

Two
C++ Programming

Every expression of switch case in constant or literals form. We cannot use condition expression and statement here. When switch case statement are not in same type as compare to other switch case statement then in this situation compiler are produced an error.

public class SwitchCase{

    public static void main(String[] args){


        String name = "C++";
        switch(name){ //pattern are string value
            
            case "C++":
                System.out.printf("C++ Programming");
                break;
            case "Java":
                System.out.printf("Java Programming");
                break;
            case 1:
                System.out.printf("One Programming");
                break;
            default:
                System.out.printf("default statement");
        }
    }
}

Error

error: incompatible types: int cannot be converted to String
            case 1:
                 ^
1 error

Default is an optional statement, we cannot define two same case expression inside same switch. break is an also optional statement that are used to terminate execution of switch.

public class SwitchCase{

    public static void main(String[] args){

        int day =  4;

        switch(day){
            //Multiple case statement without break
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                System.out.println("First Week Days");
                break;

            //Multiple case statement without break
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
                System.out.println("Second Week Days");
                break;
            default:
                System.out.println("Other week day");
        }
    }
}

Output

First Week Days

Nested Switch Case statements

When defining of one switch statement inside another switch statement then that is called nested switch. For example.

public class Test{
  public static void main(String[] args) {
  
    int node=10;
    int circuit=14;
    int connection=0;
    switch(node){

      case 5:
         connection=node*2;
         System.out.println("That is minimum node");
      break;
      case 10:
        System.out.println("Total node are 10");
        switch(circuit){

          case 7:
              connection=(node*2)+circuit;
          break;

          case 14:
              connection=node+7+(circuit/3);
          break;

          case 21:
              connection=node*5+(circuit/2);
          break;
          default:
              connection=circuit;
              System.out.println("Default Case");
        }

      break;

      default:
        connection=node;
        System.out.println("No Test not found");
    }
    System.out.println("Total Circuit :"+circuit);
    System.out.println("Total Connection :"+connection);
  }


}
Total node are 10
Total Circuit :14
Total Connection :21


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved