About switch case statements...

KennyTM~ kennytm at gmail.com
Mon Nov 16 02:05:01 PST 2009


On Nov 16, 09 17:48, MIURA Masahiro wrote:
> On 11/16/2009 06:16 PM, Denis Koroskin wrote:
>>> If I remember correctly, one of D's design policies is
>>> that a D code that looks like C code should behave like C.
>>> Are we giving up that policy?
>> Correction: either behave like C, or raise a compile-time error.
>
> Yes. I should have written "a valid D code".
>
>> Missing break statement will not cause a different behavior. It will
>> fail to compile.
>
> Could you clarify?

switch (x) {
   case 2:
     doSomething();
         // At this point:
         // Compiles fine in C.
         // Falls through to the next (irrelevant) branch.
         // Compile-time error in D (missing "break;" or "goto case 3;")
   case 3:
     doSomeTotallyDifferentThing(x, ~x);
     break;
   ...
}


In you proposal, does a break statement
> breaks out of the switch? Then,
>
> for (;;) {
> switch (foo) {
> case "FOO":
> break;
> }
> }
>
> In C, 'break' exits the for-loop. In your proposal, it doesn't.

Check with you compiler. In C the inner "break" doesn't break the for loop.



More information about the Digitalmars-d mailing list