Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Fri, 16 Oct 2026 03:36:31 -0700 Organization: A noiseless patient Spider Lines: 75 Message-ID: <864iemq6ds.fsf@linuxsc.com> 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Fri, 16 Oct 2026 10:36:36 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3382761"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Xhs9mIe+HIjUqPj73MF93PL7czHntUbc="; posting-host="6aa7975ca949d05eced06ab246205481" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:hXp5QbdFTglVVVtpbdVPDsK2rJQ= sha1:6+8FIqAa9UTsnroMfgDEhXLRQqk= sha256:+B5l1zWAoelSxqe8CYiEMOX8alFKinKH6efJgic8EOQ= sha1:B5bchV0KO+Sq9RD0SEB4x3j1jek= sha256:q87XbE411jQc0Yu7S+RAHmqJ3F2mLrX/aP6jLltHw80= Xref: csiph.com comp.lang.c:403209 Andrey Tarasevich writes: > On Sun 10/11/2026 6:33 AM, David Brown wrote: > >> It is quite obvious that it is easy to write code with loops + break >> within a switch. It is equally obvious that it is easy to avoid >> such constructs - there are always ways to avoid a "break" in a >> loop, and if the code run within a switch case is complex, it is >> often practical to refactor it into a separate function. But >> presumably Michael has reasons not do do that. > > Um... The common pattern for a "search"-style loop looks as follows > > for (/* domain condition */) > { > ... > if (/* search condition */) > break; > ... > } > > So, loops that terminate with `break` are the mainstay of every > competently written code. Moving the "early termination" condition > into the loop header (i.e. lumping the search condition with the > domain condition) is a very ugly anti-pattern in most cases. Moreover, > majority of the loops in an average C program are such "search"-style > loops, meaning that they do have an "early termination" condition. > > Now, while overwhelming majority of my loops would use `if` to check > the "early termination", I can imagine that there sill be cases where > switch` is applicable and more appropriate than an `if`. Yes, a > switch` can always be replaced with an `if` ladder, but being _forced_ > to do so by a language limitation doesn't look right. > > And writing a separate function just to work around a weird language > limitation... that doesn't look right either. I'm afraid I must simply flat out disagree with some of the comments above. Certainly it is true that many C programs use a for-if-break pattern in some or many places. It's fair to call it a common pattern. But it's wrong to say it is /the/ common pattern. There are other ways of skinning that cat, and I expect most C programmers have used some or all of them in the normal course of program development. Similarly, the assertion that "loops that terminate with `break` are the mainstay of every competently written code" is an absurd overstatement. No doubt break-exiting loops are a significant part of many programs. Probably they are a mainstay in some of them. But to say they are /the/ mainstay in any non-trivial program is at best a stretch; to assert that they are /the/ mainstay in /all/ competently written code is simply wrong. I looked at some statistics for a couple of code bases. The relative amounts of switch() versus loops (for(), while(), do/while()) was relatively constant, about one in six or one in seven. Use of break varied a lot, from close to six percent to under two percents (here percent means percentage of occurrences of all control keywords). The biggest category was if() and return, over 80% of the total and roughly eight times the amount of looping structures. In the scheme of things switch() is not very important. Loops are important. The more prominent if() and return are very important. The idea that, one, exiting a loop with break is always an essential program element and, two, it's important to be able to do that using switch(), isn't a good fit for the facts on the ground. Incidentally, the comment that writing a separate function "doesn't look right" seems backwards. Breaking large functions into smaller functions is a good way to make code easier to understand. By their nature switch() statements tend to be large (in terms of lines of code) so putting the switch() inside a separate function is likely to be a good thing, not a bad thing. The motivation for doing that is not about working around a language limitation, but about writing better code.