Inspecting Variables and Expressions in the Debugger
You'll learn how to examine and track data states at runtime to understand exactly what your code is doing.
The Core Idea
Once you've paused execution at a breakpoint, the debugger lets you peek inside your program's memory. Think of it like freezing time in a movie—you can now examine every character's position, what they're holding, and even ask "what if?" questions by evaluating new expressions on the spot.
Viewing Variable Values
When execution pauses, your debugger displays the current values of local variables, function parameters, and often global state. Instead of adding print statements everywhere (as you learned with strategic logging), you simply hover over a variable name or open the variables panel to see its current state.
This is invaluable when debugging because you can immediately verify whether a variable holds the value you expected at that exact moment in execution.
Evaluating Expressions
Beyond passive inspection, debuggers let you actively evaluate expressions using the current program state. You can type calculations, call functions, or check conditions without modifying your source code. It's like having a scratchpad that operates with your program's live data.
For example, if you're debugging a calculation error, you can test alternative formulas immediately to see which produces the correct result.
Watch Windows
Watch windows let you monitor specific variables or expressions continuously as you step through code. Add a variable to the watch list once, and the debugger updates its value automatically at each pause point—perfect for tracking how data changes during a loop or across function calls.
Key Takeaway: Debuggers transform debugging from guessing into observation—inspect variables to see actual values, evaluate expressions to test hypotheses, and use watch windows to track changes over time, all without modifying your code.