The D Programming Language: switches

Ray Dunn ray at micomvax.UUCP
Wed Mar 16 07:10:15 AEST 1988


[References to various postings giving various proposed incarnations of
switch syntax ommitted]

My 2-penny-worth is that the switch statement is anyway too restrictive with
its integer constant expression values, and that making a special case [pun
not intended] of this was Not Really Worthwhile.

Case statements should just be generalised into a "shorthand" way of
stating if...elseif...elseif...else...

So, (simplified to give jist rather than be definitive):

switch
{ [declaration]
  case (expression):
    .
    [statement]
    .

  case (expression):
    .
    [statement]
    .

  default:
    .
    [statement]
    .
}

Where the expression in each "case (expression)" is a full boolean
expression as in "if (expression)".

There could of course be a (bad) argument to say that if "switch {" appears
as "switch (expression) {" then the previous constant expression meaning of
the switch is used - pretend I didn't suggest that.

Automatic fallthrough should *not* take place, and the common fallthrough
requirement of:

case 1:
case 2:
case 3:

becomes a simple 'or'ed boolean expression.

Compilers can easily generate just as "efficient" code when only constant
expression values are involved (MSC 4.0 seems to generate worse code when you
use a case statement than when you use an if...else if...else... sequence!).

[God! I've just realized that this is aprroximately the way case statements
are defined in that *wonderful* (:-) language dBaseIII - so, I know, I've
just lost any credibility I may have had!!]


Ray Dunn.  ..{philabs, mnetor, musocs}!micomvax!ray



More information about the Comp.lang.c mailing list