• 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

while vs. if

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
when i use while(false) in the code i got an error message saying
Unreachable code.But if i use if(false) i do not get an error.Why?
please explain.
Thanx.

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
while loop functions for (true) conditions. if u give false the rest of the code inside loop cant be reached
 
n.chenththuran
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this


while(true){
System.out.println("hi world");
}


a non ending loop
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was really implemented to allow the use of development-time checks that could be easily removed when a system went to production. For example, you could write code that looked like this:



When the system went to production, you could easily change the single line to set the variable IN_PRODUCTION to true and all of your development debugging code would automatically cease executing.

Obviously, you'd never use a while statement for this type of work, so it was only necessary to make if(false) compile - while(false) could still generate a compiler error.

Today, there is no need to do this. With Java 1.4, you have the capability to use assertions to do this exact type of thing for you.
 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all for immediate reply.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the explanation in the JLS ....After reading this question put up by Manish, I checked the JLS and found this out...as to why they have implemented it that way !!
Read the last part where you see the word 'HYPOTHETICAL' (Its a long document so you would have to scroll down quite a bit)

JLS
 
reply
    Bookmark Topic Watch Topic
  • New Topic