Evaluating Expressions

Return to Module 0 page

There are two sections to this topic. The first concerns the order in which we are to do mathematical operations in an expression. An example of this is 24 − 8 * 3. Reading this left to right we might think that we should do 24 − 8 to get the value 16 and then multiply that value by 3 to get 48. Although reasonable, that interpretation is wrong. In the Order of Operations section below we will set out the rules that we need to follow in order to correctly evaluate expressions.

The second section concerns using variables, that is, names for values where we need to substitute a number for the variable and then evaluate the expression. For example, we might be given the expression 3x² − 4x + 11 and then be asked to evaluate that expression when x = 2.5. This can get a bit more complicated as we introduce multicharacter names for variables, names such as IQR or Q1 or xbar. One example from statistics is the expression Q1 − 1.5*IQR. The Using Variables section below explains and demonstrates doing this.

Order of Operation

The most basic operations that we use in arithmetic are addition, subtraction, multiplication, and division. These are familiar to us. With a few special cases, we even have agreed upon symbols that we use to denote each operation: we use + for addition, for subtraction, * for multiplication, and / for division. For the rest of this discussion we will use * for multiplication and / for division. We will not use implied multiplication.

The expression 3 + 4 * 8 shows our immediate problem. If we do the evaluation left to right, we would add 3 to 4 and get 7 as a result, then we would multiply that 7 times the 8 and get a final answer of 56. However, if we do the problem right to left, we would multiply 8 times 4 and get 32 as a result, then we would add that 32 to the 3 and get a final answer of 35. We cannot have two different correct answers for the same problem. Rather than initially choosing left to right or right to left, we create an agreed upon order of operations. Our first statement of this is:
Order of Operations
Version 1
PrecedenceOperationsDirection
2* and /left to right
1+ and −left to right
Operators with a higher precedence
are to be evaluated first.
This table tells us that * and /, multiplication and division, are performed before + and , addition and subtraction. Thus, to evaluate the expression 3 + 4 * 8 we multiply first, doing 4 time 8 to get 32 and then we add that 32 and the 3 to get 35. We would do exactly the same thing if the original expression had been 4 * 8 + 32.

Let us consider a longer expression, namely, 18 + 24 − 15 / 3 − 6 + 5 * 4. Here we have an expression that uses all four of our operators. The Order of Operations table tells us that we need to do the multilication and division first, and that we do this left to right. Therefore, our first task is to evaluate 15 / 3 to give us 5 so that the expression becomes 18 + 24 − 5 − 6 + 5 * 4. Next we do the multiplication, 5 * 4 to give us 20, making tthe expression 18 + 24 − 5 − 6 + 20. The remaining operations are + and . They have the same precedence so we do them left to right. 18 + 24 is 42 making our expression 42 − 5 − 6 + 20. Then, 42 − 5 is 37 making the expression 37 − 6 + 20. Then, 37 − 6 is 31 making the expression 31 + 20. Then, 31 + 20 is 51 which is our final answer.

Even with this simple first version of our Order of Operations we can see an immediate problem. We cannot write an expression that is equivalent to
  5 + 7 / 13 − 9
. It is clear, in the fraction, that we need to do 5 + 7 to get 12 and then do 13 − 9 to get 4 before we do the division, 12 / 4, to get the final answer, 3. We just cannot write an equivalent expression using just +, −, *, and /.

We solve this problem by introducing parentheses, (  ), at a higher level of precedence. Now our table becomes:
Order of Operations
Version 2
PrecedenceOperationsDirection
5(     )deepest to broadest
2* and /left to right
1+ and −left to right
Operators with a higher precedence are to be
evaluated first.
Using parentheses we can write
  5 + 7 / 13 − 9
as ( 5 + 7 ) / ( 13 − 9 ). The parentheses force us to do the addition and the subtraction before we do the division.

Up to this point, pretty much all of the mathematical world agrees with our Second Version Table. Let us add another operation, exponentiation, to our table. This operation is somewhat different from the others in two ways. First, we often do not even write an operator to signify exponentiation. We have no problem writing 32 to indicate raise 3 the second power. In many, but certainly not all, programming languages we use the ^ character to indicate exponentiation. Thus, if we want to indicate raise 3 the second power we would write 3 ^ 2. We will use this convention here.

A second "difference" with exponentiation is that it is most often assumed to be evaluated right to left. Thus, 3 ^ 2 ^ 4 is usually interpreted as 3 ^ (2 ^ 4). That would simplify to 3 ^ 16 or 43046721. That is far different from the interpretation, reading left to right, of 3 ^ 2 ^ 4 as ( 3 ^ 2 ) ^ 4, which simplifies to 9  4, or 6561. We will list the "right to left" direction in the table but be aware that some software and/or programming languages have adopted the opposite direction. Here are some examples:
on the Google calculator
using R
on a TI-84 Plus Silver Edition
Operating System 2.55
on a TI-84 Plus CE
Operating System 5.7.0.0017
in MATHPRINT mode
on a TI-84 Plus CE
Operating System 5.7.0.0017
in CLASSIC mode
the same calculator as the previous example
From Excel
on the Microsoft calculator
 
The top row of examples give one answer, while the bottom row of examples give a different answer. Note that the fourth and fifth examples were done on the same calculator having merely changed the MATHPRINT to the CLASSIC setting.

We place exponentiation in our Order of Operations with a higher precedence than multiplication and division but lower than parentheses.
Order of Operations
Version 3
PrecedenceOperationsDirection
5(     )deepest to broadest
3^right to left
2* and /left to right
1+ and −left to right
Operators with a higher precedence are to be
evaluated first.
Because there is no universal agreement on the direction of evaluation for exponentiation it is always best to use parentheses to force the order that you want to be used.

The last operation that we will add to our table is negation. We understand that 0 − 3 is a subtraction problem but the answer is − 3, where the raised negative sign really means take the negative of. That raised negative sign is our negation operator. It should not be confused with the operator for subtraction. Again there is no universal agreement on the precedence that should be assigned to negation. In some software and/or programming languages −  is understood to mean (− 3)², that is, in those systems, negation has a higher precedence than does exponentiation. On other systems −  is understood to mean − (3²), that is, in those systems, negation has a lower precedence than does exponentiation. Because the former understanding is more widely accepted we will add negation to our Order of Operations above exponentiation. However, given that there is some difference of views on this it is always better to use parentheses to force the order that you want to be used. Our final table becomes:
Order of Operations
Version 4
PrecedenceOperationsDirection
5(     )deepest to broadest
4 
3^right to left
2* and /left to right
1+ and −left to right
Operators with a higher precedence are to be
evaluated first.
You might note that there is no "direction" for . That is because is a unary operator. The operators +, −, *, /, and ^ are binary operators because, for example, we subtract one number from another. Therefore, we need a direction in order to correctly evaluate 15 − 7 − 5. However, with our unary operator the direction of evaluation makes no difference for something like − − 3.

Variables

There are many instances where we want to write an expression in more general terms. For example, to convert temperature in Fahrenheit degrees to Celsius degrees we subtract 32 from the Fahrenheit measure and then multiply the result by
5 / 9
. The expression that captures that process is
5 / 9
 * ( F − 32 )
. [Note: this is often written as
5 / 9
( F − 32 ) using implied multiplication but, as noted above, we will use explicit multiplication on this page.] In that expression the F is just a general placeholder for any Fahrenheit measure. We call such a placeholder a variable. Once we have the the expression then we can evaluate it for any given Fahrenheit measure. For example, we might ask for the Celsius equivalent of 95° Fahrenheit. We substitute the 95 in place of the F in our expression to get
5 / 9
 * ( 95 − 32 )
. Then, working inside the parenthesis first this becomes
5 / 9
 * ( 63 )
, which then simplifies to 35 which is our equivalent Celsius measurement.

To convert a Celsius temperature to a Fahrenheit temperature we multiply the Celsius measurement by
9 / 5
and then add 32. The expression that captures this process is
9 / 5
*C + 32
, where the variable C can be replaced by any Celsius temperature. Thus, to find the equivalent Fahrenheit value for 20° Celsius we just replace the variable C with 20 and then evaluate the expression. We have
9 / 5
*20 + 32
, which simplifies to 36 + 32, giving us 68 degrees Fahrenheit.

In the virtual reality game Beat Saber a player is presented with a given number of cubes depending on the background song selected and the difficulty level that the player is using. The player has to cut each cube with the VR light saber. A player gets points for how well they cut each cube. The goal is to get the maximum number of points. If the player has a perfect swing each time then the player gets If a game has n cubes, then what is the maximum score that a player can get? We can make an expression for this:
115*2 + 230*4 + 460*8 +920*(n − 14) some of which we can simplify to 4830 + 920*(n − 14) Knowing that expression, a player selecting a game that has 193 cubes can compute that maximum possible score to be 4830 + 920*(193 − 14) which is 4830 + 920*(179) which becomes 4830 + 164680 or 169510.

So far we have seen single letter variables. This is not a requirement. In statistics we talk about the first, second, and third quartile of a set of data. It is common to use variables to represent these three values. We might well use the variables Q1, Q2, and Q3. Furthermore, in statistics, we define the inter-quartile range as the third quartile minus the first quartile. We can use the variable IQR to represent the inter-quartile range. Then, we can express that relation by: IQR = Q3 - Q1. If someone tells us that Q1 is 83 and Q3 is 147 then we can find the IQR by evaluating 147 − 83 which is 64. We could be asked to evaluate Q1 − 1.5 * IQR. For the values we have been given, that becomes 83 − 1.5 * 64, which is 83 − 96, which is − 13. Or, for those same values, we could be asked to evaluate Q3 + 1.5 * IQR. That becomes 147 + 1.5 * 64, or 147 + 96, which is 243.

Indexed Variables

There are times when we have many related values that we want to name by a variable. Rather than having a different name for each value, we choose one name for all of them but we add an index to the name to distinguish values. We write that index as a subscript. Thus, to hold the values 89, 94, 96, 83, 99, 98, and 192 we might use the variable x along with subscripts so that x1 represents our 89, x2 represents our 94, x3 represents our 96, x4 represents our 83, x5 represents our 99, x6 represents our 98, and x7 represents our 192. To add all of those values together we could write x1 + x2 + x3 + x4 + x5 + x6 + x7. Getting such a sum happens so often in mathematics that we have a special symbol to capture that idea. We write to mean "add all of the xi's for i taking on the values 1 through 7. The Σ symbol is the upper case Greek letter "sigma".

Return to Module 0 page
©Roger M. Palay     Saline, MI 48176     December, 2022