🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #209

Re: Taming The Switch-Statement

#209
From: bart
Date: Sun, 11 Oct 2026 11:21
137 lines
6270 bytes
On 11/10/2026 03:55, Keith Thompson wrote:
> bart <bc@freeuk.com> writes:
>> On 11/10/2026 00:30, Keith Thompson wrote:
>>> bart <bc@freeuk.com> writes:
>>
>>> Huh?  "computed goto" is not a looping construct.
>>
>> You only hear about it in the context of interpreter dispatch loops.

>>> Yes, of course it does.  Did you think that a construct that's used
>>> in most existing C programs could just be removed from the language?
>>
>> I didn't say they should be removed.
> 
> So you want to add a new and less flexible looping construct on top
> of the existing one,

So how would you do it: remove existing switch completely and replace 
with the new version?

Obviously new and old will have to co-exist. This could have been done 
decades ago and the old switch deprecated by now.

> making the language more complicated with no
> real additional expressive power.

It's already getting more complicated.

>> It illustrates having too much flexibility. Do compilers check for
>> nonsense like this?
> 
> I fundamentally disagree with your claim of "too much flexibility".

So what more flexibility do you think switch could have: working across 
functions?
> "case 10: default: case 20:" perfectly valid, syntactically and
> semantically.  It's also silly, and not something someone would
> plausibly write other than to make a point.  I see no reason for
> compilers to go out of their way to do anything about it.

It is nonsense, akin to writing this:

   if (cond) {
      s1;
      else
      s2;
   }

where the else branch is in the middle of the true branch. Is it 
possible that people truly don't realise how utterly bizarre C switch is?

Or if they do, it is astounding that they just shrug it off.


> Not really.  A reasonably clever compiler or other tool can easily
> recognize a typical switch statement where each sequence of case or
> default labels is followed by a "break".  Code analysis becomes a little
> more difficult when that pattern is broken by any of several means:
> fallthrough, goto, return, continue.

See the example in my last post replying to David Brown. What 
control-flow analysis would a compiler make of that?


>> Example:
> [snip]

I see you're not very receptive of it.

> 
> [...]
> 
>>>> It could also be easy to implement as structured switch can be
>>>> syntactic sugar for an if-else-if chain.
>>>
>>> If you want an if-else-if chain, write an if-else-if chain.
>>
>> Then why bother with switch at all?
> 
> It expresses a common pattern.

A common pattern where one of N blocks of code is executed according to 
some control expression.

But that's not what C switch does: it jumps to one of N random labels 
anywhere inside any statement of any complexity and depth.

Those N blocks can overlap. Or after executing one block, it just 
carries on into the next unless stopped via 'break'.

Often one of these N will be an exception (default) but in C that can be 
shared with of the other blocks.

Is this the common pattern you're talking about; total chaos?


> It can, especially with older compilers,
> result in more efficient code (a jump table) that an if/else chain
> cannot directly express.  It lets the expression be written and
> evaluated just once.  It makes the compiler diagnose the error of
> specifying the same value more than once.  It is as expressive as the
> more restrictive case statement seen in Pascal and other languages.

It's an emulation of it but with few guardrails.

> How would your "structured switch" differ from the current switch
> statement?  Would the lack of fallthrough be the only difference?
> Would the case expressions still have to be constant?  Would you
> allow duplicate values?  Would you allow non-integer types, perhaps
> with user-defined comparisons?  These are all rhetorical questions,
> intended merely to point out that this is not as simple as you seem
> to think it is.

Yes it is simple:

* The control expression is an integer
* Case values are constants, and must be unique
* The only comparison is a nominal ==

But it implements that common pattern properly as explained above.

The fall-through question doesn't arise, any more than it does in if-else.

> 
> Would it add *any* real expressive power to a language that you
> hold in contempt anyway?

It provides more safety and more confidence that no weird stuff is going 
on, and confidence that it is properly implementing the common pattern.


> Sure, we could add every feature that's not too hard to implement.
> The end result would be a huge mess.

Then it would be right at home! This is fixing something that should 
long ago have been taken out and shot.
Technical Details
Message-ID: <11afrfq$2j2o6$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> <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> <11aehqg$24k8m$3@kst.eternal-september.org> <11aenps$27sq7$1@dont-email.me> <11aetqv$29hmu$1@kst.eternal-september.org>

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: Sun, 11 Oct 2026 12:21:31 +0100
Organization: A noiseless patient Spider
Lines: 137
Message-ID: <11afrfq$2j2o6$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> <11aehqg$24k8m$3@kst.eternal-september.org>
 <11aenps$27sq7$1@dont-email.me> <11aetqv$29hmu$1@kst.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 11 Oct 2026 11:21:31 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2722566"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1833sa4LR8/7fvfrNfyEduQ"; posting-host="bb4c63ed27c528dcfc498e66a186f254"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:M9QBDa+0A8DBPOgiSO0bHRSdnoQ= sha256:nAN0ISKJDXqIXjkJS7pVKNmA7sy7qhFcVUbSCyeMCbY=
	sha1:4gB3CqNMFaZVa0Qt7+5warFFQz0= sha256:6cXB2mNnBHNRNjqwdqH0R/wywhFEq0lz4jcPSjWZPc8=
In-Reply-To: <11aetqv$29hmu$1@kst.eternal-september.org>
Content-Language: en-GB