// learn.shawon.ch / math-for-ai / order-of-operations STUDY GUIDE
← Mathematics for AI

Mathematics · Arithmetic

The grammar of an expression

Try this first

What is 2 + 3 × 4? Don't trust your fingers — commit to one answer. Most people split between 20 and 14, and only one is right.

If you read left to right like a sentence — 2 + 3 = 5, then 5 × 4 = 20 — you get 20, and it's wrong. The agreed answer is 14, because multiplication is done before addition. This isn't a quirk; it's a convention every mathematician, calculator, and programming language shares, so that one written expression has exactly one meaning. Without it, 2 + 3 × 4 would be ambiguous, and code that disagreed on it would compute different things.

The rule is a fixed pecking order. Do what's inside parentheses first, then exponents, then multiplication and division (left to right), and finally addition and subtraction (left to right). The picture is simply: the 3 × 4 binds tighter than the +, so it resolves first.

2 + 3 × 4 do this first → 12 = 2 + 12 = 14 ✓ correct left-to-right = 5 × 4 = 20
Multiplication binds tighter than addition, so 3 × 4 resolves before the +. One expression, one meaning.

The one idea

An expression is read by a fixed order of operations, not left to right: Parentheses → Exponents → Multiplication/Division → Addition/Subtraction. It exists so a formula means one thing to everyone — including the machine running your code.

Parentheses are the override

If you actually want the addition first, you say so with parentheses: (2 + 3) × 4 = 20. They're the one tool that lets you bend the order to your intent, and the habit of adding them — even when not strictly required — is how you write a formula nobody can misread. When in doubt, bracket it.

The order, top to bottom
StepOperationExample
1Parentheses(2+3)×4 = 20
2Exponents2 × 3² = 2×9 = 18
3Multiply / divide10 − 6 ÷ 2 = 10−3 = 7
4Add / subtract2 + 3 × 4 = 14

The other half: number sense

Knowing the rules isn't enough — you also want to know roughly the answer before you grind it out, so a slip stands out. That's estimation: round the messy numbers to friendly ones and do the easy version in your head. Asked for 312 × 49, think "300 × 50 = 15,000," and now any real answer near 6,000 or 60,000 is obviously a typo. This single habit — carrying a ballpark in your head — catches more mistakes than careful arithmetic does.

Work one, then finish one

Worked: evaluate 2 + 3 × 4². Exponent first: 4² = 16. Then multiply: 3 × 16 = 48. Then add: 2 + 48 = 50. Order, every time, top of the list down.

Your turn: evaluate (8 − 2) × 3 + 4², and first estimate it to the nearest ten. (Answer: parentheses 6, exponent 16, so 6×3 + 16 = 18 + 16 = 34.)

Why this earns a place in your toolkit

Every formula you'll meet in this course — a loss function, the inside of a neuron, an attention score — is one expression that has to be read in exactly this order, and code runs on precisely these rules. When you write w * x + b in a model, the language multiplies then adds, with no mercy for what you meant; mis-bracketing is one of the most common bugs in real ML code. And estimation is the working scientist's reflex: when a training run reports a loss of 4,000,000 and you expected something near 1, the ballpark in your head is what tells you, instantly, that something is broken. Reading expressions correctly and sanity-checking them by eye are the two habits that keep the math from quietly lying to you.

Recall check · no peeking

  1. State the order of operations, and what each letter of PEMDAS stands for.
  2. Why is 2 + 3 × 4 = 14 and not 20?
  3. How would you rewrite it, with parentheses, to actually get 20?
  4. Estimate 198 × 21 in your head, and say why a quick ballpark is worth keeping.

Explain it back

In one sentence, explain why an agreed order of operations has to exist at all.

Learn · Shawon Chowdhury · a study guide, kept rough on purpose