Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site toram.UUCP Path: utzoo!utcs!mnetor!toram!chris From: chris@toram.UUCP (Chris Robertson) Newsgroups: net.lang.c Subject: Re: An amusing piece of code Message-ID: <134@toram.UUCP> Date: Tue, 8-Apr-86 09:46:33 EST Article-I.D.: toram.134 Posted: Tue Apr 8 09:46:33 1986 Date-Received: Tue, 8-Apr-86 12:42:39 EST References: <1370@ism780c.UUCP> Reply-To: chris@toram.UUCP (Chris Robertson) Organization: Toram/Globe-Tek Systems Ltd., Toronto, Canada Lines: 50 Summary: In article <1370@ism780c.UUCP> tim@ism780c.UUCP (Tim Smith) writes: >Here is an amusing piece of code ... that, if written with if-then-else >would be > > if ( thing == A ) > A-code; > else > if ( thing == B || thing == C || thing == D ) { > switch ( thing ) { > case B: B-code; break; > case C: C-code; break; > case D: D-code; break; > } > BCD-common-code; > } else > if ( thing == E ) > E-code; > >A, B, C, D, and E are constant expressions, so this is not elegant. >We would like to use a switch for everything. Here is a solution: > [ VERY inelegant switch code deleted ] Why not just use: BCD-flag=FALSE; if (thing == A) A-code; else if (thing == E) E-code; else { switch (thing) { case B: B-code;BCD-flag=TRUE;break; case C: C-code;BCD-flag=TRUE;break; case D: D-code;BCD-flag=TRUE;break; } if (BCD-flag) BCD-common-code; } This tests all your stuff, in a fairly straightforward way, and only costs 1 measly flag. -- Christine Robertson {ihnp4,linus,decvax}!utzoo!toram!chris An apple a day keeps the doctor away, especially if aimed well.