0
0
C Sharp (C#)programming~10 mins

Nested loop execution in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested loop execution
Start Outer Loop i=0
Start Inner Loop j=0
Execute Inner Loop Body
Update j = j + 1
Check j < limit?
NoEnd Inner Loop
Yes
Update i = i + 1
Check i < limit?
NoEnd Outer Loop
Back to Execute Inner Loop Body
The outer loop starts and for each iteration, the inner loop runs completely before the outer loop moves to the next iteration.
Execution Sample
C Sharp (C#)
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        Console.WriteLine($"i={i}, j={j}");
    }
}
This code prints pairs of i and j values for nested loops where i runs 0 to 1 and j runs 0 to 2.
Execution Table
StepijCondition i<2Condition j<3ActionOutput
100TrueTruePrint i=0, j=0i=0, j=0
201TrueTruePrint i=0, j=1i=0, j=1
302TrueTruePrint i=0, j=2i=0, j=2
403TrueFalseInner loop ends, increment i
510TrueTruePrint i=1, j=0i=1, j=0
611TrueTruePrint i=1, j=1i=1, j=1
712TrueTruePrint i=1, j=2i=1, j=2
813TrueFalseInner loop ends, increment i
920FalseN/AOuter loop ends
💡 i reaches 2, condition i<2 is False, so outer loop stops
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
i000011112
j01230123N/A
Key Moments - 2 Insights
Why does the inner loop run completely for each value of i before i increments?
Because the outer loop waits for the inner loop to finish all its iterations (j from 0 to 2) before moving to the next i value, as shown in execution_table rows 1-4 and 5-8.
Why does j reset to 0 when the inner loop starts again?
Each time the outer loop increments i, the inner loop starts fresh with j=0, as seen in execution_table rows 4 to 5 and 8 to 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of j at step 6?
A0
B1
C2
D3
💡 Hint
Check the 'j' column in execution_table row 6.
At which step does the outer loop condition become false?
AStep 9
BStep 8
CStep 4
DStep 5
💡 Hint
Look at the 'Condition i<2' column and the exit_note.
If the inner loop condition changed to j < 2, how many times would the inner loop print per outer loop iteration?
A1 time
B3 times
C2 times
D0 times
💡 Hint
Refer to the inner loop condition 'j < 3' in execution_table and imagine changing it to 'j < 2'.
Concept Snapshot
Nested loops run one inside the other.
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 runs from 0 to 1. For each i, the inner loop variable j runs from 0 to 2. Each pair i and j is printed. The inner loop completes all its iterations before the outer loop increments i. When j reaches 3, the inner loop ends and i increments. When i reaches 2, the outer loop ends. Variables i and j change as shown in the variable tracker. This helps understand how nested loops work step-by-step.