← Back to course
Code Crew G3 / Week 04 / Wednesday
3/6
Week 04 Β· Variables

Wednesday

// Boxes that hold things

Wednesday: Math With Boxes

Rocket's score box holds zero. He adds one point, then two, then five. "Let's see what happens!" he says.

Raven traces each line slowly. "What do you notice about the old value?" she asks.

Next, Rocket wants to trade what is inside two boxes. Nova holds up a third empty box. "This one is called temp," she says.

Let's do the box math. Then we'll try the swap.

When the value is a number, you can ADD to it or SUBTRACT from it.

score = 0
score = score + 1     (Now score = ?)
score = score + 2     (Now score = ?)
score = score + 5     (Now score = ?)
After step 2: score = ____
After step 3: score = ____
After step 4: score = ____
lives = 5
lives = lives - 1     (Now lives = ?)
lives = lives - 2     (Now lives = ?)
After step 2: lives = ____
After step 3: lives = ____

Swap the Boxes

Sometimes you want to TRADE what's inside two boxes. To do that you need a THIRD empty box (call it temp). Read carefully:

a = 7
b = 4
temp = a       (now temp = 7)
a    = b       (now a = ?)
b    = temp    (now b = ?)
After 'a = b': a = ____
After 'b = temp': b = ____

Box Math Check

DO THE BOX MATH
  • Read the question.
  • Tap your answer.
x = 4, then x = x + 3. What is x?
y = 10, then y = y - 4. What is y?
a = 2 and b = 5. After a = b, what is a?
Why do we need temp to swap two boxes?
apples = 6. Then apples = apples - 2. Then apples = apples + 5. What is apples?
WHY THIS EXERCISEUpdating one line at a time is how a computer runs box math.
β—‡ PARTNER MOVE
Use 3 cups (paper or imaginary).
Put a sticker (or pebble) in two of them. Try to swap WITHOUT temp.
Now try with temp. Easier?
← Tuesday