Java Switch Block

In this tutorial, we will learn about switch block of java, syntax, and a working example of it along with importance of break and default keywords in switch.

Switch Block

If a programmer needs to evaluate three or more conditions with alternative expressions to decide the outcome, then it requires to write three or more else if conditions, instead of writing many else - if conditions one switch block allows to evaluate conditions with alternative expressions.

Switch block is alternative to the else if ladder and it is more efficient, clean code and readability is easy as compared to else if statements.

Syntax of switch:

switch(expression) {
case expressionValue1:
           // block
 break;
case expressionValue2:
           // block
 break;
case expressionValue3:
           // block
 break;
default:
// block
}

Switch Block example with break statement:

CopiedCopy Code
public class SwitchBlockExample {
	public static void main(String[] args) {
		int value = 4;
		switch (value) {
		case 1:
			System.out.println("Case 1 executed");
			break;
		case 2:
			System.out.println("Case 2 executed");
			break;
		case 3:
			System.out.println("Case 3 executed");
			break;
		case 4:
			System.out.println("Case 4 executed");
			break;
		default:
			System.out.println("Default block is executed, no condition satisfied");
		}
	}
}

Output:

Block 4 executed

Above line gets printed when we execute the example, the following is explanation for the flow of switch block.

  1. We have declared the value variable with value 4.
  2. Passed the value variable to switch block.
  3. Only case 4 satisfies the condition as it contains value as 4.
  4. And the block inside it gets executed and prints line "Block 4 executed"

If we check example of switch block, every case block contains break statement at last, we have a default statement, small explanation of break statement. break statement is a keyword in java which tells to the JVM to break the execution of a switch block or a loop once it encounters during its execution process. In general terms break will stop the execution of a switch block or a loop associated with it and continues to execute the statements outside of the switch block or loop.

Note: break statement cannot be used outside of loop or switch block. In case break statement is missed in switch block cause to execute the remaining blocks including default block also.

default is a keyword in java, in switch block, it implies that none of the condition or expression satisfied then default block gets executed.

Switch Block example without break statement:

In this example, we will have switch block without a break statement and will see the output of the example.

CopiedCopy Code
public class SwitchBlockExample {
	public static void main(String[] args) {
		int value = 4;
		switch (value) {
		case 1:
			System.out.println("Case 1 executed");
			break;
		case 2:
			System.out.println("Case 2 executed");
			break;
		case 3:
			System.out.println("Case 3 executed");
			break;
		case 4:
			System.out.println("Case 4 executed");
		default:
			System.out.println("Default block also executed");
		}
	}
}

Output:

Case 4 executed
Default block also executed

The above output gets generated when we run the example 2, if we see in the case 4 block does not contains the break statement which caused to execute subsequent blocks, as we do not have sub sequent blocks only default block is present and it is executed as well.

In few business use cases it might require to execute switch block without break statements also. Such an example of it is when a student passed 10th standard it means he did pass 9th class, this case is just an example there might be other scenarios requires this type of execution flow.

Conclusion:

In this tutorial, we have learned about the use case of switch with working examples, break and default keywords. How to use break and default keywords in the switch block.