• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

loops

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why the "while" statement gives a compile time error but the "if" statement doesn't.
while(false) { x = 3;}
if(false) {x = 3; }
Also, in the below for one creates an error and the other does not. Can you explain why?
for (int i = 0; i < 0; i++) x = 3;
for (int i = 0; false; i++) x = 3; //this one gives an error
Thank you
 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
java compiler will give u an error when a statement is unreachable.for if block its okay because its a conditional block.
shanks.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi charlie
just to add to what shankar has said ..... the following code would not give a compilation error
do {
System.out.println("samith");
}while(false);
hope that helps
Samith.P.Nambiar
---------------------------------------------
the harder you try the luckier you get
---------------------------------------------
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi charlie,
both the ques have the same concept behind them.when u initialize a loop to a "false" the next statement in the loop would never be executed.hence u'll get the error of 'statement not reached'.but whenever u give a condition which may or maynot evaluate to a false, there is no error.u may say that logically
this for loop can be evaluated to a false at the first sight.for (int i = 0; i < 0; i++) x = 3;
but even then at compile time the compiler does not know whether it is true or not.hence only and only when u give the word "false" there would be error.definitely if the loop body is empty then there would be no problem
hope this helps.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic