Arithmetic Operations on a Computer

Robert P. Webber, Scott McElfresh, Don Blaheta, Longwood University

Order of Operations

Why are we working on this? Specifying numeric computations as algorithms requires that we agree on how to specify the steps and their order. Skills in this section: Order of operations — Evaluating arithmetic computations — Translating between notations for arithmetic computation Concepts: Algorithm representation

A computer can add, subtract, multiply, and divide numbers. The symbols for these operations are +, −, *, and / respectively. Some computer languages, such as those typically used in spreadsheets, also allow exponentiation. Its symbol is ^ . A mathematical expression can contain more than one operation, representing a series of computations; we evaluate an expression (that is, compute its result) by performing one operation at a time, with the result of each operation used in the next. In these cases it's important that we agree what order the operations are performed in, or else a single expression might yield different results.

Most computer languages use the same precedence of operations as mathematics. Exponentiation is done first in a string of operations, with multiplication and division next, and addition and subtraction last. This is also called the hierarchy of operations.

Highest ^
Next highest *, /
Lowest +, −

For example, consider the algebraic expression:

x + yz + w2

Most people would recognize that this means to square w, multiply y and z, and add x, yz, and w2 together. If x = 1, y = 2, z = 3, and w = 4, for instance, the expression evaluates to

1 + 2*3 + 42 = 1 + 6 + 16 = 23.

When operations have the same precedence, such as an addition and subtraction, or multiple subtractions, they are done from left to right. The left-to-right order is largely academic in some cases, such as multiple additions, because (since addition is commutative and associative) you get the same answer either way. But in some cases, the order makes a difference in the final result; so we understand that the order of operation within a level of the hierarchy is always left-to-right.

Example: Evaluate 5 + 3 − 4, 5 − 3 + 4, and 5 − 3 − 4.

Solution: In all three cases, we proceed left-to-right. In the first expression, we first perform the addition, so the result is 8 − 4 = 4.

In the second, we perform the subtraction first, giving us 2 + 4 = 6.

In the third, we perform the left subtraction (of 5 − 3) first, giving 2 − 4 = −2. Note that if we had done the other subtraction first, the final result would have been 6. Remember, unless the hierarchy says to do one of the operations first, always proceed left to right.

To specify a different order of operations, we use parentheses.

Example: Evaluate 10 − 6 / 2, (10 − 6) / 2, and 10 − (6 / 2).

Solution: In the first expression, do the division first, because it has higher precedence than subtraction. The result is 10 − 3 = 7 .

In the second expression, the parentheses force the subtraction to be done first: 4 / 2 = 2.

The third expression evaluates to 10 − 3 = 7 , the same as the first expression. The parentheses are not needed, but it is not incorrect to include them.

You may have learned the simple acronym PEMDAS, or a longer mnemonic sentence, to remember the order of operations. If so, you might want to mentally break that up as P-E-MD-AS; otherwise you're liable to try to always (M)ultiply before (D)ividing, when those two operations are at the same level and should be processed left-to-right. This is a very common source of error in these kinds of problems.

In-line Expressions

On a computer, there are limitations to the manner in which expressions can be entered. We must create in-line expressions that are just a sequence of characters (keystrokes at the keyboard). Some rules:
Examples:
Built-up algebraic expression Creating the in-line expression equivalent
x + yz + w2 The in-line expression equivalent would be: x + y * z + w^2
x + y
3
The inline form is (x + y) / 3. Parentheses are required around the numerator. Otherwise, only y would be divided by 3!
4
3
xy2
Exponentiation has the highest precedence, and division and multiplication have the same precedence. For emphasis, we could place parentheses around y2 and 4/3 giving (4/3) * x * (y^2): but they are not needed. The expression 4/3 * x * y^2 is simpler and is equivalent.
x + 3
2y − 5
Parentheses are needed around the numerator and around the denominator. Do not forget the multiplication symbol between 2 and y. The result is (x+3)/ (2*y−5)

In-line expression Built-up algebraic expression
xy / z + w Since division has precedence over subtraction and addition, only y is divided by z: x
y
z
+ w.
(xy) / z + wx y is divided by z. w is not part of the fraction:
x − y
z
+ w.
(xy) / (z + w) w is part of the denominator in this expression:
x − y
z + w
.

Exercises

In the first group of exercises, evaluate (compute the value of) the expression written in in-line notation.
  1. 1 + 2 * 3
  2. (1 + 2) * 3
  3. (4 + 3) ^ 2
  4. 4 + 3 ^ 2
  5. 5 * 6 ^ 2
  6. (5 * 6) ^ 2
  7. 1 + 2 * 4 − 10 / 2
  8. (1 + 2) * 4 − 10 / 2
  9. (1 + 2) * (4 − 10) / 2
  10. 1 + (2 * 4) + (10 / 2)
In the second group of exercises, convert the expression from built-up algebraic notation to in-line notation.
  1. a2 − 2ab + b2
  2. a
    a + 1
  3. P(1 + r)t
  4. P(1 +
    R
    n
    )nT
  5. a + b
    a − b
  6. a + b
    a
    + b
  7. a +
    b
    a − b
  8. a +
    a
    b
    b
In the third group of exercises, convert the expression from in-line notation to built-up algebraic notation.
  1. b ^ 2 − 4 * a * c
  2. p * (1 + r) ^ tp
  3. (b − a) / a * 100
  4. a / 2 − b / 3
  5. x + y / z + w
  6. (x + y) / z + w
  7. x + y / (z + w)
  8. (x + y) / (z + w)

Credits and licensing

This article is by Robert P. Webber, Scott McElfresh, and Don Blaheta, licensed under a Creative Commons BY-SA 3.0 license.

Version 2016-Jan-29 03:15