Article View: comp.lang.c
Article #293Re: Taming The Switch-Statement
From: David Brown
Date: Mon, 12 Oct 2026 21:14
Date: Mon, 12 Oct 2026 21:14
92 lines
5877 bytes
5877 bytes
On 12/10/2026 21:31, Michael S 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. Okay. I am not sure it is that common, and it is not a combination I make much use of in my own code. Many people aim to keep their functions short, while switches can lead to long functions - I think it is quite common in such circumstances to put the switch inside a function of its own, called from within the loop. > > 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. > I cannot remember ever having need of doing something like that - at least not so that I thought "it would be nice if there were a way to break out of the loop from here". Maybe that is an effect of the way I tend to structure my code, or split up my functions - but I do not split my functions to achieve this effect. And the other common resolution, using a goto, is also not something I have needed. Different people have different styles, and occasionally you come across a code structure that someone else thinks is common, while you have never needed it yourself. > If break out of loop and break out of switch were different keyword, it > would be very easy to do. Now that I know what you are talking about, I can see how that would have helped. However, to me, that would seem a very niche usage and not something to consider for an additional keyword in the language. Labelled breaks that can also handle nested loops would, I think, be of significantly wider use while also handling your situation. > > 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. > Only you know your own code, and what you judge as elegant, so there's no argument here. Perhaps I have written code that you would think could have been written more clearly with a switch within a loop if C had two different "break" statements - but it is not a feature that I could see myself making use of.
Technical Details
Message-ID:
<11ajika$37d$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>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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Mon, 12 Oct 2026 23:14:50 +0200 Organization: A noiseless patient Spider Lines: 92 Message-ID: <11ajika$37d$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 12 Oct 2026 21:14:51 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OwlWTXt+o+C6K7NRSxh+CxT4/AlHWEsY="; posting-host="711f10de653bed0662d2460037681553" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:JBpf7+PHANHn9f0FQ8psS0FjDC8= sha256:/D+5Gr2+AkfL0A0yOBdZG0Jhzy9MUX82mOXiJo8I98U= sha1:SRxK5zQciDgpIjvWUC6k9cjgJgs= sha256:TqjPmtoWS4dOb0kT5srUg0DD34UVf6DSTOKk4plF6zw= Content-Language: en-GB In-Reply-To: <20261012223144.00000f95@yahoo.com>