Week 2 · Unit 1: Making programs clear · 3 lessons
1
Join repeated blocks
Computing 3: Unit 1, pp. 8–27
GoalMake a Scratch program shorter by joining blocks that repeat and removing blocks that do nothing.
New words
block · a piece of code that you snap into a programsprite · a character or object that follows a Scratch programconcise · short, with nothing extra
Explanation
A Scratch program is a stack of blocks. Sometimes the same block appears again and again, side by side. “change x by (35)” twice moves the sprite 70 steps right, so one block “change x by (70)” does the same job. We can also take out a block that does nothing, such as “change size by (0)”. The shorter program is more concise, and the sprite ends up in just the same place. Always count the blocks before and after to see how many you saved.
Worked examples
Program A
Program B
x at the end
−100 + 35 + 35 = −30
−100 + 70 = −30
size at the end
100 − 5 − 5 = 90
100 − 10 = 90
blocks
7
4
A fish starts at x: −100 with size 100. In program A it moves 35 right twice and shrinks by 5 twice. In program B it moves 70 right once and shrinks by 10 once. Both end at x: −30 and size 90. Program B has 3 fewer blocks.
These blocks cannot be joined. There is a wait block between them, so the sprite pauses in the middle of its climb. Joining them would remove the pause and change what the sprite does.
In this program the red blocks do nothing, because turning by 0 degrees and moving by 0 steps change nothing. We can remove them. Only one useful block is left.
Class activity
Block cards (with a friend): write each block of a program on its own small card, for example “change x by 20”, “change x by 20”, “say Hello for 2 seconds”. Lay the cards in order. Count them. Now join cards that are the same and side by side, and take away any card that does nothing. Count again. Do both stacks make the sprite end in the same place? If you have a computer or tablet, build both programs in Scratch and compare.
Practice
1
Make this program more concise. How many blocks does your new program have?
The program: when green flag clicked, change y by (−60), wait (1) seconds, change size by (20). It has 4 blocks (the original had 7).
2
Two “change x by (25)” blocks are side by side. Which one block can replace them?
change x by (50)
3
Can the two “change x by (10)” blocks be joined into one? Explain.
No. There is a “say” block between them. Joining them would change when the sprite speaks.
4
Give two reasons why a concise program is better.
It is quicker to make and easier to check, so there are fewer mistakes. It also uses less computer power.