GoalExplain what a sequence is, and find and fix a bug caused by instructions in the wrong order.
New words
sequence · instructions that run one after the other, in orderon start · a micro:bit block that runs its blocks once, when the program startsbug · a mistake in a programtrace · to follow a program step by step and write down the values of its variables
Explanation
Every program uses sequence. The instructions run one after the other, from the top to the bottom. The order matters. A variable must be given its value before another block uses it. In micro:bit blocks a new variable starts at 0. If a calculation or a show block comes before the values are set, it uses 0, and the answer is wrong. The program does not warn us. It simply follows the order that we gave it. To find this kind of bug, we trace the program and write down the value of each variable after every block. To fix it, we move the blocks into the correct order and trace it again.
Worked examples
After the block
price
count
cost
Board shows
start
0
0
0
—
set price to 5
5
0
0
—
set count to 3
5
3
0
—
set cost to price × count
5
3
15
—
show number cost
5
3
15
15
Trace the program. The variables start at 0. Each block changes only one thing. At the end the board shows 15.
Now the show block comes before the calculation. The board shows the value that cost has at that moment, which is still 0. Then cost is worked out, but nobody sees it. The program ran with no error, and the answer on the display is wrong.
Order matters in everyday jobs too. To mix paint, we pour the water, add the paint and then stir. If we stir first, there is nothing to mix. A computer cannot guess what we meant. It only does the steps in the order that we write them.
Class activity
Strips in order (no computer): write these blocks on strips of paper: on start, set price to 5, set count to 3, set cost to price × count, show number cost. (Use your own numbers if you like.) Shuffle the strips. A partner puts them in the right order and traces the variables on paper. Then try some changes. What does the board show when the set cost strip comes before the set count strip? Try moving show number to the top. Write down the result of each change and say why it happens. If you have a computer or tablet, build the program in a micro:bit simulator and compare.
Practice
1
What does the board show? How can the program be fixed?
It shows 0, because the bill is worked out while tickets and fare are still 0. The fix is to move the set bill block below the two other set blocks. Then it shows 4 × 6 = 24.
2
Put the blocks in the right order so that the board shows 18.
1 set w to 6. 2 set h to 3. 3 set area to w × h. 4 show number area. (Blocks 1 and 2 may swap.) It shows 6 × 3 = 18.
3
What does the board show at the end? Explain.
It shows 0. The last set block puts 0 in score, and this replaces the 15 that was there before. The order of the blocks decides the result.
4
Why can instructions in the wrong order give a wrong answer without any warning?
The computer always follows the instructions in the order given. It does not know what we meant. A wrong order is still a program that can run, so there is no error, only a wrong answer.