AI worksheet builder and 715 free math exercise generators — no subscription or registration required. Optional tips help keep them free. Tip →

Article

Trace Nested Loops

Go to Math Operation

Tracing nested loops

Nested loops mean one loop runs inside another. To solve tracing questions, you must follow the program step by step and record what happens after each repetition.

1. Track the outer loop first

Start with the first value of the outer loop. For that value, run the entire inner loop before moving to the next outer value. This is the key idea: the inner loop resets each time the outer loop repeats.

2. List values in order

Write down the values of any counters, printed outputs, or updates in the exact order they occur. A table with rows for the outer loop and columns for the inner loop can help avoid skipping steps.

3. Simplify the final result

If the exercise asks for a final expression, combine like terms or reduce repeated additions after tracing all iterations. If the answer is a list of outputs, keep the order exactly as produced by the code.

4. Check your trace

Make sure each loop stops at the correct limit, the inner loop restarts correctly, and no iteration is counted twice. A good check is to compare the number of recorded steps with the total number of times the inner loop should run across all outer loop values.

© 2023-2026 AI MATH COACH