Arithmetic
After this: Work out a line of arithmetic the way the computer does.
On this page · 7 sections · 3 checks
A shop bill: one bag of rice at 3 and four bottles of water at 2 each. On paper you write 3 + 4 × 2. If you read it from left to right you get 7 × 2, which is 14. The shop charges 11, because the four bottles cost 4 × 2 = 8, and 3 + 8 is 11. Multiplication happens before addition.
A program follows the same rule as the shop. This lesson shows the rule, the symbols for arithmetic, and two places where the computer's answer can surprise you.
The symbols
| Symbol | Means | Example | Gives |
|---|---|---|---|
+ | add | 3 + 4 | 7 |
- | subtract | 10 - 4 | 6 |
* | multiply | 7 * 6 | 42 |
/ | divide | 7 / 2 | see below |
% | remainder | 7 % 2 | 1 |
// (Python only) | divide, throw the fraction part away | 7 // 2 | 3 |
Multiplication and division come first
The rule has three parts. Brackets go first. Then *, / and %. Then + and -. When two symbols are at the same level, the computer works from left to right.
print(3 + 4 * 2)print((3 + 4) * 2)print(10 - 2 - 3)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
3 + 4 * 2: multiply first, 4 × 2 = 8, then 3 + 8 = 11.(3 + 4) * 2: the brackets go first, 3 + 4 = 7, then 7 × 2 = 14. Brackets are how you say "do this part first".10 - 2 - 3: two minus signs are at the same level, so left to right: 10 − 2 = 8, then 8 − 3 = 5.
Division: where the two languages differ
Seven divided by two is three and a half. What the computer prints depends on the language and on how you write the line. Watch the first line of this trace in your language, then switch languages and run it again.
- Run the programPress Run it and step to the end. Watch what the first line prints, then switch languages with the top bar and run it again.
print(7 / 2)print(7 % 2)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
print(7 / 2) # keeps the fraction partprint(7 // 2) # throws the fraction part awayprint(6 / 2) # a float even when it comes out whole3.5 3 3.0
System.out.println(7 / 2); // both sides whole: the fraction part is thrown awaySystem.out.println(7 / 2.0); // one side has a fraction part: it is keptSystem.out.println(6 / 2); // whole, and comes out whole3 3.5 3
The remainder
% gives what is left over after whole-number division. Seven is three twos with 1 left over, so 7 % 2 is 1. Seventeen is three fives with 2 left over, so 17 % 5 is 2. When nothing is left over, the remainder is 0, and that tells you the division was exact. The remainder by 2 tells you whether a number is even, remainder 0, or odd, remainder 1. This works the same in both languages.
print(7 % 2) # 7 is 3 twos with 1 left overprint(17 % 5) # 17 is 3 fives with 2 left overprint(10 % 5) # nothing left over1 2 0
System.out.println(7 % 2); // 7 is 3 twos with 1 left overSystem.out.println(17 % 5); // 17 is 3 fives with 2 left overSystem.out.println(10 % 5); // nothing left over1 2 0
Run it
Change 7 and 2 to other numbers and see which lines change.
Check yourself
What does this print?
print(2 + 3 * 4)System.out.println(2 + 3 * 4);What does 17 % 5 give?
What does this print?
print(9 / 2)System.out.println(9 / 2);What comes next
The next lesson puts text and numbers in the same line, and shows the surprise that waits there.
- Work out a line of arithmetic: brackets first, then multiply and divide, then add and subtract, left to right at the same level.
- Say what division gives in your language, and how to get the other kind.
- Use % to get the remainder, and to tell an even number from an odd one.
- Say why 0.1 + 0.2 does not print 0.3, and why money is never stored as a float.