
Ask HN: When you should use while vs. for loop? - seriousQ
I am pythonist.
======
jetti
I don't use Python but I feel that the reasons are the same for just about any
language. The while loop is used when there is a specific condition and you
need to iterate until you meet that condition. A for loop is typically used
when you have a fixed bound.

It makes sense to use a for loop when you are using an array/list because you
know the size of that data structure going into it. You could use a while loop
to do the same thing and keep a counter and just increment it each time you
loop but at that point you are just better off using a for loop.

One thing that you didn't mention is the do/while loop. The do/while loop is
useful when you want your looping code to execute at least once. It will go
through the code and then hit the while and determine if it should still
continue or not.

------
Koshkin
It would seem that StackOverflow may be a more appropriate place to ask
programming questions. (There are already answers to many of them.) The HN
crowd is usually kind enough to provide an answer, but still.

