Order of Operations
Overview
The order of operations (or operator precedence) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.[1]
Discussion
Single values by themselves are important; however, we need a method of manipulating values (processing data). Scientists wanted an accurate machine for manipulating values. They wanted a machine to process numbers or calculate answers (that is, compute the answer). Prior to 1950, dictionaries listed the definition of computers as ” humans that do computations”. Thus, all of the terminology for describing data manipulation is math oriented. Additionally, the two fundamental data type families (the integer family and floating-point family) consist entirely of number values.
An Expression Example with Evaluation
Let’s look at an example: 2 + 3 * 4 + 5 is our expression but what does it equal?
- the symbols of + meaning addition and * meaning multiplication are our operators
- the values 2, 3, 4 and 5 are our operands
- precedence says that multiplication is higher than addition
- thus, we evaluate the 3 * 4 to get 12
- now we have: 2 + 12 + 5
- the associativity rules say that addition goes left to right, thus we evaluate the 2 +12 to get 14
- now we have: 14 + 5
- finally, we evaluate the 14 + 5 to get 19; which is the value of the expression
Parentheses would change the outcome. (2 + 3) * (4 + 5) evaluates to 45.
Parentheses would change the outcome. (2 + 3) * 4 + 5 evaluates to 25.
Operator Precedence Chart
Each computer language has some rules that define precedence and associativity. They often follow rules we may have already learned. Multiplication and division come before addition and subtraction is a rule we learned in grade school. This rule still works.
Order of Operations[2]
- Parentheses
- Exponents
- Multiplication / Division
- Addition / Subtraction
A common mnemonic to remember this rule is PEMDAS, or Please Excuse My Dear Aunt Sally. Precedence rules may vary from one programming language to another. You should refer to the reference sheet that summarizes the rules for the language that you are using. It is often called an Operator Precedence, Precedence of Operators, or Order of Operations chart. You should review this chart as needed when evaluating expressions.
A valid expression consists of operand(s) and operator(s) that are put together properly. Why the (s)? Some operators are:
- Unary – only have one operand
- Binary – have two operands, one on each side of the operator
- Trinary – have two operator symbols that separate three operands
Most operators are binary, that is they require two operands. Some precedence charts indicate of which operators are unary and trinary and thus all others are binary.
Key Terms
- associativity
- Determines the order in which the operators of the same precedence are allowed to manipulate the operands.
- evaluation
- The process of applying the operators to the operands and resulting in a single value.
- expression
- A valid sequence of operand(s) and operator(s) that reduces (or evaluates) to a single value.
- operand
- A value that receives the operator’s action.
- operator
- A language-specific syntactical token (usually a symbol) that causes an action to be taken on one or more operands.
- parentheses
- Change the order of evaluation in an expression. You do what’s in the parentheses first.
- precedence
- Determines the order in which the operators are allowed to manipulate the operands.