• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the loop statement should be
for(;;) {
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic