• Post Reply Bookmark Topic Watch Topic
  • New Topic

While or for ?  RSS feed

 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while or for loop !
which is better to use and why?

Thanks in advance
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Car or boat - which is better and why?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider that...

...is the same as...

So if your loop needs some initialization code and/or some iteration code, then the for loop is generally more convenient.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively...

...is the same as...
 
Rick Goldstein
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
Car or boat - which is better and why?


I don't think this is an apt metaphor. For-loops and while-loops are more isomorphic than cars and boats. As demonstrated, you can pretty much use them interchangeably if you structure the code properly. There are definitely things you can do with cars that you can't do with boats, and vice versa.

The choice between the two kinds of loops is mostly a matter of style. I have known people who always use one or the other, but not both. Personally, I typically use for-loops when the number of iterations is known before the loop starts to execute. For instance, I will use 'for' when I am doing operations on a Collection that must be done on every member. (With Java 5, this is actually even better done with the new for-each construct.)

I am more likely to use 'while' if I can't compute the number of iterations in advance. A good example here is when you are processing an arbitrary stream of data and you want to exit the loop when a certain condition is met.

I don't think my way is better than any other, but it reflects the way I think about loop control structures. Because I follow this style consistently, it is easier for me to understand when coming back to code after some time.

Just my 2 cents.

Best,
Rick
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are definitely things you can do with a for loop and not a while loop and vice versa. There are definitely things you can do with both a car and a boat. I think the analogy is perfect, thoguh perhaps not immediately obvious or intuitive.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...is the same as...
[/qb]<hr></blockquote>

no, it's the same as:

I agree: for the water, I use a while-loop:
while (isLiquid)
while on roads, I use for:
for (int i = 0; i < k.len; ++i)

Would anybody write:
?
And why should I write:
?
[ August 18, 2005: Message edited by: Stefan Wagner ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
... And why should I write:
for ( ; isLiquid ; ) ...

I wouldn't expect you would. It's merely to illustrate a point.
[ August 19, 2005: Message edited by: marc weber ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!