No more fall through in case statement?
BC
notmi_emayl_adreznot at hotmail.com.remove.not
Sat Jan 5 03:38:17 PST 2008
On Fri, 04 Jan 2008 17:26:54 -0000, Janice Caron <caron800 at googlemail.com>
wrote:
> On 1/4/08, BC <notmi_emayl_adreznot at hotmail.com.remove.not> wrote:
>> void main()
>> {
>> myswitch(24,
>> {
>> mycase(10, 11, 12, { writefln("10, 11, 12");
>> _continue; });
>> mycase(24, { writefln("24" );
>> _rematch; });
>> mycase(range(3, 25), { writefln("range" );
>> _rematch; });
>> mycase((int a) { return a > 40; }, { writefln(">40" );
>> _rematch; });
>> mycase(&always, { writefln("always" );
>> _rematch; });
>> });
>> _range!(int) ra = {2,3};
>> }
>
> A mighty and noble effort - but I can't help but feel that the
> following is more readable:
>
> if (x==10 || x==11 || x==12) writefln("10,11,12");
> if (x==24) writefln(24);
> if (x>=3 && x<=25) writefln("range");
> writefln("always");
>
Good point.
> The way I see it, the advantage of "switch/case" over "if" is that the
> compiler might be able to find cool ways to optimize the code (e.g
> binary search, sparse array lookup, table lookup, whatever). If the
> compiler can't do that, well then, what's the point of using it at all
> when "if" is perfectly expressive already?
Do you know if the compilers actually do this?
More information about the Digitalmars-d
mailing list