Article View: comp.lang.c
Article #221Re: Taming The Switch-Statement
From: Andrey Tarasevich
Date: Sun, 11 Oct 2026 16:01
Date: Sun, 11 Oct 2026 16:01
38 lines
3210 bytes
3210 bytes
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.
--
Best regards,
Andrey
Technical Details
Message-ID:
<11agbsh$2pbk7$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>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: Andrey Tarasevich <noone@noone.net> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 11 Oct 2026 09:01:19 -0700 Organization: A noiseless patient Spider Lines: 38 Message-ID: <11agbsh$2pbk7$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 11 Oct 2026 16:01:22 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2928263"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+I+pLLL7YjN2HVwmVTLr+W"; posting-host="4b64a6b55a0048c7998d82b9e7359498" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:XTF61wdcrXG2/443J54IHJfc0v0= sha256:xrXcpgnmBI6BIyKD8D3VKu6B1z0XA0Xu30lpxiyeWMo= sha1:U7Jxm1uj/XKS5Cv8RrxQLFD6YJk= sha256:tUU3XLSSxzwjTWU7AOQs1twk4qs6U3qOe2nrb33Zizk= In-Reply-To: <11ag37o$2ln0a$1@dont-email.me> Content-Language: en-US