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

Micro:bit inputs and jobs

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

Predict, test and debug

Computing 6: Unit 8, pp. 106–123
GoalPredict the result of a program by hand, test it, compare the two and find and fix the bug.
New words
predict · to work out by hand what a program will show before it runsdebug · to find and fix a bug in a programactual result · what the program really shows when it runsfix · a change that removes a bug
Explanation

Programs seldom work the first time, so we follow three steps. First, predict. Choose some test data and work out by hand what the program should show, and write it in a table. Second, test. Run the program, or trace it carefully, and write the actual result next to the prediction. Third, debug. If the two are different, look for the cause. Check the order of the blocks, the operators, the numbers, the variable names and the conditions. Change one thing at a time, and then run all the tests again, because a fix can break something else. Never skip the prediction. Without it, we cannot tell whether an answer is right.

Worked examples
setpriceto13show number20−priceshow iconnoifprice≤20thenelseon buttonApressed
The change-giver program takes a price. If the price is 20 or less it shows the change from a 20 note, if not it shows no. The price is 13. The prediction is 20 − 13 = 7, and the program shows 7.
pricePredictedActualResult
137−7FAIL
2000pass
25nonopass
A bug: the programmer wrote price − 20 instead of 20 − price. The prediction and the actual result are different for the price 13, so the test finds the bug. The test with the price 20 passes, because price − 20 and 20 − price are both 0 when the price is 20. This is why we use several values.
SymptomLikely cause
the board shows 0 when it should nota show block comes before the set blocks
the loop never stopsnothing in the loop changes its variable
only the edge value fails> used instead of ≥ (or < instead of ≤)
the answer is the wrong sizethe wrong operator, such as + instead of ×
Clues help us to find a bug. Match the symptom to the likely cause, and then check the blocks.
Class activity

Bug swap (no computer): each player writes a short program of 4 or 5 blocks on paper, and a prediction table for three inputs. Then the player secretly changes one thing to add a bug, such as an operator, a number or the order of two blocks. The partner traces the program for the three inputs and compares it with the table. Where do the results differ? Find the bug and fix it. How many tests did it take to find it? Swap roles. If you have a computer or tablet, build both versions in a micro:bit simulator and compare the board.

Practice
1
Predict what the board shows when pocket is 15, 12 and 9.
setpocketto15show numberpocket−12show iconnoifpocket≥12thenelseon buttonApressed
2
This program should show the average of 8 and 4, which is 6. It shows 10. Find the bug and fix it.
setato8setbto4show numbera+b÷2on buttonApressed
3
A lift takes up to 6 people. Which test fails, and what is the bug?
peopleExpectedActual
3yesyes
6yesno
7nono
4
Put these steps in the correct order: fix the bug, predict the results, find the cause of the bug, run the program, compare the results.