2,317 Pages

The If command is used to make choices. If this is true then do that. There are several different forms of the if command. For example:

```if <condition> then <do statement>
```
```if <condition> then <do statement> else <do other statement>
```
```if <condition> then <do statement>
else <do other statement>
```
```if <condition> then
<do statements>
end if
```
```if <condition> then
<do statements>
else
<do other statements>
end if
```

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 its current value. The following two forms of the if command 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
put myVar - 15 into hisVar               -- hisVar = 60
end if                                     -- end the if command
```

Both forms of the if command above will work. The top form will work when only 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
add 7 to myVar                          -- myVar = 15
put myVar+43 into mySum                 -- mySum = 50
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 myVaristor                       -- myVaristor = 55
else                                           -- if not true...
answer "Variable myVar is not proper."       -- do answer
put 66 to myVaristor                         -- myVaristor = 66
end if                                         -- end the if command
```

You can nest an if command inside another one. This can be done to as many levels down as you wish. But after a few levels, it gets complicated. For example, 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 much easier to follow. Check it out.

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