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; }


  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.