Chapter 8 · Simulation

Loop Tracer

← Chapter 8
    Variables
    Condition

    Trace table
    Output (console)

    Loop Tracer

    Purpose: see what happens in each iteration of a loop by tracing the code: in which order the initialisation, condition, body and update run, how the variables change, and what is written to the output.

    How to use it

    1. Pick a program on the left; when you change the parameters (n, start value, condition limit…) the code and the trace are recomputed.
    2. Step ▶ runs the next operation; the line being executed is highlighted, and on the right you see the variables (the changed value is coloured) and the evaluation of the condition. There are also Back, Play, Reset and a speed control; → / ← work on the keyboard too.
    3. For single loops the trace table below keeps each condition test as one row. For nested loops a grid is shown instead: each cell is one inner-loop iteration (written, skipped by continue, break, never ran).
    4. Try it yourself: predict the result of every condition test; correct guesses are counted.

    Reminders

    • for: initialisation once → condition → body → update → condition … The loop ends when the condition is false.
    • while tests the condition first (the body may never run); do-while runs the body at least once.
    • continue skips the rest of that iteration (in a for loop the update still happens); break leaves only the loop it is in.

    Related slides: Chapter 8 · Loops (code tracing, break and continue, digit count, nested loops, pattern problems).