Article View: comp.lang.c
Article #162Re: Taming The Switch-Statement
From: bart
Date: Sun, 11 Oct 2026 01:12
Date: Sun, 11 Oct 2026 01:12
79 lines
3968 bytes
3968 bytes
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.
>> (Of, course, my language has it all:
>
> Of course it does.
Well, it does; what more can I say.
>
> [...]
>
>>> 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?
>
> 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.
>> case 10: default: case 20:
>
> Doctor, it hurts when I do this.
It illustrates having too much flexibility. Do compilers check for
nonsense like this?
Having proper structure must make code analysis simpler than effectively
having a bunch of case labels randomly scattered through the code.
Example:
newswitch (expr)
case 3, 4, 5:
stmt1;
case 6...10, 12:
stmt2;
else
stmt3;
I think this is OK except for the possibility of that 'else' being
attached to stmt2 when that ends with 'if (cond) stmt;'. But C already
has a rule for that and existing means to get around it.
I haven't put in provision for fall-through. If people want that, then
old switch will still work.
In this new statement, there is no special meaning for 'break'; it will
be part of any enclosing old switch or loop.
>> 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?
An implementation HAS to support if-else, so I'm suggesting it wouldn't
be hard to implement structured switch, since lowering to if-else is one
easy option.
Technical Details
Message-ID:
<11aenps$27sq7$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-mailReferences:
<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>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 02:12:29 +0100 Organization: A noiseless patient Spider Lines: 79 Message-ID: <11aenps$27sq7$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 11 Oct 2026 01:12:28 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2356039"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IbswpJSMmYjag2jSC6Q6F"; posting-host="bb4c63ed27c528dcfc498e66a186f254" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:yOWtKZHGmpb8cQYStvNdw4vqaUU= sha256:tTf+hmDRmbTLNSL3PFe+NdoKvFFPZGsf3e/fxGb9P88= sha1:ESHLE3MVlOGbwkfsezb4Q+lSMDI= sha256:34YKuRd6OHmb5bXwAHfoJv30ObaGhIDQSuLIXAhnsIE= Content-Language: en-GB In-Reply-To: <11aehqg$24k8m$3@kst.eternal-september.org>