JMA201-01
Fundamentals of Application Development

DEPARTMENT SCHEDULE SYLLABUS RESOURCES
If Statements

Decisions with IF

At the simplest level, computer programs do one of a few things:

We looked briefly at the first 2 things... we will look at the 4th item now, and the others will follow. For now, lets make some decisions.

VB decisions are based on the if statement. What the if statement does is compare 2 values; this is usually called testing equality. In your head, look at the following statement: 4=5

Was that statement true? Does 4 equal 5? No, it doesn't. Ok, what if I said "If 4 equals 5, give me a dollar". You would evaluate the expression (does 4 equal 5? no? Then I don't need to give you a dollar.)

VB makes these same basic decisions. The syntax of an if statement is this:

if 4=5 then
msgbox("The values are equal")
end if

Now, those lines mean that if 4 is equal to 5, do the line below... but what if 4 is not equal to 5? If we ran this, nothing would happen, because 4 does not equal 5. Should there be some other action if the statement evaluated to false? In real world words, we could say "If it is my birthday, sing happy birthday". Lets add to that idea - lets add an action if the expression is false (we say it evaluates to false).

So, lets add to our birthday example:

"If it is my birthday, sing happy birthday, or else look sad because I'm not being sung to."

We have added directions to handle the situation where the statement is not true. In VB, we do this as well...

if 4=5 then
msgbox("The values are equal")
msgbox("Hooray!")
else
msgbox("The values are NOT equal")
end if

When this expression was run, it would show "The values are NOT equal" because the else clause of the statement triggered. Basically, we provide 2 options - if the testing expression evaluates to true, then do the first statement; if the expression was false, and if we provided an else clause, then do the second. The else clause is optional - if we don't provide one, the expression will do something if it is true and nothing if it is false.

Nested If statements

Lets make it a little harder. First, we can have more if statements within an if statement; this is called nesting a statement. For example (i'm going to simulate line numbers here for discussion, but remember that they are NOT a part of VB!):

Code (Simulated Line Numbers) Code (Indentation)
  1. if 4=5 then
  2. msgbox("The values are equal")
  3. msgbox("good job")
  4. else
  5. msgbox("The values are NOT equal")
  6. msgbox("Sorry")
  7. if 4<5 then
  8. msgbox("it was less")
  9. else
  10. msgbox("it was more")
  11. end if
  12. end if
  • if 4=5 then
    • msgbox("The values are equal")
    • msgbox("good job")
  • else
    • msgbox("The values are NOT equal")
    • msgbox("Sorry")
    • if 4<5 then
      • msgbox("it was less")
    • else
      • msgbox("it was more")
    • end if
  • end if

 

In this example:

Compound If Statements

Another way to make an if statement is a compound if statement. In this form, we test 2 or more expressions. Now, we need to remember that each indivudual expresstion will be simplified to true or false; next, we use the keywords AND, OR, or XOR to simplify multiple true/false values into one true or false.

AND

And will simplify to true if both operands (the expressions from the previous step) simpliify to true. Example:

Dim x = 1
Dim y = 2
Dim z = 2
If x=1 And y = 2 Then
MsgBox("It was true")
Else
MsgBox("it was false")
End If

From this example, the words "It was true" will be shown. If x was 2, "It was False" would be shown.

OR

OR will simplify to true if either or both operands (the expressions from the previous step) simpliify to true. Example:

Dim x = 1
Dim y = 2
Dim z = 2
If x=5 or y = 2 Then
MsgBox("It was true")
Else
MsgBox("it was false")
End If

From this example, the words "It was true" will be shown (the first expression was false, but the second was true.).

XOR will simplify to true if one (and only one) of the operands operands (the expressions from the previous step) simpliify to true. Example:

Dim x = 1
Dim y = 2
Dim z = 2
If x=1 xor y = 2 Then
MsgBox("It was true")
Else
MsgBox("it was false")
End If

From this example, the words "It was false" will be shown. They both are true; this would have been true for and or or, but is false for xor.

From these keywords, we can talk about a "truth table". It looks like this:
First Expression Operator Second Expression Simplifies To
True and True True
True and False False
False and True False
False and False False
True or True True
True or False True
False or True True
False or False False
True xor True False
True xor False True
False xor True True
False xor False False

Order of Operations with And and Or

One thing that can be confusing is knowing how your statement will be interpreted when you have a combination of And and Or Operators. With testing, we discover that the and operator has precedence over the or operator. Consider the following example:

Dim x = 1
Dim y = 2
Dim z = 2
If x = x Or x = y And x = z Then
MsgBox("It was true")
Else
MsgBox("it was false")
End If

Now, we have 2 comparisons working here - true or true, and false and false. We should know that we only do one operation or comparison at a time during the simplification process. So, does it happen left to right, or doing ands before ors? Let's try to predict the output both ways.

If the simplification were left to right, we would pass through the statement in steps, solving one expression at a time, as shown:


However, if we were told that ands had priority in the order of operations, we might solve like this instead:

So which is it? This demonstrated the logic behind 2 different possible order of operations. If you were to run the code, you'd see true appear. And has priority over or. However, if we wanted to force the or to happen first, we could use parentheses, as such:

If (x = x Or x = y) And x = z Then

In which case the or would be done first.

Equality Operators

There are actually 6 different equality operators we can use. They are:

Operator: Spoken as: Example: Example would be:
= equal to 4=5 false
<= less than or equal to 4<=5 true
>= greater than or equal to 4>=5

false

<> not equal to 4<>5 true
< less than 4<5 true
> greater than 4>5 false

 

Whew! Next, we'll look at another way to test for one of many possible values.