🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #403

Re: Taming The Switch-Statement

#403
From: bart
Date: Wed, 14 Oct 2026 20:56
132 lines
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.
Technical Details
Message-ID: <11aoqa9$1rgda$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> <11anobe$1dt0e$1@dont-email.me> <11ant3u$6o$1@reader2.panix.com> <11ao8af$1k8p3$1@dont-email.me> <11aol8k$adq$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 21:56:41 +0100
Organization: A noiseless patient Spider
Lines: 132
Message-ID: <11aoqa9$1rgda$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11anobe$1dt0e$1@dont-email.me>
 <11ant3u$6o$1@reader2.panix.com> <11ao8af$1k8p3$1@dont-email.me>
 <11aol8k$adq$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 20:56:42 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1950122"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/xs/eMyrXirHli2f/0jtvb"; posting-host="c3ad536068e45f6b2a132b7865af71ef"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UexVhmip4krBVuH+jvCycyFeQPk= sha256:kaYSnYILpIMxwdqfNhxUVimrokU8/bI0g95wfFO28nA=
	sha1:YESQyUxBMfgqczG0HRVGh2ojxc0= sha256:KiaAaABJUk56cBLDLi9JPxE5MpA8DInAnbD7JJVSDg8=
In-Reply-To: <11aol8k$adq$1@reader2.panix.com>
Content-Language: en-GB