Control Statements of Java -from The Complete Reference Java
Java'a program control statements can be put into three categories:
1.selection statements : if and switch
the if-else-if ladder
Nested switch statements
//default statement sequence
The expression must be of type byte,short,int or char, each of the values specified in the case statements
must be of a type compatible with the expression. Each case value must be a unique literal( that is, it must
be a constant, not a variable). Duplicate case values are not allowed.
The switch statement works like this: the value of expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If
none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.
The break statement is used inside the switch to terminate a statement sequence. This has the effect of "junping out" of the switch.
Java complier works:
When it complies a switch statement, the Java complier will inspect each of the case constants and create a
"jump table" that it will use for selecting the path of execution depending on the value of the expression. Therefore, if you need to select among a large group of values, a switch statement will run much faster than the equivalent logic coded using a sequence of if-elses. The complier can do this bacause it knows that the case constants are all the same type and simply must be compared for equality with switch expression. The complier has no such knowledge of a long list of if expression.