🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #376

Re: Taming The Switch-Statement

#376
From: Kaz Kylheku
Date: Wed, 14 Oct 2026 05:17
42 lines
3192 bytes
On 2026-10-13, Michael S <already5chosen@yahoo.com> wrote:
> Of course, I never gathered exact statistics.
> My guts feeling is that among switches nested within loop it could be as
> high as one out of five. Not one out of five case labels, but one out of
> five switch statements.

Switches inside a loop are natural whenever a state machine is being
simulated which has acceptance/failulre states which indicate that the
simulation should end.

Switches inside a loop are also natural whenever a stream of
integer-tagged cases is being handled where a terminating sentinel is
also labeled.

There are at least two workarounds for writing this which avoid
duplicated testing or loop termination flags, gotos or such.

One is to make the loop the last statement of a function and use
"return" in the loop-terminating cases.

The other is to put a "break" at the bottom of the loop and
terminate all the loop-continuing cases with "continue",
the terminating cases with "break":

  for (;;) {
    int ch = getc(in);
    switch (ch) {
    case '\n':
      lines++;
      /* fallthrough */
    default:
      continue;
    case EOF:
      break;
    }
    break;
  }

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Technical Details
Message-ID: <20261013221123.363@kylheku.com>
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> <11agv1t$30q22$2@kst.eternal-september.org> <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me> <20261012223144.00000f95@yahoo.com> <86zewhhdep.fsf@linuxsc.com> <20261013225449.00005562@yahoo.com>

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: Kaz Kylheku <046-301-5902@kylheku.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 14 Oct 2026 05:17:12 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <20261013221123.363@kylheku.com>
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> <11agv1t$30q22$2@kst.eternal-september.org>
 <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me>
 <20261012223144.00000f95@yahoo.com> <86zewhhdep.fsf@linuxsc.com>
 <20261013225449.00005562@yahoo.com>
Injection-Date: Wed, 14 Oct 2026 05:17:15 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1255690"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/qgH6TpRSML3xiWu0hQTEpi/DnttW5XP0="; posting-host="4d050e2080a6aedae146197a4f3e5ab9"
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:1nKORNgNJNtfwL28D2fXvnE2Yf4= sha256:ugWotoJgpyKLrX8MlqtMWuj1ERtdnTvpYQBu+M5EmdE=
	sha1:BysmWxCQwwVU9qkHhEVYIrvdLe0= sha256:Dd397HdOwYWQD9GDP7mhdA19s+5ESgZ5gFFjPL2suKg=