In programming languages switch is a type of conditional control statement. It has one switch statement and many cases with one default statement. Depending upon the switch value it matches with the cases value and execute the matching cases and if any break statement is used with the case statement then control will be out of the switch block and if there is no break statement then it will execute rest all of the cases as well. If non of the cases matches then it will execute the default block . Use of break in switch is optional.
Syntax for switch statement is
switch ( value ) {
case < val > : statements ;
case < val > : statement ;
..............
default : statement ;
}
The type of the value / variable specified as the argument of the switch should be of int type or its compatible type int , byte , short , char but should not be wider type of int type like long.
String cannot be used with switch but from java 7 we can be able to string value as well and from java 5 we can be able to use enum value as well.
Note : Be careful that no two cases defined in a switch block is same in any way.
for ex -
int x = 65 ;
switch ( x ) {
case 65 : System.out.println (" 65 ") ;
case 'A': System.out.println (" A " ) ;
default: System.out.println ("default block ") ;
In the above code both 65 and A represent the same value i.e 65 so it will give a compilation error saying " duplicate case label " .
Note: The type of the case value should be assignment compatible with the switch value. All the cases value must be in rage of the variable used switch variable.
for ex-
byte x = 65 ;
switch ( x ) {
case 65 : System.out.println (" 65 ") ;
case 129: System.out.println (" Not in range of byte " ) ;
default: System.out.println ("default block ") ;
In the above code switch variable is of type byte and one of the case value is 129 which is not in range of byte value. So it will give compilation error saying " possible loss of precision " .
Note : We can use case a variable as a case value but it should be a constant .
for ex-
byte y = 65 ;
final int x = 65;
switch ( x ) {
case 65 : System.out.println (" 65 ") ;
default: System.out.println ("default block ") ;
Here in the above code there are two variable holding same value but y is non-constant used as switch value and x is a constant used in the case value. In java in case of constants, compiler will place the direct value instead of using variable.
If we use any variable containing non-constant value and we used as a case value then it will give compilation error saying " constant expression required " .
Important points regarding switch statement
1> Default statesmen is optional to use but if used then there should be only one default statement. And it can be used any where in the switch block i.e between the cases, before all the cases or it may be after all the cases.
2> JVM will first search for the matching case value and if found any such case then execution will start form that point only and if break is used in that case then after executing statements control will be transferred out of the switch statement.
3> If no matching case found and if default statement available then execution will start form that default block.
4> For terminating the execution of case statements at any point while executing matched case statements use break statement.
EmoticonEmoticon