CodeVizAI / Visual learning
← Learning workspace

C PROGRAMMING · BEGINNER

C For Loop Visualization: Trace Each Iteration

Trace the initialization, condition, body and update of a C for loop. Use Next and Previous to see exactly which values print and when the loop terminates.

01 / UNDERSTAND

Start with working C code

The output is 1, 2 and 3 on separate lines. The value 4 is checked but never printed because the condition becomes false.

#include <stdio.h>
int main(void) {
    for (int i = 1; i <= 3; ++i) {
        printf("%d\n", i);
    }
    return 0;
}

02 / VISUALIZE

Interactive loop visualization

Use the controls to follow the C example above. The labels describe the current model state.

The explanatory material on this page is also available without JavaScript. Enable JavaScript to use the visual step controls.

Ready to explore the example.

How does a C for loop work?

The syntax is for (initialization; condition; update). Initialization runs once. Before every iteration, C evaluates the condition. If it is true, the body executes and then the update runs. When the condition is false, execution continues after the loop.

  1. Initialize: int i = 1.
  2. Check: i <= 3 before executing the body.
  3. Execute: print the current value of i.
  4. Update: ++i increments the counter by one.

Common C loop mistakes

Off-by-one errors: i < 3 prints only 1 and 2; i <= 3 includes 3. Infinite loops: if the update never makes the condition false, the loop may not finish. Zero iterations: starting with i = 4 makes the initial condition false and prints nothing.

The model above deliberately separates checking the condition from printing a value. A continue statement in a for loop still proceeds to its update expression before the next condition check.

03 / PRACTICE

Try it yourself

  1. Predict the output if i = 0 and i <= 3.
  2. Change the update to i += 2. Which values appear?
  3. Explain why i = 4 is evaluated but not printed in the original loop.
Check your predictions

The first example prints 0, 1, 2 and 3. The second prints 1 and 3. In the original loop, 4 makes the condition false before the body can execute.

Open the complete C loop lesson →