Article View: comp.lang.c
Article #170Re: Taming The Switch-Statement
From: Keith Thompson
Date: Sun, 11 Oct 2026 02:55
Date: Sun, 11 Oct 2026 02:55
113 lines
6073 bytes
6073 bytes
bart <bc@freeuk.com> writes:
> On 11/10/2026 00:30, Keith Thompson wrote:
>> bart <bc@freeuk.com> writes:
>
>> Huh? "computed goto" is not a looping construct.
>
> You only hear about it in the context of interpreter dispatch loops.
I hardly hear about it at all. I'm aware that it exists as a gcc
extension. I don't think I've ever used it. I see no point in
discussing it further.
>> [...]
>>
>>>> The ANSI C Rationale says "No serious consideration was given to
>>>> making the switch more structured, as in Pascal, out of fear of
>>>> invalidating working code".
>>>>
>>>> I suppose some version of C could have defined a new switch-like
>>>> statement without the fallthrough behavior. I can see some small
>>>> advantages in doing so, but the existing switch with fallthrough
>>>> would have to be kept in the language -- and such a new switch-like
>>>> statement would have added no real expressive power. We'd now have
>>>> a mix of code using the old and new constructs, pressure to convert
>>>> existing working code, and probably errors introduced by converting
>>>> code incorrectly.
>>>
>>> I guess the same rationale means it is stuck with for(;;) loops?
>>
>> Yes, of course it does. Did you think that a construct that's used
>> in most existing C programs could just be removed from the language?
>
> I didn't say they should be removed.
So you want to add a new and less flexible looping construct on top
of the existing one, making the language more complicated with no
real additional expressive power. (C++'s for loops are a different
matter, sensible because iterators are such a big part of the C++
standard library.)
>>> case 10: default: case 20:
>>
>> Doctor, it hurts when I do this.
>
> It illustrates having too much flexibility. Do compilers check for
> nonsense like this?
I fundamentally disagree with your claim of "too much flexibility".
"case 10: default: case 20:" perfectly valid, syntactically and
semantically. It's also silly, and not something someone would
plausibly write other than to make a point. I see no reason for
compilers to go out of their way to do anything about it. It's
reasonable for a compiler to warn about plausible errors. Putting
"default:" in the middle of a set of cases isn't a plausible
error, and I'm fine with the compiler not wasting its time with a
special warning. (I'd also be fine with a compiler warning about it.)
> Having proper structure must make code analysis simpler than
> effectively having a bunch of case labels randomly scattered through
> the code.
Not really. A reasonably clever compiler or other tool can easily
recognize a typical switch statement where each sequence of case or
default labels is followed by a "break". Code analysis becomes a little
more difficult when that pattern is broken by any of several means:
fallthrough, goto, return, continue.
> Example:
[snip]
[...]
>>> It could also be easy to implement as structured switch can be
>>> syntactic sugar for an if-else-if chain.
>>
>> If you want an if-else-if chain, write an if-else-if chain.
>
> Then why bother with switch at all?
It expresses a common pattern. It can, especially with older compilers,
result in more efficient code (a jump table) that an if/else chain
cannot directly express. It lets the expression be written and
evaluated just once. It makes the compiler diagnose the error of
specifying the same value more than once. It is as expressive as the
more restrictive case statement seen in Pascal and other languages.
It's what we have in the language. Nobody suggests that it's ideal
or perfect.
Or we could drop every control structure other than "if" and "goto".
> An implementation HAS to support if-else, so I'm suggesting it
> wouldn't be hard to implement structured switch, since lowering to
> if-else is one easy option.
How would your "structured switch" differ from the current switch
statement? Would the lack of fallthrough be the only difference?
Would the case expressions still have to be constant? Would you
allow duplicate values? Would you allow non-integer types, perhaps
with user-defined comparisons? These are all rhetorical questions,
intended merely to point out that this is not as simple as you seem
to think it is.
Would it add *any* real expressive power to a language that you
hold in contempt anyway?
Sure, we could add every feature that's not too hard to implement.
The end result would be a huge mess.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID:
<11aetqv$29hmu$1@kst.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11aehqg$24k8m$3@kst.eternal-september.org> <11aenps$27sq7$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sat, 10 Oct 2026 19:55:22 -0700 Organization: None to speak of Lines: 113 Message-ID: <11aetqv$29hmu$1@kst.eternal-september.org> References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11aehqg$24k8m$3@kst.eternal-september.org> <11aenps$27sq7$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sun, 11 Oct 2026 02:55:28 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2410206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/VkHiI8kv1HTImfLP+MLga"; posting-host="50698cca2518b68a86663845e71c144a" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:UQ/67QI7Y0p4jqrDSFwQ+5qBWfY= sha256:LHke+BIGsac3cCWOmM7eDQu8KzeNTHsomMzWNS4v/Cs= sha1:giw9xX0+zsa9HxtXEVvhgUWtwTs= sha256:JHP+dJzZRWzFkpT3ru7wh99KLF8csWYNj9on58e9vjs=