Article View: comp.lang.c
Article #539Re: Taming The Switch-Statement
From: David Brown
Date: Mon, 19 Oct 2026 09:16
Date: Mon, 19 Oct 2026 09:16
96 lines
6426 bytes
6426 bytes
On 18/10/2026 21:38, Michael S wrote:
> 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:
>>> 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.
>
I can appreciate wanting a syntax that is distinct from regular labels
for "goto".
(I can also appreciate that others prefer a similarity - they want to be
able to use the same label for "goto", it means less new learning, and
fewer conflicts with things like syntax highlighters. Since I
personally don't expect to use the feature much, and have almost no use
for "goto" or labels today, I am ambivalent here, and prefer to leave
the decision to those with more direct experience. There has been
plenty of discussion on the pros and cons here.)
However, I am curious as to why you see putting the name or label at the
end of the loop or switch as important. A "break <name>" will go to the
end, but a "continue <name>" will go back to the start. Do you see
"break" as more common or more important than "continue" ? Goto labels
are an exception, but the normal rule in C is that names are introduced
before their use.
>> 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.
(I am not sure that "weak" or "strong" is the right adjective here - I
see "for" and "while" as /stronger/ than "goto" because they provide
more structure and guarantees about the control flow. But I know what
you mean, and agree with you.)
> 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.
Labelled breaks can eliminate a significant use of "goto". (And if
"defer" makes it into C2y and gains popularity - admittedly a big "if" -
that could eliminate the other major use of "goto".) If there is no
"goto" in the function, there is no concern about where it might go.
There is always going to be scope for misunderstanding, no matter what
syntax is used. It is good to pick a syntax that reduces the risk, but
it cannot be eliminated - ultimately, individual programmers are
responsible for writing clear code.
I think your concerns are valid, but I think (or hope) that they are
more pessimistic than realistic.
Technical Details
Message-ID:
<11b4n4s$1q18k$1@dont-email.me>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> <20261018223859.000031d3@yahoo.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!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Mon, 19 Oct 2026 11:16:12 +0200 Organization: A noiseless patient Spider Lines: 96 Message-ID: <11b4n4s$1q18k$1@dont-email.me> 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> <20261018223859.000031d3@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 19 Oct 2026 09:16:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1901844"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tl8IcXRtfhAXQWTPqm2FCim0420Q27bw="; posting-host="f8e69aeaa618e660247a66e432a03539" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:e/v3cbj/0a5FSldFkA9g/llzZJw= sha256:RiZm/Wu8OP9iGc/8TcTfMXWAk7wCHekYfdoIaRLtzKI= sha1:jLAQ0jCqRFFLoaeGlRMSXm1DubA= sha256:fIT5BV7ZFOGTZmd//XYK3WLg2TQ2BAtsl9UY4Hz6+rc= Content-Language: en-GB, nb-NO In-Reply-To: <20261018223859.000031d3@yahoo.com>