Math for Computer Science

7 | The order of operations

Practice Table of Contents

Introduction

In many situations, we must take multiple steps to solve a problem: if we want to make it to first base in a game of kickball, we must first kick the ball, then run to first base; if we want to bake a cake, we must first mix the ingredients, then place the cake in the oven; when we go shopping, we must first choose the items we want to buy, then pay for them.

In each of these cases, the order is important: it wouldn't work to run to first base before we kick the ball; we can't place our cake in the oven before we mix the ingredients; we can't purchase items we haven't added to our cart.

When multiple steps are involved in mathematics, the order is important. For example, multiplying before we add will give us a different answer than adding before we multiply. For this reason, we use a specific order, the order of operations, to solve multi-step math problems.


What is the order of operations?

Considering addition, subtraction, multiplication and division, operations are evaluated in the following order:

  1. Evaluate operations within parentheses
  2. Evaluate multiplication and division from left to right
  3. Evaluate addition and subtraction from left to right

How is the order of operations used in computer science?

Many computer programming languages, including Python and JavaScript, evaluate expressions according to the order of operations. In video game development, multi-step expressions are often used to describe the interactions or physics that influence the movement of various game objects.

Examples

The order of operations (no parentheses)

Q: Evaluate: 3 + 4 × 5

A: The order of operations dictates that multiplication must be evaluated before addition:
3 + 4 × 5 = 3 + 20 = 23


The order of operations (no parentheses)

Q: Evaluate: 6 ÷ 3 × 2

A: The order of operations dictates that multiplication and division are evaluated from left to right:
6 ÷ 3 × 2 = 2 × 2 = 4


The order of operations (with parentheses)

Q: Evaluate: (3 + 4) × 5

A: The inclusion of parentheses changes the order in which the operations must be evaluated:
(3 + 4) × 5 = 7 × 5 = 35.

Practice | The order of operations