request switch statement with common block
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sat Aug 3 10:26:15 PDT 2013
On 8/3/13 10:21 AM, JS wrote:
> On Saturday, 3 August 2013 at 16:16:24 UTC, Ary Borenszweig wrote:
>> On 8/3/13 11:38 AM, JS wrote:
>>>
>>> switch (cond)
>>> common: always executed code here
>>> case A : etc...
>>> ....
>>> }
>>>
>>> instead of
>>>
>>> if (cond) { always executed code here }
>>> switch (cond)
>>> case A : etc...
>>> ....
>>> }
>>>
>>> which requires modification of the condition twice when necessary
>>
>> Do you mean this?
>>
>> switch(cond) {
>> case A:
>> common_code();
>> // something
>> case B:
>> common_code();
>> // something else
>> }
>>
>> (common_code() must not be executed if it doesn't hit any switch case)
>
> exactly
No because your initial rewrite suggested zero is special. But zero has
no special meaning to the switch statement. Consider:
switch (cond)
{
common: ...
case 0: ...
...
}
Andrei
More information about the Digitalmars-d
mailing list