Java Reference
In-Depth Information
10.3. switch
A switch statement allows you to transfer control to a labeled entry point
in a block of statements, based on the value of an expression. The gen-
eral form of a switch statement is:
switch ( expression ) {
case n: statements
case m: statements
. . .
default: statements
}
The expression must either be of an integer type ( char , byte , short , or
int , or a corresponding wrapper class) or an enum type. The body of the
switch statement, known as the switch block, contains statements that
can be prefixed with case labels. A case label is an integer or enum con-
stant. If the value of the switch expression matches the value of a case
label then control is transferred to the first statement following that la-
bel. If a matching case label is not found, control is transferred to the first
statement following a default label. If there is no default label, the entire
switch statement is skipped.
For example, consider our Verbose interface from page 121 :
interface Verbose {
int SILENT = 0;
int TERSE = 1;
int NORMAL = 2;
int VERBOSE = 3;
void setVerbosity(int level);
int getVerbosity();
}
 
Search WWH ::




Custom Search