Article View: comp.lang.c
Article #226Re: Taming The Switch-Statement
From: bart
Date: Sun, 11 Oct 2026 17:57
Date: Sun, 11 Oct 2026 17:57
75 lines
5277 bytes
5277 bytes
On 11/10/2026 18:17, David Brown wrote:
> On 11/10/2026 18:01, Andrey Tarasevich wrote:
>> On Sun 10/11/2026 6:33 AM, David Brown wrote:
>>>
>>> It is quite obvious that it is easy to write code with loops + break
>>> within a switch. It is equally obvious that it is easy to avoid such
>>> constructs - there are always ways to avoid a "break" in a loop, and
>>> if the code run within a switch case is complex, it is often
>>> practical to refactor it into a separate function. But presumably
>>> Michael has reasons not do do that.
>>
>> Um... The common pattern for a "search"-style loop looks as follows
>>
>> for (/* domain condition */)
>> {
>> ...
>> if (/* search condition */)
>> break;
>> ...
>> }
>>
>> So, loops that terminate with `break` are the mainstay of every
>> competently written code. Moving the "early termination" condition
>> into the loop header (i.e. lumping the search condition with the
>> domain condition) is a very ugly anti-pattern in most cases. Moreover,
>> majority of the loops in an average C program are such "search"-style
>> loops, meaning that they do have an "early termination" condition.
>>
>> Now, while overwhelming majority of my loops would use `if` to check
>> the "early termination", I can imagine that there sill be cases where
>> `switch` is applicable and more appropriate than an `if`. Yes, a
>> `switch` can always be replaced with an `if` ladder, but being
>> _forced_ to do so by a language limitation doesn't look right.
>>
>> And writing a separate function just to work around a weird language
>> limitation... that doesn't look right either.
>>
>
> Certainly plenty of loops have "break" statements, especially as an
> early exit.
>
> Sometimes code is written with big switches in a function, and the cases
> might include loops, and those loops might have break statements. The
> question is why someone might consider this a very common situation, and
> why they might consider it a failing that the same keyword is used for a
> switch "break" and for a loop "break". There are two possibilities I
> can imagine, but Michael may have other things in mind. One is that the
> code is less clear than it might otherwise have been - if that is the
> case, then I think the code should be considered for re-factoring into
> separate functions if the inner loops are too complex. The other
> possibility is that the programmer wanted to break out of the switch
> from within the middle of the inner loop. I can imagine that happening,
> and it would currently need either a bit of extra code to do the
> "breaking" in two steps, a "goto", or refactoring into a separate
> function. But it seems strange to me that someone would feel that was a
> common enough problem to want to have "switch break" and "loop break" be
> separate keywords, rather than go straight to a "break <label>" solution
> that would be more general.
>
> Only Michael can, if he wants, explain what he was thinking.
It sounds like he's pressurised into doing so. Maybe so that he can be
lectured on how to it 'properly'.
'break' to quit a loop early is part of the language, so why shouldn't
someone make use it?
It just its use can be interfered with when 'switch' is involved.
That that happens and needs a workaround, is not satisfactory. The issue
exists 100% because of sharing the 'break' keyword for two different
purposes.
>
Technical Details
Message-ID:
<11agils$2s3tg$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> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <20261010220212.000063fd@yahoo.com> <11ae56o$cjlv$4@dont-email.me> <20261010230355.00005189@yahoo.com> <864iesjx6e.fsf@linuxsc.com> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$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: Sun, 11 Oct 2026 18:57:17 +0100 Organization: A noiseless patient Spider Lines: 75 Message-ID: <11agils$2s3tg$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <20261010220212.000063fd@yahoo.com> <11ae56o$cjlv$4@dont-email.me> <20261010230355.00005189@yahoo.com> <864iesjx6e.fsf@linuxsc.com> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 11 Oct 2026 17:57:17 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3018672"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186SyZkdnwxccdBWk1/8J+Q"; posting-host="bb4c63ed27c528dcfc498e66a186f254" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:qQbMT8Nd7yeITQWno2tNnXdz01I= sha256:FsBBdhukfYus5uv7Er6xRdseiKkINOnnge7ypmJFNsE= sha1:QUPYqEV/HKlCg1PPvpLy/UmY5mk= sha256:xfzL0Bvj3elcTHFOkwjOxrO0z5yxTxMtLDdQEERiuJw= Content-Language: en-GB In-Reply-To: <11aggaq$2qqh9$1@dont-email.me>