File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 4543

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
It's not a secret anymore!