Switch Statement
If several options are there or available, highly recommended to use switch or go for switch.
(Because in case of if, the readability of code goes down)
Syntax: - switch (x)
{
case 1:
Action 1
break;
case 2:
Action 2
break;
case n:
Action n
break;
default:
default Action
}
Rule 1-
Qu- What are the arguments to the switch?
Ans-
Not allowed argument Allowed datatypes
Boolean 1.4v 1.5v 1.7v
byte Byte
long
short Short String
float char Character
double int Integer +
enum
Rule 2-
switch (x)
{
}
Note: - Curly braces are mandatory. Except switch, everywhere curly braces are optional.
Rule 3-
switch (x)
{
case 1:
System.out.println(1);
break;
default:
System.out.println(“def”);
}
int x = 10;
switch (x)
{
}
Note: - Both case and default are optional i.e., an empty switch statement is a valid java syntax.
Rule 4-
int x = 10;
switch (x)
{
System.out.println(“Alright”);
}
Compile Time Error- (case, default or …. expect) 9 errors
Note: - Inside switch every statement should be under some case are default i.e., independent
statement is not allowed otherwise we will get compile time error.
Rule 5-
int x = 10;
int y = 10;
switch (x)
{
case 10:
System.out.println(10);
break;
case y:
System.out.println(20);
break;
}
Compile Time Error- constant expression required
Note: - Every case label should be compiled time constant. (i.e., constant expression)
Rule 6-
int x = 10;
switch (x+1) // x+1 is also integer
{
case 10:
System.out.println(10);
break;
case 10+20+30:
System.out.println(60);
}
Note: - Both switch argument and case label can be expressions but case label should be constant
expression.
Rule 7-
byte b = 10;
switch (b)
{
case 10:
System.out.println(10);
break;
case 100:
System.out.println(100);
break;
case 1000:
System.out.println(1000);
}
Compile Time Error- possible loss of precision found int required byte.
byte range- (-128 to 127)
byte b = 10;
switch (b+1) // b+1 will behave like integer
{
case 10:
System.out.println(10);
break;
case 100:
System.out.println(100);
break;
case 1000:
System.out.println(1000);
}
// this program will compile.
Note: - Every case label should be in the range of switch argument type otherwise we will get
compile time error.
Rule 8-
int x = 10;
switch (x)
{
case 97:
System.out.println(97);
break;
case 98:
System.out.println(98);
break;
case 99:
System.out.println(99);
break;
case ‘a’:
System.out.println(a);
}
Compile Time Error- Duplicate case label
Case Label-
1. It should be constant expression.
2. The value should be in the range of switch argument type.
3. Duplicate case labels are not allowed.
Rule 9- fall through inside switch
switch (x)
{
case 0:
System.out.println(0);
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2);
break;
default:
System.out.println(“def”);
}
// if x = 0 then output = 0 1 (vertically)
// if x = 1 then output = 1
// if x = 2 then output = 2 def (vertically)
// if x = 3 then output = def
Note: -
Within the switch if any case is matched from that case onwards all statements will be
executed until break are end of the switch, this is called fall through inside switch.
The main advantage of fall through inside a switch is we can define common action for
multiple cases (code reusability).
Conclusion: -
Within the switch, we can take default case at most once.
default case will be executed, if and only if there is no case matched.
Within the switch we can write default case any where but it is recommended to write at
last case.