switch ( Expression ) ScopeStatement

Tim Matthews tim.matthews7 at gmail.com
Mon Jul 6 19:42:22 PDT 2009


bearophile wrote:
> Derek Parnell:
>> is valid syntax! Why is that?
> 
> To allow train-wrecks like this one:
> 
> version(Tango) import tango.stdc.stdio;
> void main(char[][] args) {
>   if (args.length > 1)
>     switch (args[1]) {
>       int x = 1; // This initialization to 1 doesn't happen,
>                  // it's the same as int x = void;
>       case "1": printf("1! x=%d\n", x); break;
>       case "2": printf("2! x=%d\n", x); break;
>     }
> }
> 
> Bye,
> bearophile


You are looking at something completely different but is not limited to 
switches. Switchs are a bit like gotos and gotos can achieve this same 
skip initialization errors.


More information about the Digitalmars-d-learn mailing list