Algorithms and debugging
Learn about programming and debugging.
This lesson includes:
- two videos to help you understand debugging
- three activities to try out
Programmers write computer programs in a language we call code. However, sometimes there are problems and the program doesn't work properly. These are known as ‘bugs’.
A program can be made up of lots of lines of code and it is normal for new programs to have some bugs.
An important part of programming is testing your program and 'debugging' (which means removing the bugs).
Watch this short film to find out more about what a bug is.
Different types of bug
An error caused by something typed in the code – it could be a spelling mistake.
The computer is able to carry out the instructions but the result isn't what the programmer intended or the user expects.
In this short film find out how to fix mistakes you might make when writing code.
Finding these mistakes and getting rid of them is really important and it is known as 'debugging'.
Let's imagine you have just created a new computer game in Kodu or Scratch. In the game, when you press the arrow keys your character moves around the screen trying to avoid an enemy - in this case an evil frog.
You want to show everybody your game because it looks great but you decide to check it works properly first.
When you run your program, the evil frog starts moving towards you but when you press the arrow keys, nothing happens. There must be a bug!.
Where do you start?
Do you look at all the programming for your game? Or do you look at the part of the program that isn't working properly?
Both options would work but a good debugging strategy would be to look for the part of the program that isn't working properly.
Rather than going through every individual line of code searching for an error, computer programmers try to think logically about where the problem might have occurred.
This is much more efficient.
You've programmed a toy car to drive around a map, but when it reaches a house the car continues to drive through it!
Does your program have a syntax bug or a logical bug?
Your program has a logical bug.
A logical bug means that the program runs but doesn't do what the programmer intended. Here, the car is not turning when it is meant to.
A syntax bug is an error in the code which usually stops a program from working.
The car is moving, it’s just not going where it should... so the problem is probably not a syntax bug.
Try this World Cup algorithm programming adventure.
Plan a route through the grid to the World Cup. Write instructions using forward, left or right turn and how many degrees.
Then work with a partner to practise testing and debugging your algorithm.
Explore more computing resources with Teachit Primary.