Logical operators are used to perform boolean operations on values and variables. These operators are used in conditional structures and loops to combine multiple conditions and obtain a final result.
The most common logical operators are AND
, OR
, and NOT
:
Operator | Example |
---|---|
AND | true AND true = true |
OR | true OR false = true |
NOT | NOT true = false |
AND and OR Operators
The AND
and OR
operations are basic logical operations, whose results are given by the following table.
A | B | A AND B | A OR B |
---|---|---|---|
False | False | False | False |
False | True | False | True |
True | False | False | True |
True | True | True | True |
AND
returnstrue
if both operands aretrue
, andfalse
otherwise.OR
returnstrue
if at least one of the operands istrue
.
If you think about it a bit, it’s the sense we usually use for the word AND
and OR
, respectively, in everyday usage.
For example, when you say
I take the coat if it rains AND it’s cold
You take the coat only if both conditions are met at the same time.
Whereas when you say,
I take the coat if it rains OR it’s cold
You take the coat if at least one of the two conditions is met.
NOT Operators
On the other hand, the NOT
operator is very simple and just inverts a logical value.
A | NOT A |
---|---|
False | True |
True | False |
The tables we have seen are associated with Boolean algebra, and are usually called truth tables.
Example in Different Languages
In languages with syntax derived from C, the operators are &&
, ||
, and !
.
Operator | Example |
---|---|
AND | true && true = true |
OR | true || false = true |
NOT | !true = false |
For example, in C, C++, C#, and Java, the logical operators are identical and have the following form.
bool or_a_b = a || b;
bool and_a_b = a && b;
bool not_a_b = !a;
Which is identical to the case of JavaScript, with the only difference being that the variable is declared with the keyword let
.
let or_a_b = a || b;
let and_a_b = a && b;
let not_a_b = !a;
Other languages, such as Python, SQL, or VB, use the words AND
, OR
, and NOT
as operators. For example, the previous examples would look like this in Python.
or_a_b = a or b
and_a_b = a and b
not_a_b = not a
Short-Circuited Operators
In many languages, both AND
and OR
offer short-circuited versions. These operators are a mechanism to speed up the evaluation of expressions.
A short-circuited operator stops the evaluation as soon as it knows the result without needing to evaluate the rest of the operators.
This is done using the “trick” that an operation with multiple AND
s will be false
as long as one of the members is false
. Similarly, a series of conditions with OR
will be true
as long as one of its members is true
.
We can use this to speed up the calculation of expressions, which is precisely what the short-circuited versions do.
AND
stops the evaluation as soon as it finds afalse
, and evaluates the expression tofalse
.OR
stops the evaluation as soon as it finds atrue
, and evaluates the expression totrue
.
That is,
Operator | Example |
---|---|
AND (&&) | false && (this will not be evaluated) |
OR (||) | true || (this will not be evaluated) |
For example, in this expression from C#,
// if (a > b) is true
// (c < d) will never be evaluated
bool result = (a > b) && (c < d);
In general, we will almost always use the short-circuited versions of the operators because they are more efficient.