🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #668

Re: Taming The Switch-Statement

#668
From: Lawrence D’Oliveiro
Date: Thu, 22 Oct 2026 00:33
97 lines
4655 bytes
On Wed, 21 Oct 2026 09:29:24 -0600, Blaine wrote:

> Just start using the switch statement. It's useful in very very very 
> many cases.

Has a well-known pitfall, though.

How not to forget to end each case-sequence in a switch statement with
a break-statement: put the break at the same indentation level as the
case-clause as well as the switch-expression. That way, you can scan
your eye down a single line for each switch-statement to be sure that
no breaks are missing. A reasonably-complex example, with a nested
switch for added interest:

    real stack_calc
      (
        stackcalc_op op,
        ...
      )
      /* starting with an empty operand stack, carries out the specified
        operations on the specified operands, at the end of which there
        should be just a single value on the operand stack. This value is
        popped and returned as the function result. */
      {
        real * opstack = NULL;
        size_t
            opstacklen = 0,
            opstackalloc = 0;
        real result;
        va_list args;
        va_start(args, op);
        bool first = true;
        bool done = false;
        for (;;)
          {
            if (not first)
              {
                op = va_arg(args, stackcalc_op);
              } /*if*/
            first = false;
            switch (op)
              {
            case STACKCALC_END:
                done = true;
            break;
            case STACKCALC_PUSH:
                  {
                    if (opstacklen == opstackalloc)
                      {
                        opstackalloc += 16;
                        opstack = realloc(opstack, opstackalloc * sizeof(real));
                        bugcheck(opstack != NULL, "unable to expand operand stack beyond %d", opstacklen);
                      } /*if*/
                    opstack[opstacklen] = va_arg(args, real);
                    ++opstacklen;
                  }
            break;
            case STACKCALC_ADD:
            case STACKCALC_SUB:
            case STACKCALC_MUL:
            case STACKCALC_DIV:
                bugcheck(opstacklen >= 2, "not enough operands for operator %d", op);
                  {
                    const real op2 = opstack[opstacklen - 1];
                    real op1 = opstack[opstacklen - 2];
                    switch (op)
                      {
                    case STACKCALC_ADD:
                        op1 += op2;
                    break;
                    case STACKCALC_SUB:
                        op1 -= op2;
                    break;
                    case STACKCALC_MUL:
                        op1 *= op2;
                    break;
                    case STACKCALC_DIV:
                        op1 /= op2;
                    break;
                      } /*switch*/
                    --opstacklen;
                    opstack[opstacklen - 1] = op1;
                  }
            break;
            default:
                bugcheck(false, "invalid operator %d", op);
            break;
              } /*switch*/
            if (done)
                break;
          } /*for*/
        va_end(args);
        bugcheck(opstacklen == 1, "expecting exactly one result left on stack");
        result = opstack[0];
        free(opstack);
        return result;
      } /*stack_calc*/
Technical Details
Message-ID: <11bbll2$du80$3@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> <11ba02r$3pl5h$1@dont-email.me> <11balon$1uor$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: Lawrence D’Oliveiro <ldo@nz.invalid>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Thu, 22 Oct 2026 00:33:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 97
Message-ID: <11bbll2$du80$3@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ba02r$3pl5h$1@dont-email.me>
	<11balon$1uor$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 22 Oct 2026 00:33:39 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="456960"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/+73Al1mQqrbB4f/z+fAAB"; posting-host="4b10b603a06c930ab64063c7fde45828"
User-Agent: Pan/0.165 (Kostiantynivka)
Cancel-Lock: sha1:irJnGsxwZ/L5a5ElYqo0e+vbuYs= sha256:X3RsV7Zz2txY6mI3QJoDEUK4QqQUKaaSS9xho5RnIF8=
	sha1:P1jjI/PkI/3tM/fCZaBy7GusSMw= sha256:cRp+WfaTlejX7sXKXe0soleYh3lnjAmXeG8ZplTzICQ=