Logical Operators

There are 4 main logical operators which may be used in algorithms (programs)...

  • NOT
  • AND
  • OR
  • XOR

Examples :

If NOT (x = 100) then output(message)
means that the message will be output if the value of x is not equal to 100.

If (x  > 19) AND (x < 50) then output(message)
means that the message will be output if both conditions (x > 19)
and (x < 50) are true...ie if x lies in the range 20..49 (inclusive).

Truth Table for AND :

AND

True False
True True False
False False False

If (x < 20) OR (y < 30) then output(message)
means that the message will be output if one
or other (or both) of the conditions (x < 20) or (y < 30) are true.

Truth Table for OR :

OR

True False
True True True
False True False

 

If (x < 20) XOR (y < 30) then output(message)
would output the message if one
or other (but not both) of the conditions (x < 20) or (y < 30) are true.

Truth Table for XOR :

XOR

True False
True False True
False True False

 

 

 

These logical operators are useful in SQL (Structured Query Language) statements which are used for searching databases.

Example

Suppose you wanted to find all the people called Smith who live in Cardiff but do not have either a dog or a cat...your query may include something like...

(name='Smith') and (town='Cardiff') and not((pet='dog') or (pet='cat'))