aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "if and while statements" Watch "if and while statements" New topic
Author

if and while statements

Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Why
compiles and

gives a compiler error
Thanks in advance
Sri
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.
-Barry


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: 3314
    
    7
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/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
jason stark
Greenhorn

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

Joined: Jan 08, 2003
Posts: 366
Jason,
I have tried this on version 1.4 and it doesnt work
Sri
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 ;
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: if and while statements