Article View: comp.lang.c
Article #383Re: Taming The Switch-Statement
From: Dan Cross
Date: Wed, 14 Oct 2026 11:27
Date: Wed, 14 Oct 2026 11:27
68 lines
3710 bytes
3710 bytes
In article <11an0q3$14pou$4@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >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.) Ah, you're right and I was simply wrong about Perl and Ruby. I must have hallucinated those. Regarding expression oriented languages where constructs like loops can return values, it's somewhat ironic that BCPL could do something like that using its, `valof`/`resultis` mechanism; I have often wanted something like that in C; it would obviate the ternary operator. >The latest draft of the C++ standard does not include labeled break >and continue statements. I don't know whether it's been proposed. That's unfortunate. I wonder if that's merely an oversight, or a conscious decision; I'll have to look it up later. >> 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. Fair. I do believe that the labeled variant is better for a host of reasons, though I don't love the syntax, but that is merely a subjective aesthetic judgement on my part. >[...] > >> 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). (Thanks; I've been meaning to download a copy of the latest draft anyway.) It seems that the matter is settled conclusively, and this entire thread is lots of heat and light generated for no reason. - Dan C.
Technical Details
Message-ID:
<11anouc$5lo$1@reader2.panix.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11amecs$10eab$1@dont-email.me> <11amune$ipi$1@reader2.panix.com> <11an0q3$14pou$4@kst.eternal-september.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail From: cross@spitfire.i.gajendra.net (Dan Cross) Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Wed, 14 Oct 2026 11:27:08 -0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: <11anouc$5lo$1@reader2.panix.com> References: <119qf6l$2rmg3$1@dont-email.me> <11amecs$10eab$1@dont-email.me> <11amune$ipi$1@reader2.panix.com> <11an0q3$14pou$4@kst.eternal-september.org> Injection-Date: Wed, 14 Oct 2026 11:27:08 -0000 (UTC) Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80"; logging-data="5816"; mail-complaints-to="abuse@panix.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: cross@spitfire.i.gajendra.net (Dan Cross)