Java Reference
In-Depth Information
The
then
-statement is reachable iff the
if-then
statement is reachable and
the condition expression is not a constant expression whose value is
false
.
• An
if-then-else
statement can complete normally iff the
then
-statement
can complete normally or the
else
-statement can complete normally.
The
then
-statement is reachable iff the
if-then-else
statement is reachable
and the condition expression is not a constant expression whose value
is
false
.
The
else
-statement is reachable iff the
if-then-else
statement is reachable
and the condition expression is not a constant expression whose value
is
true
.
This approach would be consistent with the treatment of other control struc-
tures. However, in order to allow the if statement to be used conveniently for
“conditional compilation” purposes, the actual rules differ.
The rules for the
if
statement are as follows:
• An
if-then
statement can complete normally iff it is reachable.
The
then
-statement is reachable iff the
if-then
statement is reachable.
• An
if-then-else
statement can complete normally iff the
then
-statement
can complete normally or the
else
-statement can complete normally.
The
then
-statement is reachable iff the
if-then-else
statement is reach-
able.
The
else
-statement is reachable iff the
if-then-else
statement is reachable.
As an example, the following statement results in a compile-time error:
while (false) { x=3; }
because the statement
x=3;
is not reachable; but the superficially similar case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize
that the statement
x=3;
will never be executed and may choose to omit the code
for that statement from the generated
class
file, but the statement
x=3;
is not re-
garded as “unreachable” in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define
“flag variables” such as: