0
0
C++programming~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 < 3?
NoEXIT
Yes
Start inner loop j=0
Check inner condition j < 2?
NoIncrement i
Yes
Execute inner loop body
Increment j
Back to inner condition
Increment i
Back to inner condition
The outer loop runs first. For each outer loop step, the inner loop runs completely before the outer loop moves on.
Execution Sample
C++
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        std::cout << i << "," << j << "\n";
    }
}
Print pairs of i and j where i goes from 0 to 2 and j goes from 0 to 1.
Execution Table
StepijOuter condition (i<3)Inner condition (j<2)ActionOutput
100TrueTruePrint 0,00,0
201TrueTruePrint 0,10,1
302TrueFalseExit inner loop
410TrueTruePrint 1,01,0
511TrueTruePrint 1,11,1
612TrueFalseExit inner loop
720TrueTruePrint 2,02,0
821TrueTruePrint 2,12,1
922TrueFalseExit inner loop
103-False-Exit outer loop
💡 Outer loop ends when i reaches 3 because condition i < 3 is false.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
i0001112223
j012012012-
Key Moments - 3 Insights
Why does the inner loop restart j from 0 each time the outer loop increments i?
Because the inner loop variable j is declared inside the outer loop, it resets to 0 every time the outer loop runs a new iteration, as shown in rows 1, 4, and 7 of the execution_table.
Why does the outer loop variable i only increment after the inner loop finishes?
The outer loop waits for the inner loop to complete all its iterations before increasing i, as seen in the execution_table where i stays the same while j runs through all values.
What happens when the inner loop condition becomes false?
When j reaches 2, the inner condition j < 2 is false, so the inner loop exits and control returns to the outer loop to increment i, as shown in rows 3, 6, and 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What are the values of i and j?
Ai=1, j=0
Bi=0, j=1
Ci=1, j=1
Di=2, j=1
💡 Hint
Check the 'i' and 'j' columns in row 5 of the execution_table.
At which step does the outer loop condition become false?
AStep 9
BStep 10
CStep 3
DStep 1
💡 Hint
Look at the 'Outer condition (i<3)' column for the first 'False' value.
If the inner loop condition changed to j < 3, how many total print outputs would there be?
A9
B3
C6
D12
💡 Hint
Multiply outer loop iterations (3) by new inner loop iterations (3).
Concept Snapshot
Nested loops run one loop inside another.
Outer loop runs once per iteration.
Inner loop runs fully each outer iteration.
Inner loop variable resets each time.
Use nested loops to handle grids or pairs.
Full Transcript
This example shows nested loops in C++. The outer loop variable i starts at 0 and runs while less than 3. For each i, the inner loop variable j starts at 0 and runs while less than 2. Each pair i,j is printed. The inner loop completes all its iterations before the outer loop increments i. When j reaches 2, the inner loop exits and i increments. When i reaches 3, the outer loop exits. Variables i and j change as shown in the variable tracker. Key points include the reset of j each outer iteration and the order of loop completions.