Programming
For Loop
Iteration
Coding Basics
Computer Science

For loop first iteration

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The first iteration of a for loop is where the loop setup proves whether it will run at all. Understanding that first pass makes the whole control flow clearer, because it shows the exact order of initialization, condition checking, body execution, and update.

What Happens Before the First Iteration

In C-style languages such as C, Java, and JavaScript, a for loop has three logical parts:

  1. initialization
  2. condition check
  3. update expression

Example:

javascript
for (let i = 0; i < 3; i++) {
  console.log("iteration", i);
}

For the first iteration, the runtime does this in order:

  1. run let i = 0
  2. evaluate i < 3
  3. if true, execute the loop body
  4. run i++

Only after that does the second iteration begin.

Why the First Iteration Matters

The first iteration tells you two important things immediately:

  • whether the loop body will execute at all
  • what the initial loop variable values are inside the body

For the previous example, the first printed value is:

text
iteration 0

That happens because i is initialized before the condition is checked and before the body runs.

The Loop May Never Reach a First Body Execution

A common point of confusion is that initialization does not guarantee body execution. If the condition is false immediately, the body never runs even once.

javascript
for (let i = 10; i < 3; i++) {
  console.log("this never prints");
}

Here:

  1. i becomes 10
  2. i < 3 is false
  3. the loop body is skipped entirely

So the "first iteration" of the body never happens.

Python for Loops Work Differently

Python does not use the same three-part for (init; condition; update) syntax. Instead, it iterates over an iterable directly.

python
for i in range(3):
    print("iteration", i)

The first iteration happens when the iterator yields its first value, which is 0 in this case. Conceptually, Python handles the condition and update internally through the iterator protocol rather than exposing them as separate loop clauses.

Walking Through the First Pass

Take this Java example:

java
for (int i = 2; i <= 4; i++) {
    System.out.println(i);
}

The first pass is:

  1. create i with value 2
  2. check i <= 4
  3. print 2
  4. increment i to 3

That is why the loop body sees i == 2 on its first execution, not 3.

Nested Loops and First Iterations

Nested loops repeat the same logic independently. The inner loop gets its own first iteration every time the outer loop advances.

python
for i in range(2):
    for j in range(3):
        print(i, j)

When i == 0, the inner loop has its first iteration with j == 0. Then when the outer loop advances to i == 1, the inner loop starts over and again has a first iteration with j == 0.

This is why nested loops often produce repeated initial values in the inner variable.

Practical Debugging Tip

If you are unsure what the first iteration is doing, log the loop variable before and inside the body:

javascript
for (let i = 0; i < 3; i++) {
  console.log("inside body, i =", i);
}

For Python:

python
for i in range(3):
    print("inside body, i =", i)

This is often faster than reasoning abstractly when debugging off-by-one errors.

Common Pitfalls

The biggest mistake is assuming the update expression runs before the first body execution. It does not. The body sees the initialized value first, then the update runs afterward.

Another issue is forgetting that the body may not run at all if the condition is false immediately. Many bugs around "why didn't my loop execute?" come from a wrong initial value or a wrong condition.

Finally, do not transfer C-style intuition blindly into Python. Python for loops iterate over items from an iterable, so the mental model is different even if the visible result sometimes looks similar.

Summary

  • In C-style loops, initialization happens first, then the condition, then the body.
  • The update expression runs after the body, not before the first pass.
  • If the condition is false immediately, the body never gets a first iteration.
  • Python for loops use iterables rather than explicit init/condition/update clauses.
  • Many off-by-one bugs become obvious once you trace the first iteration step by step.

Course illustration
Course illustration

All Rights Reserved.