Java Reference
In-Depth Information
If the rules in Fig. 5.22 are followed, an “unstructured' activity diagram (like the one
in Fig. 5.26) cannot be created. If you're uncertain about whether a particular diagram is
structured, apply the rules of Fig. 5.22 in reverse to reduce it to the simplest activity dia-
gram. If you can reduce it, the original diagram is structured; otherwise, it's not.
action state
action state
action state
action state
Fig. 5.26
|
“Unstructured” activity diagram.
Three Forms of Control
Structured programming promotes simplicity. Only three forms of control are needed to
implement an algorithm:
•s e
•s l ti n
• repetition
The sequence structure is trivial. Simply list the statements to execute in the order in
which they should execute. Selection is implemented in one of three ways:
•
if
statement (single selection)
•
if
…
else
statement (double selection)
•
switch
statement (multiple selection)
In fact, it's straightforward to prove that the simple
if
statement is sufficient to provide
any
form of selection—everything that can be done with the
if
…
else
statement and the
switch
statement can be implemented by combining
if
statements (although perhaps not
as clearly and efficiently).
Repetition is implemented in one of three ways:
•
while
statement
•
do
…
while
statement
•
for
statement
[
Note:
There's a fourth repetition statement—the
enhanced
for
statement
—that we discuss
in Section 7.7.] It's straightforward to prove that the
while
statement is sufficient to pro-
vide
any
form of repetition. Everything that can be done with
do
…
while
and
for
can be
done with the
while
statement (although perhaps not as conveniently).
Combining these results illustrates that
any
form of control ever needed in a Java pro-
gram can be expressed in terms of