FANDOM


If you want a certain thing done under a particular condition, use an IF statement. If you want two different things done under their own particular conditions, also use an IF statement. But if you want more than two different things done, use a Switch. It not only looks nicer, but is easier to follow. Less chance for mistakes.

A Switch is the best way to evaluate what to do for multiple options. You can also declare a default action in case the other ones are not matched. Suppose myRat = 45. For example:

switch myRat                          -- will only check values of myRat
  case 32                             -- check for 32
     answer "myRat is 32"             -- if true, then do answer
  break                               -- do not check any other value, leave switch
  case 45                             -- check for 45
     answer "myRat is 45"             -- if true, then do answer
  break                               -- do not check any other value, leave switch
end switch                            -- end the switch command

Note the break keyword, it's used to close the case option. if you don't use break, switch will continue to execute the statements in the next check. This will continue until switch intercepts a break or an end. Another example:

switch myFix                          -- will only check values of myFix
  case 52                             -- check for 52
  case 101                            -- check for 101
     answer "myFix is 52 or 101"      -- if either of above are true, then do answer
  break                               -- do not check any other value, leave switch
end switch                            -- end the switch command

Now let's see to add a default action:

 switch myTar                         -- will only check values of myTar
  case "Cucumber"                     -- check for "Cucumber"
     answer "myTar is Cucumber"       -- if true, then do answer
  break                               -- do not check any other value, leave switch
  case "Raspberries"                  -- check for Raspberries
     answer "myTar is Raspberries"    -- if true, then do answer
  break                               -- do not check any other value, leave switch
  default                             -- if nothing above is true, then this will be
     answer "What myTar is. :-("      -- then do answer
end switch                            -- end the switch command

There is another form of the switch command. Instead of checking the value of a particular variable, instead checking the case expressions for one that evaluates to true. For example:

switch                                 -- will check for a value of true
  case myZaz > 30                      -- check expression for true
    answer "myZAZ > than 30"           -- if true, then do answer
    put 55 after myTar                 -- do statement
    break                              -- do not check any other value, leave switch
  case myFiz = "Automobile"            -- check expression for true
    put myFix into fiddleFaddle        -- if true, do statement
    answer "myFiz is a car             -- then do answer
    break                              -- do not check any other value, leave switch
  case myCat = myDog                   -- check expression for true
    put 30 into myVar                  -- if true, then do statement       
    set label of button "Mine" to "BB" -- do statement
    break                              -- do not check any other value, leave switch
end switch                             -- end the switch command
Community content is available under CC-BY-SA unless otherwise noted.