Article View: comp.lang.c
Article #316Re: Taming The Switch-Statement
From: Janis Papanagnou
Date: Tue, 13 Oct 2026 10:12
Date: Tue, 13 Oct 2026 10:12
43 lines
3383 bytes
3383 bytes
On 2026-10-13 09:23, Waldek Hebisch wrote:
> [...]
> So what those counts indicate: majority of loops is simple, that
> is have no early exit. Vast majority of early exits is done
> via 'return'. "Interesting" loop control is relatively rare.
> Checking which 'break'-s are within equivalent of 'switch' would
> require manual inspection and that would be too much work. But
> reasonable guestimate to that there are tens such uses. In
> a sense that is common, that is significantly more than one
> case. If my guestimate is correct, then this may be nontrivial
> fraction of all uses of 'break'. OTOH that is tiny fraction
> of all loops.
From my (personal) perspective, I commonly have two types of
such specific constructs; a very common one is iterating over
options
while ((opt = getopts (...)) != -1)
switch (opt) ...
where, of course, and quite naturally, always the whole loop
is iterated, with no early leaving. And the second common loop
type is those where the switched data comes from the function
as well
while ((token = parse (...)) != END_TOKEN)
switch (token) ...
I'm aware that one might wants to pass and process two values
from a function (as I interpreted one of bart's recent code
fragments), but then, and if it would be "impossible" to avoid
that pattern, I'd just add a condition
int leaving = 0;
while ((token = parse (&token...)) != ERROR && !leaving)
switch (token)
case END_TOKEN: leaving = 1; ...
that's really trivial and straightforward, and it's beyond me
why anyone (but bart, of course) would see a problem here.
Janis
Technical Details
Message-ID:
<11al05t$4o6l$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> <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> <11akm9q$29tpl$1@paganini.bofh.team>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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Tue, 13 Oct 2026 12:12:13 +0200 Organization: A noiseless patient Spider Lines: 43 Message-ID: <11al05t$4o6l$1@dont-email.me> References: <119qf6l$2rmg3$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> <11akm9q$29tpl$1@paganini.bofh.team> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 13 Oct 2026 10:12:14 +0000 (UTC) Injection-Info: dont-email.me; logging-data="155861"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BGE8LlEFL44BLnI4j93GS"; posting-host="a1215c61e514a50645aa60546bc3740b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:PCNk2pCxWAa+A7ik0sGjRwrWBtI= sha256:c0sFMaEgt0DzecAcoMn7yK7f33NqO/2AFSVf8W/x5eA= sha1:AOK+22fV45C20uQMRzLjYcNkX84= sha256:37oM7JK0RbmdSQnhX5frLYTB+7bX0qGxAT7sUlSR6DU= Content-Language: en-US In-Reply-To: <11akm9q$29tpl$1@paganini.bofh.team>