Expression sequencing query

dant at tekla.UUCP dant at tekla.UUCP
Sat Oct 18 12:38:10 AEST 1986



Simon Brown ( simon at its63b.ed.ac.uk (ECSC68 S Brown CS)) writes:
>But, how about
>	c = (b=1,a==1) || (b=2,a==0) || (b=3,a==3) || (b=4,a==2);
>as a quick nice way of saying
>	switch (a) {
>	    case 1: b=1; c=1; break;
>	    case 0: b=2; c=1; break;
>	    case 3: b=3; c=1; break;
>	    case 2: b=4; c=1; break;
>	    default: c=0;
>	}
>where the evaluation will "break off" at the point where one of
>the comparisons first succeeds.
>
>(I actually wanted to use something a bit like this a few days ago,
>but now I'm not too sure its that portable at all, considering
>all the problems with "+").


These two statements are NOT identical.  To make them the same the 
default case would need to be changed to:

	    default: b=4; c=0;


The switch is superior to the assignment for three reasons:

1) You probably don't want to change b in the default case.

2) Efficiency.  In all but the first case the assignment statement
will assign several values to b before reaching the true condition.

3) Maintainability.  The switch makes it obvious what's happening
The assignment has potential in the Obfuscated C Contest.





 Dan Tilque		UUCP:		tektronix!dadla!dant
			CSnet:		dant%dadla at tektronix
			ARPAnet:	dant%dadla%tektronix at csnet-relay

 "This is a bust!" she yelled, as she ripped open her coat, boldly
 displaying her ample authority.	-- R. J. Wilcek

		 From _Son_of_"It Was a Dark and Stormy Night"_



More information about the Comp.lang.c mailing list