Week 29 · Unit 12: Be a problem solver · 3 lessons
1
Two outputs, one board
Computing 5: Unit 12, pp. 159–170
GoalUse forever with if … then … else so that a micro:bit board shows one output or the other.
New words
forever · a block that repeats the blocks inside it again and againcondition · a test that is either true or falseclear screen · a block that switches off all 25 lightsbutton A · one of the two buttons on the front of the board
Explanation
A micro:bit board checks things very fast, many times in every second. A forever block makes it repeat. Inside the forever block we can put an if … then … else block. The board checks the condition each time round. When the condition is true, it carries out the ‘then’ part, and when it is false it carries out the ‘else’ part. So the board always shows one of two outputs. An if … then block with no else is not enough for this. After the condition turns false, nothing takes the old picture away, and it stays on the lights.
Worked examples
When button A is pressed, the answer board shows the ‘yes’ icon, a tick. When A is not pressed, it shows the ‘no’ icon, a cross made of two diagonal lines. A red dot is a light that is on.
Check
1
2
3
4
5
6
A pressed?
no
yes
yes
no
no
yes
Board shows
cross
tick
tick
cross
cross
tick
Here is the board over six checks. The forever block makes the checks happen again and again. The condition is true only while the button is held down.
Compare two programs. The one on the left has no else part. It shows a tick when A is pressed, but nothing ever switches the tick off, so after the first press the tick stays for ever. The one on the right has an else part with a clear screen block, so the lights go off when the button is not pressed.
Class activity
Be the board (pairs, two paper cards with a tick and a cross drawn on a 5 × 5 grid). Player A is the board and holds up a card. Player B is the button and says ‘pressed’ or ‘not pressed’ at random, 10 times. Version 1: A holds up the tick for ‘pressed’ and the cross for ‘not pressed’. Version 2: A holds up the tick for ‘pressed’ and does nothing else. What does the audience see in version 2 after B says ‘not pressed’? Why does version 1 need an else part?
Practice
1
Button B is pressed, pressed, not pressed, pressed, not pressed in five checks. What does the board show each time?
Heart, heart, house, heart, house.
2
Button A is pressed once and then let go. What does the board show after that? How can you fix the program?
The happy face stays on the lights, because nothing switches it off. Add an else part with a clear screen block.
3
Write the algorithm for a board that shows a heart while button B is held down and switches all the lights off when it is not.
1 Repeat forever. 2 If button B is pressed, then show the heart icon. 3 Else clear the screen.
4
True or false? (a) A forever block checks its condition only once. (b) The else part runs when the condition is false. (c) With only an if block, an old picture can stay on the lights.