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

Devin_E_Ben-Hur at cup.portal.com Devin_E_Ben-Hur at cup.portal.com
Sun Jan 29 06:43:57 AEST 1989


Jeff Boeing writes:
> 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);
> 
> 
> These two iterations are indistinguishable from one another.  The "for" term
> in C is totally superfluous and is only included because it makes it look
> languages that have a more "for"-ish "for" statement, like Pascal or Modula.

Nope.  This is the actual equivalent code:

	i = 0;
	while (i <= 17) {
		stuff();
		more_stuff();
continue_here:
		++i;
	}

The for loop tests at the first iteration, and a continue statement will
branch to the increment not to the test.

The C for statement is a convenience allowing the programmer to place
all loop control statements in one place instead of spreading them all
over the loop.  I've always thought the limits placed on Pascal & Modula's
for statements were silly, but that's just MHO.

Devin_Ben-Hur at cup.portal.com
...!ucbvax!sun!cup.portal.com!devin_e_ben-hur



More information about the Comp.lang.c mailing list