Article View: comp.lang.c
Article #1239Re: Taming The Switch-Statement
From: Rosario19
Date: Sun, 01 Nov 2026 10:02
Date: Sun, 01 Nov 2026 10:02
93 lines
4629 bytes
4629 bytes
On Wed, 14 Oct 2026 00:21:00 +0100, bart wrote:
>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(¶mno, &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(¶mno, &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.
would be equivalent to
%define breakloop goto
while (1) {
goto L1;
LoopName: break;
L1:
switch (a++) {
case 1: puts("one"); break;
case 2: puts("two"); breakloop LoopName;
case 3: puts("three"); break;
default: puts("other");
}
if (a>40) break;
}
Technical Details
Message-ID:
<7b3eelldb1aldv7tvbkrp50dbtdjq9t795@4ax.com>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-mailReferences:
<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> <11amecs$10eab$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!.POSTED!not-for-mail From: Rosario19 <Ros@invalid.invalid> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 01 Nov 2026 11:02:07 +0100 Organization: A noiseless patient Spider Lines: 93 Message-ID: <7b3eelldb1aldv7tvbkrp50dbtdjq9t795@4ax.com> 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> <11amecs$10eab$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Date: Sun, 01 Nov 2026 09:54:10 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1961095"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/RG/fnKwHqogarxcY7m7HBYp0eH8kPBA="; posting-host="35da1d5d5686e3d22afe7b844cfd8830" Cancel-Lock: sha1:wGo20eB7vKVz3lZ2ZYzGk63+gg4= sha256:lDTNbEjZ4neQUCb3cvRA/3Jno51wp40WnMh2aFyjNFc= sha1:Iw60oeTyL2g0SG2NH6ZpS8KzsHo= sha256:KZ7ESBVDLqaCcbryEu4mB1jbzdSvUsCxHrasKUMHJj4= X-Newsreader: Forte Free Agent 1.93/32.576 English (American)