Article View: comp.lang.c
Article #171Re: Taming The Switch-Statement
From: Keith Thompson
Date: Sun, 11 Oct 2026 03:13
Date: Sun, 11 Oct 2026 03:13
56 lines
4236 bytes
4236 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 2026-10-11 00:32, Keith Thompson wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>> On 2026-10-10 22:03, Michael S wrote:
>>>> The cases where I want to break out of the loop from the switch in
>>>> the
>>>> body of the loop are very common.
>>>
>>> Ah, so you'd have needed something like Kornshell's 'break 2' to leave
>>> more than one level. - I suppose, you needed to used a 'goto' instead
>>> (and avoid the 'break' completely for those cases).
>> Something like that, sure, but please not that.
>> Specifying the *number* of levels to break or continue is IMHO
>> horrible. "break N" makes me count things so the computer doesn't
>> have to.
>
> I agree; that's what I also dislike about it.
>
> The point is the question what choices you have. The 'goto' is IMO
> the worst; because of semantic interdependencies that will regularly
> lead to semantical restrictions about its applicability (or placement
> of labels); various languages have various restrictions, it's just
> a mess! The 'break' is a "cheap" solution, but also very restricted.
> There's yet other variants with their own drawbacks; I won't even
> start with it.
>
>> There are proposals, and precedents in other languages, for *named*
>> loops, and I'd like to see that in a future C.
>
> One of the better possibilities, maybe; but still not satisfying for
> my (very subjective) taste.
>
> (Some language let you organize such things in completely other ways
> with no need for jumps of any sort.)
I find "break NAME;", where "NAME" is the name of an enclosing
construct, unambiguously better than "break N;", where N is the
number of levels to break out of.
The "break N" syntax is specified by POSIX. It's supported by ksh,
bash, dash, zsh, and probably other Bourne-based shells. (My guess
is that it originated in ksh.) My best guess is that because the
Bourne-like shells don't have gotos, they don't have labels, so
named loops would have required inventing those too. And admittedly
I suppose it's not too bad if it's no more than a couple of levels --
but it's still easy to, ahem, *break* if you reorganize the code.
There's a proposal for C2y to use goto labels as loop names, and a
competing one to define a new syntax like "for NAME (;;)" :
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3377.pdf
--
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:
<11aeusp$29hmu$2@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> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <20261010220212.000063fd@yahoo.com> <11ae56o$cjlv$4@dont-email.me> <20261010230355.00005189@yahoo.com> <11ae7eb$cjlv$6@dont-email.me> <11aeee0$24k8m$1@kst.eternal-september.org> <11aer17$286ml$2@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: Sat, 10 Oct 2026 20:13:29 -0700 Organization: None to speak of Lines: 56 Message-ID: <11aeusp$29hmu$2@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> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <20261010220212.000063fd@yahoo.com> <11ae56o$cjlv$4@dont-email.me> <20261010230355.00005189@yahoo.com> <11ae7eb$cjlv$6@dont-email.me> <11aeee0$24k8m$1@kst.eternal-september.org> <11aer17$286ml$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sun, 11 Oct 2026 03:13:31 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2410206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VvmtesK4j8WUvo4JslnCM"; posting-host="50698cca2518b68a86663845e71c144a" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:Hqteqr7h0b+cXAHoH1XPjZ4cDKA= sha256:JwHpdrEIpVmx2mNaYGzbwypftw3ypVo7F4XWPvwfBkc= sha1:ZXj2sNOTR+aGAjZlN2ShuVFJwWs= sha256:jmutM8A4Szsu96ptdMfBYeWZe98pwNy2I5ydyI6ZgvQ=