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

David Goodenough dg at lakart.UUCP
Tue Jan 31 02:28:15 AEST 1989


>From article <1611 at csuna.UUCP>, by abcscagz at csuna.UUCP (stepanek/cs assoc):
> Actually, C's "for" can be duplicated EXACTLY by C's "do ... while" loops.
> Consider:
> 
>     for (i = 0; i <= 17; ++i)
>     {
>         stuff();
>         more_stuff();
>     }
> 
> versus:
> 
>     i = 0;
>     do {
>         stuff();
>         more_stuff();
>     } while (++i <= 17);

Yes, but how about:

	for (i = 0; i <= a; i++)
	 {
	    lots_of_stuff();
	 }

against:

	i = 0;
	do
	 {
	    lots_of_stuff();
	 } while (++i <= a);

when the value of a is (say) -5. Are you _SURE_ these two are the same?

Better would be to say a for loop can be replaced by a while loop:

	i = 0;
	while (i <= a /* or 17 */)
	 {
	    lots_of_stuff();
	    i++;
	 }
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.lang.c mailing list