• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

For Loop question  RSS feed

 
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
 
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
 
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
Boost this thread!