GoalName the input, the condition and the two outcomes in an algorithm, and say which MakeCode group a block belongs to.
New words
algorithm · a list of steps that solves a probleminput · data that goes into a program, such as a button press or a sensor readingoutcome · what a program does as a result of a decisiongroup · a set of blocks in MakeCode that belong together, such as Input or Logic
Explanation
An algorithm is a list of steps for a program. When an algorithm makes a decision, four parts are worth finding. The input is what starts the program or gives it data, such as a button press. The condition is the question with a true or false answer. One outcome comes after ‘then’ and the other comes after ‘else’. In MakeCode for micro:bit, blocks are sorted into groups such as Input, Basic, Logic, Variables and Math. Knowing the group helps you find a block quickly. Reading an algorithm first makes the program much easier to build.
Part
Question to ask
Clue
Input
What starts the program?
a button, a shake, a touch
Condition
What is the true or false question?
it comes after ‘if’
Outcome if true
What happens when the answer is yes?
it comes after ‘then’
Outcome if false
What happens when the answer is no?
it comes after ‘else’
Worked examples
Step
Action
1
Start when button A is pressed
2
Create a variable called Thirst
3
Set Thirst to a random number from 1 to 4
4
If Thirst = 4, then
4.1
Show the word ‘Water’
5
Else
5.1
Show a heart icon
6
Stop
This is the algorithm for a plant helper. The input is button A being pressed. The condition is Thirst = 4. When it is true the board shows the word Water. When it is false the board shows a heart.
Block
Group
on logo touched
Input
show number
Basic
pause (ms)
Basic
change [score] by 1
Variables
pick random true or false
Math
true
Logic
Each step of the algorithm becomes a block. Each block lives in a group, so we can find it.
Here is the plant helper as blocks. Every step in the algorithm matches one block.
Class activity
Algorithm detectives (groups). Write each part of a made-up algorithm on its own paper strip, such as ‘Start when the board is shaken’ or ‘If the cup is full, then show the word Stop’. Mix the strips up. Another group sorts them into four envelopes labelled input, condition, true outcome and false outcome. Swap and check each other’s envelopes. Then invent an algorithm for a class door sign that shows ‘In’ or ‘Out’.
Practice
1
Find the input, the condition, the outcome if true and the outcome if false in this algorithm.
Step
Action (wake-up bell)
1
Start when the logo is touched
2
Create a variable called Pick
3
Set Pick to a random number from 1 to 5
4
If Pick = 3, then
4.1
Show the word ‘Ding’
5
Else
5.1
Clear the screen
6
Stop
Input: the logo is touched. Condition: Pick = 3. If true: the word Ding is shown. If false: the screen is cleared.
2
Which MakeCode group holds each block? (a) light level, (b) show number, (c) change [score] by 1, (d) true
(a) Input, (b) Basic, (c) Variables, (d) Logic.
3
Button B is pressed three times. The random numbers are 3, 2 and 1. What does the board show each time?
Press 1: the number 3, because 3 is not 2. Press 2: the word Bonus. Press 3: the number 1.
4
A science quiz asks ‘How many legs does a spider have?’ and the learner types a number. Say the condition and the two outcomes. What is shown if the learner types 6?
Condition: the number typed = 8. If true: a star badge is shown. If false: the words ‘Have another go’ are shown. 6 is not 8, so ‘Have another go’ is shown.