Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!uwvax!topaz!chandros From: chandros@topaz.RUTGERS.EDU (Chandros) Newsgroups: net.lang.c Subject: Good code for sometimes shared case stuff Message-ID: <4695@topaz.RUTGERS.EDU> Date: Tue, 8-Apr-86 14:18:59 EST Article-I.D.: topaz.4695 Posted: Tue Apr 8 14:18:59 1986 Date-Received: Thu, 10-Apr-86 20:32:27 EST Organization: Rutgers Univ., New Brunswick, N.J. Lines: 54 Keywords: case,shared,evil,goto /* you have found a food */ I just started reading this group regularly, sof please forgive me if I'm repeating something that was already said. David Linn suggests the following as a solution to the "shared code in a switch some of the time" problem. (I removed the goto version for the benefit of the younger readers. Shame, shame, shame on you. Evil gotos, gasp, and in C no less, what will the world think of next. ) >In posting<1370@ism780c.UUCP>, Tim Smith asks about a switch with common >code for some cases. I have seen two solutions. >1) put the common code in a subroutine > switch(thing) { > case A: A-code; break; > case B: B-code; BCD-common-code(); break; > case C: C-code; BCD-common-code(); break; > case D: D-code; BCD-common-code(); break; > case E: E-code; break; > } > [evil goto version was here] You forgot your structured programming rules. What about a (here it comes folks) a BOOLEAN variable called do_bcd_stuff?? Also, you could set do_bcd_stuff to be true in the declaration, and only change it in the case E to be false. Anyway, that's unimportant. I don't ken enough C to be sure, but it seems to me that the do_bcd_stuff declaration could go inside the switch along with the if statement. But I'm not sure, and the following will work for sure. (I hope, otherwise, BOY did I just made a fool out of myself............) ex: int do_bcd_stuff; switch(thing) { case A: A-code; do_bcd_stuff=TRUE; break; case B: B-code; do_bcd_stuff=TRUE; break; case C: C-code; do_bcd_stuff=TRUE; break; case D: D-code; do_bcd_stuff=TRUE; break; case E: A-code; do_bcd_stuff=FALSE; break; } if (do_bcd_stuff) bcd stuff goes here; This is much better than using those evil gotos, or paying the cost of a subroutine call. Jonathan A. Chandross allegra!topaz!chandros (that's 1 s kids....) "It is the little, pathetic attempts at Quality that kill...." -- R. M. Pirsig [eof]