🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #272

Re: Taming The Switch-Statement

#272
From: David Brown
Date: Mon, 12 Oct 2026 17:05
149 lines
7804 bytes
On 12/10/2026 17:32, bart wrote:
> On 12/10/2026 15:12, David Brown wrote:
>> On 12/10/2026 15:37, bart wrote:
> 
>> That is obviously a silly, invented example.
> 
> Yes, but I am not discussing that in this post. The subthread is switch- 
> break interfering with loop-break.
> 

"Switch break" does not in any sense interfere with "loop break".  There 
is no interference, no conflict, no confusion.  "break" leaves the 
current innermost switch or loop.  It is simple and obvious.  And while 
I agree that occasionally you might want a want to break out of an outer 
switch or loop from within an inner one, it applies equally to two 
nested loops, two nested switches, a switch containing a nested loop, 
and a loop containing a nested switch.

> 
>>>
>>> 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.
> 
> Why not? You seem to be talking about both IF and SWITCH here; are there 
> any other conditional control-flow statements that I'm not aware of?

I am sure you have read the C standards, so that you know the answer to 
that.

> 
> Or is your more reasonable approach involving superfluous loops or 
> superfluous functions (just to score some point)?
> 
> We're trying to avoid convoluted code.
> 

No, I don't think "we" are trying to do anything.  /You/ are trying to 
invent problems, and to persuade others that the confusions in your own 
mind are real issues for C programmers.  I am explaining that there is 
no special challenge here.

>>   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.
> 
> Yes, we know that's rule! 

So we are done, then?  One simple rule to learn - and a pretty obvious 
rule at that.  Now you know the answer, you can stop worrying about it.

> And that is the problem: the break of switch 
> interferes with normal loop break, which means that forces your hand 
> whenever you'd prefer to use the more apt 'switch' construct.

There is no interference.  There is a /limitation/ - "break" only exits 
the innermost structure, and cannot break out of outer structures.  It's 
like "return" only returns from the current function, not the function 
that called it.

> 
>>> 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?
> 
> Now you're being facetious. THE BEHAVIOUR SHOULD BE EXACTLY THE SAME:
> 

Only in your mind.

You changed the code significantly, and the behaviour changes.

>      Loop
>         ....
>         Break when some condition is true
>         ....
> 
> You should be able to choose how you write your conditions. So the fact 
> that the behaviour changes while expressing the same logic IS THE PROBLEM.

You can often express the same logic in many different ways in code, and 
the details of how you write the code need to be changed to match.  Why 
should anyone expect that an "if" statement can be changed to a "switch" 
statement and everything else should remain the same?  If I change a 
"while" loop to a "do" loop, it's still a loop - but the contents must 
be changed if I want the same behaviour.

You are inventing things that don't exist outside your imagination, and 
then complaining that C was not designed specifically to suit the rules 
you just made up.

> 
> I mean, why on earth should it? Just because some numskull decided to 
> use the same fucking keyword for two different things?
> 
> Now, everyone is trying to suggest it was in fact a wonderful idea, you 
> save one whole keyword, and that it's SO easy to try and avoid the 
> ensuing mess by programming your way around it or avoiding certain 
> statement combos.
> 

Again, I can't speak for anyone else.  But the very idea that any of 
this should be difficult or an issue for any normal (not psychotic) C 
programmer had never occurred to me until it was brought up in this thread.

> 
>>> 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.
> 
> Then you've just got your head in the sand, sorry.
> 
> People have been asking about examples of exiting a loop from within 
> switch, and in my code there are myriad such examples, even more than 
> I'd thought (I guess I don't even think about it).
> 
> That they're in a different language doesn't matter: it shows the idiom, 
> that code-pattern, can be useful and can be the more natural and 
> intuitive choice.
> 
> Of course you won't find such examples in C because the language doesn't 
> allow it!
> 
> Oh, and I also showed an easy solution to the nested loops issue.
> 
> 
>>   There is no need to waste your time or other readers' time by 
>> writing about it.  Stick to writing about C here.
> The subject is Taming the Switch-Statement, and the opening post was 
> ways to forget writing that famous 'break'.

We all know that some people sometimes forget "break" statements in 
their switches.  Mistakes and typos happen to the best of us, in all 
programming languages, and every programming language has its own 
selection of "archetypical" mistakes, and tools to help catch or avoid them.


Technical Details
Message-ID: <11aj41i$3oq6i$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> <11aipsc$3kr94$1@dont-email.me> <11aiui8$3nisn$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 19:05:54 +0200
Organization: A noiseless patient Spider
Lines: 149
Message-ID: <11aj41i$3oq6i$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> <11aipsc$3kr94$1@dont-email.me>
 <11aiui8$3nisn$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 17:05:55 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3958994"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18dgfU9NyRyUQkLMzKQcyyQ6gtCiUofaKU="; posting-host="711f10de653bed0662d2460037681553"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:scswUabbe1Tj3LwZdeqOw4XzHMI= sha256:7BjASuqpEqcIVQtmwfon9CRKYKtLh4ABw4MpaSEgVC8=
	sha1:LRcM+vTSDxystxaYYyLxQnCQCKY= sha256:hbZnyAY1Vu3F+6HUNF4R4YjUI+4X6EOnJqz4qIlLfSk=
In-Reply-To: <11aiui8$3nisn$1@dont-email.me>
Content-Language: en-GB