Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!husc6!wjh12!redsox!campbell From: campbell@redsox.UUCP (Larry Campbell) Newsgroups: comp.lang.c++ Subject: Improved switch statement (was Re: goodbye cpp ???) Message-ID: <574@redsox.UUCP> Date: 16 Dec 88 03:26:39 GMT References: <6590072@hplsla.HP.COM> <1106@etive.ed.ac.uk> <33528@bbn.COM> <1907@ogccse.ogc.edu> Reply-To: campbell@redsox.UUCP (Larry Campbell) Organization: The Boston Software Works, Inc. Lines: 49 In article <1907@ogccse.ogc.edu> wm@ogccse.UUCP (Wm Leler) writes: }I don't think you could extend switch statements to work on strings, }since there is some ambiguity between whether you want to compare }the strings by comparing their contents (using strcmp) or by comparing }their pointers (a very reasonable thing to do). You leave that decision to the person who implemented == for the class. I think it would be eminently sensible for the switch statement to operate on any type that has an == operator defined; of course the case labels would have to be the correct type (or coerced thereto). A common and useful idiom I learned in BLISS which is, unfortunately, not possible in C or C++ today, is the following code fragment (transliterated from BLISS into pseudo-C), which checks a number of boolean flags: bool flag1, flag2, flag3; switch (TRUE) { case flag1: // stuff case flag2: // stuff case flag3: // stuff default: // whatever } I think this is much clearer and neater and far less prone to error during maintenance than the current alternative: bool flag1, flag2, flag3; if (flag1) then // stuff else if (flag2) then // stuff else if (flag3) then // stuff else // whatever -- Larry Campbell The Boston Software Works, Inc. campbell@bsw.com 120 Fulton Street wjh12!redsox!campbell Boston, MA 02146