🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #106

Re: Taming The Switch-Statement

#106
From: bart
Date: Sat, 10 Oct 2026 13:11
101 lines
5394 bytes
On 10/10/2026 13:01, Janis Papanagnou wrote:
> On 2026-10-10 12:53, bart wrote:
>> On 10/10/2026 03:17, Keith Thompson wrote:
> [...]
>>> Yes, you have to remember to write "break;" at the end of each case.
>>>
>>> I haven't seen any proposals to add such a feature -- probably
>>> because nobody else is as angry about it as you are.
>>
>> I'm angry because it was SO EASY to get right in the first place 
>> (Algol68 and Pascal already existed), and nearly as easy to fix even 
>> within the intervening decades.
> 
> The C-folks had other design criteria than the Pascal or Algol 68
> people; the latter two have also fundamental different concepts in
> their respective language (also concerning their 'case' construct).
> (Specifically the Algol 68 'case' isn't very progressive, being
> restricted to - even implicit! - integer tags from 1 to some N.)

I forgot the A68 version wasn't quite the same. But both will have a 
properly structured form, where the aim is execute one of N possible 
blocks. After executing that block, control escapes the enclosing statement.

The C aim is to pass control to one of N possible locations, but there 
are virtually no rules about where those locations can be within the 
statement following 'switch()', and it doesn't care what happens from 
then on.


> I think the problem with your statement is also that you think your
> preferred construct is the only one that is "right". (You seem to
> be confident that supporting "fall-though" is generally "wrong".)

Yes. Why, you think that fall-through is right AND should be the default 
behaviour?

If you look at actual C codebases, fall-through is incredibly rare. 
Nearly always, it will use break, or sometimes other control-flow such 
as return or goto, so that fall-through doesn't happen.

Note that if-statement blocks don't have fall-through either when there 
is an else branch. Imagine having to write this:

    if (cond) {
       statement;
       break;
    } else {
       statement;
    }

because C was crazy enough to fall through to the else block unless 
'break' was added!

I guess everyone would still defend that choice (because for one person 
in a million it will be valuable feature). But it is CLEARLY wrong.


> Just imagine if some C-programmers wanted a fall-through semantics
> but all they got would be the Pascal or Algol 68 'case' construct.

Well, Algol 68 doesn't even have 'break'. For unusual control-flow it 
needs goto.

I don't have fall-through either; it is just so rare. But I do have a 
'recase' statement to pass control to an /arbitrary/ block, not just the 
one that happens to follow.

This way, BLOCKS CAN BE REORDERED without changing behaviour.

This is an important point: a switch statement where every case block 
ends with 'break', can have those case blocks appear in any order. Or 
new ones can be inserted anywhere. Blocks commented out, deleted etc etc.

But that all goes out the window as soon as fall-through is used.

> You may blame the respective language if you have problems with it.
> But I have a general (and well meant) suggestion for you...
> If you notice that you are doing specific mistakes repeatedly - and
> where you cannot change "the environment" of your mistake! - train
> your awareness! (That's more successful a strategy than persistent
> whining about it; i.e. if you're interested to get your tasks done.)
> 
> Janis
> 
> PS (disclaimer): That all said, I still think the approach in "C" is
> a low level pattern.

The kind of structured switch I favour is still low level! After all it 
basically a jump.

It is not the structural or parametric kind of pattern matching you find 
in Rust etc.

But C's switch is just /too/ low-level. For example, a structured switch 
could be easily transformed into an if-else chain.

You can't do that in general with C switch because there IS no 
structure; it can be totally chaotic.


Technical Details
Message-ID: <11addhr$1oakb$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> <11ad9fg$cjm0$4@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: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sat, 10 Oct 2026 14:11:24 +0100
Organization: A noiseless patient Spider
Lines: 101
Message-ID: <11addhr$1oakb$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> <11ad9fg$cjm0$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Oct 2026 13:11:24 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1845899"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX195rls0P9qe1KaTBEwnmT5c"; posting-host="8d9c8b578a5ce6a1f662bc81cc091c91"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:sXC0i6RxvE6cvXcs6dbJ6bQJMMY= sha256:ZPqGDRM8Lq3QPNkBDbBA8a8cQKq95poKr0DEOFdCLjo=
	sha1:MrWP+IZuhof6ieW72GXVf7rYg9Y= sha256:fKoLOT3N1HN6iuXTxbvMweUYVE+dKBXZyJziuRb9zBo=
Content-Language: en-GB
In-Reply-To: <11ad9fg$cjm0$4@dont-email.me>