Article View: comp.lang.c
Article #521Re: Taming The Switch-Statement
From: Michael S
Date: Sun, 18 Oct 2026 19:38
Date: Sun, 18 Oct 2026 19:38
158 lines
8879 bytes
8879 bytes
On Sun, 18 Oct 2026 20:47:13 +0200
David Brown <david.brown@hesbynett.no> wrote:
> On 18/10/2026 17:16, Michael S wrote:
> > On Sun, 18 Oct 2026 16:52:17 +0200
> > David Brown <david.brown@hesbynett.no> wrote:
> >
> >> On 18/10/2026 16:13, Michael S wrote:
> >>> On Sun, 18 Oct 2026 14:34:28 +0200
> >>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> >>>
> >>>> On 2026-10-18 14:09, David Brown wrote:
> >>>>>
> >>>>> [...] (I don't know why N3377 proposed "do { } while NAME ( )"
> >>>>> rather than "do NAME { } while ( )". )
> >>>>
> >>>> Oh! - I can only think that it would then be (sort of)
> >>>> "consistent" with the place of the label attacked in a 'while'
> >>>> loop; i.e. behind the 'while' keyword. - It's inconsistent in
> >>>> another interpretation, though; and I'd certainly have favored
> >>>> the latter form, to have the label consistently attached at the
> >>>> loop-entry keyword.
> >>>
> >>> I'd prefer label consistently attached to the loop-exit keyword.
> >>> Unfortunately, for() and while() loops have no such keyword.
> >>>
> >>
> >> That would help for switch inside a loop, or loop inside a switch,
> >> but do nothing for loop inside a loop or switch inside a switch. I
> >> don't think anyone involved in the development of the language has
> >> seriously considered making a solution that only handles the first
> >> two cases, and not the second two
> >
> > Pay attention that 4 patterns are not equal. The second case
> > (breaking from loop to outside of containing switch) hardly happens.
> >
>
> I know they are not equal - that's why I wrote all four combinations.
> But I have no good statistics to determine which combinations might
> be more common than others for wanting to break out of the outer
> structure. I know /you/ said that /you/ regularly want to break out
> of a switch that is inside a loop - fair enough. It is not something
> that I have needed to do, or at least not often enough to bother
> about it. Indeed, none of the cases are particularly common in my
> code. I've no doubt come across some of them at times, and dealt
> with them using extra bools or splitting up big functions into
> smaller ones where "return" could do the job. Certainly I do not
> ever remember wishing I had a "break_loop" statement, or a labelled
> break.
>
> But the C standards committee don't care about my code. They don't
> care about your code either. They care about what they think makes a
> difference to large numbers of C programmers. I can't tell you if
> they have access to good statistics, or are using the combined gut
> feelings of the committee members. However, it would strike me as a
> serious wasted opportunity if they made an addition to the language
> that only handled the one pattern that you find most common in your
> code, rather than also making it work for the other patterns.
>
> >> (to say nothing of three or more
> >> nesting layers). "break_switch" and "break_loop" style solutions
> >> have been mentioned and quickly dismissed, along with the "break
> >> <depth>" options.
> >>
> >
> > It does not look like you understood what I wrote.
> > All I wanted to say is that I prefer label to be located at where
> > the break goes.
>
> In C2y, you /do/ put the label along with the break - you write
> "break outer;". But you also have to put the label "outer : " at the
> start of the control structure.
>
> If you want to have some kind of "break to outer structure" /without/
> having a name or label on the structure, then you need some other way
> to refer to it. I thought you had said before your preference was
> for something like "break_switch" (ignoring any bike-shedding on the
> syntax), which would work in the particular case of a switch with
> nested loop(s). And as I wrote, this was considered and dismissed.
> (Equally, the other plausible syntax that does not require an
> explicit label - "break <depth>", was considered and dismissed.)
>
> > Or near this point. Within frame of current C syntax, I
> > don't see how to achieve it, except for do-while. But it does not
> > change the fact that I find it desirable.
> >
>
> Am I right in thinking you would then have :
>
> do {
> switch (...) {
> break outer;
> while outer (...);
>
> You are happy with labelling the loop or switch, but want the label
> at the end, not the start?
>
Almost. I want it at the end and I want it with syntax that can't be
confused with regular goto targets.
> Why not then just use "goto" and put a label at the end of the loop
> or switch? (This is allowed in C23.)
>
Like just about everybody else, I prefer weaker control transfers. They
are not easier to write, but easier to follow by reader.
goto is too powerful - can go everywhere within function. That part is
improved by C2y proposal.
But the labels that goto targets are also too powerful - they can be
reached from everywhere in the function. That part is not just not
improved by proposal, but made worse, because now we not just have a
too powerful label (that we have to name, which is problem by itself,
but lesser evil than break N alternative), but this label creates
another, completely unnecessary, target for goto, that the reader of
code should have in mind.
> >>>> We'll see what they finally decide (unless it's already clear;
> >>>> I'm not sure about the factual weight of the "proposal")...
> >>>>
> >>>
> >>> I tend to agree with Tim (if I correctly understood him): a.t.m.
> >>> the best outcome would be proposal scrapped completely.
> >>>
> >>
> >> The proposal has been accepted - it is coming to C and C++,
> >> whatever you or Tim may think.
> >
> > Nothing is set in stone until the standard released. But you are
> > very likely right. Which does not make me happier.
> >
>
> Okay. There are always changes that we like, changes we dislike, and
> (often the majority) changes to which we are indifferent.
>
> >> But of course there is no requirement to /use/
> >> the new feature if you don't like it.
> >
> > But others will use it and I'd have to read and understand their
> > code.
>
> True - but do you /really/ think that is going to be a problem?
>
For me, personally? May be not. Due to (or because of) my age, with
retirement not too distant.
> >> (I don't expect to use it
> >> significantly myself - I just don't seem to write code in a way
> >> that could take advantage of it.)
> >>
> >
> >
> >
> >
>
Technical Details
Message-ID:
<20261018223859.000031d3@yahoo.com>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!.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> <11aeusp$29hmu$2@kst.eternal-september.org> <86mrsdpcgp.fsf@linuxsc.com> <11au6pr$3mdne$1@kst.eternal-september.org> <11auc0s$206gh$3@dont-email.me> <11b285v$121v2$2@dont-email.me> <11b293j$12jfa$1@dont-email.me> <11b2cuh$141vj$1@dont-email.me> <11b2eck$3pvd9$1@dont-email.me> <20261018171339.00005308@yahoo.com> <11b2mf1$16mof$2@dont-email.me> <20261018181623.00007978@yahoo.com> <11b347h$1cn63$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!.POSTED!not-for-mail From: Michael S <already5chosen@yahoo.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 18 Oct 2026 22:38:59 +0300 Organization: A noiseless patient Spider Lines: 158 Message-ID: <20261018223859.000031d3@yahoo.com> 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> <11aeusp$29hmu$2@kst.eternal-september.org> <86mrsdpcgp.fsf@linuxsc.com> <11au6pr$3mdne$1@kst.eternal-september.org> <11auc0s$206gh$3@dont-email.me> <11b285v$121v2$2@dont-email.me> <11b293j$12jfa$1@dont-email.me> <11b2cuh$141vj$1@dont-email.me> <11b2eck$3pvd9$1@dont-email.me> <20261018171339.00005308@yahoo.com> <11b2mf1$16mof$2@dont-email.me> <20261018181623.00007978@yahoo.com> <11b347h$1cn63$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Injection-Date: Sun, 18 Oct 2026 19:39:09 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1262206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jOV5IWQdj0H4TGZvrcErtxoFxaUo/Nm0="; posting-host="f3ba9e4a06adc8c40e079351cfd7a0db" Cancel-Lock: sha1:HWKkms4n7gU1FGUXto1hn+BiU0E= sha256:u7VdoAe7QKPoXEdCzsL4HswO01mO1U4lW7JmOtxTDvk= sha1:aycRf0iHwjzYMDtL5D0iMKHzEA4= sha256:VvZ5QBSZNPcumwyGn0C5nrOw9uGNQSPiKSnvmlK70+8= X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)