Inside the switch statement

Derek Parnell derek at psych.ward
Tue Jun 9 23:32:46 PDT 2009


On Tue, 9 Jun 2009 16:28:58 +0000 (UTC), BCS wrote:

> Hello grauzone,
> 
>>> http://groups.google.com/group/net.lang.c/msg/66008138e07aa94c
>>> 
>> Many people (even Brian Kernighan?) have said that the worst feature
>> of C is that switches don't break automatically before each case
>> label.
>> 
>> Oh god, that's from 1984, and even today we're struggling with this
>> bullshit in the most modern dialect of C, D.
>> 
> 
> I'm sorry, you don't have my sympathy on this one. There are to many place 
> I've used fall throught to chuck it out.

It is not an either-or situation.

We achieved both models in the up-coming version of Euphoria, at the minor
cost of a new keyword.

By default, it does not do fallthru. Each case is its own master and the
physical placement inside the switch is not relevant.

   switch EXPR do
      case VAL1 then 
          STATEMENTS...
      case VAL2 then 
          STATEMENTS...
      case VAL3 then 
          STATEMENTS...
   end switch

But, if for some good reason you need to do fallthru ...

   switch EXPR with fallthru do
      case VAL1 then 
          STATEMENTS...
      case VAL2 then 
          STATEMENTS...
      case VAL3 then 
          STATEMENTS...
   end switch

It that situation, the absence of a 'break' means that program flow falls
through to the next case. You can still insert 'break' at any point to skip
to the end of the switch statement, even in the default format. However,
additionally in the default format you can use 'fallthru' to let flow drop
to the next case....

   switch EXPR do
      case VAL1 then 
          STATEMENTS...

      case VAL2 then 
          STATEMENTS...
          fallthru  -->>> This makes flow fall through.

      case VAL3 then 
          STATEMENTS...
   end switch


So, if one considers improving D's switch, it is possible to have both
models if one is not frightened of new keywords.

For example ...

  select (EXPR) {
     case VAL1: 
            STATEMENTS...
     case VAL2: 
            STATEMENTS...
     case VAL3: 
            STATEMENTS...
  }

would only execute ONE of the selected cases.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list