Java Switch Case Conditional Statement With Example

Java Switch Case Statement

Java switch case statement contains many test conditions in different cases. If it finds the exact match of the test condition, it will execute the statement. The switch case statement also contains the statementbreak and statementdefault which are optional to include in the switch case statement.

Below is the syntax of the switch case statement in Java. Switch statement java code follow the same syntax as given below.

Syntax

There are multiple numbers of test conditions in switch statement java and you can add as many conditions as possible. It checks all the cases to execute the match and execute. However, the default case will only execute if all the cases not matched with the given condition.

Java Switch Case Statement Examples

The java switch case statementexample given below contains many cases to test. The example also displays the output when you do not use the statementbreak with the cases and the output.

Let’s see each java switch statement example and analyze the output of each example to find out the difference.

Java Switch Case Statement Example Without Break

The below example does not contain any break statement with the cases. You can use the break statement optionally to each case. However, I recommend to always use the break statement with each case to remove any resulting error in the output.

See the below example for switch statement java and the output which gives the result when you do not use the break statement.

Example 1:

Output

case2:The student is Excellent in the study.
case3:The student is Excellent in the study.
case4:The student is Excellent in the study.
default:The student is Excellent in the study.

There is a string variable in the example that matches with each case. If you do not put the break statement with the cases of switch case statement in java. If the value matches with one case, the rest other cases below the matched case will also get executed and prints in the output.

In the above example, the value matches with the second case. However, the output executes the second case as well as the other cases below it including the default case. This is the reason to always use the break statement with all the cases to execute the exact result statement.

Java Switch Case Statement Example With Break

The below example contains the break statement with each case. If the value matched with any case, the output will execute only the exact case and you will get the exact output that you want to gets executed.

The example also contains the default statement. However, the default statement will only get executed when the value does not match with any given cases.

Example 2:

Output

case2:The student is Excellent in the study.

The above example executes the second case which matched with the value.

Difference between Java if else and Java switch conditional statement

Java if statement evaluates the condition and checks if the result is true and if the result is true it executes the code. But in the switch statement, you match the result with some predefined values arranged sequentially in the different cases.

You also need to put the break statement with each case to work after each case. If you do not put the break statement with each case, java parses the next case and execute each case including the default case. However, Java if statement does not have any break statement.

If you are a beginner in Java then always include a break statement after each case to remove any confusion with the switch case statement result.

Switch statement also contains a default statement which only executes when there is no condition match with the case statements. This is similar to the else statement of the if condition. But one thing you should notice here that if you do not put the break work after each case, switch statement also executes the default with the matched case you will get the two code execution after switch statement. So, always put break work after each case.

Reference
Oracle switch case doc