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
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.
The "And" operator is similar to the "Or"
operator, except it has different Effects on boolean expressions:
True And True = True
True And False = False
False And True = False
False And False = False
When using the "And" operator, the
expression will be True only if both
boolean expressions are True.
For example, copy the following code
to your Form_Load event:
Dim UserName As String
Dim Password As String
UserName = InputBox("Please enter the user name")
Password = InputBox("Please enter the password")
If (Password = "let me in") And (UserName = "elvis") Then
MsgBox "The login is correct!"
Else
MsgBox "Incorrect Login!"
End
End If
This code will pop up two InputBoxes.
The first will ask you to enter the user name,
and the second will ask you to enter the password.
The login will be correct only if both user name
and password are correct.
The boolean expression (Password = "let me in") And (UserName = "elvis")
is True only if (Password = "let me in") is True, and
(UserName = "elvis") is True, because
True And True = True, and any other
combination is equal to False.
Wednesday, 10 August 2011
Subscribe to:
Post Comments (Atom)

Subscribe to email feed



