🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #495

Re: Taming The Switch-Statement

#495
From: David Brown
Date: Sun, 18 Oct 2026 12:09
132 lines
6776 bytes
On 18/10/2026 13:04, bart wrote:
> On 18/10/2026 11:48, David Brown wrote:
>> On 17/10/2026 01:29, Janis Papanagnou wrote:
> 
>>> I've no strong opinion on that, but I find the "for NAME (;;)" to
>>> clearer express the _local scope_ of embedded breaks or continues.
>>> (Just a matter of taste, and my appreciation for locality.)
>>>
>>
>> A main objection to "name: for ..." is that with function-scope 
>> labels, you need a new name for every labelled loop or switch.  That 
>> may be an inconvenience, especially for macro with labelled loops, or 
>> for copy- and-paste code.  There is a proposal to deal with that which 
>> basically says :
>>
>> 1. Any "break label;" or "continue label;" refers to the innermost 
>> containing loop or switch with that label.
>>
>> 2. Any "label" used in "goto label;" must be unique in the function.
>>
>> Those rules give a simple and intuitive fix without affecting existing 
>> code.
> 
> 

For reference:

<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3658.htm>

> There's something missing. Labels usually live in their own, function- 
> wide name space.
> 
> But if you have two loops like this:
> 
>     L1: while () {} L2:
>     L1: while () {} L2:
> 
> Then even if no gotos exist to L1, is that rule somehow relaxed for 
> those L1s to allow them to co-exist? (While still applying to non-loop 
> labels L2, which would clash.)

Fair questions.

The rules (as given in proposal N3658) apply to all labels.

The point of the proposal is to look at how the labels are /used/, not 
how they are defined, and make sure their /use/ is clear, intuitive, 
intended, and unambiguous.

The two "L2:" labels above would not be a problem unless there is a 
"goto L2" somewhere - then it is unclear which "L2" is being referenced, 
and it is the use of "goto L2" that means the duplication is flagged as 
an error.

The two "L1:" labels above would also constitute an error if you have 
"goto L1" in the function.

But if you have "break L1" or "continue L1" within the body of one of 
the loops, it is entirely clear which L1 is being referred to - and thus 
there is no error.


> 
> Will the L1s still live within the regular name space? (Presumably each 
> will now be tagged with its associated loop.)

No - with this change, labels do not have "scope" in the same way as 
before, but context-dependent lookup.

Note that if you have "L1: while () {}", and no other label "L1", then 
you can happily use "goto L1" from anywhere in the function (assuming 
you don't fall foul of other rules) - the label "L1" is /not/ in a local 
scope within the loop.

> 
>> Beyond that, "name: for ..." or "for name ..." is, as you say, mostly 
>> a matter of taste.  (There are proposals for both versions, and you 
>> can look them up to see the arguments.)
> 
> The second form allows for those label names to be special, for example 
> by belonging in the same name space as regular identifiers and thus 
> subject to block scope rules. This is a tidier solution to the above.
> 

Yes, the "for name ..." form makes label names special.  No, it is not a 
"tidier solution" - though of course you are welcome to a personal 
opinion that you think one syntax variant is nicer than the other. 
There are pros and cons of each syntax - that's why its important that 
there is a committee (two committees, actually, since both C and C++ are 
working on this) rather than having one person declare "this is clearly 
the best".  (Just so you know I am not picking on you - in another post 
I also said Tim was wrong to say "for name ..." was "unquestionably 
better".  And I am /not/ suggesting that "label: for ..." is 
unquestionably better either.)


See :

<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3568r0.html#opposition-to-n3377>

(That is a C++ proposal, but discussions have gone back and forth 
between both C and C++ standards groups because the changes can apply to 
both languages - and no one wants different syntaxes for identical 
features.)


A key point is that labels of the form "L1: ..." are already familiar in 
C and many other programming languages, and a large number of other 
languages support something akin to "L1: while (..) { ... break L1; }".

Additionally, "L1 : do { ... break L1; } while (...)" puts the name at 
the top of the loop, rather than at the end (if it is attached to the 
"while").  (I don't know why N3377 proposed "do { } while NAME ( )" 
rather than "do NAME { } while ( )". )


And for those that like to use macros for their loops, the prefix label 
is neater:

	#define FOR_LOOP(_i, _n) for (int _i; i = 0; i < _n)

	outer : FOR_LOOP(i, 100) {
		inner : FOR_LOOP(j, 100) {
			if (i * j == 45) break outer;
		}
	}





Technical Details
Message-ID: <11b2cuh$141vj$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-mail
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>

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: Sun, 18 Oct 2026 14:09:52 +0200
Organization: A noiseless patient Spider
Lines: 132
Message-ID: <11b2cuh$141vj$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Oct 2026 12:09:54 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1181683"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+2fua6Gyimd/zChI3dNQCFRuXUg0D9BEU="; posting-host="a272f2752d0a4376a695806f42b83d2a"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:wx0HoZH9kdIZT/2hYPO+xNMso9E= sha256:sZ0+Zp/3diM+EwQht5ItbAgqgqq8PidlpOYXQuPALKA=
	sha1:Loeg/6IpWTgbX1s5pG5fl694QIE= sha256:HJTTOoQh+jx6jwF77hzGI5QPykK7wX+ibjFP8uWEEps=
In-Reply-To: <11b293j$12jfa$1@dont-email.me>
Content-Language: en-GB