Conditional Statements Tutorial

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

Using The "Not" Operator
The "Not" operator has very simple function:

Not True = False
Not False = True


Before we continue,
I'll mention that you don't have to include
"Else" in the "If" statement.

For example:

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



If the password is correct, The code above
will display a message box with the text
"The Password is correct!".
Otherwise, the code will do nothing,
because there is no "Else".


Lets see an example of using the operator "Not".
Copy the following code to your Form_Load event:

Dim Password As String
Password = InputBox("Please enter the password")
If Not (Password = "let me in") Then
   MsgBox "Incorrect Password!"
   End
End If



The boolean expression: Not (Password = "let me in")
is True only if the Password is not "let me in". Why?
If the Password is not "let me in",
(Password = "let me in") is False (Figure 3) ,
and we get the boolean expression: Not (False).
Not False = True, so eventually, the
expression value is True (Figure 3).

Figure 3

Not (Password = "let me in")

Not

False

True

In conclusion,
The code above will display a message box
with the text "Incorrect Password!" ONLY if
the password is different from "let me in"

Back  Forward