🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #173

Re: Taming The Switch-Statement

#173
From: Waldek Hebisch
Date: Sun, 11 Oct 2026 05:52
60 lines
4130 bytes
David Brown <david.brown@hesbynett.no> 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.

AFAICS Pascal-style 'case' + 'goto' that can jump inside constructs
can do everthing C 'switch' can do.  Falltrough is just jump to
another case, disallowed in Pascal, but legal in C.  Similarly,
other unstructured uses of 'switch' could be done using combination
of structured 'switch' and 'goto'.  Structured uses are much more
frequent, so really there is no argument for C-style 'switch' except
for backward compatibility (which is deciding argument).

> 2. C programmers would mostly also have been fine with a structured 
> "switch" where each "case" had its own block and there was no 
> fall-through.  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.

Using 'goto' + structured 'switch' one needs only two optimizations to
get the same code as from C 'switch':

1) 'goto' to instruction directly following it should be deleted,
2) if body of a case consists of single 'goto' for this case
   'switch' should transfer control to the target of 'goto'.

Those are very simple optimizations and could be included in
rather small compiler (like one running on PDP-11).

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11af87h$1m4em$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!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!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 05:52:51 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11af87h$1m4em$1@paganini.bofh.team>
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>
Injection-Date: Sun, 11 Oct 2026 05:52:51 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="1774038"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2