print
Advertisment
Advertisment

Switch case in java

if there several options are available in if-else then never recommended using a nested if else. because it reduces readability if several option are available highly recommended going for switch statement. The switch statement contains an expression. Due to which there are many related cases, the case which is found in that expression or declare variable. It executes the same case. If no case is found, it runs the default case. You should use break statement in each case, so that case gets terminate execution. If you don't use break then it will execute all your cases.

Note: The allow argument types for the switch statement are byte ,short,char & int until 1.4 version. but from 1.5 version corresponding wrapper classes & enum types also allow & from 1.7 version String type also allow.

Syntax

class Switch{
   public static void main(String[] args) {
      switch(expression){
          case 1:
          statement 1
          break;
          case 2:
          statement 2
          break;
          case N:
          statement N
          break;
      }
   }
}

Example

class switch_program{
    public static void main(String[] args) {
    String color = "Red";
        switch(color){
            case "White":
                System.out.println("White color");
            break;
            case "Red":
                System.out.println("Red color");
            break;
            case "Blue":
                System.out.println("Blue color");
            break;
            case "Green":
                System.out.println("Green color");
            break;
            case "Black":
                System.out.println("Black color");
            break;
            default:
                System.out.println("color no match");
        }
    }
}

Output :

  
    Red color
    

Advertisment

If we do not use the default condition in the switch then:

Note: If we don't use default modifier in switch statement. So we will not get any error but if the value of the declared variable does not match with the switch expression, it will not output anything.It is a optional.

Example

class NoDefault{
    public static void main(String[] args) {
        int Number = 2;
            switch(Number){
            case 1:
            for (int i=1;i<=5;i++ ) {
                System.out.println(i);
            }
            break;
            case 2:
            for (int i=1;i<=10 ;i++ ) {
                System.out.println(i);
            }
            break;
            case 3:
            for (int i=1;i<=15;i++ ) {
                System.out.println(i);
            }
            break;
        }
    }
}

Output :


  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

If there is no use case in switch statement then:

Example

class SwitchWithoutCase{
        public static void main(String[] args) {
        Character c = 'a';
        switch(c){
            System.out.println("Switch without case");
        }
    }
}

Output :

SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
            ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                  ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                   ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                      ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                       ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                              ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                               ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                                                    ^
SwitchWithoutCase.java:7: error: case, default, or '}' expected
            System.out.println("Switch without case");
                                                     ^
9 errors

Advertisment

What happens if we don't use break statement:

If we will not use break statement then all cases present in switch statement will be printed

Example

class Switch_break{
    public static void main(String[] args) {
        Integer num = 2;
        switch(num){
            case 1:
                System.out.println("case 1");
            case 2:
                System.out.println("case 2");
            case 3:
                System.out.println("case 3");
            default:
                System.out.println("default");
        }
    }
}

Output :


  case 1
  case 2
  case 3
  default
Advertisment
Advertisment
arrow_upward