This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes while vs. if Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "while vs. if" Watch "while vs. if" New topic
Author

while vs. if

Manish Agarwal
Ranch Hand

Joined: Jun 22, 2004
Posts: 34
hi all
when i use while(false) in the code i got an error message saying
Unreachable code.But if i use if(false) i do not get an error.Why?
please explain.
Thanx.



Thanks and Regards,<br />Manish Agarwal<br />SCJP 1.4 (100%)<br />SCWCD 1.4 (98%)<br />SCBCD 1.3 (Preparing)
n.chenththuran
Ranch Hand

Joined: Jun 16, 2004
Posts: 41
hi,
while loop functions for (true) conditions. if u give false the rest of the code inside loop cant be reached
n.chenththuran
Ranch Hand

Joined: Jun 16, 2004
Posts: 41
check this


while(true){
System.out.println("hi world");
}


a non ending loop
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
This was really implemented to allow the use of development-time checks that could be easily removed when a system went to production. For example, you could write code that looked like this:



When the system went to production, you could easily change the single line to set the variable IN_PRODUCTION to true and all of your development debugging code would automatically cease executing.

Obviously, you'd never use a while statement for this type of work, so it was only necessary to make if(false) compile - while(false) could still generate a compiler error.

Today, there is no need to do this. With Java 1.4, you have the capability to use assertions to do this exact type of thing for you.


SCJP Tipline, etc.
Manish Agarwal
Ranch Hand

Joined: Jun 22, 2004
Posts: 34
thanks for all for immediate reply.
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Read the explanation in the JLS ....After reading this question put up by Manish, I checked the JLS and found this out...as to why they have implemented it that way !!
Read the last part where you see the word 'HYPOTHETICAL' (Its a long document so you would have to scroll down quite a bit)

JLS


---<br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while vs. if
 
Similar Threads
how to use else statement in the code
if and while
Compiler inconsistencies?
while statement error
Stuck again on the Loops