## FANDOM

2,318 Pages

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. For example:

```if <condition> then <do statements>
```
```if <condition> then <do statements> else <do other statements>
```
```if <condition> then <do statements>
else <do other statements>
```
```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 if it is a particular 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
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
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"
put 55 into myVaristor                       -- myVaristor = 55
else                                           -- if not true...
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"
put 44 into yourAnimal                           -- yourAnimal = 44
else                                               -- if not true...
if myAnimal = "Eagle" then                       -- check for "Eagle"
put 17 before yourAnimal                       -- yourAnimal = 17 44
else                                             -- if not true...
if myAnimal = "Cow" then                       -- check for "Cow"