Switch Case in Java

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

switch(variable){

 	case value-1 :
 		//Statements
 		break; 
 		
 	case value-2 :
 		//Statements
 		break;
 		
 	case value-3 :
 		//Statements
 		
 		break; 
 		//If no case match the default
 	default : 
 		//Statements
 		break;
} 

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.

	switch(variable){
	
 	case value-1 :
 		
 		//Write Statements
 		//you can write many statements
 		//You can use if else
 		//you can call methods e.g.
 		// xyz(); etc. 		
 		
 		break; 
 		
 	
 	default : 
 		//Statements
 		break;
}

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”

public class Sample {

	public static void main(String[] args) {

		int var = 2;
		switch (var) {

		case 1:
			System.out.println("Case -1 block");
			break;

		case 2:
			System.out.println("Case -2 block");
			break;

		case 4:
			System.out.println("Case -4 block");

			break;
		// If no case match the default
		default:
			System.out.println("Default block");
			break;
		}

	}
}

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.

DOUBTS ON SWITCH CASE

Doubt-1:

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

public class Sample {

	public static void main(String[] args) {

		int var = 1;
		
		switch (var) {

		case 1:
			System.out.println("Case -1 block");	

		case 2:
			System.out.println("Case -2 block");
			break;
		
		
		default:
			System.out.println("Default block");
			break;
		}

	}
}

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

public class Sample {

	public static void main(String[] args) {

		int var = 2;

		switch (var) {

		case 1:
			System.out.println("Case -1 block");			
		case 2:
			System.out.println("Case -2 block");			
			
		case 3:
			System.out.println("Case -3 block");			
			
		default:
			System.out.println("Default block");
					
		
		}

	}
}

Doubt-3:

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

for example,

	switch (var) {

	case 1:
		System.out.println("Case -1 block");
		break;
		
	default:
		System.out.println("Default block");
		break;
	}

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.

public class Sample {

	public static void main(String[] args) {

		int var = 1;

		switch (var) {

		case 1:
			System.out.println("Case -1 block");
			break;
			
		default:
			System.out.println("Default block");
			break;
			
		case 2:
			System.out.println("Case -2 block");
			break;
		}

	}
}

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

public class Sample {

	public static void main(String[] args) {

		int var = 1;

		switch (var) {

		default:
			System.out.println("Default block");
			break;
		case 1:
			System.out.println("Case -1 block");
			break;
			
		
		}

	}
}

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?
public class Sample {

	public static void main(String[] args) {

		int day = 1;

		switch (day) {

		case 1:
			System.out.println("Monday");

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

		case 4:
			System.out.println("Thursday");
			break;

		default:
			System.out.println("Sunday");
			break;

		}// switch

	}

}

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?
public class Sample {

	public static void main(String[] args) {

		int fruitId = 5;

		switch (fruitId) {

		case 1:
			System.out.println("Apple");

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

		case 4:
			System.out.println("Banana");			

		default:
			System.out.println("No Fruits");
			break;

		}// switch

	}

}

Solution:
1 – output:
Apple

2 – output:
Orange
Banana
No Fruits

3- Output:
No Fruits

Related Posts