A computer is, at heart, a spectacularly fast calculator. Almost every program you will ever write — a game keeping score, a shop working out change, a phone counting down to midnight — leans on the same handful of everyday sums. In code, the little symbols that do those sums are called arithmetic operators, and the four you already know from maths lessons look almost exactly the same:
+ — add- — subtract* — multiply (a star, not a / — divide (a slash, not a Each operator sits between two numbers — its operands — and produces a new number. Press Run and watch four sums come out at once:
The comment after each // is just a note for humans — the computer ignores it. Try
editing the numbers and running again; the machine never gets the arithmetic wrong (though, as
we'll see, you can still tell it to do the wrong sum).
Operators earn their keep when we stop typing bare numbers and start working with
Notice how the multiplications happen before the addition — pens * penPrice
and books * bookPrice are each worked out first, and only then are the two totals
added. That ordering isn't luck: it's a rule the computer follows every single time, and it's
important enough to have its own name. More on that shortly.
When you divide in maths you often get a quotient and a remainder:
17 shared into groups of 5, that's
Math.floor(17 / 5)
(Math.floor rounds a decimal down to the whole number below it).% operator (read it aloud as "mod"):
17 % 5 is Let's split a bag of sweets fairly between some friends and see exactly what's left over:
Together the two operators always tell the whole story of a division:
Math.floor gives the
% gives the sweets to 20 and the remainder becomes
Here is % and Math.floor working as a team on a genuinely useful job:
turning a stopwatch reading in seconds into "minutes and seconds", exactly like a timer on a
screen. There are % 60:
Math.floor and %.
The remainder operator has a couple of tricks that show up everywhere in programming:
n % 2 is
(10 + 5) % 12 is % is the operator that does the wrapping.
What is
( ) — always worked out first;*, /, %) —
next, left to right;+, -) — last, left to right.
So % ranks alongside * and /. Run this to see precedence
and brackets in action:
Brackets are your override switch: whatever you put inside them is worked out first, no matter
what. When you want the addition to happen before the multiply — as in
(2 + 3) * 4 — brackets are how you say so.
The classic slip is assuming the computer works left to right. It doesn't —
it follows precedence, so 2 + 3 * 4 is (2 + 3) * 4.
When in doubt, add brackets. They cost nothing, and they make your intention obvious to the computer and to the next person reading your code — a program that says exactly what it means is easier to trust.
One more surprise waiting for you: in most programming languages / gives a
decimal, not a whole number. 7 / 2 is 3.5, not
3. That's often exactly what you want — but when you need a whole-number answer
(sweets per friend, whole minutes), reach for Math.floor(a / b) instead.