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 with multiple operators, not all have the same priority. If we encounter an expression like:
let a = 2 + 3 * 5
Is the =
, the +
, or the *
calculated first?
In general, operator precedence in programming is closely related to mathematical calculation rules. In fact, in many cases, the same rules are employed, such as multiplication and division being performed before addition and subtraction.
However, in programming, there are some additional operators that are continuously used. Therefore, the precedence rules are somewhat broader and more 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 mathematical calculation rules, multiplication should be performed 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 occur first, we could use parentheses to indicate which operations should be performed first. For example:
(2 + 3) * 4
In this case, the addition is performed first due to the parentheses, so the result would be 20.
(2 + 3) * 4
5 * 4 // the parentheses (2+3) are calculated first
20 // finally, we multiply, resulting in 20
Table of Common Operator Precedence
Below is a table 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 precedence and force the desired order of evaluation.
- Exponentiation: If present, exponentiation operators are evaluated after parentheses. This operator is used to raise a number to a power.
- Multiplication, division, and modulus: These operators have 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 (like less than
<
, greater than>
) have 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, we have logical operators. Generally, the logical
AND
operator has higher precedence. - Logical OR operator: Finally, we conclude our generic list with the logical
OR
operator, which has lower precedence than all the previous ones.
In the case of several operators with the same preference, 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 language SQL 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 it is designed to save us from having to put parentheses in most cases.
However, operator precedence can vary depending on the programming language we use. Therefore, it is always important to check the language documentation to understand how operators are handled in that particular case.
Best Practices 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 needed to improve readability, don’t hesitate to add extra parentheses to clarify and control the order of evaluation.
result = (a * b) + (c / a) - b
If necessary, we can even split the complex expression into several lines using intermediate variables to store partial results.
factor1 = a * b
factor2 = c / a
result = factor1 + factor2 - b