Java Reference
In-Depth Information
• Rectangles with their upper-right corners folded over are UML notes (p. 104)—explanatory re-
marks that describe the purpose of symbols in the diagram.
• Java has three types of selection statements (p. 105).
•The
if
single-selection statement (p. 105) selects or ignores one or more actions.
•The
if
…
else
double-selection statement selects between two actions or groups of actions.
•The
switch
statement is called a multiple-selection statement (p. 105) because it selects among
many different actions or groups of actions.
• Java provides the
while
,
do
…
while
and
for
repetition (also called iteration or looping) state-
ments that enable programs to perform statements repeatedly as long as a loop-continuation con-
dition remains true.
•The
while
and
for
statements perform the action(s) in their bodies zero or more times—if the
loop-continuation condition (p. 105) is initially false, the action(s) will not execute. The
do
…
while
statement performs the action(s) in its body one or more times.
•The words
if
,
else
,
switch
,
while
,
do
and
for
are Java keywords. Keywords cannot be used as
identifiers, such as variable names.
• Every program is formed by combining as many sequence, selection and repetition statements
(p. 105) as is appropriate for the algorithm the program implements.
• Single-entry/single-exit control statements (p. 105) are attached to one another by connecting
the exit point of one to the entry point of the next. This is known as control-statement stacking.
• A control statement may also be nested (p. 105) inside another control statement.
Section 4.5
if
Single-Selection Statement
• Programs use selection statements to choose among alternative courses of action.
• The single-selection
if
statement's activity diagram contains the diamond symbol, which indi-
cates that a decision is to be made. The workflow follows a path determined by the symbol's as-
sociated guard conditions (p. 106). If a guard condition is true, the workflow enters the action
state to which the corresponding transition arrow points.
•The
if
statement is a single-entry/single-exit control statement.
Section 4.6
if
…
else
Double-Selection Statement
•The
if
single-selection statement performs an indicated action only when the condition is
true
.
•The
if
…
else
double-selection (p. 105) statement performs one action when the condition is
true and another action when the condition is false.
• A program can test multiple cases with nested
if
…
else
statements (p. 107).
• The conditional operator
(p. 110;
?:
)
is Java's only ternary operator—it takes three operands.
Together, the operands and the
?:
symbol form a conditional expression (p. 110)
.
• The Java compiler associates an
else
with the immediately preceding
if
unless told to do other-
wise by the placement of braces.
•The
if
statement expects one statement in its body. To include several statements in the body
of an
if
(or the body of an
else
for an
if
…
else
statement), enclose the statements in braces.
• A block (p. 110) of statements can be placed anywhere that a single statement can be placed.
• A logic error (p. 110) has its effect at execution time. A fatal logic error (p. 110) causes a program
to fail and terminate prematurely. A nonfatal logic error (p. 110) allows a program to continue
executing, but causes it to produce incorrect results.