Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbosgd!gatech!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: An amusing piece of code Message-ID: <716@umcp-cs.UUCP> Date: Sun, 6-Apr-86 01:39:26 EST Article-I.D.: umcp-cs.716 Posted: Sun Apr 6 01:39:26 1986 Date-Received: Wed, 9-Apr-86 07:05:10 EST References: <1370@ism780c.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 76 In article <1370@ism780c.UUCP> tim@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@umcp-cs ARPA: chris@mimsy.umd.edu