Forums Register Login

why the below highlighted line is not showing unreachable compile error.

+Pie Number of slices to send: Send
Hi to all
I want to know why the below highlighted line is not showing unreachable compile error.





public class loopclass
{
public static void main(String...args)
{
int k=2;
outer: while(true)
{
++k;
inner: for(int j=6;j>2;j--)
{
k=8-j;
// if(j==3)continue inner;

break outer;

}
continue outer;//at this line compiler never reached in this program,then why not showing error.
}

System.out.println(k);
}


}



[ January 16, 2008: Message edited by: pradeep singh ]

[ January 16, 2008: Message edited by: pradeep singh ]
[ January 16, 2008: Message edited by: pradeep singh ]
+Pie Number of slices to send: Send
Hi pradeep,

the expressions in the for loop are not the compile-time constants so they are validated first at runtime. The compiler doesn't know whether the for loop are entered or not. If the for loop is not entered the continue statement is reacheable well.

If you change the for loop like this you will get unreacheable statement error:

+Pie Number of slices to send: Send
Hi Let me try to explain this according to my understanding on the subject :

When we compile the program, Compiler do a flow analysis, in this flow analysis if its found any statement unreachable, it complains to us, this is unreachable code definition.

Now in the flow analysis compiler takes following into consideration :

Constructor
Method
instance initializer
static initializer

It does not take condition expression result of While, for,Do expressions.

So in your case although the code is unreachable but from compilor point of view if inner for block never executes( as result of conditional expression doesnt come into consideration) then the continue statement becomes reachable.

I hope this will clear your doubt.
+Pie Number of slices to send: Send
the loop statement should be
for(;;) {
+Pie Number of slices to send: Send
The reason for this - from the compilers point of view - is that there could be any number of permutations possible in the loop, so it is unable to conclude whether the statement is unreachable in this case.

In more straight-forward cases it is able to advise, for example:
+Pie Number of slices to send: Send
Hi
Sunny Jain said the below quoted rules ,but when i use these rule on the code shown/given by Ian Edward then according to Sunny it should not be a compiler error but it gives unreachable error.


Sunny says:

When we compile the program, Compiler do a flow analysis, in this flow analysis if its found any statement unreachable, it complains to us, this is unreachable code definition.

Now in the flow analysis compiler takes following into consideration :

Constructor
Method
instance initializer
static initializer

It does not take condition expression result of While, for,Do expressions.

 
+Pie Number of slices to send: Send
exact rules for detecting an unreacheable statement gives �14.12 of java language specification there you can find an answer for your question.
It's feeding time! Give me the food you were going to give to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1053 times.
Similar Threads
continue statement
sample on illegal use of labeled continue and break
Please anybody clear my doubts on hashcode
plz exaplin me this Prog
unreachable code-K&B p343
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:49:10.