generalized switch

Stuart D. Gathman stuart at BMS-AT.UUCP
Tue Aug 19 11:11:51 AEST 1986


In article <1083 at inset.UUCP>, dave at inset.UUCP (Dave Lukes) writes:

> >specify open-ended ranges, to allow things like "switch (strcmp(s, t)) {
> >case LT: ... case EQ: ... case GT: ... }" where LT and GT are #define'd as
> 
> What's wrong with ``if''??

'if' requires the result of strcmp() to be stored in a register or else
evaluated twice.  i.e.  { if ((r = strcmp(s,t)) < 0) less(); else if (r == 0)
same(); else more(); }  Try writing a balanced binary tree routine.  Storing
the result in a register is certain to be at least as efficient as any
implementation of open ended ranges, however.  The switch does look a lot
neater; of course, if people restricted their compares to return -1,0,1
there would be no problem.  Also, open ended ranges need not be implemented
in the compiler since macros like MAXINT are available.  Ranges should
not be a problem, though; many compilers already convert adjacent cases
to ranges so that a jump table can be used.



More information about the Comp.lang.c mailing list