1. When there are too many if statements in the program,It difficult to understand the program.
  2. Use witch statement as an alternative.It provides more clarity and better readability.
  3. Use if, when conditions are involved,use switch when selection is involved.
  4. Every case can be replaced by if.

Syntax:

switch(variable){
 case value1:{
      statement1;
      statement2;
 }
 case value2:{
    statement3;
    statement4;
 } 
 default:{
    statement 5;
    statement 6;
}
  1. Here depends on the value of variable,particular task(statements) will be executed.
  2. If the value of variable is equal to value1,Then statement 1,statement 2 will be executed.
  3. If the value of variable is equal to value2,Then statement 3,statement 4 will be executed.
  4. If the variable value is not equal to value 1 and value 2, then none of the statements will be executed,in that case default value is executed that is statement 5 and statement 6 are executed.

Example:

switch(a+b*c) {
 case 7:
 case -25:
 case 0:{
    statement1;
    statement2;
    statement3;
    break;
 }
 case -6:
 case -15:{
    statement 4;
    statement 5;
    statement 6;
    break;
 }
 default:{
    statement 7;
    statement 8;
   statement 9;
   break;
 }
}
statement 10;
Expression evaluated(a+b*c) statements executed
7(or),-25,(or),0 1,2,3,10
-6(or),-15 4,5,6,10
None of the above 7,8,9,10

break statement

  1. When break statement is executed control goes out of switch.
  2. Statements following break are skipped.Next statement is executed after break is, statement outside the switch.
  3. break is used to suspend sequential execution.
  4. if break is omitted,Statements are in the next case are executed which is not desirable.

Example:

class Test {

	public static void main(String[] args) {
		int day = 1;
		switch (day) {
		case 0: {
			System.out.println("Sunday");
			break;
		}
		case 1: {
			System.out.println("Monday");
			break;
		}
		case 2: {
			System.out.println("Tuesday");
			break;
		}
		case 3: {
			System.out.println("Wednesday");
			break;
		}
		case 4: {
			System.out.println("Thursday");
			break;
		}
		case 5: {
			System.out.println("Friday");
			break;
		}
		case 6: {
			System.out.println("Saturday");
			break;
		}
		default: {
			System.out.println("Invalid Day");
			break;
		}
	  }
	}
}
/*
Output:
Monday
*/
  1. Curly braces are optional
    switch(x){
      case 10 :
         block 1;
      case 20 :
         block 2;
      case 30:
         block 3;
      default :
         default block;
    }
    
    
  2. Both case and default are optional.
    int x=10;
    switch(x){
    
    }
    
  3. Within switch ,every statement should be under some case or default.We are not allowed to place individual statements.
    int x=10;
    switch(x){
      System.out.println("hello");
    }
    /*
    Compile time error
    */
    
  4. The valid argument types to the switch statements are

    switch(10.5){
    }
    /*
    compile time error
    */
    
    byte     Byte
    short  + Short       + enum
    char     Character     (1.5v)
    int      Integer 
    (1.4v)   (1.5v)</li>
    
  5. Case labels should be constant expressions otherwise compile time error.
    int x=10;
    int y=20;
    switch(x){
     case 10:
      System.out.println("10");
      break;
     case y:
      System.out.println("20");
      break;
    /*
    Compile Time error:constant expression required
    If we declare y as a final there is no compile time error
    */
    
  6. All the case labels should be in the range of switch argument type.
    byte x=10;
    switch(x){
     case 10:
      System.out.println("10");
      break;
     case 100:
     case 1000:
      System.out.println("1000");
      break;
    Compile time error:Possible loss of precision
    found:int
    required:byte
    

Fall-through inside a switch:

  1. Default case is need not be last case(statement).We can take any where with in switch.But it is conversion to take default as last case.
  2. Inside a switch,once any case is matched,from that statement on wards all the remaining statements will execute until some break statement (or) end of the switch.This is called “fall-through” inside a switch statement.

Example:1

class Test {

	public static void main(String[] args) {
		switch (x) {
		case 0:
			System.out.println("0");
		case 1:
			System.out.println("1");
			break;
		case 2:
			System.out.println("2");
		default:
			System.out.println("default");
		}
	}
}

Output:

X value Output
0 0,1
1 1
2 2,default
3 default

Example:2

class Test {

	public static void main(String[] args) {
		switch (x) {
		default:
			System.out.println("default");
		case 0:
			System.out.println("0");

		case 1:
			System.out.println("1");
			break;
		case 2:
			System.out.println("2");

		}
	}
}

Output:

X value Output
0 0,1
1 1
2 2
3 default,0,1