Article View: comp.lang.c
Article #241Re: Taming The Switch-Statement
From: bart
Date: Sun, 11 Oct 2026 21:36
Date: Sun, 11 Oct 2026 21:36
65 lines
3453 bytes
3453 bytes
On 11/10/2026 02:12, bart wrote:
> 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;'.
It might have more problems than that, due to not having 'closure'. That
is, delimiting that might be done by 'end' or via indents in some
languages, or by '}' here.
I would prefer not to have enclose the whole body in braces. But as it
is, then here:
newswitch()
case // 1
newswitch()
case
case // 3
case // 4
it's not clear, when there is no 'else' part, whether case(4) continues
that second switch and from case(3) or the first and case(1), and there
is no way to disambiguate.
OK, not a big problem, it's not hard to fix. Fortunately I'm not going
to implement it.
(BTW I had a look at an old versions of my language, and it seems it
didn't acquire 'switch' until quite late.
Instead there was another, more flexible statement:
* This worked for scalar types ints, floats and pointers
* Test values could be runtime expressions
* Comparisons were done sequentially
* The whole thing could return a value and be used in expressions, or
be the last statement of a value-returning function.
I believe 'switch' was introduced as an explicit streamlined version with:
* Only 'int' expressions
* Test values had to be compile-time constants
* Comparisons were expected to be done in parallel, and via
a jump-table.
Having two forms was considered simpler than the compiler trying to
analyse the one kind of statement, and silently /not/ using a jumptable
due to some oversight.)
Technical Details
Message-ID:
<11agvhl$31ftr$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> <11aenps$27sq7$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: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 11 Oct 2026 22:36:55 +0100 Organization: A noiseless patient Spider Lines: 65 Message-ID: <11agvhl$31ftr$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> <11aenps$27sq7$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 11 Oct 2026 21:36:54 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3194811"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5RG2u8tv3+PtjeqyJDz2s"; posting-host="bb4c63ed27c528dcfc498e66a186f254" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:QlbEvGNnxeXAlOSNCOV8FegZsvs= sha256:SIJHhv6j6aAhpc/ZJGq/p89NeWncoD3SJF4VosHs3ls= sha1:rok+9j6bIMmfzky2P7Yl0ikbzxI= sha256:hi3EWQ/gh3xe9dg2/OLcMW/7sMqLllxlYYCZxUMJCrQ= Content-Language: en-GB In-Reply-To: <11aenps$27sq7$1@dont-email.me>