why the compiler behave differently for if(false) and while(false)
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sadik_shaik
Posted On:   Tuesday, September 13, 2005 05:11 AM

Hi,

I like to know why the compiler behaves differently in the following two cases


#1

			
if (false)
System.out.println("false);

No Compilation Error



#2

			
while (false)
System.out.println("false");

Comilation Error : Unreachable Statement

Re: why the compiler behave differently for if(false) and while(false)

Posted By:   Anonymous  
Posted On:   Monday, October 10, 2005 01:51 AM

Refer to
JLS


Section 14.20: Unreachable Statements


Your question shall be answered by understanding the specification on how the java language perform flow analysis.

Re: why the compiler behave differently for if(false) and while(false)

Posted By:   Almagest_FUTT  
Posted On:   Wednesday, September 14, 2005 11:51 AM

Looks like the boolean condition of an if only gets evaluated at run-time, whereas the condition of a while gets pre-evaluated at compile-time. But the for's gets not (neither gets the do's, for obvious reasons).

Re: why the compiler behave differently for if(false) and while(false)

Posted By:   WarnerJan_Veldhuis  
Posted On:   Wednesday, September 14, 2005 06:26 AM


while (false) {
//the code inside this block will never run, since the condition is false to begin with, and will never change...
//So it's "unreachable", just like your compiler said....
}
About | Sitemap | Contact