AI Features

Finding Bugs Using a Debugger

Learn how to use a debugger to find bugs.

We'll cover the following...

What is a debugger?

A debugger is a tool that can help us see what happens when a program is running. As we’ve already mentioned, some bugs can be hard to find and understand just by running the program. Often, we’ll discover a strange behavior in the program, but it might not be obvious what the reason behind this behavior is.

A debugger is an application that’s tailored for a particular programming language and can be used to pause the application at a specified code line. At this point, we can inspect what values all the variables have.

We can also resume the execution of the program or execute it one line at a time to see what happens. Let’s try using a debugger. To do this, we first need to pick a language and then write a small program that contains a logical error. We can take one of the errors that we previously looked at:

if age > 12 or age < 20 then
...
end_if

In this example, we have accidentally used or instead of and.

Let’s write this program in Python.

Python 3.10.4
age = 17
if age > 12 or age < 20:
print("You are a teenager")
else:
print("You are not a teenager")

On line 1, we declare a variable called age and assign the value 17 to it. Then comes our ...