Article View: comp.lang.c
Article #421Re: Taming The Switch-Statement
From: Dan Cross
Date: Thu, 15 Oct 2026 11:45
Date: Thu, 15 Oct 2026 11:45
133 lines
4959 bytes
4959 bytes
In article <11aqbqv$2avn3$1@dont-email.me>, bart <bc@freeuk.com> wrote: >On 15/10/2026 02:26, Dan Cross wrote: >> In article <11aoqa9$1rgda$1@dont-email.me>, bart <bc@freeuk.com> wrote: >>> On 14/10/2026 20:30, Dan Cross wrote: >>> [snip more of the same already hashed and rehashed ad naseum] >>> >>> As for multiple nested loops, this will fix that too, that's great. We >>> just require some real examples of that being needed! >> >> Here's a pretty terrible way to write part of a lexical >> analyzer. I just typed this in an hour or so ago. >.... > >Wow, it's impressive that you went to that trouble just to prove a point >about nested loops. (Even if it might have been a little bit contrived.) > >But this is an interesting pattern with all those breaks: > > [snip] > >It seems most uses of 'break;' to break out of a loop have disappeared >to be replaced with labeled versions. > >The remaining breaks are switch-breaks. > >Then I start looking at some of those "breaks;". Some (maybe one) is at >top-level, it jumps to the end of that switch, so the end of the while, >so is really 'continue'. > >Most are in nested switches, some of those jump to a 'break loop'. Some >jump somewhere else... > >Basically this is spaghetti code (I know you said it was terrible); your >eyes glaze over with breaks everywhere and you give up trying to figure >out the control flow. Perhaps if I had spent more than like, 30 minutes, on it I would have tidied it up. The point was to illustrate a contrived example where one might break out of nested switch statements, loops inside of switch, switch inside of loops, loops inside of switch inside of loops, and so on. >There are examples like this: > > continue; > default: > break; > } > break loop; > >Do you even need 'default: break;}' or even an empty default:? You don't, but some compilers will complain if it isn't there. See above. It was merely a quick and dirty example. >I then tried an experiment where I got rid of switch-break, to see what >C code with automatic switch-break would look like. > >It was a big improvement, now that you know that every case-blocks >proceeds to the end of that switch, and you don't need to double-check >every 'break' to see if it's to do with switches or loops: > > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break loop; > break sloop; > break loop; > break loop; > break loop; > break loop; > break; > break loop; > break; > >All these are known to be loops, even 'break;'. > >But also, labeled versions except three 'break loops' can be written as >'break', since 'switch break' does not exist. You're left with: > > break loop; > break loop; > break loop; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > break; > >(Not ordered.) Compare with the first set. Compare what? Without the surrounding context, this is utterly meaningless: this isn't a useful illustration of working code; this is the output of `grep`. (btw, if you `|sort |uniq -c` you can actually get counts! You can even `|sort |uniq -c |sort -n` to order by frequency of occurrence!) > >> [snip more of the same already hashed and rehashed ad naseum] > >Perhaps that reused keyword thing is a bigger deal than you think. The >effect of it is unnecessary obfuscation. This is certainly not evidence of that. This is just your assertion that you took a silly example and apparently (maybe?) improved it, based on some unstated, but apparently subjective, criteria. - Dan C.
Technical Details
Message-ID:
<11aqect$hpr$1@reader2.panix.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11aoqa9$1rgda$1@dont-email.me> <11apa4b$kv$1@reader2.panix.com> <11aqbqv$2avn3$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!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail From: cross@spitfire.i.gajendra.net (Dan Cross) Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Thu, 15 Oct 2026 11:45:33 -0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: <11aqect$hpr$1@reader2.panix.com> References: <119qf6l$2rmg3$1@dont-email.me> <11aoqa9$1rgda$1@dont-email.me> <11apa4b$kv$1@reader2.panix.com> <11aqbqv$2avn3$1@dont-email.me> Injection-Date: Thu, 15 Oct 2026 11:45:33 -0000 (UTC) Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80"; logging-data="18235"; mail-complaints-to="abuse@panix.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: cross@spitfire.i.gajendra.net (Dan Cross)