Skip to main content

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

ControlWhat it doesUse it when
BreakpointPauses the program before a line runsYou know roughly where things go wrong
Step overRuns the current line, including any method it calls, and stops on the next lineYou trust the method being called
Step intoGoes inside the method called on this lineThe bug might be inside that method
Step outFinishes the current method and stops where it returnsYou went into a method you did not need
Run to lineRuns until a chosen line (stopping at any breakpoint on the way)You want to skip ahead quickly

A simple method that finds most bugs

  1. Reproduce it: find a small input that always shows the bug.
  2. Predict: write down what each variable should be at a key line.
  3. Pause there: put a breakpoint on that line and start debugging.
  4. Compare: check the real values against your prediction.
  5. Narrow down: if they already differ, move the breakpoint earlier; if they match, move it later.
  6. 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.

Buggy sumjava
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.

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.

Java Programming course: syllabus and 1:1 mentoringCore Java from zero to OOP, collections, streams, files and threads.