Article View: comp.lang.c
Article #155Re: Taming The Switch-Statement
From: Keith Thompson
Date: Sat, 10 Oct 2026 23:30
Date: Sat, 10 Oct 2026 23:30
109 lines
5626 bytes
5626 bytes
bart <bc@freeuk.com> writes:
> On 10/10/2026 03:17, Keith Thompson wrote:
>> bart <bc@freeuk.com> writes:
>>> On 10/10/2026 01:25, Lawrence D’Oliveiro wrote:
>>>> For comparison, here is an example of Python’s match-statement. Like
>>>> all the good (i.e. non-C-based) designs, the designers saw no point in
>>>> having fall-through.
>>>
>>> C, doesn't specifically have fallthrough. It's just that C's switch is
>>> so bizarre, 'fallthrough' is a consequence of it being so dumb it
>>> doesn't know it's still inside a switch statement; it is just code
>>> that it carries ploughing through until it hits more control flow.
>>
>> C's switch statement is essentially a computed goto. It goes back
>> to BCPL (1967), which had different syntax (SWITCHON, CASE, ENDCASE)
>> but similar semantics. BCPL's predecessor, CPL, had a more explicit
>> computed goto that could be used in the same way; the target of a
>> "go to" could be a label expression.
>>
>> Think of C's "switch", not as an inferior version of Pascal's case
>> statement, but as an improved version of a computed goto.
>
> It's not that great as computed goto either. That tended to look like this:
>
> goto (L1, L2, L3) index
BCPL's "SWITCHON" is a more structured replacement for what was probably
(I'm speculating) a major use case for CPL's "go to expr".
[...]
> And, these days 'computed goto' in C tends to means something
> specific, which is a kind of looping switch but using multiple
> dispatch points (each break has its own goto *jumptable) rather the
> regular switch's one at the start).
Huh? "computed goto" is not a looping construct. But C doesn't
have computed goto, so I'm not going to discuss it here other than
in the context of how C's switch statement came to be.
[...]
> (Of, course, my language has it all:
Of course it does.
[...]
>> 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?
> That hasn't stop C++ having a higher level version.
C is not C++. A future C could have additional looping constructs,
but I haven't seen any concrete proposals.
[...]
>> Anything you could do with a more structured switch-like statement can
>> be done, with just a little care, with the existing switch statement.
>
> With a LOT more care, and with too much flexibility making it too easy
> to get things wrong. You even have this:
No, with a little care.
> case 10: default: case 20:
Doctor, it hurts when I do this.
[...]
>> Yes, you have to remember to write "break;" at the end of each case.
>> I haven't seen any proposals to add such a feature -- probably
>> because nobody else is as angry about it as you are.
>
> I'm angry because it was SO EASY to get right in the first place
> (Algol68 and Pascal already existed), and nearly as easy to fix even
> within the intervening decades.
You're angry about a decision that was made in the 1960s and 1970s,
and you're whining about it to people who can't do anything about it.
That can't be good for you.
I've explained why it doesn't need fixing, and would not be easy to fix.
> 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.
--
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:
<11aehqg$24k8m$3@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>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 16:30:23 -0700 Organization: None to speak of Lines: 109 Message-ID: <11aehqg$24k8m$3@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> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 10 Oct 2026 23:30:24 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2248982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Z5xPs7R9mo7G8Fa+EduZ/"; posting-host="29efadadb268dc6914862df94b2a05d4" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:Y2NjIXHQ1OAEWhbKVTRVh+SCD74= sha256:0MJGzNIg2mtZKSg6eFzN/qOUHUFx8F296H/tMx1Bm9s= sha1:yfyhR4kudZhsU0L5oW+ZrnD2gVI= sha256:G47TZDeqFf4i3IMmA22TChjMWWN7gevwHVLesJ2gWV0=