Java Reference
In-Depth Information
Chapter 6
More Conditionals and Loops
6.1
The
switch
Statement
SR 6.1
When a Java program is running, if the expression evaluated for a
switch
statement does not match any of the case values associated
with the statement, execution continues with the
default
case. If no
default
case exists, processing continues with the statement following
the
switch
statement.
SR 6.2
If a case does not end with a
break
statement, processing continues into
the statements of the next case. We usually want to use
break
state-
ments in order to jump to the end of the
switch
.
SR 6.3
If the user enters 72, the output is:
That grade is average
. If the user
enters 46, the output is:
That grade is not passing
. If the user enters
123, the output is:
That grade is not passing
.
SR 6.4
An equivalent
switch
statement is:
switch
(num1)
{
case
5:
myChar =
'
W
'
;
break
;
case
6:
myChar = 'X';
break
;
case
7:
myChar =
'
Y
'
;
break
;
default:
myChar = 'Z';
}
6.2
The Conditional Operator
SR 6.5
The conditional operator is a trinary operator that evaluates a condition
and produces one of two possible results. A conditional statement, such as
the
if
and
switch
statements, is a category of statements that allow condi-
tions to be evaluated and the appropriate statements executed as a result.
Search WWH ::
Custom Search