🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #296

Re: Taming The Switch-Statement

#296
From: bart
Date: Mon, 12 Oct 2026 21:46
106 lines
5171 bytes
On 12/10/2026 22:21, Scott Lurndal wrote:
> bart <bc@freeuk.com> writes:
>> On 12/10/2026 20:52, Scott Lurndal wrote:
>>> bart <bc@freeuk.com> writes:
>>>> On 12/10/2026 18:58, Dan Cross wrote:
>>>>> In article <11ainqf$3kkvd$1@dont-email.me>, bart  <bc@freeuk.com> wrote:
>>>>
>>>
>>>> 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;
>>>
>>> Aside from the incorrect syntax, it's really difficult to
>>> determine your intent from this code fragment.
>>
>> Really? It's attempting to break out of the while-loop from inside that
>> switch statement. What we've been discussing at length.
> 
> It's not clear from the context within the code fragment that
> you intended the second break to terminate the while loop,
> or for the first break to terminate the while loop (or
> for neither to continue the switch).
> 
>>
>> But it won't work like this because in this context, that 'break' is
>> switch-break not loop-break.
> 
> Which break?  There are two.

Oh, OK. The original didn't need switch-break, it only had loop-break.

I added switch-break for the C version; that is the first 'break' above.

But loop-break is also 'break'. So it is still wreaking confusion!

For the same reason too: the intent in each case is different.

> 
>>
>>
>>> It's likely that folks here could provide more idiomatic
>>> C if you described the problem you're attempting to solve
>>> rather than providing an incomplete and incorrect code
>>> fragment.
>>
>> I'd be interested in the idiomatic C that doesn't involve rewriting my
>> code or adding complexity in order to get around that silly clash.
>>
> 
> Describe the problem the code is intended to
> address.   Rather than a very small fragment
> of code without any context.  It appears to be parsing
> a command line, something done quite frequently in C.

(Yes. It is normally parsing '[options] file [options]', but when a 
filename is seen and that file is to be run immediately, it needs to 
stop parsing as those trailing options will be processed by the 
application that is run.

Maybe I should have used the simpler example.)

> In any case, using goto to leave a switch statement nested
> in a loop is certainly an idiomatic construct that is widely
> used to leave the outer loop early.  Another option is to declare
> a local bool before the switch in the while loop, initialized to false.
> 
> Set it to true when you want to bail early:
> 
> while (condition)
>     bool bail = false;
>     switch (blah) {
>     case 1:
>        break;
>     case 2:
>        if (fred == name) bail = true;
>        break;
>     }
>     if (bail) break;
>     do_something();
> }
> 
> Whether that is beautiful or ugly is a matter of opinion,
> and you've made yours very clear.

Using my proposal, that would look like:

  while (condition)
      newswitch (blah) {
      case 1:                   # this is an empty block
      case 2:                   # this is separate
         if (fred == name) break;
      }
      do_something();
  }

No 'break' is routinely needed for the switch. 'break' here exits the loop.

Not that this was serious, but probably 'case' would need a new keyword 
since the meaning of 'case 1: case 2:' is significantly different 
between old and new switch contexts.
Technical Details
Message-ID: <11ajkev$v8c$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> <GTazS.183760$ygUb.176068@fx23.iad> <11ajhka$3vuli$1@dont-email.me> <rbczS.252606$ITPb.1159@fx05.iad>

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: Mon, 12 Oct 2026 22:46:07 +0100
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <11ajkev$v8c$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>
 <GTazS.183760$ygUb.176068@fx23.iad> <11ajhka$3vuli$1@dont-email.me>
 <rbczS.252606$ITPb.1159@fx05.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 12 Oct 2026 21:46:07 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="32012"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+1nH/cJQHD9NiB37m4r2wY"; posting-host="77c9fe16ce3505b18aae67dd9ba4b8ee"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ogOiIiOYp4DfuOo5I20nUk1dBrA= sha256:rklB1XxoRBmhw2VDXQWthO71KPQUremwwwzLx1kbeRg=
	sha1:cCf79hTxdjNLyNTOtTiDOF3acMQ= sha256:fwWVGihqPqrjtx1+Wj2wcfhoarTlbIqCjuFIrgTUE1o=
In-Reply-To: <rbczS.252606$ITPb.1159@fx05.iad>
Content-Language: en-GB