The D Programming Language: cases

Richard Harter g-rh at cca.CCA.COM
Wed Mar 9 08:40:41 AEST 1988


>
>Of course, even C-like fallthrough isn't good enough if you have THREE cases
>that end up at the same point.  (Assuming the code to be executed is AC | BC |
>C, not ABC | BC | C.)  Or even two cases with different initial segments (AC |
>BC).  Might just as well use the same construct (goto) for all of these.  (But
>I do like `goto case X` and `goto default`; one ought to be able to use the
>existing label.)

A more general solution runs as follows:  Associate a flag with each such block.Set the execution control flags in the switch case blocks; execute the blocks
under if control.  E.g.

ex_A = ex_B = ex_C = 0;
switch (foo) {
  case 0: ex_A = ex_C = 1; break;
  case 1: ex_B = ex_C = 1; break;
  case 2: ex_C = 1; break;
  default: break;
  }
if (ex_A) {A}
if (ex_B) {B}
if (ex_C) {C}

This works quite nicely if the blocks are to be sequentially executed.
In the general case more than one block may be executed per case, in
varying orders, and the order is affected by results within blocks, I
would much rather use state machine logic and transition tables than
a rats nest of goto's.  This is a personal taste, no doubt, but I find
it is easier to construct and maintain the tables, then it is to go into
the code and fiddle with the logic.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list