GoalTrace a micro:bit program that uses sequence, selection and iteration, then test and debug it.
New words
variable · a named place that stores a value, which can changeselection · a choice in a program, made with an if blockiteration · repeating blocks in a looptest data · values that we give to a program to check it works
Explanation
Every micro:bit program uses sequence, so the order of the blocks matters. A variable must have its value before a block uses it. Selection uses an if block and a condition with an operator such as <, ≥ or =. Iteration repeats blocks. A repeat block runs a set number of times, and a while block runs for as long as its condition is true. Events such as buttons and shakes start the blocks that we write. To test a program, we predict the results by hand and use normal data and boundary data. If the actual result is different, we find the bug, fix one thing and test again.
Construct
Example block
What it does
sequence
blocks one under another
they run in order, from the top
selection
if … then … else
chooses one of two paths
iteration (count)
repeat 4 times
repeats a set number of times
iteration (condition)
while n < 50
repeats while the condition is true
Worked examples
This program uses all three constructs. It starts with 40, takes away 12 three times, and then chooses what to show. The loop gives 28, 16 and 4. Since 4 ≥ 5 is false, the else path runs and the board shows 4.
Step
pocket
Condition pocket ≥ 5
Board shows
start
40
—
—
round 1
28
—
—
round 2
16
—
—
round 3
4
—
—
if block
4
false
4
A trace table follows the variable round by round, and it shows the answer to the condition at the end.
score
Expected
Kind of test
5
no
normal
20
no
boundary
21
yes
boundary
60
yes
normal
A rule says that the board must show yes when a score is more than 20. We test normal data and the boundary. The score 20 is not more than 20, so the expected result is no. A program that uses ≥ 20 by mistake would pass the tests with 5, 21 and 60, and only the boundary test at 20 would fail.
Class activity
Trace relay (no computer): each pupil writes a mini program of 4 or 5 blocks on a card. It must use one loop and one if block, and it must show a number or an icon. Write the answer on the back of the card. Swap cards with a partner, who traces the program in a table and writes a prediction. Then compare it with the answer on the back. If the two are different, find the bug together. Who made the mistake, the writer or the tracer? Fix it and trace again. If you have a computer or tablet, build the best program in a micro:bit simulator.
Practice
1
What does the board show? How many times does the loop run?
x goes 3, 6, 12, 24 and 48. The loop runs 4 times. After 24, the condition 24 < 30 is still true, so there is one more round. At 48 it is false and the loop stops. The board shows 48.
2
What does the board show?
The loop adds 6 four times, so total = 24. Then 24 ≥ 25 is false, so the else path runs and the board shows 30 − 24 = 6.
3
This program should count down and finish by showing 0. It never stops. Explain why and fix it.
Each round adds 3 to n, so n gets bigger and n > 0 is always true. Change the block to change n by −5. Then n is 5, then 0, and the condition 0 > 0 is false, so the loop stops and the board shows 0.
4
A rule says that a child of 6 or under gets a free ticket. Give two normal tests and three boundary tests, with the expected results.
Normal: 3 (free) and 11 (not free). Boundary: 5 (free), 6 (free) and 7 (not free). The edge of the rule is between 6 and 7.