About switch case statements...

Don nospam at nospam.com
Tue Nov 17 04:07:00 PST 2009


Walter Bright wrote:
> Don wrote:
>> That's not fall-through, one case is *inside* the 'if' clause of 
>> another one!! Wow. Do you really want to encourage that sort of thing?
> 
> I think it's more the #if that obfuscates the code. After long 
> experience with #if, I really don't like it, which is why I adamantly 
> resist having fine-grained conditional compilation in D.

I'm used to that. If you take out the #if, it's still wierd. That one 
really ought to be a goto. It's the presence of the 'else' in particular:

case A:
      if (xxx)
      {
case B:
         yyy;
      }
      else
      {
         zzz;
      }
      break;

I had to read it several times before I could make sense of it. Although 
the zzz; looks like it's part of the B case, it's only part of the A case.

An oddity is that this compiles:

   switch(x) {
case 1:
      if (x<10)
Lcase2:
         writefln("yyy");
      else
         writefln("zzz");
      break;
      }

and so does this:

   switch(x) {
case 1:
      if (x<10)
case 2:
         writefln("yyy");
   }

but this doesn't:

   switch(x) {
case 1:
      if (x<10)
case 2:
         writefln("yyy");
      else
         writefln("zzz");
      break;
   }



More information about the Digitalmars-d mailing list