🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #264

Re: Taming The Switch-Statement

#264
From: David Brown
Date: Mon, 12 Oct 2026 14:12
149 lines
7322 bytes
On 12/10/2026 15:37, bart wrote:
> 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!
> 

No - if I had meant "nested", I would have written "nested".  In C, 
switch statements are not fully structured.  That's why I wrote 
"intermingled".  I had assumed the meaning was clear, but there is no 
standard definition of "intermingled" code.

You can write things like :

int foo(int x) {
     int y = x;
     switch (x) {
         case 0 :
             y++;
             break;
         case 1 :
             for (y = 0; y < x; y++) {
         case 2:
                 x++;
             }
     }
     return y;
}

That is obviously a silly, invented example.  But it is possible to jump 
into loops with an outer switch.  Such things may be of use to some 
people - just as "goto" jumping around might be of use.  It is of no 
interest to me, and I don't write code that intermingles switches and 
loops (or conditionals).  I limit my switch statements to having clear 
structure - using braces to create blocks within cases if I have local 
variables.

In the hypothetical example above, if there was a "break" after "x++;", 
it could easily be unclear to the reader if it applied to the loop, or 
to the switch.  Having separate "break_switch" and "break_for" keywords 
or constructs would make that difference clear - but I cannot think of a 
real example where it would not be clearer to have the entire loop 
within one case and avoid the issue altogether.

> 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;   // ??
>          }
>      }

That is not "intermingling", as I used the word - it is just a switch 
nested inside a loop.

> 
> 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.

No, they would not be reasonable choices.  It is very, /very/ simple, as 
long as your code is written in a structured manner - nesting the 
constructs as you did above.  "break" applies to the innermost loop or 
switch.  That's the rule.

> 
> 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.

Surprise!  Someone changed the code, and the behaviour changed!  Who 
could have imagined that might happen?

> In my language and in my programs,

No one else cares.  Your language is your own, and not relevant outside 
of your bubble.  You keep thinking it somehow "proves" your points, or 
that it shows how "wrong" C is - it does not.  There is no need to waste 
your time or other readers' time by writing about it.  Stick to writing 
about C here.


Technical Details
Message-ID: <11aipsc$3kr94$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-mail
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> <11ainqf$3kkvd$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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Mon, 12 Oct 2026 16:12:28 +0200
Organization: A noiseless patient Spider
Lines: 149
Message-ID: <11aipsc$3kr94$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>
 <11ainqf$3kkvd$1@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 14:12:28 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3829028"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19vReb8+HDL5rGOFCKbNLhM2/7P7kpkc8E="; posting-host="711f10de653bed0662d2460037681553"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bSvo7KBHKCW2UJgy013pZuJNQXg= sha256:X5gw4UUsEphyY0enNUk4Zi1KgeVhDV0J6y/1nk8gpYE=
	sha1:4BU7pr2RLwl6AlJNV5fORGFX6RQ= sha256:cvraIAz8X1l492WHFLcmA5GSaFGTvZ6hoFRiJlqm6cY=
Content-Language: en-GB, nb-NO
In-Reply-To: <11ainqf$3kkvd$1@dont-email.me>