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

Fixing mistakes

Week 9 · Unit 4: Be an animator · 3 lessons
1

Bugs in algorithms

Computing 2: Unit 4, pp. 54–65
GoalSay what a bug is and fix a bug in an algorithm.
New words
bug · a mistake in an algorithm or a programdebug · to find a bug and fix itswap · to change the places of two steps
Explanation

A bug is a mistake in an algorithm or a program. Often the steps are in the wrong order. To fix it, we read the steps one by one and ask: does this step make sense here? When we find the bug, we fix it. We might swap two steps. Then we read the whole algorithm again to check. Finding and fixing bugs is called debugging. In the picture, a flower cannot open before the shoot grows, so steps 2 and 3 swap.

StepWith a bugFixed
1Plant the seedPlant the seed
2A flower opensA shoot grows
3A shoot growsA flower opens
Worked examples
StepWith a bugFixed
1Dig a holeDig a hole
2Cover the seed with soilPut the seed in the hole
3Put the seed in the holeCover the seed with soil
Planting a seed. In the first column, the seed is covered with soil before it is in the hole. Steps 2 and 3 are in the wrong order. We swap them and the algorithm is correct.
StepAlgorithm
1Take two slices of bread
2Eat the sandwich
A bug is not always in the order. A step can also be missing. Algorithm: 1 Take two slices of bread. 2 Eat the sandwich. The step “put the filling between the bread” is missing.
To debug, test the algorithm, find the bug, fix it and test it again. If it still does not work, look for another bug.
Class activity

Bug hunt: write a five-step algorithm for a daily job, such as getting ready for bed. Then copy it out again but swap two steps by mistake. Give this second version to a family member. Can they find the bug and fix it? Then let them make a bug for you to find. Which kind of bug was easier to find: a swapped step or a missing step?

Practice
1
The sun algorithm says: 1 The sun rises. 2 The sun sets. 3 The sun is high in the sky. What is the bug?
StepAlgorithm
1The sun rises
2The sun sets
3The sun is high in the sky
2
What is a bug?
3
An egg algorithm says: 1 Put the egg in water. 2 Boil it for eight minutes. 3 Eat the egg. Which step is missing?
4
Which two steps must swap places?
1 egg2 chick3 egg cracks