LOGICAL OPERATORS So far, we have used only simple conditions with the If/Then/Else statements. Visual Basic lets you test more than one condition. To include two or more conditions, the extra conditions have to be linked by logical operators.
There are three logical operators: 1. AND 2. OR 3. NOT
LOGICAL OPERATORS 1. AND OPERATOR This operator can be illustrated by a simple example. In an office, a person retires on reaching the age of 55. He qualifies for a gold medal if his years of service with the company exceed 12. AND operator Condition 1
Condition 2
If age = 55 and service >= 12 then result.text = “you retire with a gold medal� Result if both conditions are true
LOGICAL OPERATORS  In other words, both conditions joined by AND have to be true in order for the statement following THEN to be carried out. If Condition1 and Condition2 then Statement Condition 1 Condition 2 Statement True
True
Carried out
True
False
Not Carried out
False
True
Not Carried out
False
False
Not Carried out
LOGICAL OPERATORS 2. OR OPERATOR Another logical operator is OR. To return to our office example, if the condition for obtaining a gold medal is reaching the age of 55 OR having served the company for more than 12 years, then an employee fulfilling any one of the conditions will qualify for a gold medal. In other words, if one condition is true, the Then part will be carried out.
LOGICAL OPERATORS If Condition1 or Condition2 then Statement Condition 1 Condition 2 Statement True True Carried out True False Carried out False
True
Carried out
False
False
Not Carried out
LOGICAL OPERATORS 3. NOT OPERATOR The NOT operator is placed immediately in front of the condition it is to work with. This operator means exactly what it says, that the condition is not fulfilled. If Not age < 18 Then statement: means the age is to be more than 18.