FANDOM


The If command is used to make choices. If true then do this, else do that. There are several different forms of the if command. It mainly depends on how many things you want to do. Suppose there is a variable myVar that changes value several different places in your program. Then at one point in your program, you need to know if the value is 32. There are two forms of the if command that will do this, for example:

if myVar = 32 then answer "Variable myVar is 32 now!"  -- checks for 32 then answer
if myVar = 75 then                         -- check for 75
  answer "Variable myVar is correct now!"  -- do answer
end if                                     -- end the if command

Both forms of the if command above will work. The top form will only work if one thing is to be done (such as the answer). If there are several things that must be done, the lower form must be used. The several statements would be added in before the end if. For example:

if myVar = 12 then                        -- check for 12
  answer "This should do it!"             -- do answer
  put 15 into myVar                       -- give new value to myVar
  put myVar+43 into mySum                 -- give new value to mySum
end if                                    -- end the if command

You can add also use an else option, look at the following example:

if myVar = "thirty-five" then                  -- check for "thirty-five"
  answer "Variable myVar is proper now!"       -- if true, do answer
  put 55 into myResistor                       -- and change value for myResistor
else                                           -- if not true...
  answer "Variable myVar is not not proper."   -- do answer
  put 81 before myResistor                     -- add value to myResistor
end if                                         -- end the if command

You can nest an if command inside another one. This can be done as many levels down as you wish. Suppose the variable myVar = "Cow" For example:

if myVar = "Rat" then                              -- check for "Rat"
  answer "Variable myVar is a rodent"              -- if true, do answer
  put 44 into myAnimal                             -- and change value for myAnimal
else                                               -- if not true...
  if myVar = "Eagle" then                          -- check for "Eagle"
    answer "Variable myVar is a bird"              -- if true, do answer
    put 17 before myAnimal                         -- give myAnamal a first item
  else                                             -- if not true...
    if myVar = "Cow" then                          -- check for "Cow"
      answer "Variable myVar is a bovine"          -- if true, do answer
      put 33 after myAnimal                        -- give myAnamal a third item
    else                                           -- if not true...
      answer "Don't know what myVar is"            -- do answer
      answer  "What is now?" & myAnimal            -- do answer
    end if                                         -- end the if command
   end if                                          -- end the if command
end if                                             -- end the if command

While the above works proper, a switch command is easier to follow when working with multiple tests.

Community content is available under CC-BY-SA unless otherwise noted.