File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes For Loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For Loop question" Watch "For Loop question" New topic

For Loop question

howie jao

Joined: Nov 08, 2009
Posts: 14
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

Joined: Nov 04, 2008
Posts: 50

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.

Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

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....

I agree. Here's the link:
subject: For Loop question
It's not a secret anymore!