🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #382

Re: Taming The Switch-Statement

#382
From: bart
Date: Wed, 14 Oct 2026 11:17
149 lines
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!
Technical Details
Message-ID: <11anobe$1dt0e$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ajcv8$3u33s$1@dont-email.me> <11aju80$28f2$6@kst.eternal-september.org> <11amecs$10eab$1@dont-email.me> <11amune$ipi$1@reader2.panix.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 14 Oct 2026 12:17:01 +0100
Organization: A noiseless patient Spider
Lines: 149
Message-ID: <11anobe$1dt0e$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ajcv8$3u33s$1@dont-email.me>
 <11aju80$28f2$6@kst.eternal-september.org> <11amecs$10eab$1@dont-email.me>
 <11amune$ipi$1@reader2.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Oct 2026 11:17:03 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1504270"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19rmo25WFNx9lpI0GR+cG3M"; posting-host="c3ad536068e45f6b2a132b7865af71ef"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:EbKSRetbzxECH/Ca9+5A3VfuDJ4= sha256:a1HgjypF5Fw40GNV0y0dpQDcV2mplvcZTBTE84RLgGU=
	sha1:xhFEUfzRFqF9J/qbeoYpf6YLrR8= sha256:nmyeZsR6V++1/8WkbKNWDy10nPWwOUykxI4OjzoENAo=
In-Reply-To: <11amune$ipi$1@reader2.panix.com>
Content-Language: en-GB