Article View: comp.lang.c
Article #324Re: Taming The Switch-Statement
From: Richard Harnden
Date: Tue, 13 Oct 2026 11:10
Date: Tue, 13 Oct 2026 11:10
64 lines
4487 bytes
4487 bytes
On 13/10/2026 11:26, bart wrote: > 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. > You could argue that 'break' is just syntactical sugar for 'goto label'. If you want to break out of multiple levels, then just use a goto. Some people have a pathological hatred of 'goto', sure. There's alway a way to avoid goto, if that's what you really want. That the code, and it's intention, is clear is the important thing. I really don't see the issue. It's a style thing at most.
Technical Details
Message-ID:
<11al3jt$fig1$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> <11al05t$4o6l$1@dont-email.me> <11al10u$eiet$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: Richard Harnden <richard.nospam@gmail.invalid> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Tue, 13 Oct 2026 12:10:52 +0100 Organization: A noiseless patient Spider Lines: 64 Message-ID: <11al3jt$fig1$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> <11al10u$eiet$1@dont-email.me> Reply-To: nospam.harnden@invalid.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 13 Oct 2026 11:10:54 +0000 (UTC) Injection-Info: dont-email.me; logging-data="510465"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180jUDky7xVd/V+tK9ZvMjGrHHuPhen0qA="; posting-host="f6a7e364578b7e6a73c680e7e909192b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:HMGaZzAJ+dP2sxXSrcUp0zTqoA4= sha256:13A4M/3lY8X+A05REX42Uwx2ifprqYlaEueNFOvnh8Q= sha1:KCHz1fYB//BKxUOYgHCNhA+UrPE= sha256:0eD5UjSz5DROZRK69pi4P34mW3SJ7kNKSHNQHW0glPM= In-Reply-To: <11al10u$eiet$1@dont-email.me> Content-Language: en-GB