2.1.13 Debugging Loops

Overview

A breakpoint inside a Zoo loop can show how its counter changes from one iteration to the next. Watch the value at the condition, in the body, and after the update. Step over rather than restarting the program for each pass.

int portions = 0;
while (portions < 3)
{
    portions++;
}
Inspect Each Iteration
Inspect Each Iteration. A breakpoint inside a Zoo loop can show how its counter changes from one iteration to the next.
Inspect Each Iteration
  • New
  • Changed
  • Removed

Pause on the while condition. In Locals, portions starts at 0. Step through the increment and return to the condition; repeat until portions reaches 3. The final condition check is false, and the next highlighted line is after the loop.

Use Step Over for the simple increment so you can focus on counter behavior without entering library code. On the last pass, check that portions becomes 3, then watch the condition evaluate false. If the program pauses on the body more times than expected, inspect both the initial value and the comparison operator for an off-by-one error.

Trace the Outcome

If the debugger appears to loop forever, inspect whether the updated variable is the same variable used in the condition. Also check whether a branch skips the update. Record three iterations and the final false check.