A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Debugging Basics

Middle & High School Depth 74 in the knowledge graph ☐ I know this ☆ Set as goal
590topics build on this
324prerequisites beneath it
See this on the map →
Conditional StatementsDefining and Calling Functions→→Algorithm Design BasicsError Handling and Exceptions
debugging errors tracing print debugging breakpoints

Core Idea

Debugging is the systematic process of identifying and fixing errors (bugs) in code. Syntax errors prevent the program from running and are reported by the interpreter or compiler with a location. Runtime errors occur during execution (e.g., division by zero, index out of bounds). Logic errors produce wrong output without crashing. Effective debugging strategies include reading error messages carefully, adding print statements to inspect values, tracing execution by hand, and using a debugger with breakpoints.

How It's Best Learned

Deliberately introduce errors into working programs and practice diagnosing them. Use a debugger to step through code line by line. Practice reading stack traces to locate the source of runtime errors.

Common Misconceptions

Explainer

Every programmer spends significant time debugging, and the difference between a frustrating hour and a productive five minutes usually comes down to approach. Debugging is not random guessing — it is a systematic process that resembles the scientific method. You observe a symptom (wrong output, a crash, unexpected behavior), form a hypothesis about what is causing it, design a test to confirm or refute that hypothesis, and repeat until you find the root cause. The worst debugging habit is changing code at random hoping something will work. The best habit is asking: "What do I *expect* to happen at this line, and what is *actually* happening?"

The first skill is reading error messages carefully. A syntax error message tells you what the interpreter expected and where it got confused. A runtime error like `IndexError: list index out of range` tells you both the type of problem (you accessed a position that does not exist) and the exact line it occurred on. A stack trace shows you the chain of function calls that led to the error — read it from bottom to top. The bottom line shows *what* went wrong; the lines above show *how the program got there*. Often the real bug is not at the crash site but several function calls earlier, where bad data was created and then passed along.

Print debugging is the simplest and most universally available technique. When you do not understand what your code is doing, add print statements to display variable values at key points: before and after a loop, at the start of a function, right before the line that crashes. Compare what the variables *actually contain* to what you *expected*. This narrows the problem from "somewhere in my program" to "between line 14 and line 22, the variable `total` becomes negative when it should not." Once you have found the mismatch between expectation and reality, the fix is usually clear.

For more complex programs, a debugger is far more powerful than print statements. A debugger lets you set breakpoints — lines where execution pauses so you can inspect every variable in scope, step through code one line at a time, and watch how state changes. Most IDEs have built-in debuggers with graphical interfaces. Learning to set a breakpoint and step through a loop iteration by iteration transforms your understanding of how code executes. Combined with the habit of forming hypotheses ("I think the loop runs one extra time"), the debugger lets you verify or disprove your theory in seconds rather than peppering your code with print statements and re-running it repeatedly.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Debugging Basics

Longest path: 75 steps · 324 total prerequisite topics

Prerequisites (2)

Leads To (2)