Switch Case
- Eclipse: Oxygen
- Java: 1.8
Switch case statement in java is the conditional statement that executes one statement out of many statements when the condition is matched. Default statement is executed when none of the conditions is matched.
Syntax
1 2 3 4 5 6 7 8 |
switch (Expression) { case value1: statement; case value2: statement; default: default statement executed if no cases are matched; } |
Example
Switch case always works on condition, here condition is int x = 3. When we execute the program, condition matches with case 3, so the output is 3
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void main(String[] args) { int x = 3; switch (x) { case 1: System.out.println ("The value of x is 1"); case 2: System.out.println ("The value of x is 2"); case 3: System.out.println ("The value of x is 3"); default: System.out.println ("The value of x is other than 1, 2, 3"); } } |
Output
The value of x is 3
The value of x is other than 1,2,3
Surprised with the output. So here is a catch that statement after the matched condition will also execute. To get rid of this condition switch case is always used with a break statement.
Break statement
The break statement is used to come out of the program flow. The line at which the program finds the break statement, it comes out of the execution flow and ignores the rest cases. It is optional to use a break statement after the default statement but it is recommended.
Example
With the use of the break statement, the program will come out of flow after the matched case is executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { int x = 3; switch (x) { case 1: System.out.println ("The value of x is 1"); break; case 2: System.out.println ("The value of x is 2"); break; case 3: System.out.println ("The value of x is 3"); break; default: System.out.println ("The value of x is other than 1,2,3"); break; } } |
Output
The value of x is 3
Do you Know?
It is not necessary to write case statements in sequence.
Only int, char, String, and Enum are allowed as switch expressions.
In the following example, we will see the use of characters in the switch case. We write ‘a’ (lower case) as a condition for the switch case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { char x = 'a'; switch (x) { case 'a': System.out.println("The value of x is a"); break; case 'b': System.out.println("The value of x is b"); break; case 'c': System.out.println ("The value of x is c"); break; default: System.out.println ("The value of x is other than a, b, c"); break; } |
Output: The value of x is a.
If you write a condition like char x = ‘A’ (upper case).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { char x = 'a'; switch (x) { case 'a': System.out.println("The value of x is a"); break; case 'b': System.out.println("The value of x is b"); break; case 'c': System.out.println ("The value of x is c"); break; default: System.out.println ("The value of x is other than a, b, c"); break; } |
Output
The value of x is other than a, b, c.
In programming, lower case and upper case characters are evaluated differently. In the below example, we will see how to condition to get rid of such a situation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static void main(String[] args) { char x = 'a'; switch (x) { case 'a': case 'A': System.out.println("The value of x is a"); break; case 'b': case 'B': System.out.println("The value of x is b"); break; case 'c': case 'C': System.out.println("The value of x is c"); break; default: System.out.println("The value of x is other than a, b, c"); break; } } |
Output
The value of x is a.
String as an expression
A String can be used as an expression in a switch statement. This switch statement expression is compared with the expression of each case. This comparison is case sensitive, so we convert all expressions into lower cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static void main(String[] args) { String name = "Team"; switch (name.toLowerCase()) { case "author": System.out.println("Chaand"); break; case "team": System.out.println("Team StudyEasy"); break; case "editor": System.out.println("Shabaz & Salim"); break; default: System.out.println("Invalid Entry"); break; } } |
Contributed by: Poonam Tomar