Article View: comp.lang.c
Article #263Re: Taming The Switch-Statement
From: bart
Date: Mon, 12 Oct 2026 13:37
Date: Mon, 12 Oct 2026 13:37
197 lines
8720 bytes
8720 bytes
On 12/10/2026 06:05, David Brown wrote:
> On 11/10/2026 19:57, bart wrote:
>> On 11/10/2026 18:17, David Brown wrote:
>
>>> Only Michael can, if he wants, explain what he was thinking.
>>
>> It sounds like he's pressurised into doing so. Maybe so that he can be
>> lectured on how to it 'properly'.
>
> The "pressure" is just because people are curious for more detail.
> Michael is an experienced C programmer, but we all work in slightly
> different ways on slightly different kinds of code bases. I can't think
> why breaking out of loops within switch cases would be "very common",
> but I have no reason to suspect Michael was not writing code "properly".
> I can't think why such code - common or not - would be problematic, or
> why having two different "break" keywords or constructs would be better
> for his code. That's why I would like Michael to give more details -
> not to tell him his "wrong", but to learn more about his coding.
>
>>
>> 'break' to quit a loop early is part of the language, so why shouldn't
>> someone make use it?
>
> I think most C programmers do, at least occasionally.
>
>>
>> It just its use can be interfered with when 'switch' is involved.
>>
>
> I personally have never found an issue there. "break" breaks out of the
> innermost loop or switch. If you write switch statements that are
> intermingled with loops in a non-structured manner, I am sure things get
> difficult and unclear. Since I do not write code with such
> intermingling, I have not had any such problems.
What do mean by 'intermingled' - nested? That's how structured
programming works!
Otherwise there is no intermingling in this example:
for (i=0; i < N; ++i) {
x = A[i];
if (x == 5 || x == 6 || x == 10 || x == 11) break;
switch (A[i]) {
case 5: case 6: case 10: case 11: break; // ??
}
}
The loop body shows two ways of aborting the loop when an exit condition
is detected:
* Using an if-statement and 'break'
* Using a switch-statement and ... break?
The second doesn't work because 'break' now shadows its outer meaning.
Both would otherwise be reasonable choices for this logic.
Here's another example:
for (i=0; i < N; ++i) {
if (A[i] == 1)
if (!f()) break;
else if (A[i] == 2)
if (!g()) break;
else
if (!h()) break;
}
Here, someone might notice that 'switch' suits this pattern better than
an if-else chain. So they try this:
for (i=0; i < N; ++i) {
switch (A[i]) {
case 1:
if (!f()) break;
case 2:
if (!g()) break;
default:
if (!h()) break;
}
}
This is valid C still, but it doesn't have the same behaviour.
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.
>> That that happens and needs a workaround, is not satisfactory. The
>> issue exists 100% because of sharing the 'break' keyword for two
>> different purposes.
>>
> First, there is no indication that conflicts between switch and loop
> "break" occurs in real code, or what the nature of such issues are.
As I said, they CAN'T occur in finished code, since some other approach
has had to be used. You won't know if the conflicts have occurred!
> (Michael's brief comments are not enough information.) Secondly, there
> is no indication that this "needs a workaround" - especially as there is
> no definition of what might be called a "workaround". Usually you use
> the term to mean C code that you want to complain about. Thirdly, the
> keyword "break" is not being used for two different purposes - it is
> being used for the same purpose in two different control structures.
One is for loops to stop iteration, it is optional, and tends to be used
sparingly.
The other is for switch, it is nearly always necessary for the common
pattern of switch, and is needed because otherwise the language leaves
you stranded in the middle of a switch statement. There is no iteration
involved.
Does that sound about the same?
> It is well established that there are occasions where you have nested
> control structures, where it would be useful to be able to break out of
> an outer structure. In C, "break" by itself does not handle such cases,
> and thus additional code is needed (what you call a "workaround", and
> other people call "writing code")
People keep bringing this up. Yes, that's an annoyance too, but it is
genuinely rare.
BTW I looked at the sources for one of my projects (in my language) to
see whether breaking a loop from switch ever occurred. I found it the
very first instance! Then I kept finding more.
This is the outline of the first example:
while pmtype := nextcmdparam(paramno, name, value, "m") do
case pmtype
when pm_option then
when pm_sourcefile then
if passlevel in [run_pass, runpcl_pass] then
exit
'case' is a sister statement to 'switch' and has the same syntax. It's
used for more general cases usually with a small number of branches.
My 'break' statement is called 'exit'. Number two:
do
lex() !first symbol on line
case lx.symbol
when eofsym then
when kendsym then
exit
There are loads more. This next is an example of breaking out of two
loop levels:
while wt do
while w do
if ... then
exit all
It is nearly always all the loops, so I simply write 'all' (an alias for
0 which denotes the outermost loop without needing to count). There are
two such 'exit all' in this 35Kloc project; all other exits are for one
level only.
Here's one more loop/switch example, but with a twist:
docase flags^
when 'S' then
when 'U' then
else
exit
end
'docase' and 'doswitch' are looping forms, which are very common in my
programs. This one is the equivalent of this in C:
while (1) {
switch (*flags) {
case 'S': ...
case 'U': ...
default:
.... // NOW WHAT?
}
}
You can't express it. Sure you can get around it, but that is just
pandering to the issue, and it means you can't use what might be the
most natural and clearest way to do it.
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.
Technical Details
Message-ID:
<11ainqf$3kkvd$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> <20261010220212.000063fd@yahoo.com> <11ae56o$cjlv$4@dont-email.me> <20261010230355.00005189@yahoo.com> <864iesjx6e.fsf@linuxsc.com> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@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: Mon, 12 Oct 2026 14:37:18 +0100 Organization: A noiseless patient Spider Lines: 197 Message-ID: <11ainqf$3kkvd$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> <20261010220212.000063fd@yahoo.com> <11ae56o$cjlv$4@dont-email.me> <20261010230355.00005189@yahoo.com> <864iesjx6e.fsf@linuxsc.com> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11ahpqi$38ir0$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 12 Oct 2026 13:37:20 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3822573"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RZynoXr54AYeHoCgmh3Iw"; posting-host="77c9fe16ce3505b18aae67dd9ba4b8ee" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:+IsMnOGNzzpfnQyEjnmHYJwf1+4= sha256:XLKtZCCdCXdVsRG+HrIz4XC2fl97RwkSncOkf/aBvf0= sha1:1iV7IWMSvVeD89x4EaXkoXTuKOo= sha256:1fhl1YqcrCo6/xB4W6DLfzZJP3YYOubUOpIZJtmCK1k= In-Reply-To: <11ahpqi$38ir0$2@dont-email.me> Content-Language: en-GB