0
0
Javaprogramming~10 mins

Nested for loop in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested for loop
Initialize outer i=0
Check outer: i < 3?
NoEXIT
Yes
Initialize inner j=0
Check inner: j < 2?
NoIncrement outer i
Yes
Execute inner loop body
Increment inner j
Back to inner check
The outer loop runs from i=0 to 2. For each i, the inner loop runs from j=0 to 1, executing its body each time.
Execution Sample
Java
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        System.out.println("i=" + i + ", j=" + j);
    }
}
Prints pairs of i and j values for nested loops where i goes 0 to 2 and j goes 0 to 1.
Execution Table
StepOuter iInner jCondition Outer (i<3)Condition Inner (j<2)ActionOutput
100truetruePrint i=0, j=0i=0, j=0
201truetruePrint i=0, j=1i=0, j=1
302truefalseInner loop ends, increment i
410truetruePrint i=1, j=0i=1, j=0
511truetruePrint i=1, j=1i=1, j=1
612truefalseInner loop ends, increment i
720truetruePrint i=2, j=0i=2, j=0
821truetruePrint i=2, j=1i=2, j=1
922truefalseInner loop ends, increment i
103-false-Outer loop ends
💡 Outer loop stops when i=3 because condition i<3 is false.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9Final
i00011122233
j0120012012-
Key Moments - 3 Insights
Why does the inner loop reset j to 0 each time the outer loop increments i?
Because the inner loop initializes j=0 at the start of each outer loop iteration, as shown in execution_table steps 3 to 4 and 6 to 7.
Why does the outer loop increment only after the inner loop finishes all its iterations?
The outer loop waits for the inner loop to complete (j reaches 2 and condition j<2 is false) before incrementing i, as seen in steps 3, 6, and 9.
What causes the entire nested loop to stop running?
When the outer loop variable i reaches 3, the condition i<3 becomes false (step 10), so the loops stop.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of j at Step 5?
A0
B1
C2
D-
💡 Hint
Check the 'Inner j' column at Step 5 in the execution_table.
At which step does the outer loop variable i first become 2?
AStep 7
BStep 6
CStep 3
DStep 9
💡 Hint
Look at the 'Outer i' column in execution_table to find when i changes to 2.
If the inner loop condition changed to j < 3, how many total print outputs would there be?
A12
B6
C9
D3
💡 Hint
Currently, inner loop runs 2 times per outer loop iteration; increasing to 3 means 3 * 3 = 9 prints.
Concept Snapshot
Nested for loop syntax:
for (int i = 0; i < N; i++) {
  for (int j = 0; j < M; j++) {
    // code using i and j
  }
}
Outer loop runs fully for each i, inner loop runs fully for each j per i.
Inner loop resets each outer iteration.
Full Transcript
This example shows a nested for loop in Java where the outer loop variable i runs from 0 to 2, and for each i, the inner loop variable j runs from 0 to 1. Each step prints the current values of i and j. The inner loop resets j to 0 every time the outer loop increments i. The loops stop when i reaches 3, making the outer loop condition false. The execution table traces each step, showing variable values and actions. Key moments clarify why inner loop resets and when loops end. The quiz tests understanding of variable values at specific steps and effects of changing loop conditions.