[cs13001] Programming question
Mikhail Nesterenko
mikhail at cs.kent.edu
Mon Oct 27 19:24:36 EDT 2014
> For incrementing in loop I know you can use for or while, but it
> seems some people are very strongly set towards one or the other
> (and will tell you to change it back and forth).
> Is there really a difference, a reason for using one over the other,
> any advantages, or style issues where it is best to choose one over
> the other?
If there is a loop variable and the variable is being incremented, I
would suggest using a for-loop.
In general, for-loop is specialized for the particular kind of use:
init-action is for declaring a loop-variable, expression is for
comparing whether the loop variable has not exceeded the limit, and
the post-action is for incrementing the loop variable.
If the loop that you are coding fits the above pattern, you should use
the for-loop. If it does not, you should use either while or do-while.
Trying to fit a particular kind of looping construct for every
occasion leads to awkward, hard to read code.
Thanks,
--
Mikhail
More information about the cs13001
mailing list