Java Reference
In-Depth Information
Section 5.6
switch
Multiple-Selection Statement
•The
switch
statement (p. 165) performs different actions based on the possible values of a constant
integral expression (a constant value of type
byte
,
short
,
int
or
char
, but not
long
), or a
String
.
• The end-of-file indicator is a system-dependent keystroke combination that terminates user in-
put. On UNIX/Linux/Mac OS X systems, end-of-file is entered by typing the sequence
<Ctrl>
d
on a line by itself. This notation means to simultaneously press both the
Ctrl
key and the
d
key.
On Windows systems, enter end-of-file by typing
<Ctrl> z.
•
Scanner
method
hasNext
(p. 168) determines whether there's more data to input. This method
returns the
boolean
value
true
if there's more data; otherwise, it returns
false
. As long as the
end-of-file indicator has not been typed, method
hasNext
will return
true
.
•The
switch
statement consists of a block that contains a sequence of
case
labels (p. 168) and an
optional
default
case (p. 168).
•In a
switch
, the program evaluates the controlling expression and compares its value with each
case
label. If a match occurs, the program executes the statements for that
case
.
• Listing cases consecutively with no statements between them enables the cases to perform the
same set of statements.
• Every value you wish to test in a
switch
must be listed in a separate
case
label.
•Each
case
can have multiple statements, and these need not be placed in braces.
•A
case
's statements typically end with a
break
statement (p. 168) that terminates the
switch
's
execution.
•Without
break
statements, each time a match occurs in the
switch
, the statements for that case
and subsequent cases execute until a
break
statement or the end of the
switch
is encountered.
• If no match occurs between the controlling expression's value and a
case
label, the optional
default
case executes. If no match occurs and the
switch
does not contain a
default
case, pro-
gram control simply continues with the first statement after the
switch
.
Section 5.7 Class
AutoPolicy
Case Study:
String
s in
switch
Statements
•
String
s can be used in a
switch
statement's controlling expression and
case
labels.
Section 5.8
break
and
continue
Statements
•The
break
statement, when executed in a
while
,
for
,
do
…
while
or
switch
, causes immediate
exit from that statement.
•The
continue
statement (p. 174), when executed in a
while
,
for
or
do
…
while
, skips the loop's
remaining body statements and proceeds with its next iteration. In
while
and
do
…
while
state-
ments, the program evaluates the loop-continuation test immediately. In a
for
statement, the in-
crement expression executes, then the program evaluates the loop-continuation test.
Section 5.9 Logical Operators
• Simple conditions are expressed in terms of the relational operators
>
,
<
,
>=
and
<=
and the equal-
ity operators
==
and
!=
, and each expression tests only one condition.
• Logical operators (p. 176) enable you to form more complex conditions by combining simple con-
ditions. The logical operators are
&&
(conditional AND),
||
(conditional OR),
&
(boolean logical
AND),
|
(boolean logical inclusive OR),
^
(boolean logical exclusive OR) and
!
(logical NOT).
• To ensure that two conditions are
both
true, use the
&&
(conditional AND) operator. If either or
both of the simple conditions are false, the entire expression is false.
• To ensure that either
or
both of two conditions are true, use the
||
(conditional OR) operator,
which evaluates to true if either or both of its simple conditions are true.