0
0
Cprogramming~10 mins

Nested loops in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested loops
Start outer loop i=0
Check outer condition i < 2?
NoEXIT
Yes
Start inner loop j=0
Check inner condition j < 3?
NoEnd inner loop
Yes
Execute inner loop body
Update inner loop j = j + 1
Back to inner condition
End inner loop
Update outer loop i = i + 1
Back to inner condition
The outer loop runs first. For each outer loop iteration, the inner loop runs completely. Then the outer loop moves to the next iteration.
Execution Sample
C
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        printf("%d %d\n", i, j);
    }
}
Print pairs of i and j where i runs 0 to 1 and j runs 0 to 2 for each i.
Execution Table
StepijCondition i<2Condition j<3ActionOutput
100TrueTruePrint 0 00 0
201TrueTruePrint 0 10 1
302TrueTruePrint 0 20 2
403TrueFalseExit inner loop
510TrueTruePrint 1 01 0
611TrueTruePrint 1 11 1
712TrueTruePrint 1 21 2
813TrueFalseExit inner loop
92-False-Exit outer loop
💡 Outer loop i=2 fails condition i<2, so loops end.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
i0000011112
j-01230123-
Key Moments - 3 Insights
Why does the inner loop restart j from 0 for each i?
Because the inner loop variable j is initialized inside the outer loop. Each time the outer loop runs, j starts fresh at 0 (see execution_table rows 1 and 5).
Why does the outer loop variable i only increase after the inner loop finishes?
The outer loop waits for the inner loop to complete all its iterations before increasing i (see execution_table rows 4 and 5). This is how nested loops work.
What happens when j reaches 3?
The inner loop condition j<3 becomes false, so the inner loop stops and control goes back to the outer loop to increase i (see execution_table rows 4 and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of j at step 6?
A0
B2
C1
D3
💡 Hint
Check the 'j' column in execution_table row 6.
At which step does the outer loop condition become false?
AStep 8
BStep 9
CStep 4
DStep 1
💡 Hint
Look at the 'Condition i<2' column and find where it is False.
If the inner loop condition changed to j < 2, how many total prints would occur?
A4
B6
C3
D2
💡 Hint
Multiply outer loop iterations (2) by inner loop iterations (2).
Concept Snapshot
Nested loops run one loop inside another.
Outer loop runs first.
Inner loop runs fully for each outer iteration.
Inner loop variable resets each time.
Useful for grids, tables, repeated pairs.
Full Transcript
This example shows nested loops in C. The outer loop variable i runs from 0 to 1. For each i, the inner loop variable j runs from 0 to 2. Each pair i,j is printed. The inner loop resets j to 0 each time the outer loop runs. When j reaches 3, the inner loop stops and the outer loop increases i. When i reaches 2, the outer loop stops. This pattern is common to repeat actions in a grid or matrix style.