Conditional Statements Tutorial

Lesson 1
Tutorials - Page - Page 2 - Page 3 - Page 4 - Page 5 - Page 6 - Page 7 - Page 8 - Page 9 - Page 10

Using "And" and "Or" In Boolean Expressions
We can use "And" and "Or" operators to make more
complicated boolean expression.

In the last example we used the boolean
expression Password = "let me in".

This boolean expression is "True" if
the Password is "let me in", and "False"
If the Password is not.

How can we make a boolean expression
that will be "True" if the Password is
"let me in" or "sam sent me", and if the
Password is other than the two above, the
boolean expression will be "False"?

To create this boolean expression we will use the "Or" operator:

(Password = "let me in") Or (Password = "sam sent me")

Put the following code in your Form_Load event:

Dim Password As String
Password = InputBox("Please enter the password")
If (Password = "let me in") Or (Password = "sam sent me") Then
   MsgBox "The Password is correct!"
Else
   MsgBox "Incorrect Password!"
   End
End If



Run the program.
If you'll type "let me in" or if you'll type "sam sent me"
the password will be correct.
If you'll type any other text the program will shut down.


The "Or" is operator, the same as "+" is operator.
How the "+" operator works we already know:
5 + 6 = 11
But how the "Or" Operator works?

The "+" operator is being executed on numbers:
Number + Other Number = The sum of both numbers.

The "Or" operator is being executed on "True" or "False":
False Or True = True
True Or False = True
True Or True = True
False Or False = False

The 4 examples above are the only options
of using the "Or" operator.

Lets see what effect has the "Or" operator on
the boolean expression we used:

(Password = "let me in") Or (Password = "sam sent me")

First, the left and right boolean expressions
are being evaluated (Figure 2).

For example, if the Password is "let me in" then
(Password = "let me in") is True
and (Password = "sam sent me") is False.

Then the boolean expression is look like this:
(True) Or (False)

As we saw in the 4 examples above, True Or False = True
So the final result of the expression is True (Figure 2).

Figure 2

(Password = "let me in") Or (Password = "sam sent me")

True

Or

False

True

This boolean expression will be True if
the password is "let me in" because True Or False = True,
the expression will be True if the password is "sam sent me"
because False Or True = True,
the expression will be False if the password is NOT "let me in"
and NOT "sam sent me" because False Or False = False.

Back  Forward