Article View: comp.lang.c
Article #321Re: Taming The Switch-Statement
From: bart
Date: Tue, 13 Oct 2026 10:33
Date: Tue, 13 Oct 2026 10:33
70 lines
4149 bytes
4149 bytes
On 13/10/2026 08:27, David Brown wrote:
> 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.
Sometimes you just want to write the simplest, more direct and most
intuitive code.
/Having/ to invent functions, and a special interface, to get around a
language issue is always a code smell.
> 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.
In my language (and others which don't have C's quirk) there is no
special syntax needed. You just write 'exit' or 'break', as the case may
be, oblivious to whether you are inside switch or not.
This is less 'cognitive load' than the calamity of having one extra
keyword, that C would have needed.
Technical Details
Message-ID:
<11al1eh$enb3$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> <11akmgh$9uvd$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: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Tue, 13 Oct 2026 11:33:53 +0100 Organization: A noiseless patient Spider Lines: 70 Message-ID: <11al1eh$enb3$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> <11akmgh$9uvd$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 10:33:54 +0000 (UTC) Injection-Info: dont-email.me; logging-data="482659"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18YtJEiREKMw7Y4GDfmPF6d"; posting-host="a4fe58cca8661e75894507eaca96fb60" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:oEi7LE80AAAI5aWRV4Y2cVwVQ9Q= sha256:2xnSwEdpRNs7ZrhRwljG/6bDbp5iJ02QY+EqlS5Qz5E= sha1:+w8NlYklgyFh8M40qzsQ3LTTLlA= sha256:3EblRwwQzpAc/QShiHIq9YcrTojfGvFZTNASUpFpfe8= In-Reply-To: <11akmgh$9uvd$1@dont-email.me> Content-Language: en-GB