Arithmetic Operations on a Computer

Robert P. Webber, Scott McElfresh, Longwood University

Order of Operations

 
A computer can add, subtract, multiply, and divide numbers.  The symbols for these operations are  +,  , *, and  /  respectively.  Some computer languages, such as spreadsheets, also allow exponentiation.  Its symbol is  ^ . 

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.  When operations have the same precedence, such as addition and subtraction, they are done from left to right.  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 .

Parentheses are used to change the order of operations.

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, although it is not incorrect to include them.    


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 inline expression equivalent would be:      x + y * z + w^2
example Parentheses are required around the numerator.  Otherwise, only  y  would be divided by  3 !  The inline form is  (x + y) /3 .
example 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.
example 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 expressionbuilt-up algebraic expression
x - y / z + wSince division has precedence over subtraction and addition, only  y  is divided by  z
: example
(x - y) / z + wx - y is divided by  z .  w  is not part of the fraction  
example   
(x - y) / (z + w)w  is part of the denominator in this expression:
example

Exercises


Exercises for this are available in a pdf document.


Credits and licensing

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

Version 2015-Aug-24 12:30