• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Unreachable code and compilation issues

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

Why the following code doesn't compile?



whereas the following compiles successfully?



In both the cases, code int a = 10; will never execute, that is, is unreachable. Then why compilation is unsuccessful only for the while statement?

cheers
A
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
false condition has any sense for while loop ? Nope.It executes only when it is true.

Although false condition for if-else statement can have else part.

 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Language Specification makes a specific exception for if statements. If you look at the relevant section, and read the example at the bottom, it explains why - basically it's there to enable conditional compilation.
reply
    Bookmark Topic Watch Topic
  • New Topic