• 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

Unreachable statement

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


On line 2 in above code, if I replace condition with false, compiler throws Unreachable statement error but if I do the same thing on line 1, no error is thrown. Can anyone explain me why?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
14.21 Unreachable Statements
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

On line 2 in above code, if I replace condition with false, compiler throws Unreachable statement error but if I do the same thing on line 1, no error is thrown. Can anyone explain me why?



I find SUN's explanation on what's reachable accurate but not easy to remember.

So may be if I give you my idea, it might be simpler:

For all loops if the boolean constant is known to be false at compile time, then you get a compilation error.

However, the if statement is just like asser(). It MIGHT be used for testing situations that are not supposed to happen. Therefore, you will not get a compliation error.

That's the logic I use and it makes sense to me. It seems it explains most unreachable code problems concerning loops and ifs.
 
Sharn Arora
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Firas, this one is quite helpful
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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:



if, if-else, if-else if-else statements are handled differently from the rest of the statements like while, for and try-catch. Good to know this.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see rationale here.

1) When you say


you can still have an else block which can be executed. So, if(false) is serving some purpose.

2) When you say


this code is never going to serve any purpose, so it is compile-time error

3) If the same logic is applied to do-while


this code will still be executed once. So, no unreachable statement.
reply
    Bookmark Topic Watch Topic
  • New Topic