Logical Operators

Also called Boolean operators.

Visual Basic, ASP, VBScript, VBA Java, JSP, C++ Description
AND & or && All must be true
OR | or || At least on must be true
NOT ! Reverses the truth

These are often used to string together a series of simple conditional expressions into a compound conditional expression:

Simple: x > y

Compound: (x > y) AND (x > z)

Evaluating compound conditionals is based on the truth tables:

AND TRUE FALSE
TRUE TRUE FALSE
FALSE FALSE FALSE

 

OR TRUE FALSE
TRUE TRUE TRUE
FALSE TRUE FALSE

Java and it's relatives use a process called short-cutting when evaluating compound conditional expressions. Take a look at this one:

(x > y) AND (x > z)

Since it is an AND, you know that all conditions must be true in order for the compound expression to evaluate to true. Imagine that, at some point in your program, x is equal to 3, y is equal to 5, and z is equal to 2. In the example above, using these values, the first condition (x > y) is false, and the second condition (x > z) is true. Therefore, the compound condition is false. However, you knew that already once you evaluated the first condition and found it to be false. Why bother evaluating the second condition.

The rules for short-cutting are:

AND: Once you find a condition that evaluates to false, return a false and stop evaluating. If all the conditions are true, then return a true.

OR: Once you find a condition that evaluates to true, return a true and stop evaluating. If all the conditions are false, then return a false.