Article View: comp.lang.c
Article #1240Re: Taming The Switch-Statement
From: bartc
Date: Sun, 01 Nov 2026 12:09
Date: Sun, 01 Nov 2026 12:09
133 lines
5967 bytes
5967 bytes
On 01/11/2026 10:02, Rosario19 wrote:
> 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;
> }
>
That would be too intrusive. Simpler is just:
#define breakloop goto
while (1) {
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;
}LoopName:
Simplest is 'goto LoopName;', but then you lose the information that
this is breaking out of the enclosing loop; that could be jumping anywhere.
The 'stepping over' part might anyway be better written as:
if (0){LoopName: break;}
or disguised via a macro, for example:
#define LOOP(name) if(0){name: break;}
#define breakloop goto
int a=0;
while (1) {
LOOP(LoopName)
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;
}
But overall this is a poor way to get around language shortcomings.
Technical Details
Message-ID:
<11c7a5v$1uj42$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-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> <7b3eelldb1aldv7tvbkrp50dbtdjq9t795@4ax.com>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: bartc <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 1 Nov 2026 12:09:35 +0000 Organization: A noiseless patient Spider Lines: 133 Message-ID: <11c7a5v$1uj42$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> <11amecs$10eab$1@dont-email.me> <7b3eelldb1aldv7tvbkrp50dbtdjq9t795@4ax.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 01 Nov 2026 12:09:36 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2051202"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19r84XXDrmdVdfL2euCL7S2"; posting-host="21107675e70675917ebe462fc92a72ff" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:IU0KuE7ec7GSlR096FAq2fZOb9s= sha256:ox3m4S2ei1HimoaXBuFH3MahDSW6PlVBInIU3IGhhh8= sha1:7BNuEy5GR1mJkWU8Rx8GDNPYDf8= sha256:Run+IkULVIMgpKwIBJ2fqEHsX1jDmgTJjcIzt+Tou/I= Content-Language: en-GB In-Reply-To: <7b3eelldb1aldv7tvbkrp50dbtdjq9t795@4ax.com>