GoalRead a micro:bit algorithm and predict what happens when some lines are changed.
New words
algorithm · a list of steps that a computer followspredict · to say what will happen before you run the programchanged line · a step that is different from the same step in another algorithm
Explanation
An algorithm for the micro:bit begins with the input that starts it. Then it lists the outputs in order, one line at a time. When someone changes an algorithm, we can predict the new result without running the program. First find what is different: a number, a word, a choice from a list or the order of the lines. Then say what each change does. A bigger volume number means a louder sound, a new word means new letters scroll, and a line that moves happens at a different time. Lines that did not change give the same result as before.
Worked examples
Step
Algorithm 1
1
Start when button A is pressed
2
Show icon house
3
Set volume to 50
4
Play sound twinkle until done
5
Show string “Hi”
Algorithm 1 starts when button A is pressed. It shows a house, sets a quiet volume of 50, plays the twinkle sound until it is finished and then scrolls the word Hi.
What changed
Algorithm 1
Algorithm 2
What happens now
the volume
50
200
the twinkle sound is much louder
the word
Hi
Hello
a longer word scrolls
show icon house
first output
last output
the house appears at the end and stays on the lights
Algorithm 2 is Algorithm 1 with three changes. Everything else is the same, so we only have to predict the three differences.
Algorithm 2 predicted result: press A, the lights stay dark while the twinkle sound plays loudly, the word Hello scrolls, and then the house appears and stays. The button and the sound are the same as before.
Class activity
Predict and perform (in pairs): one of you reads Algorithm 1 aloud, line by line, and the other performs it: stand still until the cue (the input), make a house shape with your arms for the icon, hum a tune quietly and then say the word. Now read Algorithm 2. Before you act, say what you expect to be different. Then act it out. Was your prediction right? Which of the three changes was the hardest to notice?
Practice
1
Algorithm X: 1 Start when button A is pressed. 2 Show icon heart. 3 Play sound happy until done. Algorithm Y: 1 Start when button B is pressed. 2 Show icon heart. 3 Play sound sad until done. Which lines changed? What is different when the program runs?
Lines 1 and 3 changed. In Y you press button B, not A, and the sound is sad, not happy. The heart is the same.
2
Algorithm 1 is changed so the volume is 0 and the word is Bye. Predict the outcome.
You press A and the house appears. The twinkle sound runs, but you hear nothing, because volume 0 is silent. Then the word Bye scrolls.
3
How many lines are different in these two algorithms?
Two lines: the button (A to B) and the pause (1000 ms to 3000 ms, so 1 second becomes 3 seconds).
4
Why can we predict what a changed algorithm does without running it?
Because lines that are the same give the same result, so we only have to think about the lines that changed.