Language: EN

programacion-operadores-logicos

Logical operators

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:

OperatorExample
ANDtrue AND true = true
ORtrue OR false = true
NOTNOT true = false

AND and OR operators

The AND and OR operations are basic logical operations, whose result is given by the following table.

ABA AND BA OR B
FalseFalseFalseFalse
FalseTrueFalseTrue
TrueFalseFalseTrue
TrueTrueTrueTrue
  • AND returns true if both operands are true, and false otherwise
  • OR returns true if at least one of the operands is true

If you think about it for a moment, it’s the same sense we usually use for the word AND and OR, respectively, in everyday use.

For example, when you say

I’ll take the coat if it rains AND it’s cold

You take the coat only if both conditions are met at the same time.

While when you say,

I’ll take the coat if it rains OR it’s cold

You take the coat as long as one of the two conditions is met.

NOT operators

On the other hand, the NOT operator is very simple, and simply inverts a logical value.

ANOT A
FalseTrue
TrueFalse

The tables we have seen are associated with Boolean algebra, and are normally called truth tables

Example in different languages

In languages with syntax inherited from C the operators are &&, ||, and !.

OperatorExample
ANDtrue && true = true
ORtrue || 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 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 for example Python, SQL, or VB, the words AND, OR, and NOT are used 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-circuit operator stops the evaluation as soon as it knows the result without needing to evaluate the rest of the operators

To do this, the “trick” is used that an operation with several AND will be false, with one of the members being false. Similarly, a series of conditions with OR will be true with one of its members being 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 a false and evaluates the expression as false.
  • OR stops the evaluation as soon as it finds a true, and evaluates the expression as true

That is,

OperatorExample
AND (&&)false && (this will not be evaluated)
OR (||)true || (this will not be evaluated)

For example, in this expression in 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.