🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #206

Re: Taming The Switch-Statement

#206
From: bart
Date: Sun, 11 Oct 2026 10:58
181 lines
8023 bytes
On 10/10/2026 16:50, David Brown wrote:
> On 10/10/2026 15:11, bart wrote:
>> 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:
>>> [...]
>>
>>> 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.
>>
> Keith explained the background for C's switch.  It has its origins in a 
> time when "goto" was commonly used, and it is a significant improvement 
> on many uses of calculated goto.  It is not "bizarre", dangerous, or 
> "dumb" - or "wrong".  It is simple /different/ from the structured 
> switch/case/match construct found in many other languages.  The choice 
> was made long ago, and it must be obvious to everyone that the behaviour 
> could not be changed.
> 
> This makes it flexible - you can do things with C's "switch" that you 
> cannot do with Pascal "case".  That means you can do some different, 
> interesting and useful things - and it also means you can make some 
> different and annoying mistakes.
> 
> You point out that most C code uses "switch" in a structured manner, 
> with "break" rather than implicit fall-through.  That is my experience 
> too.  What does that tell us?  I think it tells us two things :
> 
> 1. People don't have a problem writing structured code with C's switch 
> statements, using "break" as appropriate.  It works fine for that purpose.

We don't know that, we just see the end result.

The best one say about C-switch is that it can emulate a real switch 
statement. But when reading unfamiliar code, you can't be as confident 
as to the author's intentions: is that an ordinary switch, or is there 
something odd about it?

A bit like looking at a 'busy' for-statement header and trying to 
determine, is this a simple iteration, or is there something else going on?

Because unfortunately C's flexibility makes weird stuff possible.

C's 'switch' is truly bizarre. Below is an experiment where I took an 
ordinary function body, and wrapped 'switch' around it with randomly 
placed case labels.

It still worked (as I placed 'default:' near the start, and providing 
'm' is greater than 7), but What The Hell?

gcc with warnings says nothing useful about it.


>  There are, however, occasional exceptions and more complicated 
> code.  Such use-cases would need to be handled in other ways if C's 
> switch did not work as it does.  My gut feeling is that a solid 
> proportion of such cases were more relevant in the old days before more 
> optimising compilers - things like Duff's Device spring to mind as being 
> useful long ago, but unnecessary with modern tools.

This is the core C code for Duff's Device from Wikipedia:

     switch (count % 8) {
         case 0: *to = *from++;
         case 7: *to = *from++;
         case 6: *to = *from++;
         case 5: *to = *from++;
         case 4: *to = *from++;
         case 3: *to = *from++;
         case 2: *to = *from++;
         case 1: *to = *from++;
     }

This is the emulation of it in my language ('to' is a reserved word, and 
that constuct is 1-based):

     goto (count rem 8| L1, L2, L3, L4, L5, L6, L7 | L0)

     L0: d^ := s++^
     L7: d^ := s++^
     L6: d^ := s++^
     L5: d^ := s++^
     L4: d^ := s++^
     L3: d^ := s++^
     L2: d^ := s++^
     L1: d^ := s++^

Those labels need to be unique, but probably you won't need more than 
one Duff's Device in any one function.

This could be done in C too via existing extensions.


> I personally would have been happier if C's switch was more structured 
> and had no fall-through (obviously it would still need a way to have 
> multiple cases attached to the same sub-block).  

>Does that mean I think 
> C's switch is "wrong", or a bad design?  No, it does not.  Even if I did 
> think it was wrong, would that mean that it /is/ wrong?  No, it would 
> not - an opinion is not a fact.

That is astounding. But then, how many other languages, which have 
copied all sorts of dubious features from C, have also duplicated its 
bizarre switch statement?

That should tell you more.

> Like a great many things in life, we can simply say that C's "switch" 
> might have been a little more convenient if it had been defined in a 
> different manner,

I suggest you try the same exercise I did.

  and when making a new switch-like concept in a new
> language we would probably make different choices.  But it is what it 
> is, and it's not hard to use it for correct code, so why make a fuss 
> about it?  No amount of wailing and gnashing of teeth here is going to 
> change anything.
> 
> 
> To re-enforce another of Keith's points, it /would/ be possible to add a 
> new switch-like construct to C with more structure and no fall-through. 

I posted a proposal for such a statement in a reply to Keith Thompson, 
who completely dismissed it.

The fall-through feature is a minor detail.

-----------------------

static void nsieve(int m) {
     unsigned int count = 0, i, j;
     boolean * flags = (boolean *) malloc(m * sizeof(boolean));
     for (i = 0; i < m; ++i) flags[i]=1;

     for (i = 2; i < m; ++i)
         if (flags[i]) {
             ++count;
             for (j = i << 1; j < m; j += i)
                    flags[j] = 0;
     }

     free(flags);
     printf("Primes up to %8u %8u\n", m, count);
}

static void nsieve_sw(int m) {
     switch (m) {
     default:
     case 1:
         unsigned int count = 0, i, j;
     case 2:
         boolean * flags = (boolean *) malloc(m * sizeof(boolean));
         for (i = 0; i < m; ++i)
             case 3:
                 flags[i]=1;

         for (i = 2; i < m; ++i)
             if (flags[i]) {
             case 4:
                 ++count;
             case 5:
                 for (j = i << 1; j < m; j += i)
             case 6:
                        flags[j] = 0;
         }

         free(flags);
             case 7:
         printf("Primes up to %8u %8u\n", m, count);
     }
}
Technical Details
Message-ID: <11afq4b$2idte$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> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$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!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 11:58:21 +0100
Organization: A noiseless patient Spider
Lines: 181
Message-ID: <11afq4b$2idte$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>
 <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 10:58:20 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2701230"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18+YNRPez/rmjiPOb6in4ey"; posting-host="bb4c63ed27c528dcfc498e66a186f254"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:mvso3vOmEzJ5zSyEYLEdkCHh9xc= sha256:SInkQc4i4Lsyx/vy3+7ISyux1ita4mohJx/KnxKbxy8=
	sha1:KCIB0OZ087aHGhM/ym3SeiiIZcQ= sha256:k83FumLwMoE+MXgQ5vDTJMwyf5+gxiuqdfP7OxmCZw4=
In-Reply-To: <11admri$1s1lb$1@dont-email.me>
Content-Language: en-GB