An amusing piece of code
Chris Robertson
chris at toram.UUCP
Wed Apr 9 00:46:33 AEST 1986
In article <1370 at ism780c.UUCP> tim at 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.
More information about the Comp.lang.c
mailing list