Java Reference
In-Depth Information
}
Rules for switch statement
The switch statement can have a variable of type short,int,byte or char.
A Switch block can have any number of case statements with the following
syntax
Case
value :
any The value specified in case should be of same data type as variable in the
switch statement. Also this value must be any constant or literal.
When the variable of switch statement is matched with nay case value then the
statements following that case will be executed until Break statement is reached.
As the execurion reaches to a
break
statement, the switch terminates, and the
execution is transferred to the next line following the switch block.
Putting Break in each case is not necessary. In case no Break appears, the
execution falls through the subsequent given cases until a Break is executed or the
switch terminates.
There may be an optional default case in a switch at the end of switch. Default
case is executed when value of a variable is unmatched with all given cases.
Example:
public class Test1 {
public static void main(String args[]){
char letter = 'S';
switch(letter)