An amusing piece of code

Craig Johnson vince at fluke.UUCP
Thu Apr 10 02:55:55 AEST 1986


In posting<1370 at ism780c.UUCP>, Tim Smith asks about a switch with common
code for some cases.

While not necessarily the most efficient method, I think nested switches
are prefered in this situation over goto's.  Notice the inner switch need
not be concerned with 'case A', 'case E', or 'default' since 'thing' has
already been tested in the outer 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-common-code;
                break;
	case E: E-code; break;
	}

				Craig V. Johnson
				John Fluke Mfg. Co., Inc.
				Everett, WA



More information about the Comp.lang.c mailing list