Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

The Order of Operations

Not all operators are the same. Some are more important than others in the sense that they are given priority over another operator. Here we use the term precedence. The precedence of one operator over another means that one operator ranks higher than another: it has greater precedence. If you use more than one operator in a programming statement, you will need to understand the precedence of your operators so that you get the desired result from any computations you wish the computer to perform.

The operator with the highest rank/priority will be executed first by the computer. Then any other operators in the statement will be executed in the appropriate ranking order. If we write an addition or subtraction operator and a multiplication or division operator in the same expression, the multiplication/division operator will be given precedence: that means that it will be executed first before the addition/subtraction operator.

Let’s look at the following examples where each operator has a number beneath it to indicate the order in which its operation is performed.

Examples

3 + 2 * 5

In the preceding example, the multiplication is the ranking operation so 2 * 5 will be executed first—it is given precedence. Once performed (2 * 5 = 10), then the addition operator is activated (3 + 10), and the answer is 13. Here is another example:

Ranking operators        1st        2nd
                         ⇓         ⇓

                     4    *    6     –    3
                         ⇓
Becomes              24   –    3
                         ⇓
Which then becomes       21


In this example, 4 * 6 is computed first. Next, 3 is subtracted from 24 to give 21.

Ranking operators      1st       2nd
                       ⇓        ⇓
                   8    /    2    –   5
                       ⇓
Becomes            4    –    5
                       ⇓
Which then becomes –1


In this example, 8/2 is computed first. Next, 5 is subtracted from 4 to give –1.

PEMDAS (Please Excuse My Dear Aunt Sally)

Although the above phrase is perhaps a little outdated, it continues to serve its purpose well—namely to give instruction regarding the precedence of the operators. The acronym, PEMDAS, refers to the following: parentheses, exponents, multiplication, division, addition, and subtraction. These operators should be executed in the order they appear in the acronym. So parentheses are first, followed by exponents, then multiplication and division follow—with addition and subtraction last.

In the previous examples, the multiplication and division operators always precede the addition and subtraction operators. Division and multiplication have equal precedence, and they both rank higher than addition and subtraction. Likewise, addition and subtraction have equal precedence, and they both rank lower than multiplication and division.

Parentheses Still Rule

Parentheses are perhaps the most interesting operator because they tell the computer that what appears inside of them should be given precedence over all other operators—that is, they should be executed first.

Examples

                8 (5 + 2) - 7
                 2nd 1st 3rd
Becomes           8(7) - 7
                   56 - 7
                     49


Because the parentheses are present, (5 + 2) is executed first. Next, 8 is multiplied by 7 to give 56. Then 7 is subtracted from 56 to give 49.

                15 + 3 (2 – 5) * 6
                 4th 2nd 1st 3rd
Becomes           15 + 3 (–3) * 6
                   15 + –9 * 6
                     15 + –54
                       –39


Note

Remember that when the same operator appears more than once in a programming statement, each of those operators will be executed in order from left to right.


  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint