Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable code and compilation issues

 
Andy James
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic