Article View: comp.lang.c
Article #385Re: Taming The Switch-Statement
From: bart
Date: Wed, 14 Oct 2026 12:46
Date: Wed, 14 Oct 2026 12:46
69 lines
4065 bytes
4065 bytes
On 14/10/2026 06:17, Kaz Kylheku wrote:
> On 2026-10-13, Michael S <already5chosen@yahoo.com> wrote:
>> Of course, I never gathered exact statistics.
>> My guts feeling is that among switches nested within loop it could be as
>> high as one out of five. Not one out of five case labels, but one out of
>> five switch statements.
>
> Switches inside a loop are natural whenever a state machine is being
> simulated which has acceptance/failulre states which indicate that the
> simulation should end.
>
> Switches inside a loop are also natural whenever a stream of
> integer-tagged cases is being handled where a terminating sentinel is
> also labeled.
>
> There are at least two workarounds for writing this which avoid
> duplicated testing or loop termination flags, gotos or such.
>
> One is to make the loop the last statement of a function and use
> "return" in the loop-terminating cases.
>
> The other is to put a "break" at the bottom of the loop and
> terminate all the loop-continuing cases with "continue",
> the terminating cases with "break":
>
> for (;;) {
> int ch = getc(in);
> switch (ch) {
> case '\n':
> lines++;
> /* fallthrough */
> default:
> continue;
> case EOF:
> break;
> }
> break;
> }
>
Well, it's a novel workaround, but it is still a workaround if the logic
could have been more naturally expressed like this:
for (;;) {
switch (ch = getch()) { // I assume ch needed for other cases
case '\n':
lines++;
break;
case EOF:
BREAK;
}
}
BREAK represents loop exit. The point is to avoid having to restructure
your code just to get around a silly shortcoming. In practice, using
'goto' is the simplest workaround.
(This a pattern that is common enough for me to have a special syntax
for in my projects. Then it is expressed as:)
doswitch ch := getch(in)
when '\n' then
++lines
when EOF then
exit
end
Technical Details
Message-ID:
<11antja$1fsul$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> <11agils$2s3tg$1@dont-email.me> <11agv1t$30q22$2@kst.eternal-september.org> <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me> <20261012223144.00000f95@yahoo.com> <86zewhhdep.fsf@linuxsc.com> <20261013225449.00005562@yahoo.com> <20261013221123.363@kylheku.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: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Wed, 14 Oct 2026 13:46:33 +0100 Organization: A noiseless patient Spider Lines: 69 Message-ID: <11antja$1fsul$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> <11agils$2s3tg$1@dont-email.me> <11agv1t$30q22$2@kst.eternal-september.org> <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me> <20261012223144.00000f95@yahoo.com> <86zewhhdep.fsf@linuxsc.com> <20261013225449.00005562@yahoo.com> <20261013221123.363@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 14 Oct 2026 12:46:35 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1569749"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2NqJ2H+iCq5eyrw9TMjO6"; posting-host="c3ad536068e45f6b2a132b7865af71ef" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:qCYU0tXrivs4NTbPJufQQHbkhfw= sha256:7liu/wqL4qxgkAIv8vt8wTm5CEJDZ7aYLNk3I3KSy94= sha1:GxNpDrD/yizp7+snMOr2pg9oQ2Y= sha256:Wr6KniUFbFjple4AJxj0gBL7eBq5bgXo9ATT7BHa9CA= In-Reply-To: <20261013221123.363@kylheku.com> Content-Language: en-GB