AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Show it and count it

Week 20 · Unit 8: Be an innovator · 3 lessons
1

Read an algorithm

Computing 5: Unit 8, pp. 104–118
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.

PartQuestion to askClue
InputWhat starts the program?a button, a shake, a touch
ConditionWhat is the true or false question?it comes after ‘if’
Outcome if trueWhat happens when the answer is yes?it comes after ‘then’
Outcome if falseWhat happens when the answer is no?it comes after ‘else’
Worked examples
StepAction
1Start when button A is pressed
2Create a variable called Thirst
3Set Thirst to a random number from 1 to 4
4If Thirst = 4, then
4.1Show the word ‘Water’
5Else
5.1Show a heart icon
6Stop
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.
BlockGroup
on logo touchedInput
show numberBasic
pause (ms)Basic
change [score] by 1Variables
pick random true or falseMath
trueLogic
Each step of the algorithm becomes a block. Each block lives in a group, so we can find it.
Plant helpersetThirsttopick random1to4show stringWatershow iconheartifThirst=4thenelseon buttonApressed
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.
StepAction (wake-up bell)
1Start when the logo is touched
2Create a variable called Pick
3Set Pick to a random number from 1 to 5
4If Pick = 3, then
4.1Show the word ‘Ding’
5Else
5.1Clear the screen
6Stop
2
Which MakeCode group holds each block? (a) light level, (b) show number, (c) change [score] by 1, (d) true
3
Button B is pressed three times. The random numbers are 3, 2 and 1. What does the board show each time?
Bonussetbonustopick random1to3show stringBonusshow numberbonusifbonus=2thenelseon buttonBpressed
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?
Starttype a numbernumber = 8?yesnoshow a star badgeshow Have another goEnd