🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #405

Re: Taming The Switch-Statement

#405
From: Keith Thompson
Date: Wed, 14 Oct 2026 21:23
65 lines
3784 bytes
bart <bc@freeuk.com> writes:
[...]
>> 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.
>
> OK. Although in that form, it really is little different from
> 'goto'. I believe the main difference is that the label goes at the
> beginning of the loop rather than the end!

Hardly.

Like most or all "structured" control flow constructs, it's a kind of
restricted goto -- and it's the restrictions that make it useful in
writing clear code.

"break", with or without a label, can only jump forward.  That makes it
much more difficult to use it to write spaghetti code.

Yes, "break FOO;" is equivalent to a goto, given an appropriately placed
label.  So what?

    FOO:
    for (blah;blah;blah) {
        BAR:
        while (blah) {
            if (cond1) {
                break FOO;
            }
            else if (cond2) {
                break BAR;
            }
        }
    }

"break FOO" is implemented as a jump to a point just after the end
of the outer loop.  But the way I think of it is that "FOO" is the
name of the entire outer loop, not just a single point in the code.
"break FOO;" applies to the named loop, causing execution of the
loop to terminate.  The fact that it's equivalent to a goto (just
as "break;" is) is an implementation detail.

One very minor infelicity is that the same syntax is used for labels
intended to be the targets of gotos and labels intended to name
loops and switch statements.  I think this same issue occurs in
most languages that have named breaks.  (Ada is an exception; it
uses a deliberately heavy syntax for goto labels, "<<LABEL>>", and
a simpler syntax for loop names, "NAME:".  But Ada had both gotos
and named loops from the begining.)  There has been a proposal to
use a different syntax for named loops ("for NAME (;;)"), but it's
not reflected in the latest C2y draft.  The fact that a loop name
can also be used as the target of a goto is a trivial problem,
perhaps a non-problem, solved by just not doing that.

> And maybe those scoping rules will be different: goto labels have
> function-wide scope.

See the C2y draft.

[...]

-- 
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: <11aorsh$1qib2$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-mail
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> <11anobe$1dt0e$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: Wed, 14 Oct 2026 14:23:29 -0700
Organization: None to speak of
Lines: 65
Message-ID: <11aorsh$1qib2$2@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>
	<11anobe$1dt0e$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 14 Oct 2026 21:23:30 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="1919330"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19QGBI6t2o7gECWIr04/VKZ"; posting-host="8bc08a5e85dad83e60fe86919d5c20fb"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:uCK24nrl824UNtq/LEyZqFz07Xk= sha256:i/GXxxaeUGJ8m+mgrT2inwEaNtb2MaFIXhItjFEXK+0=
	sha1:GRGLj4WUvakaHIn2tD4L/30x3Bg= sha256:Gs4YmMWZV4/dclnfWtiFd+HeS2R4xUN8xV8+kP0hxCo=