Article View: comp.lang.c
Article #317Re: Taming The Switch-Statement
From: bart
Date: Tue, 13 Oct 2026 10:16
Date: Tue, 13 Oct 2026 10:16
83 lines
5339 bytes
5339 bytes
On 13/10/2026 08:23, Waldek Hebisch wrote: > Michael S <already5chosen@yahoo.com> wrote: >> 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. These are my stats across 3 of my non-C non-scripting apps, some 80Kloc (other apps share modules so would duplicate): Loops 997 Exit (1 level) 257 (ie. 'break' in C) Exit (All levels) 4 Exit (other) 0 Switch 814 (Looping switch 33 included also in Loops and Switch counts) Explicit return 1920 (Non-early returns are optional) So on average, 25% of all loops have early exits (your figures show about 4%). There's no analysis of whether they are inside switch or whatever, but observation yesterday suggested it was common. Switch is used so heavily (and there is Looping Switch which needs an explicit way to terminate!) then it's likely it is all mixed up. This is really the point - it shouldn't be necessary to care. The point has been made several times that loop-break clashing with switch-break is no different from a multi-level loop break (or even a multi-level switch break, whatever that even means). Well, it /is/ very different, and in my code-base, is infrequent. This is just playing down the consequences of the choices made in C. They are not serious, but they are not ideal, nor elegant, and involve workarounds that are a distraction. People should also be able to want to break out of enclosing loops from inside 'switch' without having to defend their choices!
Technical Details
Message-ID:
<11al0dn$ebog$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: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Tue, 13 Oct 2026 11:16:23 +0100 Organization: A noiseless patient Spider Lines: 83 Message-ID: <11al0dn$ebog$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:16:24 +0000 (UTC) Injection-Info: dont-email.me; logging-data="470800"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Xc+76X4xjPn2dcgEw4FAh"; posting-host="a4fe58cca8661e75894507eaca96fb60" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:NMM9+qjEUM4HTt5w70je0syrjRA= sha256:fYZMQWSs9KgGx6I9dFGL+HdbXIbsJHgTKudx6FT4fPY= sha1:SkVJl6IoL5Bdnb6HW/xTcmVIA20= sha256:46UrlRwpM7He6O7sNGAstocDn/NQYQOa086+59brISg= In-Reply-To: <11akm9q$29tpl$1@paganini.bofh.team> Content-Language: en-GB