0.2
switch
StatementsHere is what a switch statement looks like:
switch ( expression ) { case label1: statementList1 break; case label2: statementList2 break; case label3: statementList3 break; . . . other cases like the above default: defaultStatementList }
Here is how it works:
switch
statement.expression
determines which case is selected.expression
must evaluate to byte, short, char, or int primitive data,
a String
, or a few other types not discussed further here.expression
and the type of the label
s must agree.label
must be an integer literal (like 0, 23, or 'A'), a String
literal (like "green"), but not
an expression or variable.statementList
.statementList
is usually followed with break;
switch
statement is executed, the following happens:
expression
is evaluated.label
s after each case
are inspected one by one,
starting with the first.statementList
execute.break
statement is encountered.switch
statement is complete.expression
, then
the default
case is picked, and its statements execute.
Could the type of the expression
be a float
or a double
?