• 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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) if(false)
System.out.println("hello");

b) while(false)
System.out.println("hello");


can any one please explain why the if(false) does not say the print statement unreachable and compiles, but the while(false) says the statement is unreachable.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sankar velamuri:
can any one please explain why the if(false) does not say the print statement unreachable and compiles, but the while(false) says the statement is unreachable.



This is actually defined in the Java specification. The "if (debugging)" contruct is so often used to turn on/off debugging code, that the unreachabily check has been turned off for "if" statements.

Henry
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
Just Read Java Language Specification Which defines unreachable statement.
Unreachable statement can be made to compile using selection and loop construct.
1. if(false)
2. {
3. ;//unreachable statement
4. }

Above code gives compiler an impression that it is not able to access line 3 BASED ON A CONDITION.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

And this is true for compile time constants...



For variables ...that are evaluated at run time ..it does compile fine

Regards
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic