• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Loop question

 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me how the below two case work:

for(int i=0;i=3;i++) <-- why is this causing error? imcompatible type, found: int , require: boolean

for(int i=0;i==3;i++) <-- why this does not run.

Thank you
 
Ritesh Pareek
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Case one ;i=3;

In the above case you are putting assignment operator in place logical op. In java, conditions are always in true or false(not like the c/c++ 0,1).. so it is marking error.

Case two ;i==3;

Not running because of it dose not meet condition criteria e.g. initially i =0 and you are comparing for equality for 3, so condition false and loop breaks without running.

Thanks
 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first loop... the problem is that it does not follow the syntax of the "for" loop which is as follows...
for(initializer ; conditional stmt ; increment/decrement)

hence the type required in the second place is boolean which is not there in your question and so the error is there...

In the second loop... How is it supposed to run? 'cause the condition on first check itself returns "false" (since i=0 which is not equal to 3) So it won't run for even a single time....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic