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 if and while statements 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 "if and while statements" Watch "if and while statements" New topic

if and while statements

Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
compiles and

gives a compiler error
Thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The compiler takes special action only with if(true) and with if(false) to allow a psuedo conditional-compilation idiom.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
That's correct. The compiler makes an exception to the rule that prohibits unreachable code for if() statements to support conditional compilation. Otherwise, it is no different from while or do while.

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
jason stark

Joined: Dec 01, 2002
Posts: 10
I found this compiled
while(false) {
//some code
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
I have tried this on version 1.4 and it doesnt work
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Sri Sri,
I just want to add that do.. while(false) works fine just like if(false). This is because the compiler knows that do ... while(false) gets executed at least once.
for(;false gives compiler error just like while(false). This is because the code with in for loop never gets executed so compiler throws an error message stating that the code is not reachable. Even if the code in these cases is just a null statement represented by ;
I agree. Here's the link:
subject: if and while statements
It's not a secret anymore!