switch/case vs if/then/else

Mark Wieder mwieder at ahsoftware.net
Sun Feb 19 11:40:30 EST 2006


Stephen-

I have three rules of thumb for selecting switch/case constructs vs
if/then/else constructs (or at least three that I can think of off the
top of my head):

1. As Rob Cozens has pointed out, if I have more than two choices then
I will probably convert to a switch statement.

switch itemToTest
  case 2
  case 3
  case 4
    answer "it's a" && itemToTest
    break
  default
    answer "it's unknown"
end switch

2. If I have a few exceptions to a regular rule then I will make a
case statement with a break and put the rule in the default handler.

switch itemToTest
  case NOPPMTooHigh
  case HCLPPMTooHigh
    put "failed" into tSmogTestResult
    break
  default
    put "passed" into tSmogTestResult
end switch

3. If I have a list of targets that I will probably want to expand
then that's a candidate for a switch statement.

switch itemToTest
  case
    "sloth"
    "envy"
    "gluttony"
    put "sin" into whatIsIt
    break
  default
    put "virtue" into whatIsIt
end switch

-- 
-Mark Wieder
 mwieder at ahsoftware.net




More information about the use-livecode mailing list