Article View: comp.lang.c
Article #285Re: Taming The Switch-Statement
From: bart
Date: Mon, 12 Oct 2026 19:38
Date: Mon, 12 Oct 2026 19:38
107 lines
5488 bytes
5488 bytes
On 12/10/2026 18:58, Dan Cross wrote:
> In article <11ainqf$3kkvd$1@dont-email.me>, bart <bc@freeuk.com> wrote:
>> This is valid C still, but it doesn't have the same behaviour.
>
> Yes. I guess I fail to see the point of these, as what you are
> trying to show seems obvious to me, and I strongly suspect it is
> obvious to others, as well.
>
> Are you suggesting that people are unaware of how `break`
> interacts with `switch` inside of a loop? I don't think that is
> true; I think people are well aware, but know how to work around
> it and don't see it as a huge impediment; more of a mild
> inconvenience.
>
> Or are you suggesting that they are trying to defend the design
> of the language in this regard?
That's what it seems like: obstinately trying to pretend it is not a
flaw, that it makes perfect sense, then you never want to do any of the
stuff where it causes problems anyway etc.
There are several issues discussed regarding switch:
* That its structure is totally chaotic (there /is/ no structure)
* That 'break' is needed to escape from the middle of that statement to
the end
* That that 'break' happens also to clash, needlessly, with loop-break
* And also with 'case' only doing one label at a time, instead of
being able to do, for example: 'case a, b, c:' or 'case x...y',
although some of this is taken care of via extensions.
There's quite a lot that could have been done much better!
> I similarly don't think that is
> true; I have certainly been annoyed by this, and I suspect
> others have as well. But again, people know how to work around
> it.
Yes, various workarounds are needed, such as the gotos in your examples.
But if we're going to use 'goto', then why bother with 'break' at all?
Presumably it is to make life easier.
> C is not anyone's mother; I doubt anyone who is not a kook feels
> like they need to rise to the defense of its honor when it is
> criticized.
Defending every shortcoming or quirk to the death seems common here. And
there will always be people to whom they are an invaluable feature (see
SL's recent post).
> What I think some people are sort of mystified by is this idea
> that this one thing is such a deal-breaker as to render the
> language unuseable.
In my case I would find it unusable for major projects if I had to code
manually. I'm just to spoilt for one, but always I'm a terrible typist
and C needs a LOT of fiddly typing.
>> Maybe, some compilers with certain options may point out something about
>> fall-through, so you're aware something's amiss, but it doesn't get
>> around the problem: you can't simply swap if-else chains with switch
>> even when the latter is a better fit.
>>
>> This is a definite flaw in the language.
>
> It _can be_ an _annoyance_ in the language, sure. Is it a,
> "definite flaw"? That is subjective. You clearly feel that it
> is. I couldn't say whether anyone else feels that way; I think
> it's too minor to matter for most programmers.
It's minor until it bites - see the example below.
>> [snip]
>> In my language and in my programs, breaking out of a loop from within a
>> multi-way case/switch selection is a very, very common idiom which is
>> denied to C users.
>
> And yet, C is used successfully by millions of programmers
> around the world, the results of which are used by billions of
> users daily.
This is my first example expressed in C (note those first three args are
passed by reference):
while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
switch (pmtype)
case pm_option: ...; break;
case pm_sourcefile:
if (passlevel == run_pass || passlevel == runpcl_pass) {
break;
Except I can't use break like that. So I have to find a workaround.
This is also 30% more tokens than my original, and that's before I fix
this problem.
So I'm sure C is perfectly usable, what we can't quantity easily is how
much extra effort is needed to get things done compared to a more
amenable syntax.
Technical Details
Message-ID:
<11ajcv8$3u33s$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> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@dont-email.me> <11ainqf$3kkvd$1@dont-email.me> <11aj73v$c2u$1@reader2.panix.com>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: Mon, 12 Oct 2026 20:38:16 +0100 Organization: A noiseless patient Spider Lines: 107 Message-ID: <11ajcv8$3u33s$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@dont-email.me> <11ainqf$3kkvd$1@dont-email.me> <11aj73v$c2u$1@reader2.panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 12 Oct 2026 19:38:17 +0000 (UTC) Injection-Info: dont-email.me; logging-data="4131964"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1910Y5BadjYvjHFtB5qhRSZ"; posting-host="77c9fe16ce3505b18aae67dd9ba4b8ee" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:/HaZnO5jm6QHucWfdA65K2E8+/M= sha256:NNN4fQNaQT95zrXTbZouUgqqgx8gXf9ZDWiGeCqQgaI= sha1:D9iecuH8klD7gogc+N8prsI8iyM= sha256:sxPcq4d8CQIuMGUaYtviWq/cOwIQQ0mqnZPjPCXsac8= In-Reply-To: <11aj73v$c2u$1@reader2.panix.com> Content-Language: en-GB