Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c Subject: Re: problems/risks due to programming language Message-ID: <669@mwtech.UUCP> Date: 24 Feb 90 01:33:36 GMT References: <1597@awdprime.UUCP> <8133@hubcap.clemson.edu> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 72 In article <8133@hubcap.clemson.edu> billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu writes: [some lines deleted] > C's switch statement is badly designed, so badly designed that it is > common practice to use break statements by the dozen in order to get > it to behave reasonably. A more sensible design would give the switch > the semantics of the Ada case statement, thereby saving countless lines > of code through the elimination of all those "break" statements. Though C has a badly designed switch statement (I don't doubt) it has proven to be a useful language and seems to be in more widespread use than 'better' ones. What I am missing in this discussion is to see: #define when break;case so that you can write: switch(x) { default: when 1: /*stuff*/ when 2: /*more stuff*/ } (O.K., not quite the ADA syntax, but it should work ...) BTW: I would *not* like to go into a new round of the discussion about using the preprocessor to define new "keywords". Everyone who remembers my articles from the last war on this topic knows, that I *don't* like to have new keywords. I have made the above proposual only to heat this debate about good or bad design of the switch statement :-) Finally I would like to add another experience with switch: Some day, I had written the following piece of code: enum sound { DING, DONG, DELL } x; .... enum sound getsound() { .... } and later ..... switch (x = getsound()) { DING: /*stuff*/ break; <----- No, *this* was not missing DONG: /*other stuff*/ break; DELL: /*still other stuff* break; } Do you see, what is wrong with that? I did not write "case DING:" aso., so DING: was considered to be a goto-label (and as such had a different name space as enum names and the program compiled without errors). The problem was hard to find because this particular compiler figured out, that the code within the case statement would never be excuted (there were no case-label in it!) and optimized the whole(!) statement away. Of course, this was a little too much optimization, because the call to getsound() was also optimized away (buggy compiler). Clearly, it was not at all obvious, why my program never called getsound(): I did put a printf() right before and after the switch, which both were executed, but getsound() never was. (Today I use #define rather that enum for such things, so that this problem will not any more occur :-)) -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83