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

Numbers on the board

Week 18 · Unit 8: Be an innovator · 3 lessons
3

Order matters

Computing 6: Unit 8, pp. 106–123
GoalExplain what a sequence is, and find and fix a bug caused by instructions in the wrong order.
New words
sequence · instructions that run one after the other, in orderon start · a micro:bit block that runs its blocks once, when the program startsbug · a mistake in a programtrace · to follow a program step by step and write down the values of its variables
Explanation

Every program uses sequence. The instructions run one after the other, from the top to the bottom. The order matters. A variable must be given its value before another block uses it. In micro:bit blocks a new variable starts at 0. If a calculation or a show block comes before the values are set, it uses 0, and the answer is wrong. The program does not warn us. It simply follows the order that we gave it. To find this kind of bug, we trace the program and write down the value of each variable after every block. To fix it, we move the blocks into the correct order and trace it again.

setpriceto5setcountto3setcosttoprice×countshow numbercoston start
Worked examples
After the blockpricecountcostBoard shows
start000—
set price to 5500—
set count to 3530—
set cost to price × count5315—
show number cost531515
Trace the program. The variables start at 0. Each block changes only one thing. At the end the board shows 15.
setpriceto5setcountto3show numbercostsetcosttoprice×counton start
Now the show block comes before the calculation. The board shows the value that cost has at that moment, which is still 0. Then cost is worked out, but nobody sees it. The program ran with no error, and the answer on the display is wrong.
Order matters in everyday jobs too. To mix paint, we pour the water, add the paint and then stir. If we stir first, there is nothing to mix. A computer cannot guess what we meant. It only does the steps in the order that we write them.
Class activity

Strips in order (no computer): write these blocks on strips of paper: on start, set price to 5, set count to 3, set cost to price × count, show number cost. (Use your own numbers if you like.) Shuffle the strips. A partner puts them in the right order and traces the variables on paper. Then try some changes. What does the board show when the set cost strip comes before the set count strip? Try moving show number to the top. Write down the result of each change and say why it happens. If you have a computer or tablet, build the program in a micro:bit simulator and compare.

Practice
1
What does the board show? How can the program be fixed?
setbilltotickets×faresetticketsto4setfareto6show numberbillon start
2
Put the blocks in the right order so that the board shows 18.
show numberareasetareatow×hsethto3setwto6on start
3
What does the board show at the end? Explain.
setscoreto10changescoreby5setscoreto0show numberscoreon start
4
Why can instructions in the wrong order give a wrong answer without any warning?