Java Reference
In-Depth Information
No
Is test1 true?
Ye s
statement1
No
Is test2 true?
Ye s
statement2
No
Is test3 true?
Ye s
statement3
Figure 4.3
Flow of sequential
if
s
one statement is your objective, this construct is more appropriate than the
sequential
if
statements. It reduces the likelihood of errors and simplifies the test-
ing process.
As you can see, nesting
if
statements like this leads to a lot of indentation. The
indentation isn't very helpful, because this construct is really intended to allow the
choice of one of a number of alternatives. K&R style has a solution for this as well. If
an
else
is followed by an
if
, we put them on the same line:
if (<test1>) {
<statement1>;
} else if (<test2>) {
<statement2>;
} else if (<test3>) {
<statement3>;
}
Search WWH ::
Custom Search