🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #430

Re: Taming The Switch-Statement

#430
From: Dan Cross
Date: Thu, 15 Oct 2026 20:49
83 lines
3389 bytes
In article <11ar7rb$2mb01$1@dont-email.me>, bart  <bc@freeuk.com> wrote:
>On 15/10/2026 13:17, Tim Rentsch wrote:
>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>> 
>>> 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.
>>>
>>> [200+ lines of code]
>> 
>> This code provides an excellent example of why multi-level breaks
>> should not be added to C.
>
>Actually most of those 'break-labels' are not multi-level breaks.
>
>But they have to use that form because they are inside 'switch'.
>
>(Unless you consider 'levels' to include switch statements as loops; I 
>don't. I mean, there is a 'continue' in there too inside switch, which 
>doesn't use the label. So how can that not be multi-level but 'break' is?)
>
>It all goes back to that bad decision about 'break' that everyone is 
>playing down.
>
>>  The code would be easier to understand
>> if all the 'break <label>;' lines were replaced with one of 'goto
>> TOKEN_COMPLETE;'  or 'goto COMMENT_COMPLETE;', as appropriate,
>> and with the loop labels being removed and the two new labels put
>> in at the appropriate places.
>
>
>Once you have to write /actual labels/, which aren't even in local scope 
>within the loop, then 'break label' doesn't buy much over 'goto label':
>
>   loop:
>   while () {
>       switch () {
>       case x:
>           break loop;
>       }
>   }
>
>   while () {
>       switch () {
>       case x:
>           goto loopend;
>       }
>   }
>   loopend:
>
>The latter will at least work with every compiler.

I mentioned this already; labeled continue and labeled break
both consume only one label; `goto` would require two:

	outer:
	for (i = 0; i < n; i++)
	    for (j = 0; j < m; j++)
	        if (condx(i, j)) break outer;
		else if (condy(i, j)) continue outer;

vs:

	for (i = 0; i < n; i++) {
	    for (j = 0; j < m; j++)
	        if (condx(i, j)) goto outer_break;
		else if (condy(i, j)) goto outer_continue;
	
	outer_continue:
	}
	outer_break:

And so forth.

	- Dan C.
Technical Details
Message-ID: <11are89$peu$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-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11apa4b$kv$1@reader2.panix.com> <86bj8vqhtl.fsf@linuxsc.com> <11ar7rb$2mb01$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 20:49:13 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11are89$peu$1@reader2.panix.com>
References: <119qf6l$2rmg3$1@dont-email.me> <11apa4b$kv$1@reader2.panix.com> <86bj8vqhtl.fsf@linuxsc.com> <11ar7rb$2mb01$1@dont-email.me>
Injection-Date: Thu, 15 Oct 2026 20:49:13 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="26078"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)