Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-adm!brl-smoke!smoke!rbj@icst-cmr From: rbj@icst-cmr (Root Boy Jim) Newsgroups: net.lang.c Subject: Re: An amusing piece of code Message-ID: <2449@brl-smoke.ARPA> Date: Mon, 7-Apr-86 16:13:15 EST Article-I.D.: brl-smok.2449 Posted: Mon Apr 7 16:13:15 1986 Date-Received: Thu, 10-Apr-86 00:42:59 EST Sender: news@brl-smoke.ARPA Lines: 61 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: switch ( thing ) { case A: A-code; break; case B: B-code; if ( 0 ) { case C: C-code; if ( 0 ) { case D: D-code; }} BCD-common-code; break; case E: E-code; } Noone here has been able to come up with a reasonable style for this. The example above is not to bad, but if B-code, C-code, etc, are complicated, then it starts to get ugly. Tim Smith sdcrdcf!ism780c!tim || ima!ism780!tim || ihnp4!cithep!tim Great! You also discovered how to remove all break's from swithces too! But this method will comlain about non-conditional conditionals as well. I can recommend several solutions in my order of preference: 1) Use Goto's. After case B & C, goto BCD-common 2) Use a flag: flag = 0; switch(thing) { case A: A-code; break; case B: B-code; ++flag; break; case C: C-code; ++flag; break; case D: D-code; ++flag; break; case E: E-code; break; } if (flag) BCD-code; 3) If the switch is isolated in it's own function: switch(thing) { case A: A-code; return; case B: B-code; ++flag; break; case C: C-code; ++flag; break; case D: D-code; ++flag; break; case E: E-code; return; } BCD-code; 4) Use a switch within a switch switch(thing) { case A: A-code; break; case B: case C: case D: switch(thing) { case B: B-code; break; case C: C-code; break; case D: D-code; break; } BCD-code; break; case E: E-code; break; } ... 99) Use your solution (Root Boy) Jim Cottrell