🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #235

Re: Taming The Switch-Statement

#235
From: Keith Thompson
Date: Sun, 11 Oct 2026 21:04
55 lines
3765 bytes
bart <bc@freeuk.com> writes:
> On 11/10/2026 03:55, Keith Thompson wrote:
>> 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.
>
>>>> 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,
>
> So how would you do it: remove existing switch completely and replace
> with the new version?

No, I'd leave it alone.

C's switch statement is, like C as a whole, "quirky, flawed, and
an enormous success" (Dennis Ritchie said that).

You want to add a new construct that differs from switch only in
that it doesn't have implicit fallthrough.  You think the existence
of fallthrough behavior, or of any flexibility that you see as
excessive, is a problem that needs to be fixed.  I don't.

C's switch statement is *almost always* used to implement the same
patterns that can be implemented using Pascal's case statement.
(A minor difference is case ranges, which are proposed for C2y.)

You want to add a "newswitch" construct (presumably with a better
name) that's just like "switch" except that there's no fallthrough.
It would add no real expressive power.  (I'm tempted to propose
calling it "static switch".  To be clear, that's a joke.)

C already has such a construct, called "switch and don't forget to
write break after each case".  There are existing tools that help
with that.

Would it have been better if C's switch statement had behaved
like your "newswitch"?  Yeah, probably.  If I were inventing a new
language, I probably wouldn't copy that particular design decision
from C.  Fallthrough *is* useful sometimes, so I'd probably suggest
adding a "fallthrough" keyword.

But the existing switch statement is good enough, and the cost of
"fixing" it would be much higher than the minimal benefits.

-- 
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: <11agtkb$30q22$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-mail
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> <11aetqv$29hmu$1@kst.eternal-september.org> <11afrfq$2j2o6$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: Sun, 11 Oct 2026 14:04:06 -0700
Organization: None to speak of
Lines: 55
Message-ID: <11agtkb$30q22$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>
	<11aetqv$29hmu$1@kst.eternal-september.org>
	<11afrfq$2j2o6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sun, 11 Oct 2026 21:04:12 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="3172418"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+6s7p35Ffuh7Xxlh4p+LD5"; posting-host="50698cca2518b68a86663845e71c144a"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:CojnDpnAPC+W67yBFtl91u6oBaE= sha256:XPt+kNeI97OtGFFA8n5UQF6crti6b58X3G7/Dl0bg10=
	sha1:ErHEnMDEphu6M/HaJ3oiG9a2IqQ= sha256:O7Pspa7E70YfqC7cYwKwvQAfEAsBE919LJwdU08MQxo=