Differences between revisions 1 and 2
Revision 1 as of 2005-01-20 15:40:07
Size: 507
Editor: 207
Comment:
Revision 2 as of 2005-01-20 15:47:11
Size: 944
Editor: 207
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Many languages have a SwitchStatement Aka CaseStatement. Generally, these languages don't have convenient mapping abstractions built in, so a CaseStatement provides a slightly improved abstraction over ifs and gotos for mapping between an index and some corresponding code. Many languages have a SwitchStatement Aka CaseStatement. Generally, these languages don't have convenient mapping abstractions built in, so a CaseStatement provides a syntatic sugar (as opposed to an abstraction) to help write constructs like
{{{
if(x==3) goto case1;
if(x==6 || x=7) goto case 2;
goto default:
 case1: {a();}
 case2: {
b();}
 default: { foo; }
}}}
Line 3: Line 11:
Python fortunately has mapping constructs built in, and has not need for a SwitchStatement: Simply use a dictionary to look up the code that corresponds to the case you want to handle for a given index value, and execute it. as
{{{
Line 5: Line 14:
switch(x)
{
  case1: {a();}
  case2: {b();}
  default: {foo;}
}

}}}

over ifs and gotos for mapping between an index and some corresponding code. Gotos aren't necessarily bad, but a dictionary that maps a value to code you want to execute is an abstraction that matches the semantics intended for case statements.

Python fortunately has mapping constructs built in, and has not need for a SwitchStatement: Simply use a dictionary to look up the code that corresponds to the case you want to handle for a given index value, and execute it.

Many languages have a SwitchStatement Aka CaseStatement. Generally, these languages don't have convenient mapping abstractions built in, so a CaseStatement provides a syntatic sugar (as opposed to an abstraction) to help write constructs like

if(x==3) goto case1;
if(x==6 || x=7) goto case 2;
goto default:
 case1: {a();}
 case2: {b();}
 default: { foo; }

as

switch(x)
{
  case1: {a();}
  case2: {b();}
  default: {foo;}
}

over ifs and gotos for mapping between an index and some corresponding code. Gotos aren't necessarily bad, but a dictionary that maps a value to code you want to execute is an abstraction that matches the semantics intended for case statements.

Python fortunately has mapping constructs built in, and has not need for a SwitchStatement: Simply use a dictionary to look up the code that corresponds to the case you want to handle for a given index value, and execute it.

SwitchStatement (last edited 2008-11-15 14:00:34 by localhost)

Unable to edit the page? See the FrontPage for instructions.