Declarations in switches, errors

Tom Karzes karzes at mfci.UUCP
Thu Oct 5 03:37:01 AEST 1989


In article <1989Oct3.184849.20106 at sq.sq.com> msb at sq.com (Mark Brader) writes:
>Actually, there *is* a way to "normally enter" the switch body.
>Instead of entering the body by a jump from the switch header, you
>put a label on the body itself and jump to that!
>...
>Only slightly less bizarre is to put one of the case labels ON the
>switch body rather than IN it -- 3.6.4.2 allows this and the compilers
>we have here support it -- whereupon you get initialization IF it is
>the first case that is chosen.

Note that although normal switch statement usage almost always involves
a compound statement with all case labels at the top level within the
compound statement, the syntax actually allows any statement.  For example,
the following:

    switch (x)
        case 5: case 9: foo();

if equivalent to:

    if ((t = x) == 5 || t == 9)
        foo();

where t is some temporary to avoid evaluating x more than once.

Similarly, one might have:

    switch (x)
        case 2: if (y)
        default:    foo();
                else
        case 7:     while (bar(z)) {
        case 12:        baz();
        case 13:        z++;
                    }

Again, I'm not advocating any of this, I'm merely pointing out that it's
within the definition of the language.



More information about the Comp.std.c mailing list