Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable code and compilation issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable code and compilation issues" Watch "Unreachable code and compilation issues" New topic

Unreachable code and compilation issues

Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
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?

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

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

Joined: Apr 06, 2010
Posts: 4518

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.
I agree. Here's the link:
subject: Unreachable code and compilation issues