Language: EN

programacion-precedencia-operadores

Operator Precedence

Operator precedence in programming refers to the rules that establish the order in which operations should be executed within an expression.

In other words, when we have an expression that has multiple operators, not all of them have the same priority. If we find an expression like:

let a = 2 + 3 * 5

Is the =, +, or * calculated first?

In general, operator precedence in programming is closely related to mathematical calculation rules. In fact, in many cases the same rules are used, such as multiplication and division operations being performed before addition and subtraction.

However, in programming, there are additional operators that are continually used. Therefore, the precedence rules are more extensive and complex.

Example of operator precedence

Let’s see an example to better understand operator precedence in programming. Suppose we want to calculate the value of the following expression:

2 + 3 * 4   
14              // the previous expression equals 14

If we follow the rules of mathematical calculations, multiplication should be done first and then addition. Therefore, the result will be 14.

2 + 3 * 4
2 + (3 * 4)     // the above is the same as
2 + 12          // we first solve 3 * 4
14              // finally we add, resulting in 14

If we wanted to force the addition to be done first, we could use parentheses to indicate which operations should be done first. For example:

(2 + 3) * 4 

In this case, addition is done first due to the parentheses, so the result would be 20.

(2 + 3) * 4
5 * 4            // the parenthesis (2+3) is calculated first
20               // finally we multiply, resulting in 20

Table of common operator precedence

Next, a table is presented with the precedence of common operators in programming, from highest to lowest precedence:

  • Parentheses: Operators within parentheses are evaluated first. Parentheses are used to alter the precedence and force the desired order of evaluation.
  • Exponentiation: If it exists, the exponentiation operators are evaluated after parentheses. This operator is used to raise a number to a power.
  • Multiplication, division, and modulus: These operators have a higher precedence than addition and subtraction. They are evaluated from left to right, unless parentheses are used to alter the order.
  • Addition and subtraction: These operators are evaluated after multiplication, division, and modulus. Like the previous operators, they are evaluated from left to right unless parentheses are used.
  • Comparison operators: Comparison operators (such as less than <, greater than >) have a lower precedence than arithmetic operators.
  • Equality operators: Equality operators (equal to == and not equal to !=) have lower precedence than comparison operators.
  • Logical AND operator: Next are the logical operators. Generally, the logical AND operator has higher precedence.
  • Logical OR operator: Finally, we end our generic list with the logical OR operator, which has lower precedence than all the previous ones.

In the case of multiple operators with the same precedence, in the event of a tie, they are executed from left to right. For example:

3 * 4 / 7 % 9

((3 * 4) / 7) % 9     // is equivalent to

For example, in the SQL language, the expression:

A OR B AND C OR D

Is equivalent to this expression:

(A OR B) AND (C OR D)

This order of precedence is more or less common in most languages, and is designed to save us from putting parentheses in most cases.

However, the precedence of operators can vary depending on the programming language we use. Therefore, it is always important to review the language documentation to understand how operators are handled in that particular case.

Best Practices and Cleanup Tips Tips

Although operator precedence can be useful for writing more concise expressions, it can also be confusing in some cases.

For example, suppose we have this expression.

result = a * b + c / a - b

It is relatively simple, but imagine that in a real case it could be a longer and more complex statement.

Even if it is not necessary, if you feel that it is necessary to improve readability, do not hesitate to use extra parentheses to clarify and control the order of evaluation.

result = (a * b) + (c / a) - b

Even, if necessary, we can divide the complex expression into multiple lines using intermediate variables to store partial results.

factor1 = a * b
factor2 = c / a
result = factor1 + factor2 - b