SWITCH CASE STATEMENT

Switch Case Statement:

  • Switch statement executes one statement from multiple conditions.
  • It is like if-else-if ladder.
  • The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long (Datatype learn later).

Syntax:

switch(expression)  
{

case value1 :
// Statements
break; 
   
case value2 :
// Statements
break; 
   

default : 
// Statements
}

  • break is optional.
  • We can have any number of case statements
  • Last one is default statement, it is used when none of the cases is true.
  • If we write the default in end of the switch block, break is not needed in the default case.
  • Case value & Switch Variable both are must be same data type.
  • Case value must be constant. Variables are not allowed.
    • int a = 5;
    • case a; —>It’s wrong.
    • case 5; —> It’s right.

Ex 1:

public class Practice1 {
	public static void main(String args[]) {

		int num = 2;
		switch (num + 2) {
		case 1:
			System.out.println("Case1: Value is " + num);
		case 2:
			System.out.println("Case2: Value is " + num);
		case 3:
			System.out.println("Case3: Value is " + num);
		default:
			System.out.println("Default: Value is " + num);
		}
	}
}

Output:

Default: Value is 2

Code Explanation:

  • Here, num is 2.
  • In switch expression, we give num+2. num result is 4.
  • There is cases for 1, 2, 3. There is no case for 4.
  • Default block gets executed.

Ex 2: Without Break Statement

package tamilnadu.chennai;

public class Practice1 {
	public static void main(String args[]) {
		int i = 2;
		
		switch (i) {
		case 1:
			System.out.println("Case1");
		case 2:
			System.out.println("Case2");
		case 3:
			System.out.println("Case3");
		case 4:
			System.out.println("Case4");
		default:
			System.out.println("Default");
		}
	}
}

Output:

Case2
Case3
Case4
Default

Code Explanation:

  • Here, i = 2.
  • In switch statement, we give i. So case 2 gets executed.
  • In the case 2, we don’t have break statement.
  • So, the flow of the program gets executed till the default block.

Ex 3: with BREAK statement

public class Practice1 {
	public static void main(String args[]) {
		int i = 2;
		
		switch (i) {
		case 1:
			System.out.println("Case1");
			break;
		case 2:
			System.out.println("Case2");
			break;
		case 3:
			System.out.println("Case3");
			break;
		case 4:
			System.out.println("Case4");
			break;
		default:
			System.out.println("Default");
		}
	}
}

Output:

Case2

Code Explanation:

  • Here, i = 2.
  • In switch statement, we give i. So case 2 gets executed.
  • In case2, break statement is executed.
  • When this case 2 statements are executed, after that break statement terminates the switch case statements.

NOTE:

  • Case doesn’t need to be in an ascending order always, we can specify them in any order based on the requirement.
  • We can also use characters in switch case.

Ex 4:

public class Practice1 {
	public static void main(String args[]) {
		char ch = 'b';
		switch (ch) {
		case 'd':
			System.out.println("Good Morning");
			break;
		case 'b':
			System.out.println("Good Afternoon");
			break;
		case 'x':
			System.out.println("Good Evening");
			break;
		case 'y':
			System.out.println("Good Night");
			break;
		default:
			System.out.println("Default");
		}
	}
}

Output:

Good Afternoon


Ex 5:

public class Practice1 {
	public static void main(String[] args) {
		int a = 80;

		switch (a) {
		case 10:
		case 20:
		case 30:
			System.out.println("Small Numbers");
			break;

		case 70:
		case 80:
		case 90:
		case 100:
			System.out.println("Big numbers");
			break;

		default:
			System.out.println("Zero");
		}
	}
}

Output:

Big numbers


NOTE:

  • Duplicate case values are not allowed.
  • Case Value & Switch variable have same data type.
  • Case value must be constant. Not variable.
  • Break statement is optional.
  • Default statement is optional. We can mention anywhere in inside the switch block.
  • In case, default is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.
  • Nesting of switch statements are allowed, which means we can have switch statements inside another switch. Nested switch statements should be avoided as it makes program more complex and less readable.

Leave a comment

Design a site like this with WordPress.com
Get started