...

/

Finding Bugs Using a Debugger

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:

Press + to interact
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.

Press + to interact
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 if statement, where we’re checking if the age is greater than 12 or less than 20. The error here is that we used or.

When ...