Article View: comp.lang.c
Article #149Re: Taming The Switch-Statement
From: bart
Date: Sat, 10 Oct 2026 22:01
Date: Sat, 10 Oct 2026 22:01
130 lines
6281 bytes
6281 bytes
On 10/10/2026 22:14, Janis Papanagnou wrote:
> [ I skip most of what you wrote because it's not adding anything new
> and I basically still just disagree with all you presented; if you
> re-read my previous post you have my comments on your "new" text. ]
>
> On 2026-10-10 22:09, bart wrote:
>> On 10/10/2026 19:08, Janis Papanagnou wrote:
>>> On 2026-10-10 15:11, bart wrote:
>>>> On 10/10/2026 13:01, Janis Papanagnou wrote:
>>>>>
>>>>> Just imagine if some C-programmers wanted a fall-through semantics
>>>>> but all they got would be the Pascal or Algol 68 'case' construct.
>>>>
>>>> Well, Algol 68 doesn't even have 'break'. For unusual control-flow
>>>> it needs goto.
>>>
>>> Yes. - And while "C" has 'goto' as well it also supports 'break' for
>>> some purposes; e.g. as a loop-exit, so it makes sense to reuse that
>>> keyword for other purposes as in 'switch' where it's been useful.
>>
>> No, it makes no sense at all, because their use-cases overlap.
>>
>> It means that, inside a SWITCH statement, you can't do an early break
>> out of a case-block from inside a loop.
>>
>> And if inside a loop, you can't break out of that loop from inside a
>> switch statement:
>>
>> while (1) {
>> if (x == 3)
>> break;
>>
>> switch (x) {
>> case 3: break;
>> }
>> }
>>
>> The first break works within IF. But if you choose to use SWITCH, if
>> doesn't work.
>
> It also doesn't work in nested loops. (That's a restriction of C's
> 'break'.)
That is a separate matter, and one that occurs sparingly.
On the other hand, breaking out of one level is fairly common, and so is
the use of 'switch'.
Now you have an artificial problem caused by two brain-dead decisions:
* A crappy, crude design of 'switch' that leaves you stranded in the
middle of the statement
* Deciding to fix that by USING THE SAME KEYWORD as one of the loop
controls, for something that isn't to do with loops.
This is just very poor language design.
(Inside my C compiler, there are two AST tags called 'jbreak' and
'jbreaksw' - they are two distinct features.)
>
> I don't think it makes sense to introduce another distinct keyword
> to address a special case like that; that's very short-sighted. You
> could still not address, e.g., a "for() for() switch()" structure.
> Other languages provide more universal, thus more sensible features
> to support out-of-band conditions, or more universal "local exits"
> (that support more than one nested level).
THIS IS NOT A NESTED LOOP PROBLEM! It has been made into one by reusing
BREAK.
What's the big deal about keywords anyway?
>> You still think it makes sense?
>
> Yes, because it works as intended for the other common cases.
Would you have made the same choice?
Assuming yes, and assuming you'd also devise some scheme to cut across
nesting, what would happen here:
FOR
SWITCH
FOR
SWITCH
break...
You want to break out of that inner FOR, but now you have to count both
nested loops and switch. So, break 2?
So what would break 3 mean: does it get you to the end of that outer
SWITCH? This is something that would not previously have cross your mind:
SWITCH
SWITCH
break 2
That is, to use 'break' to cut to the end of the enclosing switch
statements. It is just not meaningful.
This just further shows that the two concepts are distinct.
>> I have a narrow view of SWITCH, which is specifically for use with
>> integer values. That allows comparison of N unique possibilities
>> simultaneously, or in O(1) time.
>> So if you're comparing strings, then it is not SWITCH IMO, unless
>> those strings are perhaps interned.
>
> A CDC 175/176 carried a Pascal TEXT object in one machine word and
> could compare it as atom. Newer standard PC CPUs have more bits in
> a word than those former supercomputers. - But the point here was
> that internals should be abstracted by a higher level programming
> language. - And compilers have principally various possibilities to
> support some "atomization" of string comparisons with compile-time
> string constants. - As said, it would have been useful in many of
> my application cases. (No more, no less.)
My point is that SWITCH should be a low level feature working on
integers. (Like how CASE works in Algol68.)
A version for higher level types, for runtime case-values, and a
possible sequential, ordered search, could share the same keyword, but I
would prefer a different keywords here since the semantics can be very
different.
Technical Details
Message-ID:
<11aecjq$24dls$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> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11aduuk$cjlv$1@dont-email.me> <11ae62d$221po$1@dont-email.me> <11ae9r3$cjlv$7@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: Sat, 10 Oct 2026 23:01:31 +0100 Organization: A noiseless patient Spider Lines: 130 Message-ID: <11aecjq$24dls$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> <11aduuk$cjlv$1@dont-email.me> <11ae62d$221po$1@dont-email.me> <11ae9r3$cjlv$7@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 10 Oct 2026 22:01:31 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2242236"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QBlOueP1+XyDDwB2mxCmE"; posting-host="8d9c8b578a5ce6a1f662bc81cc091c91" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:0wNhsZ/kjDrsFvzyART4IrnidQE= sha256:aON7cLBpoOBFYALuv0G4hypUQgCW4DEPeob8XvViuig= sha1:SyJk6TP5TK8Rw4VAMMS44agHS/Y= sha256:745qafZ5Zwccfm3lEVCEY4SqL//kmk0mGn5/0omqXPg= In-Reply-To: <11ae9r3$cjlv$7@dont-email.me> Content-Language: en-GB