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 of them have the same priority. If we encounter an expression like:
let a = 2 + 3 * 5
Which is calculated first, the =, the +, or the *?
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 some additional operators that are used continuously. Therefore, the precedence rules are somewhat 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 mathematical calculation rules, the multiplication should be performed first, followed by the 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 performed first, we could use parentheses to indicate which operations should be done 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
Precedence of Common Operators
Below is a table with the precedence of common operators in programming, from highest to lowest precedence:
In case there are several operators with the same preference, in case 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 using parentheses in most cases.
However, operator precedence 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 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 when it is not necessary, if you think 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
Furthermore, if necessary, we can split the complex expression into multiple lines using intermediate variables to store partial results.
factor1 = a * b
factor2 = c / a
result = factor1 + factor2 - b
