Article View: comp.lang.c
Article #166Re: Taming The Switch-Statement
From: Johann 'Myrkraverk' Oskarsson
Date: Sun, 11 Oct 2026 01:51
Date: Sun, 11 Oct 2026 01:51
81 lines
4937 bytes
4937 bytes
On 10/11/2026 9:41 AM, Janis Papanagnou wrote:
> On 2026-10-11 00:54, Keith Thompson wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>> On 2026-10-10 18:50, Michael S wrote:
>>>> I fully agree with your post (plus have to add that "more structured
>>>> switch" apart from new keyword(s) will need some new way to express
>>>> common handler for several cases. Ada/VHDL solution of using | would
>>>> not work in C for obvious reasons).
>>>
>>> Pascal allows '..' and ',' (for ranges and lists), and even mixed.
>>> That should (in principle) be a syntactical possibility in "C".
>>
>> C2Y will add case ranges using "..." (which is already a token used for
>> variadic functions), with syntax and semantics close to the existing gcc
>> extension.
>>
>> switch (foo) {
>> case 'a' ... 'z':
>> // ...
>> }
>>
>> This was considered and rejected (with "..") for C89. One reason for
>> rejecting it was that 'A'..'Z' is not contiguous in all character
>> sets (particularly not in EBCDIC). The C89 Rationale also mentions
>> the problem of large code or jump table space for something like
>> 0..65535, something that can be handled these days by minimally
>> clever compilers.
>>
>> One problem is that `case 0...1:` is a syntax error. 0...1 is a valid
>> preprocessing number that can't be converted to a valid constant.
>> The solution is to surround the ... with spaces, which is probably
>> a good idea anyway.
>>
>> As for ',', that would avoid having to repeat the "case" keyword,
>> but it could be visually confusing. `case 0, 1:` is currently a
>> syntax error, but only because a constant expression can't include
>> a comma operator (with some obscure exceptions). Allowing commas
>> would prevent a future version of the language from either allowing
>> comma operators in constant expressions or permitting non-constant
>> expressions in case labels. I still wouldn't mind such a change,
>> but it would add no real expressive power and I don't think anyone
>> has proposed it.
>
> Thanks for the interesting background information.
>
> Your explanation shows how difficult it is to add features (even if
> they are comparably simple appearing ones) to an existing language;
> many things have to be considered, interdependencies, and also an
> educated thought about future consequences.
>
> It's certainly a lot easier if you can design a language from scratch.
>
> Janis
Dear Javis,
Even then, people screw up, such as Java's lack of labelled /break/s in-
side switch () statements. I guess they were afraid of /goto/s, and
chose to use /break/ instead, with this annoying side effect.
[Ok, I'll probably want to review that before I post more about it, but
I have faith in my recollection.]
C++ has removed some features from C++98, and again from C++11 if I re-
call correctly, so it's not like people don't screw up from time to
time.
I've not used C# extensively enough to encounter problem spots, so
people here in comp.lang.c are welcome to chime in.
My personal guess is that it would be better for everyone, if program-
ming languages didn't escape the laboratory until at least one more non-
trivial project other than the compiler is near completion or finished.
What do you think?
--
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Technical Details
Message-ID:
<zYByS.21374$VDn8.5260@fx14.ams4>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx14.ams4.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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@dont-email.me> <11aefo0$24k8m$2@kst.eternal-september.org> <11aepgb$286ml$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!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx14.ams4.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Taming The Switch-Statement Newsgroups: comp.lang.c 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> <20261010195051.00000594@yahoo.com> <11adv82$cjlv$2@dont-email.me> <11aefo0$24k8m$2@kst.eternal-september.org> <11aepgb$286ml$1@dont-email.me> Content-Language: en-US From: Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> Organization: Watcom Pro Ltd. In-Reply-To: <11aepgb$286ml$1@dont-email.me> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Lines: 81 Message-ID: <zYByS.21374$VDn8.5260@fx14.ams4> X-Complaints-To: abuse@easynews.com X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly. Date: Sun, 11 Oct 2026 09:51:25 +0800 X-Received-Bytes: 4653