Article View: comp.lang.c
Article #143Re: Taming The Switch-Statement
From: Janis Papanagnou
Date: Sat, 10 Oct 2026 21:14
Date: Sat, 10 Oct 2026 21:14
86 lines
4924 bytes
4924 bytes
[ 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'.)
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).
> You still think it makes sense?
Yes, because it works as intended for the other common cases.
If you need a "flexible jump" just use 'goto'. The point of 'break'
is to "tame" that "harmful goto" and introduce something "lighter".
Some languages support a 'break' (and a 'continue') with a numeric
argument to handle the case of nested constructs. That is of course
yet more flexible without sacrificing the "prevent wild jumps" dogma.
[ another big snip ]
>> [*] In the GNU version of Awk - a much simpler but higher abstracted
>> language - you can nicely compare strings, and even regexps.
>
> 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.)
(BTW, it would be new to me that you'd have complexity guarantees in
"C".)
Janis
> [...]
Technical Details
Message-ID:
<11ae9r3$cjlv$7@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>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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sat, 10 Oct 2026 23:14:11 +0200 Organization: A noiseless patient Spider Lines: 86 Message-ID: <11ae9r3$cjlv$7@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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 10 Oct 2026 21:14:12 +0000 (UTC) Injection-Info: dont-email.me; logging-data="413375"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/umF2fo0YrnyEHWQuF5hwu"; posting-host="51a9598490edb80150aa60ca9e0cee6b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:/cADDGrcsfOrQi4d9dukiADnDJA= sha256:Np6ic7gOhu7+H4cLacbiOEW0gJtTedpFnyPNvyy+ZxQ= sha1:G4ApaPyBfJM60tZBuMfUPUqUMPg= sha256:0dfy5pSFb/hnkPNOhPTRs5d6aifTvL8u9KlClxceM2s= In-Reply-To: <11ae62d$221po$1@dont-email.me> Content-Language: en-US