Switch Case in Java

(Last Updated On: February 25, 2019)

Learn switch case in java language with simple explanation, doubts, code example and exercises.

Switch case statement in java is also conditional statement. In switch condition e.g. switch(value), the value of variable is matched to case written in it body. Whatever case matches the value of variable, that block get executed and exit from the switch case block on break statement.

Syntax of Switch Case in Java

 

Focus-1:

Notice the colon : symbol after case value e.g. case value-2 : to start the block of a case.

Focus-2:
In a case block, you can write many statements e.g. using if else conditions, calling methods etc. Example below.

 

Code Example of Switch Case in Java

In below switch case code example, value of variable var is 2. So, control will go directly to case 2: and executes its block and exit the entire switch case block on break statement. So, output of the program will be “Case -2 block”

 

If no value is matched with any case e.g. if value of variable var you set to 5. there is no case 5, so control will execute default block.

Doubt-1:

What if I remove break from case 1, as given below in code?

 

Answer: If we remove break from case 1 and if supply value of var to 1, then both case 1 and case 2 will execute.

Here is the output:

Case -1 block
Case -2 block

If you supply value of var to 2, then only case 2 will execute.

Doubt-2:

What if I remove all breaks from switch case block?
Answer:

If you remove all breaks, compiler will match the case and starting from the matching case, all the cases below it will execute.

For example, in below program value of var inside switch is 2. So, compiler will match the case 2 and from there all cases will execute.

Output:

Case -2 block
Case -3 block
Default block

 

Doubt-3:

DOUBT: Is it necessary to use break statement after last case e.g. default, in switch case block?

for example,

 

Answer: No, technically, using break after in default case is not necessary and this is optional.

However, it is good to use break in default to maintain consistency. Meaning, use break, for all cases in switch. It avoids the accidental mistake.

For example, Sometime programmer, writes another case after default, when they update the switch case.

Code example:
Writing case after default. So, good to use break in default.

Secondly, sometimes, people write it as a first case, at time it is necessary to use break.
Code example:

 

 

Exercises on Switch Case in Java

Exercise-1: Copy the below code in your eclipse editor.

  1. Change value of day to 2. What is output?
  2. Change value of day to 4. What is output?
  3. Change value of day to 5. What is output?

 

Output:
1 – Tuesday
2 – Thursday
3 – Sunday

 

Exercise-2:

In below code example,

  1. Modify the value of fruitId to 1. What is output?
  2. Modify the value of fruitId to 2. What is output?
  3. Modify the value of fruitId to 6. What is output?

 

Solution:
1 – output:
Apple

2 – output:
Orange
Banana
No Fruits

3- Output:
No Fruits