Step 7
Boxes that count
New to lesson cards? See what a lesson card is.
In Step 6 you stored a number in a variable (a box) and used it. A variable can also change itself.
Take a store in box1 block. In its number slot, plug a + block from the Math palette. In the first slot of the +, put the box1 block, and in the second slot, type a 1. Now store in box1 does this, in order:
- The
+block looks insidebox1and adds 1. store in box1puts that answer back inbox1.
If box1 held 5, it holds 6 afterwards. Put that store in box1 block inside a repeat and the variable counts: one more every lap.
Watch a box count
Open the frame and press Run. The Output box at the top right of the drawing shows what the print block says. Here it prints box1 on every lap, and the same number is the length of the side the turtle draws.
Before you change anything, work out:
- The box starts at 0. What does the first lap print? What does the last lap print?
- Change the
10inbox1 + 10to a5and press Run. What does the last lap print now? - Change it back to 10, then change the
8on therepeatto a 12. What does the last lap print?
Check your answers
- The first lap prints 10, because the box goes from 0 to 10 before it is printed. The last lap prints 80.
-
- Eight laps of 5 is 40.
-
- Twelve laps of 10.
Trace a box on paper
Programmers follow a box by hand to see what a program does. Try it with this one, which multiplies instead of adds. Pretend you are the computer and write down what is in box1 after every lap.
store in box1: 2
repeat 3
store in box1: box1 × 2
print box1
- What does it print, lap by lap?
- What would the last number be if the
repeatsaid 5 instead of 3?
Check your answers
- It prints 4, then 8, then 16. The box starts at 2, and each lap doubles it.
-
- The five laps print 4, 8, 16, 32 and 64.
Counting down
A box can count down too. This sample counts with box1 - 1.
- What is in the box when the circle starts to draw?
- Why does the circle wait until the numbers have finished?
- Change the 3 in
store in box1to a 5, but leave therepeatat 3. What does it print? Then change therepeatto 5 as well.
Check your answers
-
- It started at 3 and lost 1 on each of three laps.
- The
repeathas to finish all of its laps before the blocks below it run. - With only 3 laps it prints 5, 4 and 3, then stops early. With a
repeatof 5 it prints 5, 4, 3, 2 and 1. The number in the box and the number of laps are two separate numbers, so you have to change both.
Make a spiral of your own
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Make a spiral where each side is longer than the last. A box holds the length of the side. On every lap the turtle draws a side as long as box1, turns, and then makes box1 bigger.
- Start with a turn of 90 and make the box 8 bigger each lap.
- Predict, then try a turn of 120. Then 60. What shapes do you get?
- Put the
store in box1that makes the box bigger inside therepeat. If it is outside, it only happens once.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Make the turn change too. Use a second box, box2, that starts at 10 and grows by 2 each lap, and use box2 as the number in right. Describe what you see.