A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
OCA Java SE 8 Programmer I Study Guide 1Z0-808
this week in the
For Loop question
Joined: Nov 08, 2009
Mar 24, 2010 21:47: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.
Joined: Nov 04, 2008
Mar 24, 2010 22:05:43
Case one ;i=3;
In the above case you are putting assignment operator in place logical op. In
, 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.
Joined: Mar 16, 2010
Mar 24, 2010 22:05:52
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
subject: For Loop question
Reg. for loop
While nested in do-while loop
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015