• 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 Code in While Statement

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

I have a code snippet from javachamps and it is given below.



The confusion is, till the do while loop, the program compiles fine. As soon as it reaches the while loop
it gives unreachable code. Why it is giving unreachable code in second while loop. The condition is same
as do while loop. It should give the same at do-while loop as well.

Any pointers are appreciated.

BR,

 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because do..while will anyways execute once so always reachable.

But the false in while(flase)...do is the culprit for the compiler error you saw.
Make it while(true)---do and it will not give compiler error atleast
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The condition is not the same. A do-while guarantees that the loop will be executed once. The while(false) loops does not.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

Thankyou for the prompt response. I have figured out the answer from JLS Unreachable Code

I changed the condition as follows.


In code where the exception occurred, JVM couldn't figure out a possible flow of execution. There is a special exemption of if statements in such a regard
as they can be used for debugging purposes as well. I would include a pointer to it in my SCJP Tips section for others as well.

Thanks Again.

BR,



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compiler wont complain about it. actually it is good.

the reason is , In earlier days people dont have modern IDE, so they used this if statement to commenting out the block of code. it is less error pron compare to block comment(/* */) . since you cant apply nested block comment(/* /* */ */) , compiler will complain about it.

so, if a block of code already contain block comment, they started to put if(false){} around the block of code.

but nowadays modern IDE will do inline comment to a particular lines of statement.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you can try this way also. in this way it will not give any error.

boolean isAlive=false;
while(isAlive){
}
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Thanks for the quick turn around.

The last one pointed out by Javin is interesting. It will compile because it is a variable which may be set later
to true in order for the loop to exit so compiler can determine a proper execution path. I hope this will be the
answer to the last snippet posted.

BR,
 
reply
    Bookmark Topic Watch Topic
  • New Topic