No more fall through in case statement?
Janice Caron
caron800 at googlemail.com
Fri Jan 4 09:26:54 PST 2008
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");
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?
More information about the Digitalmars-d
mailing list