Article View: comp.lang.c
Article #371Re: Taming The Switch-Statement
From: Keith Thompson
Date: Wed, 14 Oct 2026 03:40
Date: Wed, 14 Oct 2026 03:40
59 lines
3988 bytes
3988 bytes
bart <bc@freeuk.com> writes:
> On 13/10/2026 01:33, Keith Thompson wrote:
[...]
>> That's already happening. If you take a look at the N3886 working
>> draft for C2y, it has named break statements.
[...]
>> There is already a solution for the problem you're complaining
>> about.
>>
>> (It also solves the problem of breaking out of nested loops.)
>> If you're unable to use this feature, you can use goto. That's not
>> ideal, but with well chosen label names and no backward branching
>> it's not too bad.
>
> I've got a better idea. I added an extra keyword to my C compiler
> called "breakloop". It's like 'break' but always works with the
> nearest enclosing loop, even from inside switch.
Adding a keyword to your personal C compiler is easy. That's a tiny
fraction of the effort needed to make a change to the language.
Getting the WG14 committee to agree that your solution is a good idea,
nailing down the exact wording that describes it, and waiting for all
C compilers to implement it, is not.
Sure, your "breakloop" could work, but it's blatantly nothing more
than a workaround for one specific minor problem. Presumably if
"breakloop" appears outside a switch statement it means exactly
the same thing as "break". How are programmers to decide which
one to use? Would you want to deprecate the use of "break" other
than to break out of a switch statement?
We have decades of precedent that "break" is how you break out of a
loop. Your solution is to add a second way to do the same thing.
Sorry, but it's ugly. I'd rather use goto. And there's already
a more general solution planned for C2y.
[...]
> Simple, yes? Then you think that this could be done even more easily
> 50 years ago.
It could have been done more easily 50 years ago than now. Using,
say, "breaksw" for switch statements and "break" for loops would
have been equally easy, and a better solution that what you propose.
Allowing name for loops and switch statements is a more general
solution to this and other problems, it's already been implemented,
and it will almost certainly be standardized.
I dislike the default fallthrough behavior of switch statements.
If I were designing C today, I'd probably (a) *not* have default
fallthrough, (b) add something like a "fallthrough" keyword for
when you want it, and (c) have named loops. As of C2y, we'll have
(c) but not (a) and (b) -- and that's good enough.
--
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:
<11amtjk$14pou$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> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@dont-email.me> <11ainqf$3kkvd$1@dont-email.me> <11aj73v$c2u$1@reader2.panix.com> <11ajcv8$3u33s$1@dont-email.me> <11aju80$28f2$6@kst.eternal-september.org> <11amecs$10eab$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: Tue, 13 Oct 2026 20:40:31 -0700 Organization: None to speak of Lines: 59 Message-ID: <11amtjk$14pou$1@kst.eternal-september.org> References: <119qf6l$2rmg3$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@dont-email.me> <11ainqf$3kkvd$1@dont-email.me> <11aj73v$c2u$1@reader2.panix.com> <11ajcv8$3u33s$1@dont-email.me> <11aju80$28f2$6@kst.eternal-september.org> <11amecs$10eab$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 14 Oct 2026 03:40:36 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="1206046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mGvyhpntj+Clvl5KKx/tJ"; posting-host="8bc08a5e85dad83e60fe86919d5c20fb" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:NMzqlUPvg2ki5DPVk7Z6BtWnTCc= sha256:CV1vZ5lmdHJT5/ujktVS1wLnPnAwOgxRytmM5tVggik= sha1:nF3JgkqK5rWSHe2uzhD3VQnDNy4= sha256:YtLwZjEFG+s3UrV2Zo6I6oHLOkrL5I9aEVNJ/n57lPg=