Switch Case Statement in Java
Switch case statements in java are used when we have multiple options and we need to do a different function for each option. The syntax of the switch case statement looks like this –
switch (variable or an integer expression)
{
case constant:
//Java code;
case constant:
//Java code;
default:
//Java code;
}
Switch case statements are mostly used with break statements. Here we will first see an example of switch case statements without a break statement and then an example of switch case statements with a break statement.
Example of Switch Case Statement in Java without break statement
public class SwitchCaseExample1 {
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
the explanation – Here an expression has been given in the switch, a variable can also be taken here. In the above example, expression, num + 2 is taken, where num’s value is 2, expression, num + 2 will return a result of 4 after executing. No case is defined with a stipulated value (value) 4. That is why the default case was executed. This is why we should use default in the switch case, because if no case matches then the default block gets executed.
Operators In Java: What Are Operators In Java?
Switch Case Flow Diagram: Switch Case Statement in Java
The variable, value or expression written in the switch field is first evaluated and the case block corresponding to it is executed based on the result.
Break statements in switch case
The break statement is optional in the switch case but is used almost every time in the switch case. Before discussing about break statement, let us have a look at the example below where break statement is not used.
public class SwitchCaseExample2 {
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
In the above program we passed integer value 2 to switch, so control went to case 2, however after case 2 there is no break statement in the program which causes the program to go from case 2 to end. The break statement is used to troubleshoot this problem.
Now, let us see the above example with the use of break statement –
public class SwitchCaseExample2 {
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
Now you can see that only case 2 was executed and the remaining cases were ignored.
Why was the break statement not used after the default here?
After default the control automatically comes out of the switch, so here break is not used after default, however if you want, you can use break after default.
Some key points related to the switch case
- It is not always necessary to write the case in order 1,2,3 ……, the case keyword can be followed by any integer value, plus the case does not always need to be written in ascending order, you They can be specified in any order depending on the need.
- You can also use characters in the switch case.
for example
public class SwitchCaseExample2 {
public static void main(String args[]){
char ch="b";
switch(ch)
{
case 'd':
System.out.println("Case1 ");
break;
case 'b':
System.out.println("Case2 ");
break;
case 'x':
System.out.println("Case3 ");
break;
case 'y':
System.out.println("Case4 ");
break;
default:
System.out.println("Default ");
}
}
}
- The value inside the given expression inside the switch must be constant, otherwise it will not be valid.
valid expression for switch: –
switch (1 + 2 + 23)
switch (1 * 2 + 3% 4)
invalid expression for switch: –
switch (ab + cd)
switch (a + b + c)
- Nesting switch statements are allowed in Java which means that you can use any other switch statement inside a switch, although using nested switch statements in the program should be avoided as this makes the program complex and less readable ( less readable).
Always keep in mind that every program created using the switch case can also be created by the if else statement, but not every program created by the if else statement can be created by the switch case statement because in the switch case statement The condition can never be used.