🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #104

Re: Taming The Switch-Statement

#104
From: bart
Date: Sat, 10 Oct 2026 10:53
159 lines
6599 bytes
On 10/10/2026 03:17, Keith Thompson wrote:
> bart <bc@freeuk.com> writes:
>> On 10/10/2026 01:25, Lawrence D’Oliveiro wrote:
>>> For comparison, here is an example of Python’s match-statement. Like
>>> all the good (i.e. non-C-based) designs, the designers saw no point in
>>> having fall-through.
>>
>> C, doesn't specifically have fallthrough. It's just that C's switch is
>> so bizarre, 'fallthrough' is a consequence of it being so dumb it
>> doesn't know it's still inside a switch statement; it is just code
>> that it carries ploughing through until it hits more control flow.
> 
> C's switch statement is essentially a computed goto.  It goes back
> to BCPL (1967), which had different syntax (SWITCHON, CASE, ENDCASE)
> but similar semantics.  BCPL's predecessor, CPL, had a more explicit
> computed goto that could be used in the same way; the target of a
> "go to" could be a label expression.
> 
> Think of C's "switch", not as an inferior version of Pascal's case
> statement, but as an improved version of a computed goto.

It's not that great as computed goto either. That tended to look like this:

    goto (L1, L2, L3) index

But C's switch index need not have consecutive values from 0 to N-1 or 1 
to N. This C switch statement (when I first wrote it I'd forgotten the 
breaks!):


     switch (a) {
     case 3: case 5:
         b = 100;
         break;

     case 2:
         b = 200;
         break;

     default:
         b = 300;
     }

Corresponds to this explicit jump-table using using gnu extensions for 
label pointers:

     void* jumptable[] = {&&def, &&def, &&case2, &&case35, &&def, &&case35};

     if (a>=0 && a<=5)
         goto *jumptable[a];
     else
         goto def;

     case35:
         b = 100;
         goto next;

     case2:
         b = 200;
         goto next;

     def:
         b = 300;
     next:

(Or it can correspond, since a compiler may choose other strategies.)

And, these days 'computed goto' in C tends to means something specific, 
which is a kind of looping switch but using multiple dispatch points 
(each break has its own goto *jumptable) rather the regular switch's one 
at the start).

The other day I posted this example from lua.c:

     vmcase(OP_ADDK) {
         op_arithK(L, l_addi, luai_numadd);
         vmbreak;
       }

vmcase and vmbreak are macros that either expand to 'case' and 'break', 
or to normal labels and goto *jumptable when gnuC is detected.

(Of, course, my language has it all:

     []ref label jumptable = (L1, L2, L3)  # first class labels

     goto (index | L1, L2, L3 | Ldef)      # 'computed goto'

     switch index when... else... end      # proper structured switch

     doswitch index...                     # looping version

     doswitchx index                       # multiple dispatch points

(So regular switch can be changed to full multi-dispatch points by 
adding that 'x'.)

But then, mine's a lower level systems language where I can consider 
such things fundamental. C is obviously higher level!)


> The ANSI C Rationale says "No serious consideration was given to
> making the switch more structured, as in Pascal, out of fear of
> invalidating working code".
> 
> I suppose some version of C could have defined a new switch-like
> statement without the fallthrough behavior.  I can see some small
> advantages in doing so, but the existing switch with fallthrough
> would have to be kept in the language -- and such a new switch-like
> statement would have added no real expressive power.  We'd now have
> a mix of code using the old and new constructs, pressure to convert
> existing working code, and probably errors introduced by converting
> code incorrectly.

I guess the same rationale means it is stuck with for(;;) loops?

That hasn't stop C++ having a higher level version.

Yet C23 has multiple ways of specifying a 32-bit integer type:

    int
    int32_t
    long        (on some platforms)
    _BitInt(32)


> Anything you could do with a more structured switch-like statement can
> be done, with just a little care, with the existing switch statement.

With a LOT more care, and with too much flexibility making it too easy 
to get things wrong. You even have this:

    case 10: default: case 20:

While if you want to do that multi-dispatch point stuff like the Lua 
example:

* You need to rewrite a normal switch into that ugly form

* There must be conditional macros somewhere to set them according
   to compiler

* A table of labels must be maintained that /exactly corresponds/ to the
   enumerations used for the index

* It doesn't work in standard C so you must use extensions

> 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.

It could also be easy to implement as structured switch can be syntactic 
sugar for an if-else-if chain.
Technical Details
Message-ID: <11ad5eq$1l8hg$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>

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 11:53:13 +0100
Organization: A noiseless patient Spider
Lines: 159
Message-ID: <11ad5eq$1l8hg$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 10 Oct 2026 10:53:15 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1745456"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18TeQ1PllMMnyCvnBby98cR"; posting-host="8d9c8b578a5ce6a1f662bc81cc091c91"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:7XpWkBu2s0JCgps9I8oTzwy7ibs= sha256:crluAShTqAub7ljSePqLn+jgZcFSU21gklezfbgg8G8=
	sha1:QeaOERyc3FD7Au/W2LEdyBZexhI= sha256:WA73d7jTuxuHNB5XfJlGN9WckSRRwb+jN2WKFv9r2LQ=
Content-Language: en-GB
In-Reply-To: <11ac783$1bkuo$1@kst.eternal-september.org>