Search Placementyogi.com
 

switch case in Java

Switch statement is an alternative to if-else ladder in java. It provides us a simple and organized way to execute statements based on a condition.

When the condition increases, the if-else ladder becomes clumsy and untidy,switch control solves this using cases.

Syntax of switch in Java:

switch(expression)
{
		case value1: //statements
					break;
		case value2: //statements
					break;
}

Some Important points to remember :

  1. When a case matches, the corresponding statements get executed until a break is encountered or the end of switch block is reached.
  2. There can be as many cases as you want inside switch block.
  3. The expression can be only byte, short, char, int ,String class and other classes like Byte, Character, Short,Integer
  4. The value in case should be same as the expression in switch.
  5. The default case is optional
  6. The default case handles all the cases  that are not present i.e. if none of the cases match default case is executed!
  7. break statement is very important as without it  switch fall-through will happen i.e. All the cases after the matching case are executed in sequence until a break is found or the end of switch block is encountered. 
  8. The last break is not necessary as the switch block ends anyways.
  9. The default case should always be placed at the end , as any case placed after it can be never executed.

Note:

  1. if-else can test different type of conditions but switch statement can only test expressions based on a single integer, enumerated value, or String object.
  2. Switch is also more efficient than if-else because the compiler creates a jump table for each case under switch. Moreover, since switch only tests for equality and knows the data type of the variable, it runs much faster for a large number of cases.

Example of switch in Java:

class SwitchControl 
{
	public static void main(String[] args) 
	{
		int i=10;
		switch(i)
		{
			case 10 : System.out.println("Case 10!");
					 break;
			case 20 :  System.out.println("Case 20!");
					 break;
		    default :  System.out.println("Default Case");
					 break;
		}
	}
}

Output:

Case 10!

The value of is 10 which matches the first case value and hence the statements in case 10 are executed. "Case 10!" is printed and then break takes the control out of the switch block and the program ends.

Nested Switch in Java

When we place another switch inside the statements of an outer switch it is called Nested Switch.

Syntax of Nested Switch in Java:

switch (variable1)
{
	case value1: //...statements
				 break;
	case value2: 
				switch(variable2)
				{
					case value1: //... statements
								break;
					case value2: //statements
								break;
					default: // statments
								break;
				}
				break;
	default :  //statements
				break;
}

Points to remember:

1. The value1 inside the outer and inner switch are different and do not conflict.

2. The inner switch is executed only when the value of variable1 is equal to value2. Even if the value of variable2 in equal to inner value1 or inner value2, it will not get executed until the value of variable1 == value2

3. The inner break only exits the inner switch.

Example of Nested Switch in Java:

class SwitchControl 
{
	public static void main(String[] args) 
	{
		int i=20,j=30;
		switch(i)
		{
			case 10 : System.out.println("i == 10");
						break;
					 
			case 20 :  
				switch(j)
				{
					case 20: System.out.println("i==20 and j==20");
								break;
					case 30: System.out.println("i==20 and j==30");
								break;
					default: System.out.println("Inner Default Case");
							break;
				}
				break;
		    default :  System.out.println("Outer Default Case");
						break;
		}
	}
}

Output:

i==20 and j==30

Banner to LogicGuns.com

Hide Page Information

Java Switch Case Tutorial with syntax and Example and explanation

Switch case in java is well explained on placementyogi.com along with full explanation, syntax and real world example.