🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #518

Re: Taming The Switch-Statement

#518
From: David Brown
Date: Sun, 18 Oct 2026 18:47
130 lines
7501 bytes
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?

Why not then just use "goto" and put a label at the end of the loop or 
switch?  (This is allowed in C23.)

>>>> 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?

>> (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: <11b347h$1cn63$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> <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>

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 20:47:13 +0200
Organization: A noiseless patient Spider
Lines: 130
Message-ID: <11b347h$1cn63$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Oct 2026 18:47:14 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1465539"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19xLqnzCHK3k6C2iCYDBBKTlG78o+Igu8M="; posting-host="a272f2752d0a4376a695806f42b83d2a"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:u6FLyGBCSrGCQJUDBgi4BFBFFDk= sha256:WLPVQdmfyNNRgTPR/xsUW9TAi1JxgD6+57QZtXEdk/0=
	sha1:IgMlPgo1rxRyuugKIH0hcqYNaQo= sha256:u4u3hzck1IpGZlzW0zBaMBC2PErEukLn5wET9IxBb+g=
Content-Language: en-GB
In-Reply-To: <20261018181623.00007978@yahoo.com>