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 FunctionsAlgorithm 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsDebugging Basics

Longest path: 75 steps · 324 total prerequisite topics

Prerequisites (2)

Leads To (2)