How to debug code step by step (Java, Python, C, C++)
7 min readUpdated
Debugging is the skill of finding out why a program does something different from what you expected. Instead of guessing, you pause the program at the right place and watch what really happens, line by line. This guide explains the debugger controls every IDE shares and a simple method that finds most bugs.
Why stepping beats print statements
Print statements show you a value at one moment. A debugger lets you stop anywhere, look at every variable, the call stack and the objects in memory, then move one step and look again. For loops, recursion and references it is much faster.
The five controls you need
| Control | What it does | Use it when |
|---|---|---|
| Breakpoint | Pauses the program before a line runs | You know roughly where things go wrong |
| Step over | Runs the current line, including any method it calls, and stops on the next line | You trust the method being called |
| Step into | Goes inside the method called on this line | The bug might be inside that method |
| Step out | Finishes the current method and stops where it returns | You went into a method you did not need |
| Run to line | Runs until a chosen line (stopping at any breakpoint on the way) | You want to skip ahead quickly |
A simple method that finds most bugs
- Reproduce it: find a small input that always shows the bug.
- Predict: write down what each variable should be at a key line.
- Pause there: put a breakpoint on that line and start debugging.
- Compare: check the real values against your prediction.
- Narrow down: if they already differ, move the breakpoint earlier; if they match, move it later.
- Fix and re-run: change the code and debug again with the same input.
Example: an off-by-one bug
This method should add the numbers 1 to n, but sum(5) returns 10 instead of 15.
static int sum(int n) {
int total = 0;
for (int i = 1; i < n; i++) {
total += i;
}
return total;
}Put a breakpoint on total += i; and step through. You will see i go 1, 2, 3, 4 and then the loop ends: it never reaches 5. The condition should be i <= n. Watching i change makes the bug obvious in seconds.
Common mistakes when debugging
- Changing many things at once, so you cannot tell which change fixed it
- Stepping into library code you do not need to read (step over or step out instead)
- Debugging with a big input; shrink it first
- Not reading the error message and line number before starting
Try it in your browser
You can practise all of this in the browser with the MentorNxt online debugger (breakpoints and step controls for Java, Python, C and C++), from the Free tools menu.
Frequently asked questions
What is a breakpoint?
A marker on a line that tells the debugger to pause the program just before that line runs, so you can look at the variables.
What is the difference between step over and step into?
Step over runs the whole line, including any method it calls, and stops on the next line. Step into goes inside the called method so you can follow it line by line.
Can I debug Java or Python online without an IDE?
Yes. MentorNxt's online debugger runs in the browser with breakpoints and step controls for Java, Python, C and C++.
Is debugging asked about in interviews?
Often indirectly: interviewers watch how you find and fix a bug in your own code. A calm, step-by-step method makes a strong impression.
Learn it step by step
Try it yourself
Write and run Java code in the free online compiler, or press Visualize to watch it run line by line. Nothing to install.
Practise this, step by step
Short lessons with quizzes, practice and interview questions, from your first program to job-ready.