🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #419

Re: Taming The Switch-Statement

#419
From: bart
Date: Thu, 15 Oct 2026 11:01
140 lines
4783 bytes
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:

     break;
     break loop;
     break loop;
     break loop;
     break;
     break loop;
     break loop;
     break loop;
     break loop;
     break;
     break;
     break loop;
     break loop;
     break;
     break;
     break loop;
     break loop;
     break loop;
     break loop;
     break sloop;
     break loop;
     break;
     break;
     break;
     break;
     break;
     break loop;
     break loop;
     break loop;
     break;   // loop
     break loop;
     break;  // loop

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.

There are examples like this:

         continue;
     default:
         break;
     }
     break loop;

Do you even need 'default: break;}' or even an empty default:?

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.

 >> [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.
Technical Details
Message-ID: <11aqbqv$2avn3$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-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ao8af$1k8p3$1@dont-email.me> <11aol8k$adq$1@reader2.panix.com> <11aoqa9$1rgda$1@dont-email.me> <11apa4b$kv$1@reader2.panix.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: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Thu, 15 Oct 2026 12:01:51 +0100
Organization: A noiseless patient Spider
Lines: 140
Message-ID: <11aqbqv$2avn3$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ao8af$1k8p3$1@dont-email.me>
 <11aol8k$adq$1@reader2.panix.com> <11aoqa9$1rgda$1@dont-email.me>
 <11apa4b$kv$1@reader2.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 15 Oct 2026 11:01:52 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2457315"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/c9ct4Uuk7GE0SGNknD/ML"; posting-host="3f7a5532ffaf673b54a2292b8a86a9eb"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:U8DOYOuCTuuJlFjITD21q9K0lkU= sha256:mr5I/YD34wIMBkrYomhssZtipYaXZ/S0HVMyjWA19zk=
	sha1:9euOvD1ZC+/qwCet7QrvxkfxmMk= sha256:toFqXkTaoDESeVVnx4DHIq/eq+Ru2UbDo3C5zpN+qY0=
In-Reply-To: <11apa4b$kv$1@reader2.panix.com>
Content-Language: en-GB