Learning Goals 3 min
By the end of this lesson you will be able to:
- Find the four arithmetic blocks — () + (), () - (), () * (), () / () — in the green Operators palette and identify them as round reporters (they report a number).
- Nest an arithmetic block inside another block — for example, set [score v] to ((score) + (1)) — and explain why that's different from change [score v] by (1).
- Use nesting to control evaluation order, because Scratch has no precedence rules:
((10) + (5)) * (2)gives 30, but(10) + ((5) * (2))gives 20. Same numbers, different nesting, different answer.
Warm-Up — predict the maths 8 min
You've used numbers in every Scratch lesson — move (10) steps, wait (1) seconds, set [score v] to (0). Today is the first time a number slot contains another block instead of a typed value. Predict what each cat says.
Puzzle 1
when flag clicked
say ((3) + (4)) for (2) seconds
Reveal puzzle 1
The cat says "7". The () + () block is a round reporter — it doesn't run on its own. It calculates 3 + 4 = 7 and hands the answer to the say block. The say block then displays the answer as its speech bubble text. Whenever you see a block tucked inside another block's slot, the inner block runs first and its answer flows out.
Puzzle 2
when flag clicked
say ((10) - (3)) for (1) seconds
say ((10) * (3)) for (1) seconds
say ((10) / (3)) for (1) seconds
Reveal puzzle 2
The cat says "7", then "30", then "3.3333333333333335". The first two are simple. The third one is honest: 10 ÷ 3 is not a whole number, and Scratch shows the full repeating decimal. (In real games you'll round it — that's lesson SCR-L03-04 next class.) Notice that () * () uses an asterisk for multiply, not the × symbol — Scratch follows the same convention as every programming language.
Puzzle 3
when flag clicked
set [score v] to (10)
say ((score) + (5)) for (1) seconds
say (score) for (1) seconds
Reveal puzzle 3
The cat says "15", then "10". The arithmetic block read the score (10), added 5, and reported 15 — but it did not change the actual score variable. The variable is still 10. Reporters report. They don't change anything. If you wanted to permanently bump the score, you'd wrap it in set [score v] to ((score) + (5)) — and we'll meet that pattern in the next section.
New Concept — the four arithmetic blocks 15 min
Open the green Operators palette. The four arithmetic blocks sit right at the top:
() + ()
() - ()
() * ()
() / ()
Two slots, one answer
Every arithmetic block takes two number inputs (left and right) and produces one number output. The block itself has no behaviour on the Stage — it doesn't move, doesn't speak, doesn't draw. It just calculates and hands the answer to whatever it's plugged into. That's what "reporter" means.
Where they go
Round reporters fit anywhere you see a round hole. That's most number slots in Scratch:
- move (10) steps → the 10 can be replaced by ((speed) * (2)).
- set [score v] to (0) → the 0 can be replaced by ((score) + (1)).
- wait (1) seconds → the 1 can be replaced by ((delay) + (0.5)).
- say [Hi!] for (2) seconds → either slot, even the text slot (numbers will display as text).
set vs change — the most important nesting
You've been using change [score v] by (1) since L2. That block is shorthand for a very common pattern:
change [score v] by (1)
set [score v] to ((score) + (1))
So why bother with the long way? Because change can only add. If you want to double the score, or halve the lives, or square the speed, you need the full set + arithmetic pattern:
set [score v] to ((score) * (2))
set [lives v] to ((lives) / (2))
set [speed v] to ((speed) + (0.5))
Scratch has NO order of operations
This is the part that surprises everyone. In maths class, you learn BODMAS: brackets, divide/multiply, add/subtract. So in maths class, 10 + 5 × 2 means do the multiply first — the answer is 20.
Scratch doesn't have BODMAS. Scratch evaluates whichever block is nested inside whichever block. There is no "multiply happens first" — there's only "the inside happens first". You control the order by where you put the blocks.
say (((10) + (5)) * (2)) for (2) seconds
say ((10) + ((5) * (2))) for (2) seconds
The top one says "30" — because (10 + 5) is computed inside first, giving 15, then × 2. The bottom one says "20" — because (5 × 2) is computed inside first, giving 10, then 10 + that. The arithmetic blocks themselves don't fight over who runs first. The visible nesting decides everything.
Speed = pixels-per-step in real maths
Here's a pattern you'll use in every L3 game from now on. A sprite that speeds up over time:
when flag clicked
set [speed v] to (2)
forever
move (speed) steps
if <touching [edge v] ?> then
turn cw (180) degrees
set [speed v] to ((speed) + (0.5))
end
end
Worked Example — the Lucky Number Machine's maths engine 12 min
We build the core maths of the Lucky Number Machine. The cat holds two dice values as variables, adds them, multiplies for a bonus, then announces the total. Eight steps.
die1 and die2 as variables and announces the total. The panels are watcher widgets from Variables. In Lesson 3, pick random will fill those numbers automatically.Step 1 — Open the Lucky Number Machine project
Open the Lucky Number Machine project you saved in Lesson 1. Default cat. We work on the same project for all five lessons.
Step 2 — Make three variables
Variables palette → Make a Variable. Make three, all "For all sprites": die1, die2, total. Tick their boxes so the watchers appear on the Stage.
Step 3 — Set the dice to placeholder values
when flag clicked
set [die1 v] to (4)
set [die2 v] to (5)
Step 4 — Add the two dice
From Variables: set [total v] to (). Into its empty slot, drag () + (). Drop (die1) on the left and (die2) on the right.
Step 5 — Announce the total
From Operators: join [] []. Type Total: in the first slot and drop (total) in the second. Drop the whole join into say () for (2) seconds.
Step 6 — Calculate a bonus (multiply the total by 2)
After the say, make a variable bonus. Set it to ((total) * (2)). Then say the bonus for 2 seconds too.
Step 7 — Full assembled stack
when flag clicked
set [die1 v] to (4)
set [die2 v] to (5)
set [total v] to ((die1) + (die2))
say (join [Total: ] (total)) for (2) seconds
set [bonus v] to ((total) * (2))
say (join [Bonus: ] (bonus)) for (2) seconds
Step 8 — Click the flag and check
The cat should say "Total: 9" for two seconds, then "Bonus: 18". Change die1 to 6 and try again — the cat now says "Total: 11" and "Bonus: 22". The arithmetic updates automatically because the blocks re-read the variables every time.
What you just built: the maths engine of the Lucky Number Machine. One arithmetic block adds, another multiplies. Any number you put into die1 and die2 flows through both calculations instantly. Next lesson, pick random fills those values for you.
Try It Yourself — three arithmetic drills 15 min
Goal: Make a sprite that says 10 × 7 = 70 using two arithmetic blocks plugged into a join. (You'll need the green join [] [] block from Operators.)
when flag clicked
say (join [10 x 7 = ] ((10) * (7))) for (3) seconds
Think: You just nested three blocks deep — say contains join, join contains multiply. Each inner block runs first and hands its answer outward. This is the reading order: innermost first.
Goal: Build a sprite that asks the user for two numbers (use ask [] and wait from L02-18) and says the sum, difference, product, and quotient. Use a variable called num1 and a variable called num2 to store the two inputs.
when flag clicked
ask [Give me the first number:] and wait
set [num1 v] to (answer)
ask [Give me the second number:] and wait
set [num2 v] to (answer)
say (join [Sum is: ] ((num1) + (num2))) for (2) seconds
say (join [Difference is: ] ((num1) - (num2))) for (2) seconds
say (join [Product is: ] ((num1) * (num2))) for (2) seconds
say (join [Quotient is: ] ((num1) / (num2))) for (2) seconds
Think: You just built a four-function calculator in 9 blocks. Try entering 10 and 0 for the second number — what does the quotient bubble show? Now you know why "divide by zero" is a famous warning.
Goal: A sprite walks across the Stage. Its speed is calculated from two variables: baseSpeed = 2, and bonusSpeed changes when the space key is held. The total speed is baseSpeed + bonusSpeed. When space is held, bonusSpeed is 3 (so total = 5). When released, bonusSpeed is 0 (so total = 2). Plug the full arithmetic into the move block.
when flag clicked
set [baseSpeed v] to (2)
set [bonusSpeed v] to (0)
forever
if <key [space v] pressed?> then
set [bonusSpeed v] to (3)
else
set [bonusSpeed v] to (0)
end
move ((baseSpeed) + (bonusSpeed)) steps
if <touching [edge v] ?> then
turn cw (180) degrees
end
end
Think: The move ((baseSpeed) + (bonusSpeed)) steps block re-reads both variables on every frame. Hold space → instant speed boost. Release → instant slowdown. This is exactly how the "sprint" mechanic in most games works — base movement plus an optional modifier.
Mini-Challenge — Daniel's wrong total 5 min
"Why is the bill RM30 instead of RM13?"
Daniel is building a kuih-stall calculator. He wants: one teh tarik (RM3) plus five kuih (RM2 each) = RM3 + RM10 = RM13. Read his stack and figure out what he actually computed.
when flag clicked
say (((3) + (2)) * (5)) for (3) seconds
What number does Daniel actually see in the bubble? And how should the nesting change?
Reveal one valid solution
Daniel sees "25". The inner block is ((3) + (2)) which is 5, then (5) * (5) which is 25. He multiplied the teh-tarik price plus one kuih by 5 — instead of multiplying just the kuih by 5 and then adding the teh tarik.
The fix is to move the brackets — that is, change which arithmetic is nested inside which:
when flag clicked
say ((3) + ((2) * (5))) for (3) seconds
Now the inner block is ((2) * (5)) = 10 (the kuih total), and then (3) + (10) = 13 (plus the teh tarik). Bubble shows "13". Same five numbers, same two operations — different nesting, different answer. This is exactly the BODMAS-doesn't-exist lesson from earlier. In Scratch, nesting is the brackets.
Recap 3 min
You met Scratch's four arithmetic blocks today — the calculator buttons of the green Operators palette. Each one is a round reporter: it takes two numbers in, calculates, and hands the answer out. They go inside any number slot — move blocks, wait blocks, say blocks, set blocks. Nesting one inside another is how you build bigger maths, and because Scratch has no order of operations, the nesting itself decides what runs first. For the Lucky Number Machine, you used () + () to add the two dice and () * () to compute a bonus — the machine can now do maths on two numbers.
- Arithmetic operator
- Any of the four green blocks () + (), () - (), () * (), () / (). Takes two numbers in, reports one number out.
- Reporter
- A round (or hexagonal) block that has no behaviour of its own — it just calculates and hands the answer to whatever it's plugged into. Arithmetic blocks are round (number) reporters.
- Nesting
- Plugging one block inside another block's slot. In Scratch, the inner block runs first and its answer flows outward. Nesting is how you build expressions.
- Precedence (and why Scratch doesn't have it)
- The maths rule that says multiply happens before add (BODMAS). Scratch ignores it — only nesting decides order.
((10) + (5)) * (2)= 30, but(10) + ((5) * (2))= 20. - Variable nesting (set vs change)
- change [x v] by (1) is shorthand for set [x v] to ((x) + (1)). Use change for adding; use set + arithmetic for anything else.
Homework 2 min
The Teh-Tarik Calculator. Build a small Scratch project that asks the user for an order and prints the bill, using arithmetic blocks.
- Open a fresh project. Pick any sprite to be the cashier.
- Make four variables:
tehCount,kuihCount,subtotal,finalBill. - Ask the user "How many teh tarik?" — store the answer in
tehCount. Ask "How many kuih?" — store inkuihCount. - Calculate
subtotalas ((tehCount) * (3)) plus ((kuihCount) * (2)). (You'll need to nest one of those inside a set block, then change-by the other. Or do it all in one big nested set.) - Calculate
finalBillassubtotalplus 10% SST: set [finalBill v] to ((subtotal) + ((subtotal) * (0.1))). - Say the final bill in a speech bubble for 3 seconds.
Save as HW-L3-02-Teh-Tarik-Calculator.sb3.
Bring back next class:
- The
.sb3file. - Your answer to: "If you wanted to give a 50% discount instead of adding 10% tax, which arithmetic block would change, and how?"
Heads up for next class: SCR-L03-03 meets the green Operators palette's most-loved block — pick random () to (). We'll use it to make the Lucky Number Machine's dice actually roll — every press of the flag gives different numbers.