Java Reference
In-Depth Information
the evaluation of a logical expression, is called a switch structure. Java's switch structure
gives the computer the power to choose from many alternatives.
The general syntax of a switch statement is:
switch (expression)
{
case value1:
statements1
break ;
4
case value2:
statements2
break ;
.
.
.
case valuen:
statementsn
break ;
default :
statements
}
In Java, switch , case , break , and default are reserved words. In a switch structure,
the expression is evaluated first. The value of the expression is then used to perform
the actions specified in the statements that follow the reserved word case . (Recall that, in
a syntax template, the shading indicates an optional part of the definition.)
Although it need not be, the expression is usually an identifier. Whether it is an identifier
or an expression, thevalueoftheidentifierortheexpression can only be of type int , byte , short ,
or char .The expression is sometimes called the selector. Its value determines which
statements are selected for execution. A particular case valuemustappearonlyonce.Oneor
more statements may follow a case label, so you do not need to usebracestoturnmultiple
statements into a single compound statement. The break statement may or may not appear
after each statements1 , statements2 , ... , statementsn .A switch structure may or
may not have the default label. Figure 4-5 shows the flow of execution of a switch
statement.
 
Search WWH ::




Custom Search