Operators precedence
Operators are simple when used one at a time:
You can combine multiple operators in the same expression:
In this case you need to apply some rules to know which operation is executed first.
Take this example:
The result is 2.5, but why?
Some operations have higher precedence than the others. The precedence rules are:
*
/
%
(multiplication/division/remainder)+
-
(addition/subtraction)=
(assignment)
We have more than those, but itβs a good start to understand precedence rules.
Operations on the same level (like +
and -
) are executed in the order they are found, from left to right.
Following these rules, the operation
can be solved in this way:
And if you feel brave, you can omit the parentheses altogether, and the result will be the same:
Last updated
Was this helpful?