Concepts / Debugging Loops and Tracing Execution

Debugging Loops and Tracing Execution

Iteration is the repeated execution of statements in a loop or recursive function, and understanding loop terminology is essential for writing correct loops.

  • Programming
Interactive lab

Try it: Loop Tracer

How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.

How it works

  1. Initialise the accumulator before the loop.
  2. Each iteration, the loop variable takes the next value from the list.
  3. An if inside the loop decides whether to update the accumulator.
  4. After the last item the loop ends and the accumulator holds the answer.

Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.

Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.

Educational simulation

Loading the simulation…

Interactive lab

Try it: Debug by Tracing Execution

How to debug a program that runs but gives the wrong answer: compare expected and actual output, trace the lines in the order they really run with a variable watch table (or an inserted debug print), find the first line where a value goes wrong, then choose a fix and re-run to check it.

How it works

  1. State the expected output from the specification, run the program and compare: a difference is the evidence you debug from.
  2. Trace in execution order, not file order: loops repeat lines, if/elif picks one branch, and the watch table records every variable before each line runs.
  3. An inserted print('debug: ...', value) shows the same state in the output; it must not change what the program itself prints.
  4. Look for the first place a value differs from what it should be (an off-by-one range, > instead of >=, a total reset inside the loop, two names for one list, a str from input()). Where the wrong value shows up is only the symptom.
  5. Change one thing with a reason, re-run, and compare again: a fix that makes an error disappear can still leave the output wrong.

Default run (14 steps): Sum 1..n: Add up the whole numbers from 1 to n (n included) and print the sum. Press Play to trace it line by line. … Finished, but the output is wrong: got sum of 1 to 5 is 10, expected sum of 1 to 5 is 15.

Simplified: The lab's own small Python interpreter runs fixed, hand-written programs (you pick the program, the fix and a value; your text is never executed). Every program the lab can show was run in CPython 3.12 and the lab reproduces its output, its traceback text and its line-by-line trace. Six short programs, one editable value each; input() receives the value you type in the lab. Lines starting with "debug:" are ignored when comparing the output with the expected output.

Educational simulation

Loading the simulation…

Why Loop State Matters

A loop repeats a group of statements. Each repetition is an iteration. To debug a loop, do not look only at the statements inside the loop; track the values of the variables that determine what happens next. Initialization gives those variables starting values, the loop body uses or changes them, and an increment or decrement updates them for another possible iteration.

Loop conditioncheck stateLoop bodyrepeated statementsVariable updateincrement or decrementLoop stopscondition is falsetrueafter bodycheck againfalse
How does control return to the loop condition after one iteration, and what executes again on the next pass?

The Iteration Control Sequence

A useful tracing sequence has four parts. First, initialization assigns a starting value before the loop begins. Next, the loop condition is checked. If the condition allows another iteration, the loop body executes. Finally, an update changes one or more loop variables, and control returns to the condition. The same sequence repeats until the condition is false.

Initializationstarting valueCondition checkcontinue or stopLoop bodyone iterationIncrementincrease by a fixed amountDecrementdecrease by a fixed amountthencondition allows iterationfixed increasefixed decreasecheck againcheck again
What is the execution order of initialization, condition checking, the loop body, and increment or decrement operations?

Counters and Accumulators

A counter and an accumulator both change during a loop, but they represent different ideas. A counter tracks how many times an event occurs. It increases by 1 or by another fixed amount on each iteration. An accumulator builds a running total by adding values that may vary from one iteration to the next. Many counters and accumulators begin at 0, although their purposes remain different.

Countertracks occurrencesFixed increase1 or another fixed amountAccumulatorbuilds a totalVarying valuesadded to running totalupdates withcombines
How does a counter track iteration quantity differently from an accumulator that combines changing values?
Variable roleWhat it representsTypical update
CounterHow many times an event occursIncrease by 1 or another fixed amount
AccumulatorA running totalAdd a value that can vary by iteration

The key difference is what the changing variable means.

A State Trace Across Iterations

Consider a loop with two variables. The counter starts at 0 and increases by 1 after each iteration. The accumulator also starts at 0, but it adds the changing values 4, 2, and 5. The table shows the state after each iteration. This is a generated example for practicing the tracing method.

Point in executionCounterAccumulatorWhat changed
Before the loop00Both variables are initialized
After iteration 114The counter increases by 1; 4 is added
After iteration 226The counter increases by 1; 2 is added
After iteration 3311The counter increases by 1; 5 is added

A generated state trace separating fixed counting from changing-value accumulation.

Startcounter 0, total 0Iteration 1counter 1, total 4Iteration 2counter 2, total 6Iteration 3counter 3, total 11Endcondition is falseadd 4, count by 1add 2, count by 1add 5, count by 1condition becomes false
What happens to each loop variable after initialization, during every iteration, and when does the loop stop?
Worked example

Separating the Two Meanings

A loop processes three changing values: 4, 2, and 5. Track a counter and an accumulator, both initialized to 0.

  1. 1
    Initialize

    Set the counter to 0 because no iterations have been completed, and set the accumulator to 0 because no values have been added.

  2. 2
    First iteration

    Increase the counter by 1, giving 1. Add 4 to the accumulator, giving 4.

  3. 3
    Second iteration

    Increase the counter by 1, giving 2. Add 2 to the accumulator, giving 6.

  4. 4
    Third iteration

    Increase the counter by 1, giving 3. Add 5 to the accumulator, giving 11.

Result

After three iterations, the counter is 3 and the accumulator is 11.

Recognizing Infinite Loops

An infinite loop occurs when the loop condition never becomes false. A common cause is a missing update: the variable used by the condition keeps its old value. Another cause is an incorrect update, such as changing the variable in a direction that does not move it toward the stopping condition. During debugging, identify the variable controlling the condition and check its value after every iteration.

Loop variableinitial valueConditionremains trueLoop bodyexecutes againRepeatno stopping pointMissing updatevalue does not changeIncorrect updatemoves away from stoppingtesttruepossible causepossible causecondition stays truecondition stays truetest again
How does control flow continue when the loop condition never becomes false, and which missing or incorrect update causes the problem?
  • Common mistake

    Using a loop variable before giving it a starting value.

    What it looks like

    The variable that controls the loop has no initialization.

    Why it fails

    The loop begins without a defined starting state, making the loop's behavior unreliable.

    Do this instead

    Initialize each loop variable before the loop begins.

  • Common mistake

    Forgetting to update the variable that controls the condition.

    What it looks like

    The loop body runs, but the controlling variable keeps the same value.

    Why it fails

    The condition can remain true indefinitely.

    Do this instead

    Check that every iteration performs the required increment or decrement.

  • Common mistake

    Confusing an accumulator with a counter.

    What it looks like

    A variable intended to count events is updated by adding changing values.

    Why it fails

    Its final value no longer represents the number of events.

    Do this instead

    Use a fixed update for a counter and varying additions for an accumulator.

  • Common mistake

    Updating in the wrong direction.

    What it looks like

    A decrement is used when the loop variable needs to increase toward the stopping condition.

    Why it fails

    The variable may move farther from the value that would make the condition false.

    Do this instead

    Trace the variable after each update and verify that it moves toward termination.

Practice the Trace

EASY

A loop starts a counter at 0 and an accumulator at 0. On each of four iterations, the counter increases by 1. The accumulator receives the values 3, 1, 4, and 2 in that order. Record the counter and accumulator after every iteration. Then explain which variable is the counter, which is the accumulator, and why the loop can stop after the fourth iteration.

Hints
  • Write the initial state before any iteration: counter 0 and accumulator 0.
  • Increase the counter by the same fixed amount on every iteration.
  • Add one changing value to the accumulator on each iteration.
MEDIUM

A loop variable begins at 5. After each iteration, it is supposed to move toward a condition that stops the loop at 0. Trace what you would inspect if the loop never stops: the initial value, the condition, the update, and the value after each iteration. Decide whether the likely issue is initialization, a missing update, or an update in the wrong direction.

Hints
  • First identify the variable used by the stopping condition.
  • Check whether its value changes after the loop body.
  • Compare the direction of the update with the direction needed to reach 0.

Reliable Loop Tracing

  1. 1
    Write down the initialization value for every variable involved in the loop.
  2. 2
    Identify which variable controls whether another iteration occurs.
  3. 3
    Record the state before the first iteration and after each loop body execution.
  4. 4
    Separate fixed updates used by counters from varying additions used by accumulators.
  5. 5
    Verify that every update moves the controlling variable toward the condition becoming false.

Iteration means repeated execution. Initialization establishes the starting state, increments and decrements change variables by fixed amounts, counters measure occurrences, and accumulators build running totals from changing values. A loop is safe to continue only when its state changes in a direction that can eventually make its condition false. Tracing those state changes turns an unclear loop into a sequence of observable steps.

Key Takeaways

  • Initialization assigns starting values before a loop begins.
  • A counter tracks occurrences with a fixed update, while an accumulator builds a running total from varying values.
  • An increment increases a variable and a decrement decreases it; both can update loop state.
  • Tracing the value of a loop variable after every iteration reveals whether execution is progressing.
  • A missing or incorrectly directed update can keep the loop condition true and cause an infinite loop.

Related concepts

More concepts starting with D