[Tutor] programming style question:

Magnus Lyckå magnus@thinkware.se
Wed May 7 06:58:02 2003


At 23:25 2003-05-06 -0700, Sean 'Shaleh' Perry wrote:
>I recently had this discussion with a coder on one of my projects (in C++)
>when while loops started showing up.  Seems his coworker believes 'for' is
>syntactic sugar and only 'while' should be used.  We chatted about this for a
>while and came up with the following.
[snip]

Right!

In C and derived languages, for *is* only an alternate
syntax for a while loop, and the fact that you can rewrite...

while condition() {
     a();
     b();
     c()
}

...as...

for (; condition(); a(), b(), c());

...shows us how easy it is for "creative" programmers to abuse
the C for loop.

In most other languages, for loops have a very different meaning,
and can only be used to loop over a sequence of some sort. Often
only over a sequence of numbers.

Basically a for loop implies: "Do something for every item in some
kind of collection". Do something for every number between 2 and
9, or for every odd number between 1 and 15, or do something for
every line in that file etc. Just as you said.

A while loop is much more general: Do something repeatedly as long
as a certain condition exists. This could be for a certain amount
of time, until some external event happens, until a problem is solved
or found unsolvable or whatever.

It might be a good idea to use for-loops in the same context in
C, C++, Java, as you would in Pascal, Ada, Basic or Python. This
is likely to be the least confusing, even if the syntax doesn't
stop you from using a for loop where ever a while loop occurs in
C derivates.

For-loops are not considered a deprecated feature in C++!

Kernighan & Richie says (The C programming language, 2nd ed, p. 14):

"The choice between while and for is arbitrary, based on which
seems clearer. The for loop is usually appropriate for loops in
which the initialization and increment are single statements and
logically related, since it is more compact than while and it
keeps the loop statement together in one place."

This is fairly fuzzy, but certainly has a big overlap with our
view in practice. Loops over sequences the way they are used in
e.g. Python or Pascal, certainly fits the pattern suggested by
K&R, and most other loops don't.

Stroustrup pronounces "for(;;)" as the standard way to specify an
infinite loop, with "while(true)" as an alternative. (The C++
Programming Language, Special Edition, p. 109). He calls this the
"forever" loops. In page 136, he writes: "The for-statement is
intended for fairly regular loops. The loop variable, the termination
condition, and the expression that updates the loop can be presented
'up front' on a single line. [...] If the loop isn't of the simple
''introduce a loop variable, test the condition, update the loop
variable'' variety, it's often better expressed as a while-statement."

I think "often" is a clear understatement here...



--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program