"for" loops (was Re: C++ vs. Modula2)

Jeff A. Bowles bowles at eris.berkeley.edu
Sat Jan 28 02:18:57 AEST 1989


In article <738 at atanasoff.cs.iastate.edu> hascall at atanasoff.cs.iastate.edu (John Hascall) writes:
>In article <739 at jupiter.iis.UUCP> heiser at iis.ethz.ch (Gernot Heiser) writes:
>
>>worst features. C's 'for' is really a 'while' with an initialization clause and
>>a designated spot that may contain an incrementation clause. What I consider a
>>"real" 'for' (as opposed to the while/repeat/loop family) is a construct that
>>executes a specific number ot times, the iteration count being determined
>>BEFORE the processing of the body starts....

I hate to fan flames like this, but I can't resist. I have strong reservations
about certain things in C, but the "for" loop is something that's really
kinda nice:

1. It's not restrictive on the types of the indices, because it defines a
   more general construct. If I had a nickel for every time, in Fortran,
   that I needed a loop that ran from 0.0 to 1.0 by 0.1 (or the like) and
   had to use INTEGER to do it. Or Pascal, which lacked the "step" clause
   so that you couldn't increment by more than what the language-designer
   wanted.
2. Yes, you're right, it's redundant - the "while (expr) statement;" and
   the "do statement; while (expr)" and the "for (expr;expr;expr) statement;"
   have a lot of redundancy, probably for brevity. So?
3. Because it's not restrictive on the types, and because I don't have to
   know EXACTLY how many times it will run through the body, I can do things
   like:
	for (p = headoflist; p != NULL; p = p->l_next)
		process(p);
   The article said that you could code loops that run a arbitrary
   number of times, using something like:
	for (i = 0; i < thingwithsideeffects(i); i++)
		munge();
   And while the author of the article is correct, you can code garbage
   like this in most languages. Fault the coder, in this case.

The only thing I really miss is something you Unix-types will recognize
from awk (and perhaps from Algol 68?) -
	for (t in table)
		process(table[t]);
But that's another story....

	Jeff Bowles



More information about the Comp.lang.c mailing list