🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #364

Re: Taming The Switch-Statement

#364
From: bart
Date: Tue, 13 Oct 2026 23:21
78 lines
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(&paramno, &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(&paramno, &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.





Technical Details
Message-ID: <11amecs$10eab$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> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@dont-email.me> <11ainqf$3kkvd$1@dont-email.me> <11aj73v$c2u$1@reader2.panix.com> <11ajcv8$3u33s$1@dont-email.me> <11aju80$28f2$6@kst.eternal-september.org>

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 00:21:00 +0100
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <11amecs$10eab$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11agils$2s3tg$1@dont-email.me>
 <11ahpqi$38ir0$2@dont-email.me> <11ainqf$3kkvd$1@dont-email.me>
 <11aj73v$c2u$1@reader2.panix.com> <11ajcv8$3u33s$1@dont-email.me>
 <11aju80$28f2$6@kst.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 13 Oct 2026 23:21:01 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1063243"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18pNLrtD1L9nHGsu2de01b3"; posting-host="a4fe58cca8661e75894507eaca96fb60"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Wy/Is/x2NGPBQCdVrYE991bplDs= sha256:6CIqWBYy/aArDkMVnZFcRKnHUtQwSUVkD9HTeSanrAc=
	sha1:mFnD5i/jKUYiHBDz92YyfdCEOck= sha256:ItmPxb4y+/KcG6bRHLYJYIYvIXfZsP2+7GeQGrOMnWs=
In-Reply-To: <11aju80$28f2$6@kst.eternal-september.org>
Content-Language: en-GB