switch ( Expression ) ScopeStatement

Tim Matthews tim.matthews7 at gmail.com
Mon Jul 6 20:28:24 PDT 2009


Derek Parnell wrote:
> On Tue, 07 Jul 2009 14:57:39 +1200, Tim Matthews wrote:
> 
>> Switch is really a neat form of comparison and gotos but with actual 
>> labels replaced with a case statement. A block statement is usually used 
>> to have more than one case statement valid. This is valid code but 
>> uncomment the next line and it becomes invalid:
>>
>> module test;
>>
>> import tango.io.Stdout;
>>
>> void main(char[][] args)
>> {
>>    if (args.length > 1)
>>      switch (args[1])
>>        case "1": Stdout("1"); break;
>>        //case "2": Stdout("2"); break; //uncomment and error
>>
>> }
> 
> Actually that is not valid code. The first "break;" breaks the program.

yeah I forgot to remove the break as it was no longer part of the switch 
statement.


> Ok, why on earth would a one-case switch be useful? And that still doesn't
> help me understand why allowing other statement types in that position is a
> good idea.
> 
> 

C compatibility. It's not really a problem to D. If there is a compiler 
that only accepted block statements for switch not many people would 
care that it doesn't conform to C spec because no one uses it like that 
anyway.


More information about the Digitalmars-d-learn mailing list