Livecode Wiki
Advertisement

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 it is a particular value. 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
  hisVar = myVar - 15                      -- hisVar = 60
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 (in this case the answer statement). If there are several statements to executed, the lower form must be used. Those several statements would be inserted before the end if. Another example:

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

You can add also use an else option for when the check is not true, 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                       -- myResistor = 55
else                                           -- if not true...
  answer "Variable myVar is not not proper."   -- do answer
  add 81 to myVaristor                         -- myVaristor = 81 more than before
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 myAnimal contains a string naming an animal:

if myAnimal = "Rat" then                           -- check for "Rat"
  answer "myAnimal is a rodent"                    -- if true, do answer
  put 44 into yourAnimal                           -- yourAnimal = 44
else                                               -- if not true...
  if myAnimal = "Eagle" then                       -- check for "Eagle"
    answer "myAnimal is a bird"                    -- if true, do answer
    put 17 before yourAnimal                       -- yourAnimal = 17 44
  else                                             -- if not true...
    if myAnimal = "Cow" then                       -- check for "Cow"
      answer "myAnimal is a bovine"                -- if true, do answer
      put 33 after yourAnimal                      -- yourAnimal = 17 44 33
    else                                           -- if not true...
      answer "Don't know what myAnimal is?"        -- do answer
      answer  "What is now?" & yourAnimal          -- 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. Use the switch command when working with multiple tests.

Advertisement