Article View: comp.lang.c
Article #99Re: Taming The Switch-Statement
From: Keith Thompson
Date: Sat, 10 Oct 2026 02:17
Date: Sat, 10 Oct 2026 02:17
46 lines
3386 bytes
3386 bytes
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.
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.
Anything you could do with a more structured switch-like statement can
be done, with just a little care, with the existing switch statement.
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.
[...]
--
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:
<11ac783$1bkuo$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>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: Fri, 09 Oct 2026 19:17:35 -0700 Organization: None to speak of Lines: 46 Message-ID: <11ac783$1bkuo$1@kst.eternal-september.org> References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$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 02:17:40 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="1430488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GPJ7f42VNSIXK3hLQK3CP"; posting-host="29efadadb268dc6914862df94b2a05d4" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:ggflDc3aNOcG2NOktBV9dYgO7sU= sha256:c2cDEY17cjHXO6SU5RDeVcTySXT52tr7uwGPZDXvZy8= sha1:PJ/bodz+xNaZS6ddBqqVenFgepE= sha256:2DGSiny9boighd6EwA+H8x/w5NPlJSXK+kbtQrj+Ztw=