Switch Case Statement in Java.
Switch case proclamation is utilized when we have number of alternatives (or decisions) and we may have to play out an alternate assignment for every decision.
The linguistic structure of Switch case explanation resembles this –
switch (variable or a whole number articulation)
{
case consistent:
/Java code
case consistent:
/Java code
;
default:
/Java code
;
}
Switch Case proclamation is for the most part utilized with break articulation despite the fact that it is discretionary.
We will initially see a model without break explanation and afterward we will examine switch case with break
A Simple Switch Case Example
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
Clarification: In switch I gave an articulation, you can give variable too. I gave num+2, where num esteem is 2 and after expansion the articulation came about
4. Since there is no case characterized with esteem 4 the default case got executed. This is the reason we should utilize default in switch case, so that if there is no trick that coordinates with the condition, the default block gets executed.
Switch Case Flow Diagram
First the variable, worth or articulation which is given in the switch enclosure is assessed and afterward dependent on the outcome, the relating case block is executed that coordinates with the outcome.
switch case stream outline
Break proclamation in Switch Case
Break proclamation is discretionary in switch case however you would utilize it pretty much every time you manage switch case.
Before we examine about break proclamation, Let's view the model beneath where I am not utilizing the break explanation:
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 ");
}
}
}
Yield:
Case2
Case3
Case4
Default
In the above program, we have passed number worth 2 to the switch, so the control changed to the case 2, anyway we don't have break proclamation after the case 2 that made the stream pass to the resulting cases till the end. The answer for this issue is break explanation
Break explanations are utilized when you need your program-stream to emerge from the switch body.
At whatever point a break articulation is experienced in the switch body, the execution stream would straightforwardly emerge from the switch, disregarding rest of the cases
We should take a similar model however this time with break articulation.
Model with break proclamation
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
Presently you can see that lone case 2 had been executed, rest of the cases were disregarded.
For what reason didn't I utilize break explanation after default?
The control would itself emerge from the switch after default so I didn't utilize it, notwithstanding assuming you actually need to utilize the break after default, you can utilize it, there is no damage in doing that.
Not many focuses about Switch Case
1) Case doesn't generally have to have request 1, 2, 3, etc. It can have any number worth after case catchphrase.
Likewise, case shouldn't be in a climbing request consistently, you can indicate them in any request dependent on the prerequisite.
2) You can likewise utilize characters in switch case. for instance –
public class SwitchCaseExample2 {
public static void main(String args[]){
scorch 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 ");
}
}
}
3) The articulation given inside switch should bring about a consistent worth else it would not be legitimate.
For instance:
Legitimate articulations for switch:
switch(1+2+23)
switch(1*2+3%4)
Invalid switch articulations:
switch(ab+cd)
switch(a+b+c)
4) Nesting of switch proclamations are permitted, which implies you can have switch explanations inside another switch.
Anyway settled switch proclamations ought to be kept away from as it makes program more intricate and less discernible.







0 Comments:
Post a Comment