IF

# IF Statement Introduction

The ability to make a choice is what separates humans from computers. Machines only do what you tell them, while humans can think for themselves. By using the IF statement, you are allowing your programs to think about what is happening, and you can make them act accordingly.

# Booleans

A boolean is a type of variable that has 1 of 2 values: True or False. These types of variables are used all over the place, but they are used the most with IF statements.

## Boolean Operators

Boolean operators are signs you can put inbetween two numbers or variables, and the result will be True or False.

### Greater Than

The operation > is used for Greater Than.

The result of 2 > 1 is true
The result of 8 > 9 is false

### Less Than

The operation < is used for Less Than.

The result of 12 < 45 is true
The result of 2 < 1 is false

### Equal To

The operation = is used for Equal To.

The result of 45 = 45 is true
The result of 4 = 1 is false

### Not

The operation 'not' is used for Not. Not is commonly used with other boolean operators.

The result of 2 not= 1 is true
The result of 45 not> 20 is false

## Variables and Numbers

You can use both variables and numbers in any combination when using boolean statements.

The result of iNumber > 20 is True only for when iNumber is larger then 20 (i.e. 21+).
The result of iNumber not= 13 is False when iNumber is exactly 13, but True for any other number.
The result of iNum1 <= iNum2 is False when iNum1 is larger then iNum2.

# IF Statement Syntax

In order to get some real Artificial Intelligence if our programs, we need to master the IF statement. This is how a general IF statement looks like:

``````if <BooleanCondition> then
%Code Goes Here
end if```
```

This can be particularly useful for when you wish to do something when certain conditions are met. For example, say we have a program like this:
``````get iMark
if iMark >= 50 then
put "You Passed :)"
end if```
```

When the user inputs their mark, your program can behave differently depending on what the user's input it.

# IF…ELSE

As you may have noticed in the previous example, if the user's mark is less then 50, then nothing will happen. In the real world, they would have failed the course. This is where the IF…ELSE statement becomes useful. The general syntax is:

``````if <BooleanCondition> then
%Code Goes Here
else
%Code Goes Here
end if```
```

With the previous iMark example, if we wanted to tell the user that they failed, we would have to write a separate IF statement for when they had a mark lower then 50. Instead, we can do:
``````get iMark
if iMark => 50 then
put "You Passed :)"
else
put "You Failed :("
end if```
```

Since we use boolean statements to determine which 'path' to take on our IF statement, if the user didn't pass, then they must have failed, since there are only two choices.

The key thing to realize is that if the IF part of the IF…ELSE statement is executed, then the ELSE part of the IF…ELSE statement will be ignored. Likewise, if the ELSE part is executed, then the IF part will be ignored.

It is important to note that the ELSE was between the IF and the END IF. If the ELSE is not placed before the END IF, your code will not work properly.

# IF…ELSIF…ELSE

Finally, if there are more then one or two options to test for, then the IF…ELSIF…ELSE statement is in order. The basic framework looks like this:

``````if <BooleanCondition> then
%Code Goes Here
elsif <BooleanCondition> then
%Code Goes Here
else
%Code Goes Here
end if```
```

This is used for situations where there are many different outcomes that need to be tested for. In our previous example, the program only determined if we passed or failed…not a very accurate program. But by using the IF…ELSIF…ELSE statement, we can tell the user what grade they received instead! Our example would look something like this:
``````get iMark
if iMark => 80 then
put "You got an A"
elsif iMark => 70 then
put "You got an B"
elsif iMark => 60 then
put "You got an C"
elsif iMark => 50 then
put "You got an D"
else
put "You got an F"
end if```
```

This programs uses two main features about the IF…ELSIF…ELSE statement to it's advantage. First, it uses multiple ELSIF commands. You may have as many as you need, and this program took advantage of that. The second feature this program used was the fact that in the entire IF…ELSIF…ELSE blocks, only one of the blocks are ever executed. If the user's mark is 80 or more, then it will print "You got an A", but it will skip the rest of the IF…ELSEIF…ELSE block. This is important, because then we can check if they got a B, then skip the rest, and so on.

By carefully choosing what type of IF statement you use, you can create some truly powerful Artificial Intelligence.

# Nested IF

Much like how FOR loops, can be nested, all three of the different IF statements can be nested as well, and in the same fashion as the FOR loops too! The basic syntax is:

``````if <BooleanCondition> then
if <BooleanCondition> then
%Code goes here
end if
end if```
```

As usual, you can use IF…ELSE or IF…ELSIF…ELSE blocks anywhere there is an IF block, so allow for a great deal of utility.

# AND IF Statements

Sometimes, you wish to check more then one Boolean condition at a time. Using these tricks allows you to do just that!
When you nest IF statements like so:

``````if iMark > 50 then