Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
A while statement is executed by first evaluating the Expression. If evaluation of the
Expression completes abruptly for some reason, the while statement completes abruptly for
the same reason. Otherwise, execution continues by making a choice based on the resulting
value:
If the value is true, then the contained Statement is executed. Then there is a choice:
If execution of the Statement completes normally, then the entire while statement
is executed again, beginning by re-evaluating the Expression.
If execution of the Statement completes abruptly, see �14.11.1 below.
If the value of the Expression is false, no further action is taken and the while
statement completes normally.
A while statement can complete normally iff at least one of the following is true:
The while statement is reachable and the condition expression is not a constant
expression with value true.
There is a reachable break statement that exits the while statement.
The contained statement is reachable iff the while statement is reachable and the
condition expression is not a constant expression whose value is false.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
ACTUAL: An if-then statement can complete normally iff it is reachable. The then-statement is reachable iff the if-then statement is reachable.
ACTUAL: 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. 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 regarded as "unreachable" in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define "flag variables" such as:
static final boolean DEBUG = false;
and then write code such as:
if (DEBUG) { x=3; }
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
This ability to "conditionally -->
[/qoute]
Ok i agree that u have to use the constant expression like above but i think it answers the doubt about why final booleans wont work.
But the same case that applies to if applies to while also why wont it apply although not as useful.
[qoute]
in the 2nd eg:
things are slightly different though,here also the compiler finds a break inside and actually breaks at runtime making it come out of the loop,hence the compiler complains "i was not initialised".
here it doesnt give statement not reached due to the same reason,The compiler finds a break in loop.
[/qoute]
here in this case my doubt was why stmt notreached is not coming in line 1 since it will be never executed
[qoute]
in eg 3:
here the breake inside while is not reached hence the compiler does find a break inside the while and hence the s.o.p(i); is not reacheble. [/qoute]
here s.o.p(i) is reached but line marked line 1 is not reached
Someone please answer
Cherry
[This message has been edited by Cherry Mathew (edited February 09, 2001).]
[This message has been edited by Cherry Mathew (edited February 09, 2001).]
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport