Arithmetic in Code: Integer Division and Remainder
The arithmetic a program actually offers, including whole-number division and the remainder, and the order operators are applied in.
What a learner can do afterwards
- Work out the result of an expression mixing multiplication, addition and brackets
- Use whole-number division and remainder to split a total, such as seconds into minutes and seconds
- Say which operator runs first without running the code
1 · Read
Your code can add, subtract, multiply and divide like your calculator. Two extra tools split division in two: // keeps the whole part and throws the leftover away, while % keeps only the leftover. So 10 // 3 is 3, and 10 % 3 is 1.
Turn 200 seconds into minutes and seconds the way code does. 200 // 60 is 3 whole minutes, and 200 % 60 is the 20 seconds left over. Together they rebuild the total, since 3 times 60 plus 20 is 200.
When operators mix, brackets run first, then multiply, divide, // and %, then add and subtract last. That is why 3 + 4 * 2 is 11, while 2 * (3 + 4) is 14. Say which operator runs first before you run the code.
Ask yourself what each piece keeps. // keeps the wholes and drops the rest, % drops the wholes and keeps the rest.
Brackets first, then multiply and divide, then add; // keeps wholes and % keeps the leftover.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.