Article View: comp.lang.c
Article #312Re: Taming The Switch-Statement
From: David Brown
Date: Tue, 13 Oct 2026 07:27
Date: Tue, 13 Oct 2026 07:27
55 lines
3385 bytes
3385 bytes
On 12/10/2026 23:46, bart wrote:
> On 12/10/2026 22:21, Scott Lurndal wrote:
>
>> 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();
>> }
Alternatively :
bool looping = true;
while (looping && condition)
switch (blah) {
case 1:
break;
case 2:
if (fred == name) looping = false;
break;
}
do_something();
}
(The semantics for when "do_something()" is called is slightly different
here, but that depends on what something you want to do.)
Or you put your loop in a function - and then "return" breaks out of
everything. There are plenty of ways to structure the code.
Like every programming language, C provides some features and not
others. You use the features the language has in the way that gives the
best results for your code. There are /always/ going to be some
mismatches - features that you think are unnecessary, and things that
you think would be convenient if they existed. No two programmers are
going to agree on those mismatches, though they might have some overlap
of opinion.
Trying to invent a new special syntax just for one particular use-case
is absurd. It might make sense in a little private language, but not
for a mainstream programming language.
Technical Details
Message-ID:
<11akmgh$9uvd$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> <GTazS.183760$ygUb.176068@fx23.iad> <11ajhka$3vuli$1@dont-email.me> <rbczS.252606$ITPb.1159@fx05.iad> <11ajkev$v8c$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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Tue, 13 Oct 2026 09:27:13 +0200 Organization: A noiseless patient Spider Lines: 55 Message-ID: <11akmgh$9uvd$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> <11ajkev$v8c$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 13 Oct 2026 07:27:14 +0000 (UTC) Injection-Info: dont-email.me; logging-data="326637"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ywcDXgTlwSP19nCotnGDjg0BkeaIKPHI="; posting-host="0453d936fb977b9ac4728f0f8db2cabc" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:S3cKEBHSgj6HJhKFl8EXbluaE8g= sha256:RA5Xndb0d+/KwpYmyJTU18IA+T70wvQJJug8nwLkeLQ= sha1:Am6DowTHUgH64efuweqBxdQ3uIw= sha256:GIl591QygaFDS10cd0WST1cgoWSBCvO+ZnOqzx3Yrvk= Content-Language: en-GB, nb-NO In-Reply-To: <11ajkev$v8c$1@dont-email.me>