Topic 8 ยท Programming ยท 8.4

Arithmetic Operators Explained Simply

Arithmetic operators are the calculation tools used by programs. They tell the computer whether to add, subtract, multiply, divide or find a remainder.

Invitation

How does a program perform a calculation?

Programs often need to calculate totals, prices, scores, averages and many other values.

An arithmetic operator is a symbol or command that tells the computer which mathematical operation to perform.

The values are the ingredients. The operator tells the computer what to do with them.
Figure 1
A simple calculation
Value: 8
๏ผ‹
Value: 4
โ†“
Result: 12
Big Idea

Each operator performs a different job

+ adds, - subtracts, * multiplies and / divides.

Programming also uses DIV for whole-number division and MOD for the remainder after division.

Choose the operator that matches the calculation the program needs to perform.
Figure 2
The operator toolkit
+ Add    โˆ’ Subtract
* Multiply    / Divide
DIV Whole number
MOD Remainder
FutureLogic Bridge

Arithmetic operators are tools in a calculator toolbox

Imagine a toolbox containing several calculator tools. One tool adds values, another subtracts them, and another finds what is left over after sharing.

You would not use a screwdriver when you need a hammer. In the same way, a programmer must choose the correct arithmetic operator for the job.

The values are waiting. The operator is the tool that performs the job.
Figure 3
Choose the right tool
Find a total โ†’ +
Find a difference โ†’ โˆ’
Share equally โ†’ DIV
Find leftovers โ†’ MOD
Worked Example

Calculate the total cost of three tickets

Each ticket costs 6. The program multiplies the ticket price by the number of tickets.

PseudocodeMultiplication
TicketPrice โ† 6
NumberOfTickets โ† 3
TotalCost โ† TicketPrice * NumberOfTickets
OUTPUT(TotalCost)

Follow the calculation

6 ร— 3 = 18, so the value stored in TotalCost is 18.

Output: 18
DIV and MOD

Whole groups and leftovers

17 / 5Normal division gives 3.4.
DIV(17, 5)There are 3 complete groups.
MOD(17, 5)There are 2 left over.

Think of sharing 17 biscuits equally between groups of 5. You can make 3 complete groups, with 2 biscuits left over.

DIV gives the whole-number result. MOD gives the remainder.
Exam Tip

Know exactly what DIV and MOD return

These two operators are frequently confused. Write a quick division fact before answering:

23 รท 4 = 5 remainder 3
Therefore, DIV(23, 4) = 5 and MOD(23, 4) = 3.

MOD is also useful for checking whether a number is odd or even. If Number MOD 2 = 0, the number is even.

Common Mistake

โ€œMOD gives the decimal part.โ€

MOD does not return the digits after the decimal point. It returns the whole-number remainder.

Incorrect: โ€œMOD(17, 5) is 0.4.โ€
Correct: โ€œMOD(17, 5) is 2 because 17 รท 5 gives 3 complete groups with 2 left over.โ€
Summary

The calculation toolkit

+ and โˆ’Add or subtract values.
* and /Multiply or divide values.
DIVReturn the whole-number quotient.
MODReturn the remainder.
โ†Store the calculated result.
()Make bracketed calculations happen first.