Thread View: comp.lang.c
412 messages
412 total messages
Page 4 of 9
Started by Lawrence D’Oliveiro
Sat, 10 Oct 2026 00:25
Page 4 of 9 • 412 total messages
Re: Taming The Switch-Statement
Author: Michael S
Date: Tue, 13 Oct 2026 19:54
Date: Tue, 13 Oct 2026 19:54
107 lines
6690 bytes
6690 bytes
On Tue, 13 Oct 2026 07:40:30 -0700 Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: > Michael S <already5chosen@yahoo.com> writes: > > > On Mon, 12 Oct 2026 21:09:25 +0200 > > David Brown <david.brown@hesbynett.no> wrote: > > > >> On 12/10/2026 21:02, Michael S wrote: > >> > >>> On Sun, 11 Oct 2026 14:28:29 -0700 > >>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > >>> > >>>> bart <bc@freeuk.com> writes: > >>>> [...] > >>>> > >>>>> 'break' to quit a loop early is part of the language, so why > >>>>> shouldn't someone make use it? > >>>> > >>>> Agreed. (There are coding styles that require loops to exit only > >>>> at the bottom, but that's not what we're talking about.) > >>>> > >>>>> It just its use can be interfered with when 'switch' is > >>>>> involved. > >>>> > >>>> Yes. Everyone here knows that, and knew it before you started > >>>> talking about it. > >>>> > >>>> Michael S wrote above that "The cases where I want to break out > >>>> of the loop from the switch in the body of the loop are very > >>>> common." Tim Rentsch questioned the "very common" part of that, > >>>> and asked for an example. You jumped in and asserted that an > >>>> example isn't necessary, because you misunderstood what was > >>>> being said. > >>> > >>> I didn't read the whole subthread, but from what I read it seems > >>> that the only reader that misunderstood my statement was David > >>> Brown. The rest, including bart, understood just fine. > >> > >> If I have misunderstood, I would love to be corrected. In a number > >> of my posts I said I was not sure what you were talking about, and > >> would need more explanation. > > > > I thought that my comment is hard to misunderstand, but you proved > > me wrong. So I'd try to elaborate: > > > > switch() nested within loop is very common in my practice and I > > suppose in practice of everybody else. > > > > In my practice, but obviously not in practice of Tim Rentsch, it > > is sufficiently common to want to transfer control from point within > > such nested switch to point immediately following the containing > > loop. > > Let me clarify my earlier comment. I'm not surprised that people > routinely use switch() inside of loops. What did surprise me is the > statement that wanting to break out of the loop from inside the > switch() is "very common". Not happens occasionally; not happens > commonly or even just sufficiently commonly; not happens routinely; > but "very common". I am wondering now how common it actually is, > for switch() in general and for switch() inside of a loop. Is it > one in a hundred? One in ten? One in two? More than that? > Of course, I never gathered exact statistics. My guts feeling is that among switches nested within loop it could be as high as one out of five. Not one out of five case labels, but one out of five switch statements. > I asked for an example because I am curious about what the proximate > cause of such commonality might be. Is it a general style matter? > Is it that you are used to certain patterns of local control flow? bart gave decent example. My own use cases are mostly related to processing of incoming network messages that are distinguished by opcode at the beginning of the message. But not just that. Generally, I like small functions that return specialized enum as result code. When such functions used within loop, it often leads to pattern in question. > Is it that some use cases are more common in the kinds of programs > you deal with? It would be good to get some data here, even just ad > hoc data. Sorry, right now I have no time for doing such analysis. It is not trivial exactly because 'C' has no such feature. So I don't know how to automate a search within codebases. > > In my own experience, wanting to escape multiple levels (including > the case of provoking a loop exit from inside a switch()) doesn't > happen often enough to merit considering a language change. But I > would like to know more about the experiences of other people. > > I'm reminded of a quote from (if memory serves) Tony Hoare, about > language design. Paraphrasing: A programming language should not > reflect the union of all the features proposed by its designers; > rather it should reflect the union of the designers' prejudices > about what features should NOT be put into the language. In this particular case Ritchie did not have to innovate in order to create this feature. Quite opposite, as others (Keith ?) mentioned, he had to innovate in order to merge distinct break-loop and break-switch features of ancestor language into unified break.
Re: Taming The Switch-Statement
Author: Michael S
Date: Tue, 13 Oct 2026 20:16
Date: Tue, 13 Oct 2026 20:16
57 lines
3755 bytes
3755 bytes
On Tue, 13 Oct 2026 10:52:55 -0000 (UTC) cross@spitfire.i.gajendra.net (Dan Cross) wrote: > In article <11ajsfi$28f2$5@kst.eternal-september.org>, > Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > >[snip] > >Yes, we all know that. BCPL had BREAK and ENDCASE. B apparently > >dropped both, and then re-added just BREAK. C inherited break > >from B. I haven't seen anyone say that that wasn't a mistake. > >We just don't spend decades worrying about it. > > A minor historical quibble: BCPL, at the time of C's invention, > did not have `ENDCASE`; that came later. We tend to forget that > BCPL did not stop evolving after C's invention; in fact, it went > on as a going concern for quite a few more years. > > From Dennis Ritchie's HOPL paper: > > |Not every difference between the BCPL language documented in > |Richards's book [Richards 79] and B was deliberate; we started > |from an earlier version of BCPL [Richards 67]. For example, the > |`endcase` that escapes from a BCPL `switchon` statement was not > |present in the language when we learned it in the 1960s, and so > |the overloading of the `break` keyword to escape from the B and > |C `switch` statement owes to divergent evolution rather than > |conscious change. > (https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html) > Good catch. Thank you. > Note this is the only place in that paper where Dennis Ritchie > mentions `break`. Why was it overloaded for `switch`? He does > not say, exactly, but he _does_ alude to space being a > consideration for B, and some of the resulting syntactic quirks > then escaping into C: > > |Storage limitations on the B compiler demanded a one-pass > |technique in which output was generated as soon as possible, > |and the syntactic redesign that made this possible was carried > |forward into C. > > This is from a section discussing why C did not implement the, > `valof`/`resultis` mechanism of BCPL: BCPL parsed the whole > program into memory and operated on the parsed representation; > B could not afford that luxury. > > Btw, one hypothesizes that some of the subsequent evolution of > BCPL came from C influence; BCPL added the PL/1-style > `/* ... */` comments, and it wouldn't surprise me if `ENDCASE` > came from C's `break` inside of a `switch`, though I have no > evidence of that. > > - Dan C. >
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Tue, 13 Oct 2026 20:52
Date: Tue, 13 Oct 2026 20:52
29 lines
1944 bytes
1944 bytes
In article <11alkqa$4o6l$3@dont-email.me>, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >On 2026-10-13 16:38, Dan Cross wrote: >> In article <11al8uc$hk7a$1@dont-email.me>, bart <bc@freeuk.com> wrote: >>> [snip] >>> So your workaround had an error. That's what happens. > >I could foresee such a stupid response! - You're a hopeless case. >Given that you've had even stupid syntax errors in your original >code I'm astonished that you even dare to get into such argument. > >> >> There is no programming that prevents all errors. > >(That was not even programming; it was a quick write down for the >post as an example structure to explain what's to be visualized.) > >> >>> [snip] >>> Jesus. I take it that you don't agree it is a hack then? > >Writing a loop with a predicate that is composed by an 'and' is >certainly not a "hack" but usual programming. Nowadays you learn >such things in kindergarten. Were you referring to me, Janis? Or to bart? - Dan C.
Re: Taming The Switch-Statement
Author: bart
Date: Tue, 13 Oct 2026 21:24
Date: Tue, 13 Oct 2026 21:24
28 lines
2474 bytes
2474 bytes
On 13/10/2026 21:52, Dan Cross wrote: > In article <11alkqa$4o6l$3@dont-email.me>, > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >> On 2026-10-13 16:38, Dan Cross wrote: >>> In article <11al8uc$hk7a$1@dont-email.me>, bart <bc@freeuk.com> wrote: >>>> [snip] >>>> So your workaround had an error. That's what happens. >> >> I could foresee such a stupid response! - You're a hopeless case. >> Given that you've had even stupid syntax errors in your original >> code I'm astonished that you even dare to get into such argument. >> >>> >>> There is no programming that prevents all errors. >> >> (That was not even programming; it was a quick write down for the >> post as an example structure to explain what's to be visualized.) >> >>> >>>> [snip] >>>> Jesus. I take it that you don't agree it is a hack then? >> >> Writing a loop with a predicate that is composed by an 'and' is >> certainly not a "hack" but usual programming. Nowadays you learn >> such things in kindergarten. > > Were you referring to me, Janis? Or to bart? If there's any insult implied then it will have been me.
Re: Taming The Switch-Statement
Author: Lawrence D’Oliveiro
Date: Tue, 13 Oct 2026 21:54
Date: Tue, 13 Oct 2026 21:54
12 lines
2199 bytes
2199 bytes
On Tue, 13 Oct 2026 07:23:40 -0000 (UTC), Waldek Hebisch wrote: > So what those counts indicate: majority of loops is simple, that is > have no early exit. Vast majority of early exits is done via > 'return'. My experience in my own C code is quite the opposite. Lots of loops have exits in the middle, leading me to use forms like “for (;;)”. I also try to avoid putting “return” anywhere other than the end of a function, to make it easier to see the flow of control. *cough* Nassi-Shneiderman diagrams *cough*
Re: Taming The Switch-Statement
Author: Lawrence D’Oliveiro
Date: Tue, 13 Oct 2026 22:00
Date: Tue, 13 Oct 2026 22:00
36 lines
2612 bytes
2612 bytes
On Sat, 10 Oct 2026 00:25:54 -0000 (UTC), I wrote:
> For comparison, here is an example of Python’s match-statement. Like
> all the good (i.e. non-C-based) designs, the designers saw no point
> in having fall-through.
>
> This is a very simple example, involving straight matching of values
> against instances of an enum. There are also more complex
> pattern-matching options available.
Another, slightly more involved example, where the matching is against
the components of a tuple. This is from a parser for the Varlink
interface definition language. Note the use of the “_” wildcard to
indicate don’t-care parts of matches:
match token.value, token.rule_name :
case "[]", _ :
nexttoken()
valtype = parse_typedef(None)
result = ArrayType(name, valtype)
case "[string]", _ :
nexttoken()
valtype = parse_typedef(None)
result = DictType(name, valtype)
case _, "elttypedef" :
result = parse_elttypedef(name)
case _, "typedef" :
result = parse_typedef(name)
case _ :
raise AssertionError \
(
"unrecognized typedef token %s: %s"
%
(token.rule_name, repr(token.value))
)
#end match
Re: Taming The Switch-Statement
Author: Janis Papanagnou
Date: Tue, 13 Oct 2026 22:03
Date: Tue, 13 Oct 2026 22:03
34 lines
2630 bytes
2630 bytes
On 2026-10-13 22:52, Dan Cross wrote: > > Were you referring to me, Janis? Or to bart? You can identify the association of the answers by its attribution indent level. - Most was attributed to bart's text that was quoted. The only part that was attributed to what you said was this: Dan wrote: >> >> There is no programming that prevents all errors. I commented that, in parenthesis (i.e. just as a side remark): > > (That was not even programming; it was a quick write down for the > post as an example structure to explain what's to be visualized.) Bart perceived my program text as a "programming error", I explained that it was just a sloppy "Usenet-posting writing error". Of course programming errors can happen. As can writing errors happen. Given his text in your post gave the impression that he considers my sloppy code-writing in my post as a problem of the technical language design of "C"; it wasn't, IMO. - That was all I tried to communicate. While we seem to have a different view on whether the C-code is to be considered a "hack" (or as I see it, just typical programming) I didn't think that this difference in views is worth discussing since that's just (our personal) opinions. Janis
Re: Taming The Switch-Statement
Author: bart
Date: Tue, 13 Oct 2026 22:19
Date: Tue, 13 Oct 2026 22:19
62 lines
3497 bytes
3497 bytes
On 13/10/2026 23:00, Lawrence D’Oliveiro wrote:
> On Sat, 10 Oct 2026 00:25:54 -0000 (UTC), I wrote:
>
>> For comparison, here is an example of Python’s match-statement. Like
>> all the good (i.e. non-C-based) designs, the designers saw no point
>> in having fall-through.
>>
>> This is a very simple example, involving straight matching of values
>> against instances of an enum. There are also more complex
>> pattern-matching options available.
>
> Another, slightly more involved example, where the matching is against
> the components of a tuple. This is from a parser for the Varlink
> interface definition language. Note the use of the “_” wildcard to
> indicate don’t-care parts of matches:
>
> match token.value, token.rule_name :
> case "[]", _ :
> nexttoken()
> valtype = parse_typedef(None)
> result = ArrayType(name, valtype)
> case "[string]", _ :
> nexttoken()
> valtype = parse_typedef(None)
> result = DictType(name, valtype)
> case _, "elttypedef" :
> result = parse_elttypedef(name)
> case _, "typedef" :
> result = parse_typedef(name)
> case _ :
> raise AssertionError \
> (
> "unrecognized typedef token %s: %s"
> %
> (token.rule_name, repr(token.value))
> )
> #end match
This is not quite structural pattern matching. Isn't this just
equivalent to:
match token.value:
case "[]":
nexttoken()
valtype = parse_typedef(None)
result = ArrayType(name, valtype)
case "[string]":
nexttoken()
valtype = parse_typedef(None)
result = DictType(name, valtype)
else:
match token.rule_name:
case "elttypedef":
result = parse_elttypedef(name)
case "typedef":
result = parse_typedef(name)
else
.... error
Except that 'else' probably doesn't work like this (why not).
Re: Taming The Switch-Statement
Author: bart
Date: Tue, 13 Oct 2026 23:21
Date: Tue, 13 Oct 2026 23:21
78 lines
4147 bytes
4147 bytes
On 13/10/2026 01:33, Keith Thompson wrote:
> bart <bc@freeuk.com> writes:
> [...]
>> This is my first example expressed in C (note those first three args
>> are passed by reference):
>>
>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>> switch (pmtype)
>> case pm_option: ...; break;
>> case pm_sourcefile:
>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>> break;
>>
>> Except I can't use break like that. So I have to find a workaround.
>
> The code appears to be valid C that doesn't do what you want. I can't
> easily tell what you want it to do. I don't know which of the two break
> statements was intended to break out of the loop. A comment would have
> solved that.
>
> I'm going to guess that the first break is supposed to apply to the
> switch statement, and the second to the while loop.
>
> You want the language to be changed to make this kind of thing easier.
>
> That's already happening. If you take a look at the N3886 working
> draft for C2y, it has named break statements. In C2y (or in gcc
> if you use an extension), the above could be written as:
>
> WHILE:
> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
> SWITCH:
> switch (pmtype)
> case pm_option: ...; break SWITCH;
> case pm_sourcefile:
> if (passlevel == run_pass || passlevel == runpcl_pass) {
> break WHILE;
>
> assuming it's the second "break' that's supposed to break out of
> the loop. (I'd probably use more meaninful names.)
>
> There is already a solution for the problem you're complaining about.
> (It also solves the problem of breaking out of nested loops.)
>
> If you're unable to use this feature, you can use goto. That's not
> ideal, but with well chosen label names and no backward branching
> it's not too bad.
I've got a better idea. I added an extra keyword to my C compiler called
"breakloop". It's like 'break' but always works with the nearest
enclosing loop, even from inside switch.
So this example (a starts at 0):
while (1) {
switch (a++) {
case 1: puts("one"); break;
case 2: puts("two"); breakloop;
case 3: puts("three"); break;
default: puts("other");
}
if (a>40) break;
}
finishes after displaying 'two', instead of after the 37th or so 'other'.
Modding the compiler took 90 seconds, and needed 4 extra lines of code.
Setting up this test took 2-3 minutes.
Simple, yes? Then you think that this could be done even more easily 50
years ago.
Re: Taming The Switch-Statement
Author: Tim Rentsch
Date: Tue, 13 Oct 2026 23:45
Date: Tue, 13 Oct 2026 23:45
125 lines
7748 bytes
7748 bytes
Michael S <already5chosen@yahoo.com> writes: > On Tue, 13 Oct 2026 07:40:30 -0700 > Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: > >> Michael S <already5chosen@yahoo.com> writes: >> >>> On Mon, 12 Oct 2026 21:09:25 +0200 >>> David Brown <david.brown@hesbynett.no> wrote: >>> >>>> On 12/10/2026 21:02, Michael S wrote: >>>> >>>>> On Sun, 11 Oct 2026 14:28:29 -0700 >>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>>> >>>>>> bart <bc@freeuk.com> writes: >>>>>> [...] >>>>>> >>>>>>> 'break' to quit a loop early is part of the language, so why >>>>>>> shouldn't someone make use it? >>>>>> >>>>>> Agreed. (There are coding styles that require loops to exit only >>>>>> at the bottom, but that's not what we're talking about.) >>>>>> >>>>>>> It just its use can be interfered with when 'switch' is >>>>>>> involved. >>>>>> >>>>>> Yes. Everyone here knows that, and knew it before you started >>>>>> talking about it. >>>>>> >>>>>> Michael S wrote above that "The cases where I want to break out >>>>>> of the loop from the switch in the body of the loop are very >>>>>> common." Tim Rentsch questioned the "very common" part of that, >>>>>> and asked for an example. You jumped in and asserted that an >>>>>> example isn't necessary, because you misunderstood what was >>>>>> being said. >>>>> >>>>> I didn't read the whole subthread, but from what I read it seems >>>>> that the only reader that misunderstood my statement was David >>>>> Brown. The rest, including bart, understood just fine. >>>> >>>> If I have misunderstood, I would love to be corrected. In a number >>>> of my posts I said I was not sure what you were talking about, and >>>> would need more explanation. >>> >>> I thought that my comment is hard to misunderstand, but you proved >>> me wrong. So I'd try to elaborate: >>> >>> switch() nested within loop is very common in my practice and I >>> suppose in practice of everybody else. >>> >>> In my practice, but obviously not in practice of Tim Rentsch, it >>> is sufficiently common to want to transfer control from point within >>> such nested switch to point immediately following the containing >>> loop. >> >> Let me clarify my earlier comment. I'm not surprised that people >> routinely use switch() inside of loops. What did surprise me is the >> statement that wanting to break out of the loop from inside the >> switch() is "very common". Not happens occasionally; not happens >> commonly or even just sufficiently commonly; not happens routinely; >> but "very common". I am wondering now how common it actually is, >> for switch() in general and for switch() inside of a loop. Is it >> one in a hundred? One in ten? One in two? More than that? > > Of course, I never gathered exact statistics. > My guts feeling is that among switches nested within loop it could be as > high as one out of five. Not one out of five case labels, but one out of > five switch statements. Gut feelings are fine. Thank you. >> I asked for an example because I am curious about what the proximate >> cause of such commonality might be. Is it a general style matter? >> Is it that you are used to certain patterns of local control flow? > > My own use cases are mostly related to processing of incoming network > messages that are distinguished by opcode at the beginning of the > message. Okay. Does this mean the loop exit indicates the end of the last packet so the last packet can be processed (and start another one presumably)? Or am I misunderstanding you? > But not just that. Generally, I like small functions that return > specialized enum as result code. When such functions used within > loop, it often leads to pattern in question. I expect this pattern leads to using switch() more often. So that could account for part of it. >> Is it that some use cases are more common in the kinds of programs >> you deal with? It would be good to get some data here, even just >> ad hoc data. > > Sorry, right now I have no time for doing such analysis. It is not > trivial exactly because 'C' has no such feature. So I don't know how > to automate a search within codebases. An approximate measure is how often 'goto' is used inside a switch() body. That would be interesting to know in any case. Whether it would be easy to do that heuristically probably depends on what code base is being processed. >> In my own experience, wanting to escape multiple levels (including >> the case of provoking a loop exit from inside a switch()) doesn't >> happen often enough to merit considering a language change. But I >> would like to know more about the experiences of other people. >> >> I'm reminded of a quote from (if memory serves) Tony Hoare, about >> language design. Paraphrasing: A programming language should not >> reflect the union of all the features proposed by its designers; >> rather it should reflect the union of the designers' prejudices >> about what features should NOT be put into the language. > > In this particular case Ritchie did not have to innovate in order to > create this feature. Quite opposite, as others (Keith ?) mentioned, > he had to innovate in order to merge distinct break-loop and > break-switch features of ancestor language into unified break. Sorry, it seems my comment led you astray. I was talking about something else. In any case Ritchie was operating under very different circumstances, including in particular more severe memory constraints. At the time adding another keyword to the language may have seemed like an extravagance.
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 01:27
Date: Wed, 14 Oct 2026 01:27
14 lines
2348 bytes
2348 bytes
bart <bc@freeuk.com> writes:
[...]
> My view is that using distinct keywords for loop-break and
> switch-break (accepting that the language wants the latter) would have
> had pretty much zero costs, but worthwhile benefits.
I agree, and I don't think that's particularly controversial.
The point on which we disagree is what can and should be done about
it now.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 01:34
Date: Wed, 14 Oct 2026 01:34
27 lines
3157 bytes
3157 bytes
Michael S <already5chosen@yahoo.com> writes:
[...]
> In this particular case Ritchie did not have to innovate in order to
> create this feature. Quite opposite, as others (Keith ?) mentioned, he
> had to innovate in order to merge distinct break-loop and break-switch
> features of ancestor language into unified break.
As Dan Cross pointed out, I didn't quite get this right. At the time
C was invented, BCPL didn't have ENDCASE. BCPL continued to evolve
independently after B and C were derived from it.
This information is from Ritchie's HOPL (History or Programming
Languages) paper, available at
https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html
Not every difference between the BCPL language documented in
Richards's book [Richards 79] and B was deliberate; we started
from an earlier version of BCPL [Richards 67]. For example,
the endcase that escapes from a BCPL switchon statement was
not present in the language when we learned it in the 1960s,
and so the overloading of the break keyword to escape from the
B and C switch statement owes to divergent evolution rather
than conscious change.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 01:44
Date: Wed, 14 Oct 2026 01:44
19 lines
2031 bytes
2031 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
[...]
> Note this is the only place in that paper where Dennis Ritchie
> mentions `break`. Why was it overloaded for `switch`? He does
> not say, exactly, but he _does_ alude to space being a
> consideration for B, and some of the resulting syntactic quirks
> then escaping into C:
[...]
Pure speculation follows.
I'm skeptical that space concerns were the reasons for not having a
separate endcase keyword, though I suppose it's possible. (C had
23 keywords as of 1974.) It seems more likely that Ritchie just
didn't think of it.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Taming The Switch-Statement
Author: Kenny McCormack
Date: Wed, 14 Oct 2026 01:50
Date: Wed, 14 Oct 2026 01:50
25 lines
2000 bytes
2000 bytes
In article <11amlq9$12efg$1@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >bart <bc@freeuk.com> writes: >[...] >> My view is that using distinct keywords for loop-break and >> switch-break (accepting that the language wants the latter) would have >> had pretty much zero costs, but worthwhile benefits. > >I agree, and I don't think that's particularly controversial. > >The point on which we disagree is what can and should be done about >it now. The answer to that is: Invent new languages with C-like syntax, but better. You can't do anything about C, but you can do better as long as you call the result something else. Like C#. Like C++. Like Swift. -- People seem to think that Youtube (and Facebook and Twitter and so on) is (are) some sort of resource created for the public good. Why they delude themselves into believing this is beyond my ability to comprehend. Usenet will always be here. Longer than people remember what port 80 was for.
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 03:40
Date: Wed, 14 Oct 2026 03:40
59 lines
3988 bytes
3988 bytes
bart <bc@freeuk.com> writes:
> On 13/10/2026 01:33, Keith Thompson wrote:
[...]
>> That's already happening. If you take a look at the N3886 working
>> draft for C2y, it has named break statements.
[...]
>> There is already a solution for the problem you're complaining
>> about.
>>
>> (It also solves the problem of breaking out of nested loops.)
>> If you're unable to use this feature, you can use goto. That's not
>> ideal, but with well chosen label names and no backward branching
>> it's not too bad.
>
> I've got a better idea. I added an extra keyword to my C compiler
> called "breakloop". It's like 'break' but always works with the
> nearest enclosing loop, even from inside switch.
Adding a keyword to your personal C compiler is easy. That's a tiny
fraction of the effort needed to make a change to the language.
Getting the WG14 committee to agree that your solution is a good idea,
nailing down the exact wording that describes it, and waiting for all
C compilers to implement it, is not.
Sure, your "breakloop" could work, but it's blatantly nothing more
than a workaround for one specific minor problem. Presumably if
"breakloop" appears outside a switch statement it means exactly
the same thing as "break". How are programmers to decide which
one to use? Would you want to deprecate the use of "break" other
than to break out of a switch statement?
We have decades of precedent that "break" is how you break out of a
loop. Your solution is to add a second way to do the same thing.
Sorry, but it's ugly. I'd rather use goto. And there's already
a more general solution planned for C2y.
[...]
> Simple, yes? Then you think that this could be done even more easily
> 50 years ago.
It could have been done more easily 50 years ago than now. Using,
say, "breaksw" for switch statements and "break" for loops would
have been equally easy, and a better solution that what you propose.
Allowing name for loops and switch statements is a more general
solution to this and other problems, it's already been implemented,
and it will almost certainly be standardized.
I dislike the default fallthrough behavior of switch statements.
If I were designing C today, I'd probably (a) *not* have default
fallthrough, (b) add something like a "fallthrough" keyword for
when you want it, and (c) have named loops. As of C2y, we'll have
(c) but not (a) and (b) -- and that's good enough.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 03:59
Date: Wed, 14 Oct 2026 03:59
114 lines
5578 bytes
5578 bytes
In article <11amecs$10eab$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 13/10/2026 01:33, Keith Thompson wrote:
>> bart <bc@freeuk.com> writes:
>> [...]
>>> This is my first example expressed in C (note those first three args
>>> are passed by reference):
>>>
>>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>>> switch (pmtype)
>>> case pm_option: ...; break;
>>> case pm_sourcefile:
>>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>>> break;
>>>
>>> Except I can't use break like that. So I have to find a workaround.
>>
>> The code appears to be valid C that doesn't do what you want. I can't
>> easily tell what you want it to do. I don't know which of the two break
>> statements was intended to break out of the loop. A comment would have
>> solved that.
>>
>> I'm going to guess that the first break is supposed to apply to the
>> switch statement, and the second to the while loop.
>>
>> You want the language to be changed to make this kind of thing easier.
>>
>> That's already happening. If you take a look at the N3886 working
>> draft for C2y, it has named break statements. In C2y (or in gcc
>> if you use an extension), the above could be written as:
>>
>> WHILE:
>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>> SWITCH:
>> switch (pmtype)
>> case pm_option: ...; break SWITCH;
>> case pm_sourcefile:
>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>> break WHILE;
>>
>> assuming it's the second "break' that's supposed to break out of
>> the loop. (I'd probably use more meaninful names.)
>>
>> There is already a solution for the problem you're complaining about.
>> (It also solves the problem of breaking out of nested loops.)
>>
>> If you're unable to use this feature, you can use goto. That's not
>> ideal, but with well chosen label names and no backward branching
>> it's not too bad.
>
>I've got a better idea. I added an extra keyword to my C compiler called
>"breakloop". It's like 'break' but always works with the nearest
>enclosing loop, even from inside switch.
>
>So this example (a starts at 0):
>
> while (1) {
> switch (a++) {
> case 1: puts("one"); break;
> case 2: puts("two"); breakloop;
> case 3: puts("three"); break;
> default: puts("other");
> }
>
> if (a>40) break;
> }
>
>finishes after displaying 'two', instead of after the 37th or so 'other'.
>
>Modding the compiler took 90 seconds, and needed 4 extra lines of code.
>Setting up this test took 2-3 minutes.
>
>Simple, yes? Then you think that this could be done even more easily 50
>years ago.
Not really; for a general mechanism, if suffers from the obvious
deficiency that it doesn't compose.
For example, if the switch is inside of nested loops, and I
want to break out of everything, or I have two embedded switches
(which I have written when parsing firmware tables); I can think
of a number of instances where you might want to have switch
inside of nested loops and want to break out to function scope,
such as advancing states a la a Thompson-style NFA, where one
has a loop over the input text, a loop over the set of all
states, and a switch matching state against the current input
character, and you want to break out when you find a match.
Many other languages solve this using a "labeled break" (Go,
Rust, C#, Kotlin, Java, Ada, Pike, and now C), though some have
used a "counted"/"multi-level" break (Ruby, Perl; I think PHP).
You could have elegantly solved this problem by making `break`
take an optional integer argument, which is the number of levels
of structure to break out of. For a `switch` inside of a loop,
`break 2;` would exit both the case and the enclosing loop; for
a nested switch, `break 2;` would break out of both. Workable,
but in some respects the labeled form is better because it is
resilient to code shuffling around and changing nesting levels
and so forth.
An obvious next extension would be augmenting `continue`
similarly.
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.
This illustrates one reason why changing a language can be hard:
your idea sounds good at first blush, but it has its own
deficiencies.
- Dan C.
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 04:35
Date: Wed, 14 Oct 2026 04:35
49 lines
3285 bytes
3285 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
[...]
> Many other languages solve this using a "labeled break" (Go,
> Rust, C#, Kotlin, Java, Ada, Pike, and now C), though some have
> used a "counted"/"multi-level" break (Ruby, Perl; I think PHP).
The only languages I know of that have a "counted break" (where
"break 2" breaks out of two levels of loops) are the Bourne shell
and shells derived from it, and PHP (you're right about that).
Perl has named break (spelled "last" or "last NAME") and continue
(spelled "next" or "next NAME). It does not have counted versions.
Ruby allows an argument on "break", but it's not the number of levels.
It specifies the value of the expression that it breaks out of. (A
loop is an expression that can yield a value.)
The latest draft of the C++ standard does not include labeled break
and continue statements. I don't know whether it's been proposed.
> You could have elegantly solved this problem by making `break`
> take an optional integer argument, which is the number of levels
> of structure to break out of.
I don't consider that elegant, for reasons I've already ranted about
here.
[...]
> 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.
Yes, labeled "break" in C2y applies to both loops and switch statements.
There's also a labeled "continue" that applies only to loops.
The labels are optional, so existing code isn't broken.
The latest C2y draft is
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf>
(3.3 MB, 793 pages).
> This illustrates one reason why changing a language can be hard:
> your idea sounds good at first blush, but it has its own
> deficiencies.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Taming The Switch-Statement
Author: Kaz Kylheku
Date: Wed, 14 Oct 2026 05:17
Date: Wed, 14 Oct 2026 05:17
42 lines
3192 bytes
3192 bytes
On 2026-10-13, Michael S <already5chosen@yahoo.com> wrote:
> Of course, I never gathered exact statistics.
> My guts feeling is that among switches nested within loop it could be as
> high as one out of five. Not one out of five case labels, but one out of
> five switch statements.
Switches inside a loop are natural whenever a state machine is being
simulated which has acceptance/failulre states which indicate that the
simulation should end.
Switches inside a loop are also natural whenever a stream of
integer-tagged cases is being handled where a terminating sentinel is
also labeled.
There are at least two workarounds for writing this which avoid
duplicated testing or loop termination flags, gotos or such.
One is to make the loop the last statement of a function and use
"return" in the loop-terminating cases.
The other is to put a "break" at the bottom of the loop and
terminate all the loop-continuing cases with "continue",
the terminating cases with "break":
for (;;) {
int ch = getc(in);
switch (ch) {
case '\n':
lines++;
/* fallthrough */
default:
continue;
case EOF:
break;
}
break;
}
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 10:53
Date: Wed, 14 Oct 2026 10:53
14 lines
1403 bytes
1403 bytes
In article <11am9r9$4o6m$4@dont-email.me>, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >On 2026-10-13 22:52, Dan Cross wrote: >> >> Were you referring to me, Janis? Or to bart? > >You can identify the association of the answers by its attribution >indent level. - Most was attributed to bart's text that was quoted. Sure. I was just surprised that the reply to my post was the vehicle to reply to bart. Anyway, no worries! - Dan C.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 10:58
Date: Wed, 14 Oct 2026 10:58
37 lines
2373 bytes
2373 bytes
In article <11amn48$7ell$1@news.xmission.com>, Kenny McCormack <gazelle@shell.xmission.com> wrote: >In article <11amlq9$12efg$1@kst.eternal-september.org>, >Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>bart <bc@freeuk.com> writes: >>[...] >>> My view is that using distinct keywords for loop-break and >>> switch-break (accepting that the language wants the latter) would have >>> had pretty much zero costs, but worthwhile benefits. >> >>I agree, and I don't think that's particularly controversial. >> >>The point on which we disagree is what can and should be done about >>it now. > >The answer to that is: Invent new languages with C-like syntax, but better. >You can't do anything about C, but you can do better as long as you call >the result something else. > >Like C#. Like C++. Like Swift. I mean...we've got a ton of those now. Go, Rust, Zig, Switch, and on and on. We don't really need to invent new languages. OCaml, Haskell, Julia, ad infinitum. In my view, the bigger impediments to moving off of C as the dominant systems programming language are two fold: 1. providing a clean path from C to the new language(s), and 2. overcoming the enormous inertia of decades of defaulting to C for systems programs. I love C, but I'm not in love with C anymore, and if I am starting a new project these days, it's not the first thing I reach for anymore. - Dan C.
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 14 Oct 2026 11:17
Date: Wed, 14 Oct 2026 11:17
149 lines
7272 bytes
7272 bytes
On 14/10/2026 04:59, Dan Cross wrote:
> In article <11amecs$10eab$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>> I've got a better idea. I added an extra keyword to my C compiler called
>> "breakloop". It's like 'break' but always works with the nearest
>> enclosing loop, even from inside switch.
>>
>> So this example (a starts at 0):
>>
>> while (1) {
>> switch (a++) {
>> case 1: puts("one"); break;
>> case 2: puts("two"); breakloop;
>> case 3: puts("three"); break;
>> default: puts("other");
>> }
>>
>> if (a>40) break;
>> }
>>
>> finishes after displaying 'two', instead of after the 37th or so 'other'.
>>
>> Modding the compiler took 90 seconds, and needed 4 extra lines of code.
>> Setting up this test took 2-3 minutes.
(I've now simplified it to adding one extra line and tweaking another!
I'd even suggest that using distinct keywords in the original C compiler
would have made it a tiny bit simpler.)
>> Simple, yes? Then you think that this could be done even more easily 50
>> years ago.
>
> Not really; for a general mechanism, if suffers from the obvious
> deficiency that it doesn't compose.
>
> For example, if the switch is inside of nested loops, and I
> want to break out of everything,
This isn't the problem being discussed. IME breaking of nested loops is
uncommon, but breaking out of a loop from inside switch /is/ common.
It's using the same 'break' keyword that makes that awkward in C. And my
experiment solves that easily.
> or I have two embedded switches
That's not a problem; 'breakloop' will completely ignore any switch
statements, in the same way that 'continue' does.
Unless you've invented a new requirement where you want 'break' within a
switch (itself solving an artificial problem) to jump straight to the
end of an outer switch.
> (which I have written when parsing firmware tables); I can think
> of a number of instances where you might want to have switch
> inside of nested loops and want to break out to function scope,
> such as advancing states a la a Thompson-style NFA, where one
> has a loop over the input text, a loop over the set of all
> states, and a switch matching state against the current input
> character, and you want to break out when you find a match.
'Jumping to function scope' is poorly defined. Do you mean cutting
across not only all loops, but any containing nested 'if' and 'switch'
statements?
So that would be a separate feature from loop-break? Would it still
involve loops at all, or could it be used from within any statement?
That sounds too general and wide-reaching; I think 'goto' is a much
better fit for that, if 'return' or 'defer' wouldn't work.
> Many other languages solve this using a "labeled break" (Go,
> Rust, C#, Kotlin, Java, Ada, Pike, and now C), though some have
> used a "counted"/"multi-level" break (Ruby, Perl; I think PHP).
>
> You could have elegantly solved this problem by making `break`
> take an optional integer argument, which is the number of levels
> of structure to break out of.
I could have done that in C but it would have taken more than 5 minutes,
and this was anyway a proof of concept.
I've already used that approach for some decades in my other languages,
but as I showed in the tables I posted, it is not common, and where
used, it was always to break out of all loops.
Anyway, I normally use 'all' instead of a specific number to quit all
loops. A number works too but I dislike it.
> For a `switch` inside of a loop,
> `break 2;` would exit both the case and the enclosing loop; for
> a nested switch, `break 2;` would break out of both. Workable,
> but in some respects the labeled form is better because it is
> resilient to code shuffling around and changing nesting levels
> and so forth.
No, it just has a different set of problems. For example, you label the
outer loop and exit to that. Now you add an extra outer loop layer: does
that label go inside this new loop or outside?
That depends on the intended logic, which you don't know.
Maybe the original set of loop performed some task T, and the new loop
wants to repeat T, in which case it must stay inside.
Or maybe it is really has to quit ALL loops including any future outer
ones. Neither labeling or numbering solves this.
Labeled loops may also have a new problem with scoping: is the loop name
local to this nest of loops, or could an outer loop name especially
clash with other loop elsewhere?
> An obvious next extension would be augmenting `continue`
> similarly.
In my other languages, I use 'exit' for break, and 'nextloop' for
continue, and 'redoloop' to repeat iteration. (The latter will have
different behaviour inside a for-loop with formal loop variables.)
I've just done a survey in my major apps and haven't found an instance
where those latter two needed to cross two or more loops.
Although I am sure C programmers would put 'continue x' to good use if
it was available!
> 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!
And maybe those scoping rules will be different: goto labels have
function-wide scope.
> This illustrates one reason why changing a language can be hard:
> your idea sounds good at first blush, but it has its own
> deficiencies.
I appreciate your concern. I've only been doing this (devising and
implementing systems languages) for 45 years!
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 11:27
Date: Wed, 14 Oct 2026 11:27
68 lines
3710 bytes
3710 bytes
In article <11an0q3$14pou$4@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: >[...] >> Many other languages solve this using a "labeled break" (Go, >> Rust, C#, Kotlin, Java, Ada, Pike, and now C), though some have >> used a "counted"/"multi-level" break (Ruby, Perl; I think PHP). > >The only languages I know of that have a "counted break" (where >"break 2" breaks out of two levels of loops) are the Bourne shell >and shells derived from it, and PHP (you're right about that). > >Perl has named break (spelled "last" or "last NAME") and continue >(spelled "next" or "next NAME). It does not have counted versions. > >Ruby allows an argument on "break", but it's not the number of levels. >It specifies the value of the expression that it breaks out of. (A >loop is an expression that can yield a value.) Ah, you're right and I was simply wrong about Perl and Ruby. I must have hallucinated those. Regarding expression oriented languages where constructs like loops can return values, it's somewhat ironic that BCPL could do something like that using its, `valof`/`resultis` mechanism; I have often wanted something like that in C; it would obviate the ternary operator. >The latest draft of the C++ standard does not include labeled break >and continue statements. I don't know whether it's been proposed. That's unfortunate. I wonder if that's merely an oversight, or a conscious decision; I'll have to look it up later. >> You could have elegantly solved this problem by making `break` >> take an optional integer argument, which is the number of levels >> of structure to break out of. > >I don't consider that elegant, for reasons I've already ranted about >here. Fair. I do believe that the labeled variant is better for a host of reasons, though I don't love the syntax, but that is merely a subjective aesthetic judgement on my part. >[...] > >> 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. > >Yes, labeled "break" in C2y applies to both loops and switch statements. >There's also a labeled "continue" that applies only to loops. >The labels are optional, so existing code isn't broken. > >The latest C2y draft is ><https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf> >(3.3 MB, 793 pages). (Thanks; I've been meaning to download a copy of the latest draft anyway.) It seems that the matter is settled conclusively, and this entire thread is lots of heat and light generated for no reason. - Dan C.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 12:38
Date: Wed, 14 Oct 2026 12:38
300 lines
13326 bytes
13326 bytes
In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 14/10/2026 04:59, Dan Cross wrote:
>> In article <11amecs$10eab$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>>> I've got a better idea. I added an extra keyword to my C compiler called
>>> "breakloop". It's like 'break' but always works with the nearest
>>> enclosing loop, even from inside switch.
>>>
>>> So this example (a starts at 0):
>>>
>>> while (1) {
>>> switch (a++) {
>>> case 1: puts("one"); break;
>>> case 2: puts("two"); breakloop;
>>> case 3: puts("three"); break;
>>> default: puts("other");
>>> }
>>>
>>> if (a>40) break;
>>> }
>>>
>>> finishes after displaying 'two', instead of after the 37th or so 'other'.
>>>
>>> Modding the compiler took 90 seconds, and needed 4 extra lines of code.
>>> Setting up this test took 2-3 minutes.
>
>(I've now simplified it to adding one extra line and tweaking another!
>
>I'd even suggest that using distinct keywords in the original C compiler
>would have made it a tiny bit simpler.)
Unlikely. I'm trying to chase down the citation, but it was
known that getting B to fit on the PDP-7 was a challenge. From
the C history paper,
|After the TMG version of B was working, Thompson rewrote B in
|itself (a bootstrapping step). During development, he
|continually struggled against memory limitations: each language
|addition inflated the compiler so it could barely fit, but each
|rewrite taking advantage of the feature reduced its size. For
|example, B introduced generalized assignment operators, using
|`x=+y` to add `y` to `x`. The notation came from Algol 68
|[Wijngaarden 75] via McIlroy, who had incorporated it into his
|version of TMG. (In B and early C, the operator was spelled
|`=+` instead of `+=`; this mistake, repaired in 1976, wa
| induced by a seductively easy way of handling the first form
|in B's lexical analyzer.)
(https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html)
[Context: "TMG" was a compiler-compiler, similar to yacc]
Early C was not much better. Compiler sources for some of the
earliest Unix C compilers are available, and pretty interesting
reads: one can see that great pains were taken so that the
compiler actually fits onto the machine. By the time they were
on a PDP-11/45 class machine (with split I- and D-space) things
were somewhat better, but by then the basic design of the
language was set and adding an extra keyword just for
terminating cases in a `switch` was likely seen as unnecessary.
>>> Simple, yes? Then you think that this could be done even more easily 50
>>> years ago.
>>
>> Not really; for a general mechanism, if suffers from the obvious
>> deficiency that it doesn't compose.
>>
>> For example, if the switch is inside of nested loops, and I
>> want to break out of everything,
>
>This isn't the problem being discussed. IME breaking of nested loops is
>uncommon, but breaking out of a loop from inside switch /is/ common.
The irony here, of course, is that you are claiming that C is
deficient because it doesn't solve what you see as a problem,
but trying to force the problem space specifically so that it
fits the solution your proposed, so that you can avoid
confronting that proposal's deficiencies.
The problem space is actually much wider than what you have
narrowly defined it to be. *You* don't see that as interesting,
so you simply ignore it.
In this sense, your proposal is strictly a regression: it adds
complexity to the language by introducing a new keyword, but it
only solves one tiny problem, but nothing beyond that.
Yes, I am aware that you will tell me that it is justified
because that problem is actually the one worth solving, and the
others are not; you needn't bother. Simply put, I disagree that
that one problem is sufficiently important to warrant its own
solution, particularly when something more general is already
known and available in the language (labeled break/continue).
>It's using the same 'break' keyword that makes that awkward in C. And my
>experiment solves that easily.
...except for all the other cases where it is still awkward and
solves nothing.
>> or I have two embedded switches
>
>That's not a problem; 'breakloop' will completely ignore any switch
>statements, in the same way that 'continue' does.
Yes, but it does nothing to break out of the outer switch, if
that's what I want; the inner switch might be followed by more
statements in the case it is nested in.
>Unless you've invented a new requirement where you want 'break' within a
>switch (itself solving an artificial problem) to jump straight to the
>end of an outer switch.
Yes, that's exactly what I'm saying. Again, you've identified
one narrow problem, proposed a solution for that problem, and
decided any identified issues with that solution don't apply
because they don't affect the narrowly defined problem that you
have decided is important.
Past behavior suggests that you imagine that anyone pointing out
a reason why your solution may not be everything you think it is
is somehow unfairly changing the rules of the game in order to
give you a hard time. But consider: perhaps they're just
pointing out a reason why your solution isn't optimal.
I think this is the central reason people don't take your
criticism of C particularly seriously. There is space for
discussing how C _could_ be different, but if you are not
willing to engage in actually understanding the problems, and
actually, I don't know, having a discussion about your proposed
solutions and recognizing when they fall short, then you're not
being serious.
>> (which I have written when parsing firmware tables); I can think
>> of a number of instances where you might want to have switch
>> inside of nested loops and want to break out to function scope,
>> such as advancing states a la a Thompson-style NFA, where one
>> has a loop over the input text, a loop over the set of all
>> states, and a switch matching state against the current input
>> character, and you want to break out when you find a match.
>
>'Jumping to function scope' is poorly defined. Do you mean cutting
>across not only all loops, but any containing nested 'if' and 'switch'
>statements?
What I mean is this:
void
foo(void)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; i++) {
switch (something) {
case ...:
// I want to break out
// of the switch and
// both loops here.
goto label;
}
}
}
label:
}
>So that would be a separate feature from loop-break? Would it still
>involve loops at all, or could it be used from within any statement?
>
>That sounds too general and wide-reaching; I think 'goto' is a much
>better fit for that, if 'return' or 'defer' wouldn't work.
Why? The labeled break mechanism that looks like it's coming in
C2Y neatly solves this and your original problem; it's a
consistent, general mechanism. It also handles loops inside of
switches, and other cases your proposal doesn't handle.
>> Many other languages solve this using a "labeled break" (Go,
>> Rust, C#, Kotlin, Java, Ada, Pike, and now C), though some have
>> used a "counted"/"multi-level" break (Ruby, Perl; I think PHP).
>>
>> You could have elegantly solved this problem by making `break`
>> take an optional integer argument, which is the number of levels
>> of structure to break out of.
>
>I could have done that in C but it would have taken more than 5 minutes,
>and this was anyway a proof of concept.
Yup. And it took less than that to point out problems.
>I've already used that approach for some decades in my other languages,
>but as I showed in the tables I posted, it is not common, and where
>used, it was always to break out of all loops.
>
>Anyway, I normally use 'all' instead of a specific number to quit all
>loops. A number works too but I dislike it.
That sounds overly specific.
>> For a `switch` inside of a loop,
>> `break 2;` would exit both the case and the enclosing loop; for
>> a nested switch, `break 2;` would break out of both. Workable,
>> but in some respects the labeled form is better because it is
>> resilient to code shuffling around and changing nesting levels
>> and so forth.
>
>No, it just has a different set of problems. For example, you label the
>outer loop and exit to that. Now you add an extra outer loop layer: does
>that label go inside this new loop or outside?
This is actually the argument against the counted multi-level
break: if I add or remove a level of nesting, I have to find and
adjust all of the counts. The labeled break mechanism ties the
`break` or `continue` to another syntactic element (a label), so
if I add or remove a nesting level, I'm still breaking to where
I had intended to before.
That said, applying the argument to the labeled case is silly.
Software engineering and good programming practice still matter.
If you are changing the nested looping structure of your
program, then you should know where you want to break to;
presumably your program's behavior depends on it.
I'd say the stronger argument for the labeled control structure
is actually nested switch statements, where I may change a
series of `if`s to a `switch` with a `case` that contains a
nested `switch`, or I may change a `switch` to an `if`; if the
intent is to break out of an enclosing loop, labeled breaks are
resilient to that, but counted breaks are not.
>That depends on the intended logic, which you don't know.
Yes.
>Maybe the original set of loop performed some task T, and the new loop
>wants to repeat T, in which case it must stay inside.
Yes.
>Or maybe it is really has to quit ALL loops including any future outer
>ones. Neither labeling or numbering solves this.
No. If I want to break out of *all* loops, I just make sure
that the outermost loop is labeled thusly.
>Labeled loops may also have a new problem with scoping: is the loop name
>local to this nest of loops, or could an outer loop name especially
>clash with other loop elsewhere?
Since labels are, you know, labels, they follow the semantics of
... labels.
>> An obvious next extension would be augmenting `continue`
>> similarly.
>
>In my other languages, I use 'exit' for break, and 'nextloop' for
>continue, and 'redoloop' to repeat iteration. (The latter will have
>different behaviour inside a for-loop with formal loop variables.)
>
>I've just done a survey in my major apps and haven't found an instance
>where those latter two needed to cross two or more loops.
Anecdotal evidence, I'm afraid.
>Although I am sure C programmers would put 'continue x' to good use if
>it was available!
Yes.
>> 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!
The problem there is that `break` and `continue` would require
different labels: `continue loop;` conceptually jumps to the
_end_ of the loop body, and resumes on the next iteration, while
`break loop;` jumps to the next statement immediately following
the loop body. With the named-label mechanism, the same "name"
can be used for both.
>And maybe those scoping rules will be different: goto labels have
>function-wide scope.
It's easy enough to look it up in the draft document; Keith
posted a link.
>> This illustrates one reason why changing a language can be hard:
>> your idea sounds good at first blush, but it has its own
>> deficiencies.
>
>I appreciate your concern. I've only been doing this (devising and
>implementing systems languages) for 45 years!
That really doesn't mean anything. Just doing something for a
long time doesn't mean you're good at it: I have been shooting
baskets for 45 years, but I will never be a point guard in the
NBA.
- Dan C.
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 14 Oct 2026 12:46
Date: Wed, 14 Oct 2026 12:46
69 lines
4065 bytes
4065 bytes
On 14/10/2026 06:17, Kaz Kylheku wrote:
> On 2026-10-13, Michael S <already5chosen@yahoo.com> wrote:
>> Of course, I never gathered exact statistics.
>> My guts feeling is that among switches nested within loop it could be as
>> high as one out of five. Not one out of five case labels, but one out of
>> five switch statements.
>
> Switches inside a loop are natural whenever a state machine is being
> simulated which has acceptance/failulre states which indicate that the
> simulation should end.
>
> Switches inside a loop are also natural whenever a stream of
> integer-tagged cases is being handled where a terminating sentinel is
> also labeled.
>
> There are at least two workarounds for writing this which avoid
> duplicated testing or loop termination flags, gotos or such.
>
> One is to make the loop the last statement of a function and use
> "return" in the loop-terminating cases.
>
> The other is to put a "break" at the bottom of the loop and
> terminate all the loop-continuing cases with "continue",
> the terminating cases with "break":
>
> for (;;) {
> int ch = getc(in);
> switch (ch) {
> case '\n':
> lines++;
> /* fallthrough */
> default:
> continue;
> case EOF:
> break;
> }
> break;
> }
>
Well, it's a novel workaround, but it is still a workaround if the logic
could have been more naturally expressed like this:
for (;;) {
switch (ch = getch()) { // I assume ch needed for other cases
case '\n':
lines++;
break;
case EOF:
BREAK;
}
}
BREAK represents loop exit. The point is to avoid having to restructure
your code just to get around a silly shortcoming. In practice, using
'goto' is the simplest workaround.
(This a pattern that is common enough for me to have a special syntax
for in my projects. Then it is expressed as:)
doswitch ch := getch(in)
when '\n' then
++lines
when EOF then
exit
end
Re: Taming The Switch-Statement
Author: Scott Lurndal
Date: Wed, 14 Oct 2026 14:35
Date: Wed, 14 Oct 2026 14:35
12 lines
1507 bytes
1507 bytes
bart <bc@freeuk.com> writes: >On 13/10/2026 01:33, Keith Thompson wrote: >> bart <bc@freeuk.com> writes: >> [...] >I've got a better idea. I added an extra keyword to my C compiler called >"breakloop". It's like 'break' but always works with the nearest >enclosing loop, even from inside switch. It's hardly a new idea. The 1967 BPL compiler at burroughs supported 'EXITLOOP', 'EXITCOND', 'EXITCASE' and 'EXITBLOCK'; each with the expected semantics for the name.
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 14 Oct 2026 15:49
Date: Wed, 14 Oct 2026 15:49
240 lines
10120 bytes
10120 bytes
On 14/10/2026 13:38, Dan Cross wrote:
> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>> I'd even suggest that using distinct keywords in the original C compiler
>> would have made it a tiny bit simpler.)
>
> Unlikely. I'm trying to chase down the citation, but it was
> known that getting B to fit on the PDP-7 was a challenge. From
> the C history paper,
Perhaps Switch should have been left out then if it was that tight.
Sharing a keyword is just crass. Or maybe they should have considered
not needing 'break' for switch.
> The irony here, of course, is that you are claiming that C is
> deficient because it doesn't solve what you see as a problem,
> but trying to force the problem space specifically so that it
No, /you/ are trying to discredit a simple solution by widening the
goalposts.
The main problem being discussed, and the one my test fixed, is that of
'break' being used for two purposes and therefore resulting in clashes
when Switches and Loops are mixed.
That could easily have been fixed by DMR choosing to use distinct
keywords. Do you agree so far?
That of breaking out of nested loops is a different problem that would
need a different solution, and nothing to do with the clash with switch.
It is also one that seems to be about 100 times less common IME.
> ...except for all the other cases where it is still awkward and
> solves nothing.
IT WAS ONLY SUPPOSED TO SOLVE THIS PROBLEM! Namely the issues that
ensued from DMR overloading 'break'. NOTHING ELSE.
In fact the confusion between switch-break and loop-break will still be
there even if labels are added.
>> Unless you've invented a new requirement where you want 'break' within a
>> switch (itself solving an artificial problem) to jump straight to the
>> end of an outer switch.
>
> Yes, that's exactly what I'm saying. Again, you've identified
> one narrow problem,
It's not that narrow a problem, unless you want to call loops themselves
a narrow feature.
It is one that many languages solve for LOOPs by using break or
equivalent. But they tend not to broaden it either to include breaks out
of arbitrary kinds of blocks.
> I think this is the central reason people don't take your
> criticism of C particularly seriously.
I'm sorry but here I don't take yours seriously either. (Have you ever
implemented any of this stuff, or even used such features that you propose?)
>> 'Jumping to function scope' is poorly defined. Do you mean cutting
>> across not only all loops, but any containing nested 'if' and 'switch'
>> statements?
>
> What I mean is this:
>
> void
> foo(void)
> {
> for (int i = 0; i < n; i++) {
> for (int j = 0; j < m; i++) {
> switch (something) {
> case ...:
> // I want to break out
> // of the switch and
> // both loops here.
> goto label;
> }
> }
> }
> label:
> }
This example just breaks out of both loops. That the outer one is at
function scope is incidental.
If the outer loop was contained within an IF statement, could you jump
to outside that statement using your proposed feature? If not then that
is not function scope.
>> I could have done that in C but it would have taken more than 5
minutes,>> and this was anyway a proof of concept.
>
> Yup. And it took less than that to point out problems.
But it solved the problem I wanted to solve! It would solve the problem
Michael S reported! That is, using distinct keywords for two different
things.
>> I've already used that approach for some decades in my other languages,
>> but as I showed in the tables I posted, it is not common, and where
>> used, it was always to break out of all loops.
>>
>> Anyway, I normally use 'all' instead of a specific number to quit all
>> loops. A number works too but I dislike it.
>
> That sounds overly specific.
So is breaking out of an inner loop with 'break;'! But it works well as
it's by far the most common requirement. The next most common IME is to
quit all loops (like you do in your example).
In fact, if there are only two nested loops, the inner and outer cover
all the possibilities.
> This is actually the argument against the counted multi-level
> break: if I add or remove a level of nesting, I have to find and
> adjust all of the counts. The labeled break mechanism ties the
> `break` or `continue` to another syntactic element (a label), so
No, it's just another set of problems as I said.
You can add new inner loops, delete inner loops, add outer ones, and
each time you will need to consider whether the labels need to be moved,
or extra ones added.
Here's another solution I favour (not implemented), but it only works
for FOR loops:
for i in a..b
for j in c..d
... break i
It's a little like using a label, but here that 'label' is the loop
index. (This won't work in C as there are no formal loop indices.)
> if I add or remove a nesting level, I'm still breaking to where
> I had intended to before.
Yes, but that might now be the wrong loop.
> That said, applying the argument to the labeled case is silly.
> Software engineering and good programming practice still matter.
> If you are changing the nested looping structure of your
> program, then you should know where you want to break to;
> presumably your program's behavior depends on it.
Agreed; see below.
> I'd say the stronger argument for the labeled control structure
> is actually nested switch statements, where I may change a
> series of `if`s to a `switch` with a `case` that contains a
> nested `switch`, or I may change a `switch` to an `if`; if the
> intent is to break out of an enclosing loop, labeled breaks are
> resilient to that, but counted breaks are not.
You're assuming that the count in indexed breaks includes switch
statements. It shouldn't; that would be preposterous, and it all still
comes down to the shared 'break' keyword.
How about fixing that first, or even admitting it was wrong, before
>> Maybe the original set of loop performed some task T, and the new
loop>> wants to repeat T, in which case it must stay inside.
>
> Yes.
>
>> Or maybe it is really has to quit ALL loops including any future outer
>> ones. Neither labeling or numbering solves this.
>
> No. If I want to break out of *all* loops, I just make sure
> that the outermost loop is labeled thusly.
But in my first example, that is not what you want! It all depends.
I don't like using multi-level loop exit, it's an untidy feature even
though I implement it. And I don't like using labeled solutions, as they
just turn them into thinly disguised gotos.
>> Labeled loops may also have a new problem with scoping: is the loop name
>> local to this nest of loops, or could an outer loop name especially
>> clash with other loop elsewhere?
>
> Since labels are, you know, labels, they follow the semantics of
> ... labels.
Careful here: do you really mean that loop-labels will be in the same
name-space as ordinary labels? That means:
* Getting clashes when a labeled loop is copied elsewhere within the
function
* Being able to do an ordinary 'goto' to a loop-label ...
* ... which will also be confusing when loop labels are placed at the
beginning of a loop rather than the end:
L1:
while () {
goto L1: This jumps to L1
goto L2; This jumps to L2
continue L1; This jumps to L2
break L1; This jumps to L3
L2:
}
L3:
See there's a bit more to this language-design lark!
>> I've just done a survey in my major apps and haven't found an instance
>> where those latter two needed to cross two or more loops.
>
> Anecdotal evidence, I'm afraid.
You're welcome to post your own surveys. Although unless the languages
used in your survey support such features, you may find that hard.
> It's easy enough to look it up in the draft document; Keith
> posted a link.
Is that that 793-page document? Perhaps you can summarise how it works!
>
>>> This illustrates one reason why changing a language can be hard:
>>> your idea sounds good at first blush, but it has its own
>>> deficiencies.
>>
>> I appreciate your concern. I've only been doing this (devising and
>> implementing systems languages) for 45 years!
>
> That really doesn't mean anything. Just doing something for a
> long time doesn't mean you're good at it: I have been shooting
> baskets for 45 years, but I will never be a point guard in the
> NBA.
And yet I've solved all the problems in this thread and many others.
Re: Taming The Switch-Statement
Author: Janis Papanagnou
Date: Wed, 14 Oct 2026 15:54
Date: Wed, 14 Oct 2026 15:54
30 lines
2668 bytes
2668 bytes
On 2026-10-14 12:53, Dan Cross wrote: > In article <11am9r9$4o6m$4@dont-email.me>, > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >> On 2026-10-13 22:52, Dan Cross wrote: >>> >>> Were you referring to me, Janis? Or to bart? >> >> You can identify the association of the answers by its attribution >> indent level. - Most was attributed to bart's text that was quoted. > > Sure. I was just surprised that the reply to my post was the > vehicle to reply to bart. Anyway, no worries! I'm sorry for that. I stopped reading bart's posts for various reasons rooted in his personal, um.., communication style, and the mass of his posts of, um.., peculiar content. I don't want to contribute to that flood any more. But occasionally I see that other posters take that task (or burden?) to reply to him, and reading their posts I then see also what bart wrote, and occasionally still comment on what I then see. (Going to try reducing that as well; as it's rarely worth the hassle in this specific case.) Janis PS: I've just read another long (300 lines) post you made in reply to bart. Let me express my admiration for your patience and countenance (that I'm clearly lacking in this case), and the very clear form and conclusively explained content.
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 14 Oct 2026 17:51
Date: Wed, 14 Oct 2026 17:51
30 lines
2292 bytes
2292 bytes
On 14/10/2026 13:38, Dan Cross wrote:
> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
> Yes, I am aware that you will tell me that it is justified
> because that problem is actually the one worth solving, and the
> others are not; you needn't bother. Simply put, I disagree that
> that one problem is sufficiently important to warrant its own
> solution, particularly when something more general is already
> known and available in the language (labeled break/continue).
I missed this bit.
Is labeled break now available in C?
If so, how would it be used here:
while (1) {
switch (x) {
case y:
Break;
where that Break is meant to quit the loop.
Remember that if DMR hadn't made that screw-up in 1972 it would simply
have been as it is above with his choice of keyword in place of Break.
But I would be interested in trying out the new feature.
Re: Taming The Switch-Statement
Author: Peyton
Date: Wed, 14 Oct 2026 18:04
Date: Wed, 14 Oct 2026 18:04
50 lines
2646 bytes
2646 bytes
bart <bc@freeuk.com> writes:
> On 14/10/2026 13:38, Dan Cross wrote:
>> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>
>> Yes, I am aware that you will tell me that it is justified
>> because that problem is actually the one worth solving, and the
>> others are not; you needn't bother. Simply put, I disagree that
>> that one problem is sufficiently important to warrant its own
>> solution, particularly when something more general is already
>> known and available in the language (labeled break/continue).
>
> I missed this bit.
>
> Is labeled break now available in C?
>
> If so, how would it be used here:
>
>
> while (1) {
> switch (x) {
> case y:
> Break;
>
> where that Break is meant to quit the loop.
>
> Remember that if DMR hadn't made that screw-up in 1972 it would simply
> have been as it is above with his choice of keyword in place of Break.
>
> But I would be interested in trying out the new feature.
>
>
No it's definetely not supported yet. I am very much in support of it
though. Not sure when or if. In the meantime I guess you can use the
forbidden goto like so:
while(1) {
switch(x) {
case 1:
doThing();
break;
case 2:
goto exitLoop;
default:
break;
}
}
exitLoop;
--
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 19:30
Date: Wed, 14 Oct 2026 19:30
436 lines
16632 bytes
16632 bytes
In article <11ao8af$1k8p3$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 14/10/2026 13:38, Dan Cross wrote:
>> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>>> I'd even suggest that using distinct keywords in the original C compiler
>>> would have made it a tiny bit simpler.)
>>
>> Unlikely. I'm trying to chase down the citation, but it was
>> known that getting B to fit on the PDP-7 was a challenge. From
>> the C history paper,
>
>Perhaps Switch should have been left out then if it was that tight.
Why? It's genuinely useful.
>Sharing a keyword is just crass.
Don't you share `end`?
>Or maybe they should have considered
>not needing 'break' for switch.
Maybe? And yet here we are, 50+ years later.
>> The irony here, of course, is that you are claiming that C is
>> deficient because it doesn't solve what you see as a problem,
>> but trying to force the problem space specifically so that it
>
>No, /you/ are trying to discredit a simple solution by widening the
>goalposts.
Here we go again. Suffice it to say, no one is trying to
persecute you. People would take you more seriously if you
stopped whining about being a victim all the time.
>The main problem being discussed,
No, that is the problem _you_ are discussing. You're ignoring
any other problem anyone else points out, and you seem incapable
of considering that people might be dismissing your proposed
solution because it doesn't solve enough of a problem, and other
mechanisms are more general.
I recognize you are not going to understand, let alone
appreciate, the distinction here.
>and the one my test fixed, is that of
>'break' being used for two purposes and therefore resulting in clashes
>when Switches and Loops are mixed.
And what many others in this thread have told you is that that
is a "problem" that is trivially worked around, that a special
case for _that problem only_ is not worth it, and that a more
general solution is being added to the language in C2y.
What more do you want anyone to say?
>That could easily have been fixed by DMR choosing to use distinct
>keywords. Do you agree so far?
No. You've described what I consider to be a non-problem; a
mild annoyance at best, and certainly not something that
deserves complicating the language for.
The general category is more interesting, which is what I (and
probably others) have been trying to tell you. Instead of
appreciating that a more general solution to the entire category
of problems, that oh-by-the-way also solves this specific
problem exists, you instead whine about people moving goalposts.
No wonder the language never gets better in your view; you can't
see anything beyond your way of doing things.
>That of breaking out of nested loops is a different problem that would
>need a different solution, and nothing to do with the clash with switch.
Wrong. The new solution shows that it's actually the same
solution to the entire category of problems. And that's the
issue: it doesn't _need_ a different solution.
>It is also one that seems to be about 100 times less common IME.
...and yet it's easily solved using the labeled break mechanism
that is _already in the C2y draft_.
>> ...except for all the other cases where it is still awkward and
>> solves nothing.
>
>IT WAS ONLY SUPPOSED TO SOLVE THIS PROBLEM! Namely the issues that
>ensued from DMR overloading 'break'. NOTHING ELSE.
But that problem is easily solved using labeled breaks.
Overloading `break` is orthogonal to the solution.
>In fact the confusion between switch-break and loop-break will still be
>there even if labels are added.
I don't know that many C programmers are "confused" by the two
uses of that single keyword. Certainly, I've seen no evidence
that such confusion is widespread.
>>> Unless you've invented a new requirement where you want 'break' within a
>>> switch (itself solving an artificial problem) to jump straight to the
>>> end of an outer switch.
>>
>> Yes, that's exactly what I'm saying. Again, you've identified
>> one narrow problem,
>
>It's not that narrow a problem, unless you want to call loops themselves
>a narrow feature.
>
>It is one that many languages solve for LOOPs by using break or
>equivalent. But they tend not to broaden it either to include breaks out
>of arbitrary kinds of blocks.
I posted a bunch of languages that use labeled breaks earlier.
>> I think this is the central reason people don't take your
>> criticism of C particularly seriously.
>
>I'm sorry but here I don't take yours seriously either. (Have you ever
>implemented any of this stuff, or even used such features that you propose?)
Yes.
>>> 'Jumping to function scope' is poorly defined. Do you mean cutting
>>> across not only all loops, but any containing nested 'if' and 'switch'
>>> statements?
>>
>> What I mean is this:
>>
>> void
>> foo(void)
>> {
>> for (int i = 0; i < n; i++) {
>> for (int j = 0; j < m; i++) {
>> switch (something) {
>> case ...:
>> // I want to break out
>> // of the switch and
>> // both loops here.
>> goto label;
>> }
>> }
>> }
>> label:
>> }
>
>This example just breaks out of both loops. That the outer one is at
>function scope is incidental.
>
>If the outer loop was contained within an IF statement, could you jump
>to outside that statement using your proposed feature? If not then that
>is not function scope.
I thought that the context would have made it obvious what I
meant. I think you are attempting to be pedantic because you
want to save face by scoring a rhetorical point. Fine, my
earlier phrasing was poor. Happy?
With labeled break, this is actually quite clear: `break foo;`
jumps to the point immediately after the loop or switch
construct "named" by the label `foo`. Perhaps if you were more
familiar with the problem space it would have have been clearer.
> >> I could have done that in C but it would have taken more than 5
>minutes,>> and this was anyway a proof of concept.
>>
>> Yup. And it took less than that to point out problems.
>
>But it solved the problem I wanted to solve! It would solve the problem
>Michael S reported! That is, using distinct keywords for two different
>things.
That's the thing: that's not what the fundamental problem is.
The _actual_ problem is that, because of that and absent some
more expressive mechanism like a labeled break, you have no way
to express that you want to leave anything more than the most
deeply nested level.
for (;;) {
int ch = getchar();
switch (ch) {
case EOF:
break; // No way to say break out of the loop.
case 'a':
// ...whatever you want to do with 'a'....
break; // Continue the loop.
default:
// whatever....
break;
}
}
You are fixating on the overloaded keyword as _the problem_ and
thus intent on addressing that as _the solution_. But there the
_problem_ is actually more general. Here's how this might be
addressed using a labeled break:
loop: for (;;) {
int ch = getchar();
switch (ch) {
case EOF:
break loop;
case 'a':
// whatever.
break;
default:
// whatever.
break;
}
}
// Control resumes here after `break loop` above.
See? Problem solved. Along with a bunch of _other_ problems:
sw: switch (foo) {
case 'a':
while ((bar = whatever()) != -1) {
if (bar == SOMETHING) break sw;
}
whatnot();
break;
default:
// etc....
break;
}
>>> I've already used that approach for some decades in my other languages,
>>> but as I showed in the tables I posted, it is not common, and where
>>> used, it was always to break out of all loops.
>>>
>>> Anyway, I normally use 'all' instead of a specific number to quit all
>>> loops. A number works too but I dislike it.
>>
>> That sounds overly specific.
>
>So is breaking out of an inner loop with 'break;'! But it works well as
>it's by far the most common requirement. The next most common IME is to
>quit all loops (like you do in your example).
>
>In fact, if there are only two nested loops, the inner and outer cover
>all the possibilities.
...and all of this can be done with labeled break, which is
being added to the language.
Look, bart! Rejoice! The 50 year sin cardinal sin of Ritchie is
being corrected! Surely you must be simultaneously vindicated
and elated.
>> This is actually the argument against the counted multi-level
>> break: if I add or remove a level of nesting, I have to find and
>> adjust all of the counts. The labeled break mechanism ties the
>> `break` or `continue` to another syntactic element (a label), so
>
>No, it's just another set of problems as I said.
Not really.
>You can add new inner loops, delete inner loops, add outer ones, and
>each time you will need to consider whether the labels need to be moved,
>or extra ones added.
Yup. Welcome to being a programmer. Play your cards right and
one day you may even graduate to being a software engineer.
>Here's another solution I favour (not implemented), but it only works
>for FOR loops:
>
> for i in a..b
> for j in c..d
> ... break i
>
>It's a little like using a label, but here that 'label' is the loop
>index. (This won't work in C as there are no formal loop indices.)
And with C2y, you can write:
outer: for (int i = a, i < b; i++) {
for (int j = c; j < d; j++) {
// ...
break outer;
}
}
// We continue here after `break outer`.
Problem solved.
>> if I add or remove a nesting level, I'm still breaking to where
>> I had intended to before.
>
>Yes, but that might now be the wrong loop.
Yup. But note the difference between "may be" and "will be".
With counts, it will be. And as I said, none of these
constructs absolve one of the responsibility to be a competent
programmer.
>> That said, applying the argument to the labeled case is silly.
>> Software engineering and good programming practice still matter.
>> If you are changing the nested looping structure of your
>> program, then you should know where you want to break to;
>> presumably your program's behavior depends on it.
>
>Agreed; see below.
>
>> I'd say the stronger argument for the labeled control structure
>> is actually nested switch statements, where I may change a
>> series of `if`s to a `switch` with a `case` that contains a
>> nested `switch`, or I may change a `switch` to an `if`; if the
>> intent is to break out of an enclosing loop, labeled breaks are
>> resilient to that, but counted breaks are not.
>
>You're assuming that the count in indexed breaks includes switch
>statements. It shouldn't; that would be preposterous, and it all still
>comes down to the shared 'break' keyword.
>
>How about fixing that first, or even admitting it was wrong, before
That's the thing; it's orthogonal to the root problem. "Admit
that it was wrong" is the wrong way to look at it. Was it
weird? I dunno; maybe. Unconventional at the time? I have no
idea. Fundamentally broken? No.
> >> Maybe the original set of loop performed some task T, and the new
>loop>> wants to repeat T, in which case it must stay inside.
>>
>> Yes.
>>
>>> Or maybe it is really has to quit ALL loops including any future outer
>>> ones. Neither labeling or numbering solves this.
>>
>> No. If I want to break out of *all* loops, I just make sure
>> that the outermost loop is labeled thusly.
>
>But in my first example, that is not what you want! It all depends.
As I said, welcome to being a programmer. Hard, isn't it?
>I don't like using multi-level loop exit, it's an untidy feature even
>though I implement it. And I don't like using labeled solutions, as they
>just turn them into thinly disguised gotos.
ALL of these control constructs are "thinly disguised gotos."
That is the arc of development over time: we started with blunt
instruments like `goto`, and refined practices to where we have
identified useful patterns, that we then extract into first
class constructs of their own.
This ebbs and flows over time; C++-style exceptions were seen as
a necessary feature at one point, now many people strive to
avoid them; Google prohibits their use outright, in a code base
with billions of lines of code written in that language.
>>> Labeled loops may also have a new problem with scoping: is the loop name
>>> local to this nest of loops, or could an outer loop name especially
>>> clash with other loop elsewhere?
>>
>> Since labels are, you know, labels, they follow the semantics of
>> ... labels.
>
>Careful here: do you really mean that loop-labels will be in the same
>name-space as ordinary labels? That means:
Yes, that's what I mean. Go read the draft standard yourself.
>[snip chest thumping puffery]
>
>See there's a bit more to this language-design lark!
Indeed! You demonstrated just how easily it can break down with
your `breakloop` proposal.
>>> I've just done a survey in my major apps and haven't found an instance
>>> where those latter two needed to cross two or more loops.
>>
>> Anecdotal evidence, I'm afraid.
>
>You're welcome to post your own surveys. Although unless the languages
>used in your survey support such features, you may find that hard.
I'm not the one trying to make the point you are attempting to
make. You made a claim; the onus is on you to provide evidence
for that claim.
>> It's easy enough to look it up in the draft document; Keith
>> posted a link.
>
>Is that that 793-page document? Perhaps you can summarise how it works!
I understand; it's a difficult document to follow when you are
already pre-biased to disregard what it says, as you have
repeatedly shown yourself to be.
In this case, it's not too hard, though; the second page says,
|The following changes were made on or after the 2024
|September/October Minneapolis, USA meeting:
|
| ...about 2/3rds of the way down the list...
| - N3355: Named/Labeled Loops
So we can look at N3355 for discussion:
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm
That's much shorter than the 793-page draft standard. Perhaps
you'll find it easier to understand.
>>>> This illustrates one reason why changing a language can be hard:
>>>> your idea sounds good at first blush, but it has its own
>>>> deficiencies.
>>>
>>> I appreciate your concern. I've only been doing this (devising and
>>> implementing systems languages) for 45 years!
>>
>> That really doesn't mean anything. Just doing something for a
>> long time doesn't mean you're good at it: I have been shooting
>> baskets for 45 years, but I will never be a point guard in the
>> NBA.
>
>And yet I've solved all the problems in this thread and many others.
Congratulations! And yet, despite you being obviously more
intelligent than all of Ritchie, McIlroy, Thompson, Johnson,
Kernighan, Plauger, and all of those who have and continue to
contribute to the development of C combined, we don't see your
name on the list of contributors to that language, nor does
anyone other than you use your language.
But keep shooting hoops, bart. Maybe some day a talent scout
will see you in your driveway and give you a starting position
on a playoff team.
- Dan C.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 19:47
Date: Wed, 14 Oct 2026 19:47
79 lines
3201 bytes
3201 bytes
In article <11aoff0$1n82v$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 14/10/2026 13:38, Dan Cross wrote:
>> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>
>> Yes, I am aware that you will tell me that it is justified
>> because that problem is actually the one worth solving, and the
>> others are not; you needn't bother. Simply put, I disagree that
>> that one problem is sufficiently important to warrant its own
>> solution, particularly when something more general is already
>> known and available in the language (labeled break/continue).
>
>I missed this bit.
>
>Is labeled break now available in C?
It's in C2y, and has been implemented in (at least) LLVM/clang
and GCC. I don't know about other compilers.
>If so, how would it be used here:
>
>
> while (1) {
> switch (x) {
> case y:
> Break;
>
>where that Break is meant to quit the loop.
term% cat lb.c
#include <stdio.h>
int
main(void)
{
int sum = 0;
outer:
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
int p = i * j;
switch (p) {
case 500:
break outer;
default:
sum += p;
break;
}
}
}
printf("sum = %d\n", sum);
return 0;
}
term% clang-23 -std=c2y -o lb lb.c
term% ./lb
sum = 235000
term% gcc-16 -std=c2y -o lb lb.c
term% ./lb
sum = 235000
term% sed 's/outer./\;/' lb.c >lb2.c
term% clang-23 -std=c2y -o lb2 lb2.c
term% ./lb2
sum = 24500500
term%
>Remember that if DMR hadn't made that screw-up in 1972 it would simply
>have been as it is above with his choice of keyword in place of Break.
As I said in my other post, you incorrectly assume that that was
the fundamental issue, when really it's just a detail: the
problem was that no facility to break out of nested control
structures as provided. Remedied now!
>But I would be interested in trying out the new feature.
So do it. You don't need me to hold your hand.
- Dan C.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Wed, 14 Oct 2026 19:48
Date: Wed, 14 Oct 2026 19:48
12 lines
1357 bytes
1357 bytes
In article <11ao8k4$4o6l$5@dont-email.me>, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >[snip] >PS: I've just read another long (300 lines) post you made in >reply to bart. Let me express my admiration for your patience >and countenance (that I'm clearly lacking in this case), and >the very clear form and conclusively explained content. Thank you! That's very kind of you to say. - Dan C.
Re: Taming The Switch-Statement
Author: Michael S
Date: Wed, 14 Oct 2026 20:28
Date: Wed, 14 Oct 2026 20:28
42 lines
2666 bytes
2666 bytes
On Wed, 14 Oct 2026 19:30:28 -0000 (UTC)
cross@spitfire.i.gajendra.net (Dan Cross) wrote:
> In article <11ao8af$1k8p3$1@dont-email.me>, bart <bc@freeuk.com>
> wrote:
> >On 14/10/2026 13:38, Dan Cross wrote:
> >> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com>
> >> wrote:
> >>> I'd even suggest that using distinct keywords in the original C
> >>> compiler would have made it a tiny bit simpler.)
> >>
> >> Unlikely. I'm trying to chase down the citation, but it was
> >> known that getting B to fit on the PDP-7 was a challenge. From
> >> the C history paper,
> >
> >Perhaps Switch should have been left out then if it was that tight.
>
> Why? It's genuinely useful.
>
C switch is in the middle between low level construct (computed goto)
and higher level select/case/whatever of many other languages.
I suppose that implementing true low level alternative would have been
simpler for compiler. And it would be even more powerful than current
construct. Of course, also more dangerous, but being dangerous is not
against C philosophy.
int foo()
{
...
case 42:
....
switch(bar(x)); // can go not just to 6 and 7, but to 42 as well.
// if there is no 'default:' in the scope then default ends up here
....
case 6:
...
case 7:
etc...
}
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 14 Oct 2026 20:56
Date: Wed, 14 Oct 2026 20:56
132 lines
5702 bytes
5702 bytes
On 14/10/2026 20:30, Dan Cross wrote:
> In article <11ao8af$1k8p3$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>> Perhaps Switch should have been left out then if it was that tight.
>
> Why? It's genuinely useful.
If-else chains can do the same job.
>> Sharing a keyword is just crass.
>
> Don't you share `end`?
No, that has one meaning which is to terminate a block, there are no
repercussions as there are with C-break which has two meanings.
>> That could easily have been fixed by DMR choosing to use distinct
>> keywords. Do you agree so far?
>
> No. You've described what I consider to be a non-problem; a
> mild annoyance at best, and certainly not something that
> deserves complicating the language for.
I'd call it a 'howler'. Even if trivial, it was ludicrously simple to
avoid, and Michael S wouldn't have needed workarounds or to have waited
for C2y to be generally available which provides an official workaround.
Yes, in the simple case of while-switch (see below) it is still
something extra that is need to overcome that 1972 boo-boo.
> So we can look at N3355 for discussion:
> https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm
I found it eventually. But I saw two proposals, I think the other was
N3357(?). The difference is that the latter puts the loop label after
the keyword, so 'for label (' rather than 'label: for (', which makes
the labels local to the loop, so avoiding the issues below.
gcc 16.2.0 seems to implement N3355, so the points I made are actually
true, which is surprising. So here:
L1:
while (1) {
goto L1; // jumps to L1
continue L1; // jumps to just before }
break L1; // jumps to just after }
}
L1: // error: duplicate label
while ...
Meanwhile:
while (1) {
if (x == 3)
break; // this quits the loop as before
}
while (1) {
switch (x) {
case 3:
break; // this doesn't break the loop
}
}
So this part hasn't changed. What's changed is that there is a more
direct workaround:
loop:
while (1) {
switch (x) {
case 3:
break loop; // this doesn't break the loop
}
}
Which looks remarkably like the solution I knocked up in 2 minutes:
while (1) {
switch (x) {
case 3:
breakloop; // this doesn't break the loop
}
}
The difference is, I can consistently use 'breakloop' everywhere: I can
duplicate this in the same function. But the 'loop' label is unique to
that while-loop. (This is what NN3357 or whatever could have fixed.)
As for multiple nested loops, this will fix that too, that's great. We
just require some real examples of that being needed!
>> And yet I've solved all the problems in this thread and many others.
> Congratulations! And yet, despite you being obviously more
> intelligent than all of Ritchie, McIlroy, Thompson, Johnson,
> Kernighan, Plauger, and all of those who have and continue to
> contribute to the development of C combined,
Well, my personal C has 'breakloop'! Usually these micro-extensions are
shortlived, but I'll keep this one.
we don't see your
> name on the list of contributors to that language, nor does
> anyone other than you use your language.
It's enough that I used it and benefited from it.
This is that last example in my syntax (if the switch were the only
statement within the loop otherwise two more tokens needed):
doswitch x
when 3 then exit
end
7 tokens vs. 20 tokens using N3355, and it's repeatable within the
function because 'exit' is relative.
> But keep shooting hoops, bart. Maybe some day a talent scout
> will see you in your driveway and give you a starting position
> on a playoff team.
I really don't care. Most things in the world are not going the way I'd
like, and I'm not the only one who thinks that.
This language stuff is small potatoes but it gives me kick to have long
ago come up with solutions that some languages take ... 54 years to
produce a compromise.
Anyway, at least you've been a sport, unlike Mr. P.
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 20:58
Date: Wed, 14 Oct 2026 20:58
51 lines
3157 bytes
3157 bytes
bart <bc@freeuk.com> writes:
> On 14/10/2026 13:38, Dan Cross wrote:
>> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>
>> Yes, I am aware that you will tell me that it is justified
>> because that problem is actually the one worth solving, and the
>> others are not; you needn't bother. Simply put, I disagree that
>> that one problem is sufficiently important to warrant its own
>> solution, particularly when something more general is already
>> known and available in the language (labeled break/continue).
>
> I missed this bit.
How?? I've mentioned it more than once in this thread.
> Is labeled break now available in C?
It's proposed for C2y, and implemented as an extension in gcc and clang.
The latest C2y draft is
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3886.pdf
6.8.2 describes named iteration and switch statements.
6.8.7.3 and 6.8.7.4 describe the "continue" and "break" statements.
> If so, how would it be used here:
>
> while (1) {
> switch (x) {
> case y:
> Break;
>
> where that Break is meant to quit the loop.
I have no doubt that you can figure it out after reading the cited
sections. (I'm not sure why you spelled "break" with an uppercase 'B'.)
[...]
Elsewhere, you've mentioned that labels have function scope.
That's not a problem. A named "break" or "continue" can only
refer to the label of an enclosing iteration or switch statement.
"break FOO;" where FOO does not apply to an enclosing loop or switch
is a constraint violation. But "goto FOO;" is valid as long as
"FOO:" is within the same function (with some restrictions that
aren't relevant here).
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 14 Oct 2026 20:59
Date: Wed, 14 Oct 2026 20:59
10 lines
1665 bytes
1665 bytes
On 14/10/2026 21:56, bart wrote: > On 14/10/2026 20:30, Dan Cross wrote: > Â Â Â Â Â Â Â Â Â Â Â break loop;Â Â Â Â Â // this doesn't break the loop > Â Â Â Â Â Â Â Â Â Â Â breakloop;Â Â Â Â Â // this doesn't break the loop Ignore those comments, which are not true!
Re: Taming The Switch-Statement
Author: Keith Thompson
Date: Wed, 14 Oct 2026 21:23
Date: Wed, 14 Oct 2026 21:23
65 lines
3784 bytes
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 */
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Thu, 15 Oct 2026 01:26
Date: Thu, 15 Oct 2026 01:26
283 lines
6593 bytes
6593 bytes
In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 14/10/2026 20:30, Dan Cross wrote:
>[snip more of the same already hashed and rehashed ad naseum]
>
>As for multiple nested loops, this will fix that too, that's great. We
>just require some real examples of that being needed!
Here's a pretty terrible way to write part of a lexical
analyzer. I just typed this in an hour or so ago.
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static size_t lineno = 1;
static size_t colno = 0;
#define MAXTOKLEN 1024
typedef enum TokenType TokenType;
enum TokenType {
TkERROR = -1,
TkEOF = 0,
TkSTAR = '*',
TkSLASH = '/',
TkPLUS = '+',
TkMINUS = '-',
TkLT = '<',
TkGT = '>',
TkEQ = '=',
TkBANG = '!',
TkGE = 0x1000,
TkLE,
TkSTRING,
TkIDENT,
// etc....
};
typedef struct Token Token;
struct Token {
TokenType type;
char *token;
};
Token *
newtoken(TokenType tt, char *s, char *e)
{
size_t len = sizeof(Token) + (e - s) + 1 + (e == s);
Token *token = malloc(len);
if (token == NULL) abort();
memset(token, 0, len);
token->type = tt;
token->token = (char *)token + sizeof(*token);
token->token[0] = tt;
if (e != s)
memcpy(token->token, s, e - s);
return token;
}
static int
peek(FILE *fp)
{
int ch = fgetc(fp);
if (ch != EOF)
ungetc(ch, fp);
return ch;
}
Token *
nexttoken(FILE *fp)
{
char tokstr[MAXTOKLEN], *tp = tokstr, *te = tokstr + MAXTOKLEN - 1;
enum TokenType tt = TkEOF;
Token *next = NULL;
int ch;
unsigned char c;
loop:
while ((ch = fgetc(fp)) != EOF) {
tt = TkERROR;
++colno;
switch (ch) {
case '\n':
++lineno;
colno = 0;
case ' ':
case '\t':
case '\f':
case '\v':
case '\r':
break;
case '*':
tt = TkSTAR;
break loop;
case '/':
tt = TkSLASH;
ch = peek(fp);
switch (ch) {
case EOF:
break loop;
case '/':
while ((ch = fgetc(fp)) != '\n')
if (ch == EOF)
break loop;
ungetc(ch, fp);
continue;
default:
break;
}
break loop;
case '+':
tt = TkPLUS;
break loop;
case '-':
tt = TkMINUS;
break loop;
case '<':
tt = TkLT;
*tp++ = '<';
ch = peek(fp);
switch (ch) {
case EOF:
break loop;
case '=':
tt = TkLE;
*tp++ = '=';
break;
default:
break;
}
*tp++ = '\0';
break loop;
case '>':
tt = TkGT;
*tp++ = '>';
ch = peek(fp);
switch (ch) {
case EOF:
break loop;
case '=':
tt = TkGE;
*tp++ = '=';
break;
default:
break;
}
*tp++ = '\0';
break loop;
case '=':
tt = TkEQ;
break loop;
case '!':
tt = TkBANG;
break loop;
case '"':
tt = TkERROR;
sloop:
while (tp != te) {
ch = fgetc(fp);
switch (ch) {
case EOF:
break loop;
case '"':
break sloop;
case '\\':
ch = fgetc(fp);
switch (ch) {
case EOF:
break loop;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
default:
break;
}
default:
*tp++ = ch;
break;
}
}
*tp++ = '\0';
tt = TkSTRING;
break loop;
default:
c = ch;
if (c != '_' && !isalnum(c))
break loop;
*tp++ = c;
while (tp != te) {
ch = fgetc(fp);
if (ch == EOF)
break loop;
c = ch;
if (c != '_' && !isalnum(c)) {
ungetc(ch, fp);
break;
}
*tp++ = c;
}
*tp++ = '\0';
tt = TkIDENT;
break loop;
}
}
next = newtoken(tt, tokstr, tp);
return next;
}
int
main(void)
{
Token *tok;
while ((tok = nexttoken(stdin)) != NULL) {
if (tok->type == TkEOF)
break;
if (tok->type != TkERROR)
printf("tok: tt = %x, token = '%s'\n", tok->type, tok->token);
free(tok);
}
return 0;
}
>>> And yet I've solved all the problems in this thread and many others.
>
>> Congratulations! And yet, despite you being obviously more
>> intelligent than all of Ritchie, McIlroy, Thompson, Johnson,
>> Kernighan, Plauger, and all of those who have and continue to
>> contribute to the development of C combined,
>
>Well, my personal C has 'breakloop'! Usually these micro-extensions are
>shortlived, but I'll keep this one.
Consider that perhaps your obstinance about keeping this sort
of poorly thought-through extension is WHY no one but you uses
your stuff.
> we don't see your
>> name on the list of contributors to that language, nor does
>> anyone other than you use your language.
>
>It's enough that I used it and benefited from it.
>
>This is that last example in my syntax (if the switch were the only
>statement within the loop otherwise two more tokens needed):
>
> doswitch x
> when 3 then exit
> end
>
>7 tokens vs. 20 tokens using N3355, and it's repeatable within the
>function because 'exit' is relative.
Ok. Don't really care.
>> But keep shooting hoops, bart. Maybe some day a talent scout
>> will see you in your driveway and give you a starting position
>> on a playoff team.
>
>I really don't care. Most things in the world are not going the way I'd
>like, and I'm not the only one who thinks that.
>
>This language stuff is small potatoes but it gives me kick to have long
>ago come up with solutions that some languages take ... 54 years to
>produce a compromise.
>
>Anyway, at least you've been a sport, unlike Mr. P.
*shrug*
- Dan C.
Re: Taming The Switch-Statement
Author: Tim Rentsch
Date: Thu, 15 Oct 2026 04:30
Date: Thu, 15 Oct 2026 04:30
49 lines
2870 bytes
2870 bytes
Michael S <already5chosen@yahoo.com> writes:
> On Wed, 14 Oct 2026 19:30:28 -0000 (UTC)
> cross@spitfire.i.gajendra.net (Dan Cross) wrote:
>
>> In article <11ao8af$1k8p3$1@dont-email.me>, bart <bc@freeuk.com>
>> wrote:
>>
>>> On 14/10/2026 13:38, Dan Cross wrote:
>>>
>>>> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com>
>>>> wrote:
>>>>
>>>>> I'd even suggest that using distinct keywords in the original C
>>>>> compiler would have made it a tiny bit simpler.)
>>>>
>>>> Unlikely. I'm trying to chase down the citation, but it was
>>>> known that getting B to fit on the PDP-7 was a challenge. From
>>>> the C history paper,
>>>
>>> Perhaps Switch should have been left out then if it was that tight.
>>
>> Why? It's genuinely useful.
>
> C switch is in the middle between low level construct (computed goto)
> and higher level select/case/whatever of many other languages.
> I suppose that implementing true low level alternative would have been
> simpler for compiler. And it would be even more powerful than current
> construct. Of course, also more dangerous, but being dangerous is not
> against C philosophy.
>
> int foo()
> {
> ...
> case 42:
> ....
> switch(bar(x)); // can go not just to 6 and 7, but to 42 as well.
> // if there is no 'default:' in the scope then default ends up here
> ....
> case 6:
> ...
> case 7:
> etc...
>
> }
I'm reminded of a quote from E.W. Dijkstra:
"No, gentlemen. Three times no."
Re: Taming The Switch-Statement
Author: Janis Papanagnou
Date: Thu, 15 Oct 2026 10:08
Date: Thu, 15 Oct 2026 10:08
32 lines
2649 bytes
2649 bytes
On 2026-10-14 22:58, Keith Thompson wrote: > bart <bc@freeuk.com> writes: >> On 14/10/2026 13:38, Dan Cross wrote: >>> In article <11anobe$1dt0e$1@dont-email.me>, bart <bc@freeuk.com> wrote: >> >>> Yes, I am aware that you will tell me that it is justified >>> because that problem is actually the one worth solving, and the >>> others are not; you needn't bother. Simply put, I disagree that >>> that one problem is sufficiently important to warrant its own >>> solution, particularly when something more general is already >>> known and available in the language (labeled break/continue). >> >> I missed this bit. > > How?? If operating one's intellect in a batten-down-the-hatches mode, or if a brain is working in /dev/null mode, are two possibilities that occur to me to miss that. I also see the possibility that in a heated discussion one might be tempted to prioritize a rebuttal in a post without beforehand taking the time thinking about the provided information. (Although the former two possibilities seem more likely in this case.) > I've mentioned it more than once in this thread. A couple other posters as well. Janis > [...]
Re: Taming The Switch-Statement
Author: bart
Date: Thu, 15 Oct 2026 11:01
Date: Thu, 15 Oct 2026 11:01
140 lines
4783 bytes
4783 bytes
On 15/10/2026 02:26, Dan Cross wrote:
> In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>> On 14/10/2026 20:30, Dan Cross wrote:
>> [snip more of the same already hashed and rehashed ad naseum]
>>
>> As for multiple nested loops, this will fix that too, that's great. We
>> just require some real examples of that being needed!
>
> Here's a pretty terrible way to write part of a lexical
> analyzer. I just typed this in an hour or so ago.
....
Wow, it's impressive that you went to that trouble just to prove a point
about nested loops. (Even if it might have been a little bit contrived.)
But this is an interesting pattern with all those breaks:
break;
break loop;
break loop;
break loop;
break;
break loop;
break loop;
break loop;
break loop;
break;
break;
break loop;
break loop;
break;
break;
break loop;
break loop;
break loop;
break loop;
break sloop;
break loop;
break;
break;
break;
break;
break;
break loop;
break loop;
break loop;
break; // loop
break loop;
break; // loop
It seems most uses of 'break;' to break out of a loop have disappeared
to be replaced with labeled versions.
The remaining breaks are switch-breaks.
Then I start looking at some of those "breaks;". Some (maybe one) is at
top-level, it jumps to the end of that switch, so the end of the while,
so is really 'continue'.
Most are in nested switches, some of those jump to a 'break loop'. Some
jump somewhere else...
Basically this is spaghetti code (I know you said it was terrible); your
eyes glaze over with breaks everywhere and you give up trying to figure
out the control flow.
There are examples like this:
continue;
default:
break;
}
break loop;
Do you even need 'default: break;}' or even an empty default:?
I then tried an experiment where I got rid of switch-break, to see what
C code with automatic switch-break would look like.
It was a big improvement, now that you know that every case-blocks
proceeds to the end of that switch, and you don't need to double-check
every 'break' to see if it's to do with switches or loops:
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break loop;
break sloop;
break loop;
break loop;
break loop;
break loop;
break;
break loop;
break;
All these are known to be loops, even 'break;'.
But also, labeled versions except three 'break loops' can be written as
'break', since 'switch break' does not exist. You're left with:
break loop;
break loop;
break loop;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
(Not ordered.) Compare with the first set.
>> [snip more of the same already hashed and rehashed ad naseum]
Perhaps that reused keyword thing is a bigger deal than you think. The
effect of it is unnecessary obfuscation.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Thu, 15 Oct 2026 11:45
Date: Thu, 15 Oct 2026 11:45
133 lines
4959 bytes
4959 bytes
In article <11aqbqv$2avn3$1@dont-email.me>, bart <bc@freeuk.com> wrote: >On 15/10/2026 02:26, Dan Cross wrote: >> In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote: >>> On 14/10/2026 20:30, Dan Cross wrote: >>> [snip more of the same already hashed and rehashed ad naseum] >>> >>> As for multiple nested loops, this will fix that too, that's great. We >>> just require some real examples of that being needed! >> >> Here's a pretty terrible way to write part of a lexical >> analyzer. I just typed this in an hour or so ago. >.... > >Wow, it's impressive that you went to that trouble just to prove a point >about nested loops. (Even if it might have been a little bit contrived.) > >But this is an interesting pattern with all those breaks: > > [snip] > >It seems most uses of 'break;' to break out of a loop have disappeared >to be replaced with labeled versions. > >The remaining breaks are switch-breaks. > >Then I start looking at some of those "breaks;". Some (maybe one) is at >top-level, it jumps to the end of that switch, so the end of the while, >so is really 'continue'. > >Most are in nested switches, some of those jump to a 'break loop'. Some >jump somewhere else... > >Basically this is spaghetti code (I know you said it was terrible); your >eyes glaze over with breaks everywhere and you give up trying to figure >out the control flow. Perhaps if I had spent more than like, 30 minutes, on it I would have tidied it up. The point was to illustrate a contrived example where one might break out of nested switch statements, loops inside of switch, switch inside of loops, loops inside of switch inside of loops, and so on. >There are examples like this: > > continue; > default: > break; > } > break loop; > >Do you even need 'default: break;}' or even an empty default:? You don't, but some compilers will complain if it isn't there. See above. It was merely a quick and dirty example. >I then tried an experiment where I got rid of switch-break, to see what >C code with automatic switch-break would look like. > >It was a big improvement, now that you know that every case-blocks >proceeds to the end of that switch, and you don't need to double-check >every 'break' to see if it's to do with switches or loops: > > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break sloop; > break loop; > break loop; > break loop; > break loop; > break; > break loop; > break; > >All these are known to be loops, even 'break;'. > >But also, labeled versions except three 'break loops' can be written as >'break', since 'switch break' does not exist. You're left with: > > break loop; > break loop; > break loop; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > >(Not ordered.) Compare with the first set. Compare what? Without the surrounding context, this is utterly meaningless: this isn't a useful illustration of working code; this is the output of `grep`. (btw, if you `|sort |uniq -c` you can actually get counts! You can even `|sort |uniq -c |sort -n` to order by frequency of occurrence!) > >> [snip more of the same already hashed and rehashed ad naseum] > >Perhaps that reused keyword thing is a bigger deal than you think. The >effect of it is unnecessary obfuscation. This is certainly not evidence of that. This is just your assertion that you took a silly example and apparently (maybe?) improved it, based on some unstated, but apparently subjective, criteria. - Dan C.
Re: Taming The Switch-Statement
Author: Tim Rentsch
Date: Thu, 15 Oct 2026 12:17
Date: Thu, 15 Oct 2026 12:17
21 lines
2201 bytes
2201 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes: > In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote: > >> On 14/10/2026 20:30, Dan Cross wrote: >> [snip more of the same already hashed and rehashed ad naseum] >> >> As for multiple nested loops, this will fix that too, that's great. We >> just require some real examples of that being needed! > > Here's a pretty terrible way to write part of a lexical > analyzer. I just typed this in an hour or so ago. > > [200+ lines of code] This code provides an excellent example of why multi-level breaks should not be added to C. The code would be easier to understand if all the 'break <label>;' lines were replaced with one of 'goto TOKEN_COMPLETE;' or 'goto COMMENT_COMPLETE;', as appropriate, and with the loop labels being removed and the two new labels put in at the appropriate places.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Thu, 15 Oct 2026 14:37
Date: Thu, 15 Oct 2026 14:37
48 lines
2783 bytes
2783 bytes
In article <86bj8vqhtl.fsf@linuxsc.com>, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: > >> In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote: >> >>> On 14/10/2026 20:30, Dan Cross wrote: >>> [snip more of the same already hashed and rehashed ad naseum] >>> >>> As for multiple nested loops, this will fix that too, that's great. We >>> just require some real examples of that being needed! >> >> Here's a pretty terrible way to write part of a lexical >> analyzer. I just typed this in an hour or so ago. >> >> [200+ lines of code] > >This code provides an excellent example of why multi-level breaks >should not be added to C. It is probably true that this snippet is a good example of how labeled break/continue can be abused. The code is awful. However, the current C2y draft standard already has labeled break and continue; the probability that it won't make it into the next standard revision is low. Moreover, other languages have had it for years (in some cases, decades) at this point and the sky has not fallen. One bad example, meant only to illustrate a couple of use cases, should not be used as evidence either way to draw a general conclusion about, well, anything. >The code would be easier to understand >if all the 'break <label>;' lines were replaced with one of 'goto >TOKEN_COMPLETE;' or 'goto COMMENT_COMPLETE;', as appropriate, >and with the loop labels being removed and the two new labels put >in at the appropriate places. Whenever one says that, "the code would be easier to understand" one is necessarily making a subjective judgement. In this case, _I_ think that code would be far easier to understand written in an entirely different way that didn't reach for `goto` at all. - Dan C.
Re: Taming The Switch-Statement
Author: bart
Date: Thu, 15 Oct 2026 14:57
Date: Thu, 15 Oct 2026 14:57
72 lines
3625 bytes
3625 bytes
On 15/10/2026 12:45, Dan Cross wrote:
> In article <11aqbqv$2avn3$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>> Perhaps that reused keyword thing is a bigger deal than you think. The
>> effect of it is unnecessary obfuscation.
>
> This is certainly not evidence of that. This is just your
> assertion that you took a silly example and apparently (maybe?)
> improved it, based on some unstated, but apparently subjective,
> criteria.
I am stating that 'break' used to fix C's poor 'switch' feature
interferes with 'break' used for loops.
It's not even my opinion: it's fact. In your example, about a third of
'break' tokens are for taking care of switch.
My opinion is that it generates pointless clutter (this it would do even
if a different keyword was used), and it can get confusing when switch
and loop are mixed up.
I actually took your program and ported it to my syntax, as I was
curious how it would look. This is the result:
https://github.com/bart-2026/langs/blob/main/lexertest.m
(It's been spaced out more, vertically.)
There were some tricky bits of the C to translate, such as:
break;
default:
break;
}
default:
*tp++ = ch;
break;
}
}
*tp++ = '\0';
tt = TkSTRING;
where it seems to break from one switch then sneakily falls through into
the default clauses of the enclosing one!
Well, here are some stats:
C Mine
Size 3374 2997 characters (both with hard tabs)
Non-blank lines 210 174 Lines
I did try and run mine:
c:\mapps>test
1234
tok: tt = tkident, token = '1234'
abc
tok: tt = tkident, token = 'abc'
It shows both of these as an identifier. But yours does the same:
c:\c>a
1234
tok: tt = 1003, token = '1234'
abc
tok: tt = 1003, token = 'abc'
Whatever '1003' means, it's showing the same. So it will do (I don't
care if the code is buggy, just whether my version is reasonably faithful).
Of course, my actual lexers have a different structure.
Re: Taming The Switch-Statement
Author: bart
Date: Thu, 15 Oct 2026 17:10
Date: Thu, 15 Oct 2026 17:10
39 lines
2987 bytes
2987 bytes
On 15/10/2026 15:57, bart wrote: > On 15/10/2026 12:45, Dan Cross wrote: >> In article <11aqbqv$2avn3$1@dont-email.me>, bart <bc@freeuk.com> wrote: > >>> Perhaps that reused keyword thing is a bigger deal than you think. The >>> effect of it is unnecessary obfuscation. >> >> This is certainly not evidence of that. This is just your >> assertion that you took a silly example and apparently (maybe?) >> improved it, based on some unstated, but apparently subjective, >> criteria. > I am stating that 'break' used to fix C's poor 'switch' feature > interferes with 'break' used for loops. > > It's not even my opinion: it's fact. In your example, about a third of > 'break' tokens are for taking care of switch. > > My opinion is that it generates pointless clutter (this it would do even > if a different keyword was used), and it can get confusing when switch > and loop are mixed up. > > I actually took your program and ported it to my syntax, as I was > curious how it would look. This is the result: > > https://github.com/bart-2026/langs/blob/main/lexertest.m > > (It's been spaced out more, vertically.) > Well, here are some stats: > I did try and run mine: Enough worked that I decided to try a speed test. Both use exactly the same algorithm, and I didn't expect either to be fast. Still, the version in my language/compiler was 50% faster than the C compiled with gcc-O3. I don't know why. (My production lexers, even in my language so unoptimised, tend to be at least a magnitude faster and do more stuff such as symbol table lookups.)
Re: Taming The Switch-Statement
Author: Janis Papanagnou
Date: Thu, 15 Oct 2026 18:50
Date: Thu, 15 Oct 2026 18:50
30 lines
2484 bytes
2484 bytes
On 2026-10-15 16:37, Dan Cross wrote: > In article <86bj8vqhtl.fsf@linuxsc.com>, > Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: >> [...] > > It is probably true that this snippet is a good example of how > labeled break/continue can be abused. [...] Else-thread (in a reply to bart) you had already written: > > The point was to illustrate a contrived > example where one might break out of nested switch statements, > loops inside of switch, switch inside of loops, loops inside of > switch inside of loops, and so on. In a newsgroup (also generally) where part of the audience isn't interested in understanding the context and intention of such examples it's "good" fodder for those to continue their campaign. Of course your example with its intent were obvious and clear. Your formerly posted explanations without code had already been clear. (I.e., for those that were interested to understand the point. For those that aren't, with every additional post, and especially with lots of code, you just feed the... - campaigners. Wasted time, IMO.) Janis > [...]
Re: Taming The Switch-Statement
Author: bart
Date: Thu, 15 Oct 2026 18:59
Date: Thu, 15 Oct 2026 18:59
60 lines
3205 bytes
3205 bytes
On 15/10/2026 13:17, Tim Rentsch wrote:
> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>
>> In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>>
>>> On 14/10/2026 20:30, Dan Cross wrote:
>>> [snip more of the same already hashed and rehashed ad naseum]
>>>
>>> As for multiple nested loops, this will fix that too, that's great. We
>>> just require some real examples of that being needed!
>>
>> Here's a pretty terrible way to write part of a lexical
>> analyzer. I just typed this in an hour or so ago.
>>
>> [200+ lines of code]
>
> This code provides an excellent example of why multi-level breaks
> should not be added to C.
Actually most of those 'break-labels' are not multi-level breaks.
But they have to use that form because they are inside 'switch'.
(Unless you consider 'levels' to include switch statements as loops; I
don't. I mean, there is a 'continue' in there too inside switch, which
doesn't use the label. So how can that not be multi-level but 'break' is?)
It all goes back to that bad decision about 'break' that everyone is
playing down.
> The code would be easier to understand
> if all the 'break <label>;' lines were replaced with one of 'goto
> TOKEN_COMPLETE;' or 'goto COMMENT_COMPLETE;', as appropriate,
> and with the loop labels being removed and the two new labels put
> in at the appropriate places.
Once you have to write /actual labels/, which aren't even in local scope
within the loop, then 'break label' doesn't buy much over 'goto label':
loop:
while () {
switch () {
case x:
break loop;
}
}
while () {
switch () {
case x:
goto loopend;
}
}
loopend:
The latter will at least work with every compiler.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Thu, 15 Oct 2026 20:42
Date: Thu, 15 Oct 2026 20:42
174 lines
7989 bytes
7989 bytes
In article <11aqpl3$2gdup$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 15/10/2026 12:45, Dan Cross wrote:
>> In article <11aqbqv$2avn3$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>
>>> Perhaps that reused keyword thing is a bigger deal than you think. The
>>> effect of it is unnecessary obfuscation.
>>
>> This is certainly not evidence of that. This is just your
>> assertion that you took a silly example and apparently (maybe?)
>> improved it, based on some unstated, but apparently subjective,
>> criteria.
>
>I am stating that 'break' used to fix C's poor 'switch' feature
>interferes with 'break' used for loops.
Sure.
>It's not even my opinion: it's fact. In your example, about a third of
>'break' tokens are for taking care of switch.
Ok.
>My opinion is that it generates pointless clutter (this it would do even
>if a different keyword was used), and it can get confusing when switch
>and loop are mixed up.
Ok.
>I actually took your program and ported it to my syntax, as I was
>curious how it would look. This is the result:
>
>https://github.com/bart-2026/langs/blob/main/lexertest.m
>
>(It's been spaced out more, vertically.)
>
>There were some tricky bits of the C to translate, such as:
>
> break;
> default:
> break;
> }
> default:
> *tp++ = ch;
> break;
> }
> }
> *tp++ = '\0';
> tt = TkSTRING;
>
>where it seems to break from one switch then sneakily falls through into
>the default clauses of the enclosing one!
Yup.
>Well, here are some stats:
>
> C Mine
>
> Size 3374 2997 characters (both with hard tabs)
> Non-blank lines 210 174 Lines
Those seem like pretty meaningless statistics. I could really
care less about 377 fewer bytes. Naturally, different languages
will express these things differently.
>I did try and run mine:
>
> c:\mapps>test
> 1234
> tok: tt = tkident, token = '1234'
> abc
> tok: tt = tkident, token = 'abc'
>
>It shows both of these as an identifier. But yours does the same:
>
> c:\c>a
> 1234
> tok: tt = 1003, token = '1234'
> abc
> tok: tt = 1003, token = 'abc'
Yup. It's a poorly written program; I am not surprised there
are bugs. It wasn't meant to be an illustration of exemplary
programming, it was meant to illustrate a use case for a
language construct that addresses a use case you appeared to
want support for, but that are now upset about, because it was
not provided in the way you like. Or something.
>Whatever '1003' means, it's showing the same. So it will do (I don't
>care if the code is buggy, just whether my version is reasonably faithful).
This is actually a good example of the problem you often run
into in these endless c.l.c threads: You seem to take a thing
that was something that was only meant to show one thing, and
asserting that it represents something other than what it was
meant to show, and no matter how many times it is explained to
you that it is not, in fact, what you seem to think that it is,
you persist in your delusion and think that it proves something.
But it doesn't, since you are starting from a false premise, at
which point you whine that you are picked on, treated poorly,
misunderstood, or generally dismissed, to which you conclude
that this can only be due to a slavish devotion to C as it is,
and a refusal to entertain that the language is not perfect. It
is totally lost on you that people may be objecting to what you
are saying on its own merits, since (ironically) you don't seem
able to admit that your own ideas may have their own
deficiencies.
This whole discussion could be summarized as:
Bart: "C sucks because Dennis Ritchie stupidly reused `break`
for both loops and switch. He was a tasteless fool."
All: "You can work around that really easily."
Keith: "C added labeled break and continue...."
Bart: "C sucks. My language is better. Here, I fixed C in 4
lines of code with my `breakloop`, but it still sucks
because C is hopeless."
Me: "No, the real problem is that there's no structured way
break out of nested contexts; your thing only addresses
one special case, and does not compose, so can't cover
the general case."
Bart: "No, it's the overloading. C is terrible and you're all
just too blind to see it. You are all fools, like
Dennis Ritchie."
Me: "I mean, Keith told you how the language is being changed
to address your problem _and_ the other problems in this
general category."
Bart: "Those other problems don't matter. C will never change.
It will suck forever. I mean, have you ever even seen a
use case for these labeled blocks? I don't like them,
therefore they suck, just like C. They also suck
because they solve a problem I don't care about. I have
been doing this for 45 years."
Me: "Here's a program that illustrates the problem. The code
is shit, but shows how it can be used...."
Bart: "This program sucks! You don't _need_ to do it this
way."
Me: "...I mean, I told you it sucked when I posted it...."
Bart: "...I rewrote it in my language. I saved 377 bytes. See
how much C sucks now? My language is better.
Me: "...."
Bart: "My lexer is 50% faster than your lexer. My production
code is an order of magnitude faster. I am a very smart
boy, and deserve a very special gold star on my report
card."
Me: "...Bruh."
Bart: "Dennis Ritchie was a fool. A fool, I say! Haha!
Ahahaha! Muwhahaha!"
>Of course, my actual lexers have a different structure.
If I actually wanted a real lexer for C, I'd probably use a
lexer generator to generate one from a declarative description
of the language's lexemes.
Now then. Did you see how it used labeled break and continue to
move about through the various control structures in ways that
would not be representable if you only had `breakloop`? Yes, of
course you can restructure the code, but that's not the point;
the point was only to show how one might use labeled loops and
switches and so on to implement control flows that one cannot
express _without_ such facil
The irony is that you are saying, in effect, "oh but you don't
actually _need_ to do it that way..." to something you don't
care for, when throughout the thread you have been specifically
rejecting that same argument as a response your assertion that
something that addressed this very use case was badly needed, to
fix Dennis Ritchie's "mistake" of using `break` for both loops
and `switch`. Seriously. The stunning lack of self-awareness
is really quite breathtaking.
- Dan C.
Re: Taming The Switch-Statement
Author: Dan Cross
Date: Thu, 15 Oct 2026 20:49
Date: Thu, 15 Oct 2026 20:49
83 lines
3389 bytes
3389 bytes
In article <11ar7rb$2mb01$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>On 15/10/2026 13:17, Tim Rentsch wrote:
>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>
>>> In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>>>
>>>> On 14/10/2026 20:30, Dan Cross wrote:
>>>> [snip more of the same already hashed and rehashed ad naseum]
>>>>
>>>> As for multiple nested loops, this will fix that too, that's great. We
>>>> just require some real examples of that being needed!
>>>
>>> Here's a pretty terrible way to write part of a lexical
>>> analyzer. I just typed this in an hour or so ago.
>>>
>>> [200+ lines of code]
>>
>> This code provides an excellent example of why multi-level breaks
>> should not be added to C.
>
>Actually most of those 'break-labels' are not multi-level breaks.
>
>But they have to use that form because they are inside 'switch'.
>
>(Unless you consider 'levels' to include switch statements as loops; I
>don't. I mean, there is a 'continue' in there too inside switch, which
>doesn't use the label. So how can that not be multi-level but 'break' is?)
>
>It all goes back to that bad decision about 'break' that everyone is
>playing down.
>
>> The code would be easier to understand
>> if all the 'break <label>;' lines were replaced with one of 'goto
>> TOKEN_COMPLETE;' or 'goto COMMENT_COMPLETE;', as appropriate,
>> and with the loop labels being removed and the two new labels put
>> in at the appropriate places.
>
>
>Once you have to write /actual labels/, which aren't even in local scope
>within the loop, then 'break label' doesn't buy much over 'goto label':
>
> loop:
> while () {
> switch () {
> case x:
> break loop;
> }
> }
>
> while () {
> switch () {
> case x:
> goto loopend;
> }
> }
> loopend:
>
>The latter will at least work with every compiler.
I mentioned this already; labeled continue and labeled break
both consume only one label; `goto` would require two:
outer:
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (condx(i, j)) break outer;
else if (condy(i, j)) continue outer;
vs:
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
if (condx(i, j)) goto outer_break;
else if (condy(i, j)) goto outer_continue;
outer_continue:
}
outer_break:
And so forth.
- Dan C.
Page 4 of 9 • 412 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads