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

Term 2 review

Week 21 · Review: Term 2 · 3 lessons
3

Review: micro:bit programs

Computing 6: Units 5–8, pp. 68–123
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.

ConstructExample blockWhat it does
sequenceblocks one under anotherthey run in order, from the top
selectionif … then … elsechooses one of two paths
iteration (count)repeat 4 timesrepeats a set number of times
iteration (condition)while n < 50repeats while the condition is true
Worked examples
setpocketto40changepocketby-12repeat3timesshow iconyesshow numberpocketifpocket≥5thenelseon buttonApressed
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.
SteppocketCondition pocket ≥ 5Board shows
start40——
round 128——
round 216——
round 34——
if block4false4
A trace table follows the variable round by round, and it shows the answer to the condition at the end.
scoreExpectedKind of test
5nonormal
20noboundary
21yesboundary
60yesnormal
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?
setxto3setxtox×2whilex<30show numberxon buttonApressed
2
What does the board show?
settotalto0changetotalby6repeat4timesshow iconyesshow number30−totaliftotal≥25thenelseon buttonApressed
3
This program should count down and finish by showing 0. It never stops. Explain why and fix it.
setnto10changenby3whilen>0show numbernon buttonApressed
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.