🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #307

Re: Taming The Switch-Statement

#307
From: Keith Thompson
Date: Tue, 13 Oct 2026 00:33
50 lines
3255 bytes
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.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID: <11aju80$28f2$6@kst.eternal-september.org>
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!kst.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>

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!kst.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Mon, 12 Oct 2026 17:33:04 -0700
Organization: None to speak of
Lines: 50
Message-ID: <11aju80$28f2$6@kst.eternal-september.org>
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>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Tue, 13 Oct 2026 00:33:05 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="74210"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX193kRr+lzKfue9dSssRULgW"; posting-host="11ac7c4b547d7ca6b42084bf5cca4a8e"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:prZ8dOwjBDeheFSnN+fDxNuFdWc= sha256:pSKv1xPLMNrwUUruZMl5K8iypyAvmkU8pjImUTiUQeU=
	sha1:AzVnnUbtWue+Ke7FD06OeNCObuo= sha256:+2EFaJAz62FueM01f9tYbCTZo1jBO5cplTo9H94Y1NM=