🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #294

Re: Taming The Switch-Statement

#294
From: Scott Lurndal
Date: Mon, 12 Oct 2026 21:21
107 lines
4387 bytes
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.

>
>
>> 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.

e.g.
    /*
     * Process the input parameters.
     */
    while ((c = getopt(argc, argv, "al:t:L:e:vV")) != EOF) {
        switch (c) {
        case 'a':
            level = c_mp::REV_A;            // Select revision A behavior
            break;
        case 'l':
            logfile = optarg;
            break;
        case 't':
            tracefile = optarg;
            break;
        case 'L':
            libpath = optarg;
            break;
        case 'e':
            startupfile = optarg;
            break;
        case 'v':
            verbose = true;
            break;
        case 'V':
            lp->log("%s\n", vsim_built);
            exit(EXIT_SUCCESS);
        }
    }

The remaining arguments in argv[] are are application dependent,
and don't need to be handled in the body of the switch or the
while loop.  They can easily be handled in a subsequent loop
over the remaining arguments.

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.
Technical Details
Message-ID: <rbczS.252606$ITPb.1159@fx05.iad>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx05.iad.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx05.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Taming The Switch-Statement
Newsgroups: comp.lang.c
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>
Lines: 107
Message-ID: <rbczS.252606$ITPb.1159@fx05.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 12 Oct 2026 21:21:27 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 12 Oct 2026 21:21:27 GMT
X-Received-Bytes: 4153