🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #320

Re: Taming The Switch-Statement

#320
From: bart
Date: Tue, 13 Oct 2026 10:26
51 lines
3873 bytes
On 13/10/2026 11:12, Janis Papanagnou wrote:
> 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.
Because it's a hack. But if this is acceptable, then why does C even 
have loop-break? Since there is always some workaround (or 'writing 
code' as David Brown puts it.)

What would be the consequences of C not having it: would they be 
positive or negative overall?

Now apply the same analysis to a hypothetical loop-break that works 
inside switch.
Technical Details
Message-ID: <11al10u$eiet$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-mail
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> <11al05t$4o6l$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: Tue, 13 Oct 2026 11:26:38 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <11al10u$eiet$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>
 <11al05t$4o6l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 13 Oct 2026 10:26:39 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="477661"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18ZIiwzlLeu2zKZOQgt+gtt"; posting-host="a4fe58cca8661e75894507eaca96fb60"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:23KiuFAaSgL2RxO0ZtjZtz3vsA8= sha256:mc6mGUboPJwg8icR/Sp9RFi6IGIfJ+evGMDK9lxmz1I=
	sha1:7StEP72WJNnD/Lcj8Cv64xXHkPU= sha256:k1OkvF2bo9u/cWYd+MPkaFeMYwRmtS8dELZX7ResmL0=
In-Reply-To: <11al05t$4o6l$1@dont-email.me>
Content-Language: en-GB