Java Reference
In-Depth Information
bols in the diagram. Figure 4.1 uses UML notes to show the Java code associated with each
action state. A
dotted line
connects each note with the element it describes. Activity diagrams
normally do
not
show the Java code that implements the activity. We do this here to illustrate
how the diagram relates to Java code. For more information on the UML, see our optiona
Selection Statements in Java
Java has three types of
selection statements
(discussed in this chapter and Chapter 5). The
if
statement
either performs (selects) an action, if a condition is
true
, or skips it, if the con-
dition is
false
. The
if
…
else
statement
performs an action if a condition is
true
and per-
forms a different action if the condition is
false
. The
switch
statement
(Chapter 5)
performs one of
many
different actions, depending on the value of an expression.
The
if
statement is a
single-selection statement
because it selects or ignores a
single
action (or, as we'll soon see, a
single group of actions
). The
if
…
else
statement is called a
double-selection statement
because it selects between
two different actions
(or
groups of
actions
). The
switch
statement is called a
multiple-selection statement
because it selects
among
many different actions
(or
groups of actions
).
Repetition Statements in Java
Java provides three
repetition statements
(also called
iteration statements
or
looping
statements
) that enable programs to perform statements repeatedly as long as a condition
(called the
loop-continuation condition
) remains
true
. The repetition statements are the
while
,
do
…
while
,
for
and enhanced
for
statements. (Chapter 5 presents the
do
…
while
and
for
statements and Chapter 7 presents the enhanced
for
statement.) The
while
and
for
statements perform the action (or group of actions) in their bodies zero or more
times—if the loop-continuation condition is initially
false
, the action (or group of actions)
will
not
execute. The
do
…
while
statement performs the action (or group of actions) in its
body
one or more
times. The words
if
,
else
,
switch
,
while
,
do
and
for
are Java keywords.
A complete list of Java keywords appears in Appendix C.
Summary of Control Statements in Java
Java has only three kinds of control structures, which from this point forward we refer to
as
control statements
: the
sequence statement
,
selection statements
(three types) and
repetition
statements
(three types). Every program is formed by combining as many of these state-
ments as is appropriate for the algorithm the program implements. We can model each
control statement as an activity diagram. Like Fig. 4.1, each diagram contains an initial
state and a final state that represent a control statement's entry point and exit point, re-
spectively.
Single-entry/single-exit control statements
make it easy to build programs—
we simply connect the exit point of one to the entry point of the next. We call this
control-
statement stacking
. We'll learn that there's only one other way in which control state-
ments may be connected—
control-statement nesting
—in which one control statement
appears
inside
another. Thus, algorithms in Java programs are constructed from only three
kinds of control statements, combined in only two ways. This is the essence of simplicity.
Programs use selection statements to choose among alternative courses of action. For ex-
ample, suppose that the passing grade on an exam is 60. The
pseudocode
statement