Article View: comp.lang.c
Article #375Re: Taming The Switch-Statement
From: Keith Thompson
Date: Wed, 14 Oct 2026 04:35
Date: Wed, 14 Oct 2026 04:35
49 lines
3285 bytes
3285 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
[...]
> Many other languages solve this using a "labeled break" (Go,
> Rust, C#, Kotlin, Java, Ada, Pike, and now C), though some have
> used a "counted"/"multi-level" break (Ruby, Perl; I think PHP).
The only languages I know of that have a "counted break" (where
"break 2" breaks out of two levels of loops) are the Bourne shell
and shells derived from it, and PHP (you're right about that).
Perl has named break (spelled "last" or "last NAME") and continue
(spelled "next" or "next NAME). It does not have counted versions.
Ruby allows an argument on "break", but it's not the number of levels.
It specifies the value of the expression that it breaks out of. (A
loop is an expression that can yield a value.)
The latest draft of the C++ standard does not include labeled break
and continue statements. I don't know whether it's been proposed.
> You could have elegantly solved this problem by making `break`
> take an optional integer argument, which is the number of levels
> of structure to break out of.
I don't consider that elegant, for reasons I've already ranted about
here.
[...]
> But as Keith pointed out, W14 is now entertaining proposals to
> add labeled `break` to the language. I haven't looked myself,
> but if that is usable in a `switch` context, then it would solve
> the problem for good.
Yes, labeled "break" in C2y applies to both loops and switch statements.
There's also a labeled "continue" that applies only to loops.
The labels are optional, so existing code isn't broken.
The latest C2y draft is
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf>
(3.3 MB, 793 pages).
> This illustrates one reason why changing a language can be hard:
> your idea sounds good at first blush, but it has its own
> deficiencies.
--
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:
<11an0q3$14pou$4@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> <11ajcv8$3u33s$1@dont-email.me> <11aju80$28f2$6@kst.eternal-september.org> <11amecs$10eab$1@dont-email.me> <11amune$ipi$1@reader2.panix.com>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 21:35:14 -0700 Organization: None to speak of Lines: 49 Message-ID: <11an0q3$14pou$4@kst.eternal-september.org> References: <119qf6l$2rmg3$1@dont-email.me> <11ajcv8$3u33s$1@dont-email.me> <11aju80$28f2$6@kst.eternal-september.org> <11amecs$10eab$1@dont-email.me> <11amune$ipi$1@reader2.panix.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 14 Oct 2026 04:35:15 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="1206046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+A2Ta9tegg5/17CeGFPPfy"; posting-host="8bc08a5e85dad83e60fe86919d5c20fb" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:X4yq51zzudXYSRANJLLqExPnCaM= sha256:lvTYQ3/Q+u7NIYScemYyH0YcE4Ugu6iAic9url5Vs0o= sha1:o3pU/BKuST+9mp+0zHRKs1NCi8k= sha256:eqCt6ViLIy4SuUSsQ6F6vOgSXk1BEpP5rRVgUGofEic=