Switch

The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.

Syntax:

switch (expression)
{
case value1:
//statement sequence 
break; 
case value2: 
//statement sequence 
break; 
... 
case valueN: 
//statement sequence 
break;
default:
//default statement sequence
}

Rules:

  1. The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression.

  2. Each case value must be a unique literal (that is, it must be a constant, not a variable). Duplicate case values are not allowed.

Working:

  1. The value of the expression is compared with each of the literal values in the case statements.

  2. If a match is found, the code sequence following that case statement is executed.

  3. If none of the constants matches the value of the expression, then the default statement is executed.

  4. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.

Note:

The break statement is used inside the switch to terminate a statement sequence.

When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of “jumping out” of the switch.Ex:

Example 1:

package com.test.test1;
public class TestBreak {
	public static void main(String[] args) {
		int marks = 20;
		switch (marks) {
		case 10:
			System.out.println(10);
			break;
		case 20:
			System.out.println(20);
			break;
		case 30:
			System.out.println(30);
		default:
			System.out.println("default");
			break;
		}

	}
}
Output: 20

Compare if-else-if with switch:

If-else:

package com.test.test1;

public class TestIFELSE{
	
	public static void main(String[] args) {
		
		String day = "SUN";
		
		if (day == "MON" || day == "TUS" || day == "WED" || day == "THU") {
			
			System.out.println("Working days");
			
		} else if (day == "FRI") {
			
			System.out.println("Weekend is about to come");
			
		} else if (day == "SAT" || day == "SUN") {
			
				System.out.println("Wow its weekend");
			
		} else {
			
			System.out.println("Invalid day");
		}

	}
}

Same code with switch:

package com.test.test1;

public class TestSwitch{

	public static void main(String[] args) {

		String day = "SUN";

		switch (day) {
		
		case "MON":
		case "TUE":
		case "WED":
		case "THU":
			System.out.println("Working days");
			break;
		case "FRI":
			System.out.println("Weekend is about to come");
			break;
		case "SAT":
		case "SUN":
			System.out.println("Wow its weekend");
			break;
		default:
			System.out.println("Invalid Day");

		}

	}
}

Note:

case statement cannot contain null,floating point number.
But it can contain compile time constants like:

final int a=2;
final int b=3;

case a+b:

What will happen if break is removed ?

Examine and find the output of the below program:

package com.test.test1;

public class TestBreak {

	public static void main(String[] args) {

		String day = "MON";

		switch (day) {
		
		case "MON":
		case "TUE":
		case "WED":
		case "THU":
			System.out.println("Working days");
		
		case "FRI":
			System.out.println("Weekend is about to come");
		
		case "SAT":
		case "SUN":
			System.out.println("Wow its weekend");
		
		default:
			System.out.println("Invalid Day");

		}

	}
}

Summary:

There are three important features of the switch statement to note:

  • The switch differs from the 'if', that switch can only test for equality, whereas if can evaluate any type of Boolean expression. That is, the switch looks only for a match between the value of the expression and one of its case constants.

  • No two case constants in the same switch can have identical values. Of course, a switch statement and an enclosing outer switch can have case constants in common.

  • A switch statement is usually more efficient than a set of nested ifs.

You can use a switch statement to compare the value of a variable with multiple values.For each of these values, you can define a set of statements to execute.

Last updated