🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #311

Re: Taming The Switch-Statement

#311
From: Waldek Hebisch
Date: Tue, 13 Oct 2026 07:23
82 lines
5008 bytes
Michael S <already5chosen@yahoo.com> wrote:
> On Mon, 12 Oct 2026 21:09:25 +0200
> David Brown <david.brown@hesbynett.no> wrote:
> 
>> On 12/10/2026 21:02, Michael S wrote:
>> > On Sun, 11 Oct 2026 14:28:29 -0700
>> > Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> >   
>> >> bart <bc@freeuk.com> writes:
>> >> [...]  
>> >>> 'break' to quit a loop early is part of the language, so why
>> >>> shouldn't someone make use it?  
>> >>
>> >> Agreed.  (There are coding styles that require loops to exit only
>> >> at the bottom, but that's not what we're talking about.)
>> >>  
>> >>> It just its use can be interfered with when 'switch' is involved.
>> >>>  
>> >>
>> >> Yes.  Everyone here knows that, and knew it before you started
>> >> talking about it.
>> >>
>> >> Michael S wrote above that "The cases where I want to break out of
>> >> the loop from the switch in the body of the loop are very common."
>> >> Tim Rentsch questioned the "very common" part of that, and asked
>> >> for an example.  You jumped in and asserted that an example isn't
>> >> necessary, because you misunderstood what was being said.
>> >>  
>> > 
>> > I didn't read the whole subthread, but from what I read it seems
>> > that the only reader that misunderstood my statement was David
>> > Brown. The rest, including bart, understood just fine.
>> >   
>> 
>> If I have misunderstood, I would love to be corrected.  In a number
>> of my posts I said I was not sure what you were talking about, and
>> would need more explanation.
>>
> 
> 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.
> 
> If break out of loop and break out of switch were different keyword, it
> would be very easy to do. 
> 
> But since the keyword is shared I have to use workarounds. Many of them
> were mentioned in the sub-thread. All workarounds work. They are all
> less simple and less elegant than what would have been possible if break
> keyword was not shared.

I think there is still not clear what "common" in your text above
means.  To clarify a bit, I have a non-C codebase where one can
use 'break' within equivalent of 'switch'.  My gut feelings were
that such use is reasonably common (I certainly remeber such uses
in the codebase).  But counting kerwords shows that I have 6053
occurences of keyword indicating loop (some may be in comments,
but that should not affect statistics too much), 223 occurences
of 'break', 197 occurences of equivalet of 'continue', 2390
occurences of 'return'.  In this language 'return' is only
used for ealy returns, otherwise last value computed (which
may be void) is returned.

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.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11akm9q$29tpl$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Tue, 13 Oct 2026 07:23:40 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11akm9q$29tpl$1@paganini.bofh.team>
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>
Injection-Date: Tue, 13 Oct 2026 07:23:40 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="2422581"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2