An amusing piece of code
Chris Torek
chris at umcp-cs.UUCP
Sun Apr 6 16:39:26 AEST 1986
In article <1370 at ism780c.UUCP> tim at ism780c.UUCP (Tim Smith) writes:
>The situation is that we have something 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.
[followed by code using `if (0) {', which works, since case labels
are just that---*labels*---and are effectively `goto'ed.]
Beware! Here is yet another opportunity for us `goto-ists' to come
out of the woodwork. I include here an actual example from a piece
of my own code. (I should probably change the gotos to be forward
references, rather than backward, though I am not certain it would
help.)
/*
* Now that we have the parameter, perform the
* command.
*/
switch (DVI_DT(c)) {
.
. [some material deleted]
.
case DT_DOWN:
move_down:
dvi_v += p;
/*
* `Vertical motion is done similarly, but
* with the threshold between ``small'' and
* ``large'' increased by a factor of 5. The
* idea is to make fractions like $1\over2$
* round consistently, but to absorb
* accumulated rounding errors in the
* baseline-skip moves.'
*/
if (ABS(p) >= CurrentFont->vspace)
vv = SPtoDEV(dvi_v);
else {
vv += SPtoDEV(p);
p = SPtoDEV(dvi_v);
FIXDRIFT(vv, p);
}
break;
case DT_Y:
dvi_y = p;
goto move_down;
case DT_Z:
dvi_z = p;
goto move_down;
.
.
.
}
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP: seismo!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list