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

Make it work

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

Find and fix the bug

Computing 4: Unit 8, pp. 104–122
GoalFind a mistake in a program by comparing it with the algorithm, fix it and extend the program.
New words
bug · a mistake in a programdebug · to find and fix the bugs in a programextend · to add a new feature to a programsense · one of the five ways in which people take in information
Explanation

A bug is a mistake in a program. To debug, compare the program with the algorithm one block at a time. Common bugs are the wrong button, no pause (so the picture flashes by too fast), a missing clear screen, a wrong number in a repeat block and blocks in the wrong order. Fix one bug at a time and test again in the simulator. To extend a program is to add something new, such as a second button. People have input devices too: eyes for sight, ears for hearing, skin for touch, a nose for smell and a tongue for taste. The brain is the processor.

BugWhat you noticeThe fix
wrong buttonnothing happens when you press Achange B to A
no pausethe picture flashes by too fast to seeadd a pause block
no clear screenthe old picture staysadd clear screen
wrong repeat numberthe picture shows too few timeschange the number
Worked examples
show iconheartpause (ms)1000clear screenrepeat3timeson buttonApressed
The algorithm says: when button A is pressed, show the heart for 2 seconds and clear the screen, 3 times in all. The red block is the bug. The pause is only 1 second, so it must be changed to 2 seconds.
Button A (old)show iconheartpause (ms)2000clear screenrepeat3timeson buttonApressedButton B (new)show iconduckpause (ms)2000clear screenrepeat2timeson buttonBpressed
Extending a program. The first script was already working. The second script is new: it makes button B show the duck icon 2 times.
A person works like a board. The eyes are a sensor, the brain is the process and the legs are the output.
Class activity

Debug detectives (in pairs): each person writes a small algorithm and the matching program on paper, with blocks on separate cards. Hide one bug, for example a wrong button, a missing pause or a repeat number that does not match. Swap. The detective traces the program step by step with a finger and finds the bug. Then fix it and say which type of bug it was. Finish by naming one thing that each of your five senses noticed today.

Practice
1
The algorithm says the house is shown for 3 seconds after button A is pressed. In the program the house never seems to appear. What is the bug?
show iconhouseclear screenon buttonApressed
2
The algorithm says to show the diamond 4 times in all. Find the bug and fix it.
show icondiamondpause (ms)1000clear screenrepeat3timeson buttonApressed
3
Which sense is the input for each of these? A smelling smoke, B hearing a bell, C feeling a hot cup.
4
Extend the program in question 2 so that it also shows the diamond 4 times when button B is pressed. What do you add?