• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if and while statements

 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why
compiles and

gives a compiler error
Thanks in advance
Sri
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler takes special action only with if(true) and with if(false) to allow a psuedo conditional-compilation idiom.
-Barry
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jason stark
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this compiled
while(false) {
//some code
}
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
I have tried this on version 1.4 and it doesnt work
Sri
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic