Article View: net.lang.c
Article #9600Re: Re: Expression sequencing query
From: dant
Date: Sat, 18 Oct 1986 02:38
Date: Sat, 18 Oct 1986 02:38
49 lines
1935 bytes
1935 bytes
Simon Brown ( simon@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@tektronix
ARPAnet: dant%dadla%tektronix@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"_
Technical Details
Message-ID:
<822@tekla.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!rutgers!nike!ucbcad!ucbvax!decvax!tektronix!teklds!dadla!tekla!dantShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!rutgers!nike!ucbcad!ucbvax!decvax!tektronix!teklds!dadla!tekla!dant From: dant@tekla.UUCP Newsgroups: net.lang.c Subject: Re: Re: Expression sequencing query Message-ID: <822@tekla.UUCP> Date: Fri, 17-Oct-86 22:38:10 EDT Article-I.D.: tekla.822 Posted: Fri Oct 17 22:38:10 1986 Date-Received: Tue, 21-Oct-86 20:32:45 EDT Organization: Tektronix Inc., Beaverton, Or. Lines: 49