Article View: comp.lang.c
Article #339Re: Taming The Switch-Statement
From: bart
Date: Tue, 13 Oct 2026 15:46
Date: Tue, 13 Oct 2026 15:46
83 lines
5344 bytes
5344 bytes
On 13/10/2026 15:40, Tim Rentsch wrote:
> Michael S <already5chosen@yahoo.com> writes:
>
>> I thought that my comment is hard to misunderstand, but you proved
>> me wrong. So I'd try to elaborate:
>>
>> switch() nested within loop is very common in my practice and I suppose
>> in practice of everybody else.
>>
>> In my practice, but obviously not in practice of Tim Rentsch, it
>> is sufficiently common to want to transfer control from point within
>> such nested switch to point immediately following the containing loop.
>
> Let me clarify my earlier comment. I'm not surprised that people
> routinely use switch() inside of loops. What did surprise me is the
> statement that wanting to break out of the loop from inside the
> switch() is "very common". Not happens occasionally; not happens
> commonly or even just sufficiently commonly; not happens routinely;
> but "very common". I am wondering now how common it actually is,
> for switch() in general
For switch in general is not meaningful: since this is about breaking
out of a loop: a loop needs to be involved!
> and for switch() inside of a loop. Is it
> one in a hundred? One in ten? One in two? More than that?
These are some stats for 5 of my non-C projects:
All All Of which,
Project Loops Breaks Breaks inside Switch
A 538 131 72
B 438 103 62
C 412 132 58
D 216 57 34
E 47 20 7
These are averages. It is conceivable all those 72 breaks on the first
line are within one Switch statement, but it is unlikely.
I can't do the same exercise for C examples, since that language doesn't
allow breaks inside 'switch'. (All breaks I'm talking about are
loop-breaks.)
Not sure what percentages you are after: how many loops have at least
one break within switch? That might be around 1 in 7 (14%).
What proportion of all breaks are inside switch? That would be about half.
> I asked for an example because I am curious about what the proximate
> cause of such commonality might be. Is it a general style matter?
Since this is something C doesn't directly support, getting stats is
tricky. People will use workarounds and you'd have to analyse those to
try and infer what they're trying to achieve. (Humans have to do the
same exercise; that is the problem.)
My view is that using distinct keywords for loop-break and switch-break
(accepting that the language wants the latter) would have had pretty
much zero costs, but worthwhile benefits.
> In my own experience, wanting to escape multiple levels (including
> the case of provoking a loop exit from inside a switch())
No it doesn't, but this is not about that; here we just want to break
out of one loop level!
> I'm reminded of a quote from (if memory serves) Tony Hoare, about
> language design. Paraphrasing: A programming language should not
> reflect the union of all the features proposed by its designers;
> rather it should reflect the union of the designers' prejudices
> about what features should NOT be put into the language.
Perhaps C could have done without switch-break then.
A very rough survey suggests that about 1% of lines of code in a project
are purely switch-breaks. How many billions of lines of C are there...?
Technical Details
Message-ID:
<11aljoo$m15u$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>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 16:46:32 +0100 Organization: A noiseless patient Spider Lines: 83 Message-ID: <11aljoo$m15u$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 13 Oct 2026 15:46:33 +0000 (UTC) Injection-Info: dont-email.me; logging-data="722110"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195cXTjxODkTinYmKs63Tqk"; posting-host="a4fe58cca8661e75894507eaca96fb60" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:daHBGcawOS1kUAtuZsZGB6mjf7k= sha256:wRhiTTLA772cjRD9AYE01LJj4a5+EyAtPZrMJTMRemo= sha1:OAoQ5v9VgWLZauqSPE/dOml6+bA= sha256:VYl/8rMHZr0OxcqnEy2ozmGiaISntPnDvs5Uak5Ww2w= In-Reply-To: <86zewhhdep.fsf@linuxsc.com> Content-Language: en-GB