Order of operations (operator precedence and associativity)
Rules that determine the sequence in which parts of an expression are evaluated, including precedence, associativity, parentheses, common mnemonics, and differences in programming languages.
Overview
Order of operations is the set of conventions that determine how to evaluate expressions containing more than one operator. In everyday arithmetic it decides, for example, whether to perform multiplication before addition. More generally in mathematics, logic and computer science it is called operator precedence or operator rank; when two operators share the same precedence, associativity rules (left-to-right or right-to-left) decide how to group them. These conventions let many expressions be written without excessive parentheses while still having an unambiguous meaning.
Precedence and associativity
Precedence assigns a relative priority to each operator. Operators with higher precedence are applied before operators with lower precedence. Precedence is typically not a strict total order: several operators can share the same level. Associativity resolves how to evaluate a sequence of operators that all have the same precedence. For example, addition and subtraction usually share precedence and are left-associative, so a - b + c is grouped as (a - b) + c. Exponentiation in many conventions is right-associative, so a ^ b ^ c is interpreted as a ^ (b ^ c).
Parentheses and bracketing
Parentheses (or other brackets) override the default rules: any expression inside a matching pair of brackets is evaluated first and treated as a single operand. This both clarifies intent and changes the result when necessary. For instance, without brackets, multiplication has higher precedence than addition, so 2 + 3 * 4 is evaluated as 2 + (3 * 4) = 14; with brackets (2 + 3) * 4 evaluates to 20. Brackets must enclose complete subexpressions including the operators and operands they involve.
Common rules, examples and mnemonics
Elementary arithmetic precedence is commonly remembered by mnemonics such as PEMDAS, BODMAS or BIDMAS, which expand to parentheses/brackets, exponents/orders, multiplication and division, addition and subtraction. A few typical facts follow:
- Parentheses first.
- Exponents or powers next (when present).
- Multiplication and division have equal precedence and are evaluated according to associativity (usually left-to-right).
- Addition and subtraction have equal precedence and are typically evaluated left-to-right.
Examples: 8 ÷ 4 × 2 is usually (8 ÷ 4) × 2 = 4, not 8 ÷ (4 × 2). For exponent chains, 2^3^2 is normally 2^(3^2) = 512 in right-associative conventions.
Programming languages and parsing
In programming, languages define precise precedence and associativity for their operators; the rules can differ from mathematical notation or from one language to another. Compilers and interpreters rely on these rules when building parse trees. Well-known parsing techniques such as the shunting-yard algorithm convert infix expressions into a form that enforces precedence and associativity. Some languages also define evaluation order for side effects (for example which operand of an operator is evaluated first), which is a separate concern from operator precedence.
Non-commutative operators, unary operators and special cases
When operators are non-commutative (e.g., subtraction, division, or matrix multiplication), associativity matters to avoid ambiguity. Unary operators (like negation) often have higher precedence than binary operators so that -3^2 can be parsed as -(3^2) unless the language specifies otherwise. Different contexts may assign unusual precedence to symbols: for example, logical connectives or string concatenation in some languages have placement and associativity choices driven by practicality.
Historical notes and practical advice
Mnemonics such as PEMDAS are pedagogical aids rather than formal laws; conventions have varied historically and across countries. In technical writing and programming it's generally good practice to use parentheses to make intent explicit, even when precedence rules would give the same result, because explicit grouping reduces errors and improves readability. When reading or writing complex formulas, consult the language or notation reference for the exact precedence table.
Quick reference checklist
- Apply parentheses first.
- Apply exponentiation (if present) next.
- Perform multiplication and division (same level) according to associativity.
- Perform addition and subtraction (same level) according to associativity.
- When in doubt, add parentheses to show grouping explicitly.
Ranking of different operators
For the standard arithmetic operations of mathematics, the following order of precedence (in descending order of priority) is common:
- Exponentiation
- Multiplication and division ("point calculation")
- Addition and subtraction ("stroke calculation")
In programming languages and computer programs for formula evaluation (e.g., the Unix utility bc), there are additional categories. One of them is the sign, which usually enjoys an even higher priority over exponentiation. Thus, while in mathematical formulas the expression
read as , in the expressions of such evaluation programs it is often read as
.
In logic, it is not always common to define an operator precedence. Where this is done, the following is usually chosen (in descending priority):
- Negation
- Conjunction
- Disjunction
- Conditional
- Biconditional
Also in many programming languages this order of precedence is chosen for Boolean operators.
For example, after applying the above operator rank sequences, the arithmetic expression evaluated as
, the logical expression
as
.
Sequence of equivalent operators
In addition, an associativity can be specified for operations, which determines the order in which adjacent, equivalent operators are to be evaluated. An operator is called left-associative if A op B op C op D is evaluated as ((A op B) op C) op D; an operator is called right-associative if A op B op C op D is evaluated as A op (B op (C op D)). Of the above arithmetic operators, exponentiation is defined to be right-associative, that is:
.
Likewise the arrow operator:
The remaining two-digit operators are defined as left-associative, i.e., for example, .
In logic, junctors are usually defined in a left-associative way, but there are also authors who use at least the conditional in a right-associative way.
There are also programming languages, such as Occam, that set all operators to the same rank and evaluate them from left to right.
Questions and answers
Q: What is the order of operations?
A: The order of operations is a set of rules used to evaluate and simplify expressions and equations in math and algebra.
Q: Why is the order of operations important?
A: The order of operations is important because it determines the correct order in which different mathematical operations should be done when solving a problem with more than one operation. Not following the correct order can result in an incorrect answer.
Q: What are the standard mathematical operations?
A: The standard mathematical operations are addition (+), subtraction (-), multiplication (* or ×), division (/), and exponentiation (^n or n).
Q: What are brackets?
A: Brackets are grouping symbols used to indicate the order of operations, which include () or parentheses, [] or square brackets, and {} or curly braces.
Q: What is exponentiation?
A: Exponentiation is the mathematical operation of raising a base number to a certain power, commonly represented as ^n or n (also called orders or indices).
Q: Who has agreed on the correct order to use operations?
A: Mathematicians have agreed on the correct order to use operations.
Q: What happens if you do not follow the correct order of operations when solving a problem with more than one operation?
A: If you do not follow the correct order of operations when solving a problem with more than one operation, the answer will be wrong.
Related articles
Author
AlegsaOnline.com Order of operations (operator precedence and associativity) Leandro Alegsa
URL: https://en.alegsaonline.com/art/73010