What is a computer bug?
A mistake or error in an algorithmA precise set of ordered rules or instructions that can be followed by a human or a computer to achieve a task. or computer programA sequence of instructions written in a coding language that a computer can understand. is called a bugAn error or mistake in a computer program that affects the outcome. .
We know we have a bug when our computer programs don't work as expected.
They might be simple typing errors in the program codeA language that a computer can understand.or more serious problems in the order or sequence of the code.
They may even be errors that we made when we first organised our instructions into an algorithm.
When we find the bugs, we can correct them. This is called debugging.
Checking for bugs as we go
Debugging is a very important part of writing a good algorithm or computer program and it works best when we check for bugs as we go along, rather than at the end.
Let's think about an everyday example, like writing a set of directions for your friend to get to the science museum.
The first part of solving this problem is to break it down.
- Tell your friend the name of the museum, and what area it is in.
- Find their address on a map.
- Work out the quickest way to get from their house to the science museum.
- Write it all down for your friend to send to their parent or guardian in a message.
You could make an error in any of these steps.
You might:
- type the name of the park incorrectly
- use an old address for your friend and start in the wrong place
- pick a route that has been changed by a new building site
- make a spelling mistake in the text you send them
A bug in any of these stages will end up with your friend being very lost so it's important to check each stage carefully.
Watch: All about debugging
This film from 91热爆 Teach explains more about debugging.
Bugs in a computer program
When we create a computer program to solve a problem or complete a task, we have a good idea of what we want to happen when the finished program is run. We know what the solution or outcome looks like.
If the outcome is NOT what we expected, or the program stops working altogether then we probably have a bug somewhere in our program.
Even if you check your algorithms and programs for bugs as you go along, some might still end up in the finished program.
Hungry Alien
You have just created a new computer game called Hungry Alien.
In the game, when you press the arrow keys on the keyboard, the main character - a hungry looking alien - moves around the screen collecting food.
Here is the code you have written to control the Hungry Alien character:
You share the finished game with your friends, but after they play you start getting messages saying the game doesn't work - oh no!
Finding the bug
The best place to start is by remembering what you want to happen when the program is run. You want the alien to move when the arrow keys are pressed.
In the game the alien character did not move when the arrow keys were pressed.
It makes sense then to start debugging by looking at the section of the code that controls the alien character movement.
Can you spot the mistake in the code?
Aha! You found the bug!
The number of steps is set to 0, so that's why the character didn't move!
Persevering
Debugging is an important part of learning to write algorithms and computer programs.
We need to check our algorithms and programs as we go along, rather than at the end.
We must keep on checking and testing until our program behaves in the way we planned, and we can complete the task or solve the problem we started with.
Are bugs always bad?
Making mistakes, spotting and correcting them is an important part of learning for everyone.
This means that debugging our algorithms and programs can actually be helpful if it means we are trying out new ideas and challenging ourselves.
There will always be bugs to find. Checking for them and fixing them helps us become better at writing computer programs.
Look below to see the correct code.
The number of steps needs to be set to 1 so the character will move.
Activity: Quiz
Computing - Dance Mat Typing. gameComputing - Dance Mat Typing
Build and test your computing skills with different levels of touch type challenges
More on Computer science
Find out more by working through a topic
- count9 of 24
- count10 of 24
- count11 of 24
- count12 of 24