Friday, February 29, 2008

If / ElseIf / Else [Continued]

We learned in class that If statements could be more complicated than just one case or the other. We learned that there may be more than one thing that we want to check, and we can do this by using a different If/ElseIf/Else structure:

If condition1 Then
'Do these statements for condition 1
ElseIf condition2 Then
'Do these statements for condition 2
Else
'Do these default statements
End If


We also learned that there can be several ElseIf statements before hitting the final Else.

More Boolean Operators
We learned that we can check more than one relationship at a time by combining them using one of two special operators:

Operator            Example
And 0 <= x And x <= 10
Or 0 <= x Or x <= 10
Not Not ( x = y )


The And operator is only true if both conditions on either side are true. If either side is false, then the entire combined expression is false.

The Or operator is different, because it only need one of the expressions to be true in order for the entire expression to be true. So if one side is true then the entire expression is true, and if both sides are false then the entire combined expression is false.

Truth Tables
We can make truth tables in order to clarify these expressions.

Suppose we use x to represent Condition 1 and y to represent Condition 2. So we have the following relationships:


+-----+-----+--------+----------+----------+
| x | y | Not x | x And y | x Or y |
+-----+-----+--------+----------+----------+
| T | T | F | T | T |
+-----+-----+--------+----------+----------+
| T | F | F | F | T |
+-----+-----+--------+----------+----------+
| F | T | T | F | T |
+-----+-----+--------+----------+----------+
| F | F | T | F | F |
+-----+-----+--------+----------+----------+


T represents True
F represents False

So if the conditions x and y are combined with an And operator, the entire combined condtion is only true if both x and y are True [T]. We can see this in the table above. And of course: x And Not x results in a column of all False entries.

Also, anytime that x or y are True and are combined with an Or operator the entire combined condition is True. We can see this relationship in the table as well.

Not is an operator that takes the current condition and makes its value the opposite. That is, if a condition is True then Not condition makes it False.

No comments: