comma operator: keep away?

Peter da Silva peter at ficc.uu.net
Mon Apr 24 23:53:51 AEST 1989


In article <1104 at aplcen.apl.jhu.edu>, bink at aplcen.apl.jhu.edu (Ubben Greg) writes:
> In article <2179 at pur-phy> (Sho Kuwamoto) writes:
> >	for(i=0, p=head; p->next != NULL; i++, p=p->next)

> People too often cram expressions in the for(;;) that are not related
> to the loop control, just to avoid a couple of extra lines.

I dispute that's the motivation in this case. But putting the i=0 and i++
in the loop you're providing extra information: that the variable 'i' is
tightly bound to the loop, and may be serving as a manufactured loop index.
The alternative:

	i = 0;
	for(p=head; p->next != NULL; p=p->next)
	{
		...
		i++;
	}

implies that 'i' might not be bound to the loop, and also means that you
would have to special-case any 'continue' statement in the expression if it
is so bound.

		if(gottagetouttahere)
			i++, continue;	/* :-> */

(Yes, I know you can't do that).
-- 
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.

Business: uunet.uu.net!ficc!peter, peter at ficc.uu.net, +1 713 274 5180.
Personal: ...!texbell!sugar!peter, peter at sugar.hackercorp.com.



More information about the Comp.lang.c mailing list