Bird
Raised Fist0
C Sharp (C#)programming~10 mins

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

Choose your learning style10 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?
No→End Inner Loop
Yes↓
Update i = i + 1
↓
Check i < limit?
No→End 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.

Practice

(1/5)
1. What happens when you use a nested loop in C#?
easy
A. Both loops run at the same time independently.
B. The outer loop runs only once and the inner loop runs multiple times.
C. The loops run only if a condition outside the loops is true.
D. The inner loop runs completely for each iteration of the outer loop.

Solution

  1. Step 1: Understand nested loop structure

    In nested loops, the outer loop controls how many times the inner loop runs fully.
  2. Step 2: Analyze loop execution order

    For each iteration of the outer loop, the inner loop runs from start to finish before the outer loop moves to the next iteration.
  3. Final Answer:

    The inner loop runs completely for each iteration of the outer loop. -> Option D
  4. Quick Check:

    Nested loops = inner loop runs fully each outer loop [OK]
Hint: Inner loop finishes all steps per outer loop step [OK]
Common Mistakes:
  • Thinking both loops run simultaneously
  • Assuming outer loop runs only once
  • Confusing loop execution order
2. Which of the following is the correct syntax for a nested for loop in C#?
easy
A. for(int i = 0; i < 3; i++) { for(int j = 0; j < 2; j++) { Console.WriteLine(i + j); } }
B. for i in range(3) { for j in range(2) { Console.WriteLine(i + j); } }
C. for(int i = 0; i < 3; i++) for(int j = 0; j < 2; j++) Console.WriteLine(i + j); }
D. foreach(int i = 0; i < 3; i++) { foreach(int j = 0; j < 2; j++) { Console.WriteLine(i + j); } }

Solution

  1. Step 1: Check C# for loop syntax

    The correct for loop uses parentheses with initialization, condition, and increment separated by semicolons.
  2. Step 2: Verify nested loop braces and statements

    for(int i = 0; i < 3; i++) { for(int j = 0; j < 2; j++) { Console.WriteLine(i + j); } } correctly nests two for loops with braces and valid syntax. Other options use incorrect syntax or keywords.
  3. Final Answer:

    for(int i = 0; i < 3; i++) { for(int j = 0; j < 2; j++) { Console.WriteLine(i + j); } } -> Option A
  4. Quick Check:

    C# for loop syntax = for(int i = 0; i < 3; i++) { for(int j = 0; j < 2; j++) { Console.WriteLine(i + j); } } [OK]
Hint: Remember C# for loop uses semicolons inside parentheses [OK]
Common Mistakes:
  • Using Python-like syntax in C#
  • Missing semicolons in for loop
  • Using foreach incorrectly for numeric loops
3. What is the output of this C# code?
for(int i = 1; i <= 2; i++) {
  for(int j = 1; j <= 3; j++) {
    Console.Write(i * j + " ");
  }
  Console.WriteLine();
}
medium
A. 6 12
B. 1 2 3 2 4 6
C. 1 1 1 2 2 2
D. 1 2 3 4 5 6

Solution

  1. Step 1: Trace outer loop values

    Outer loop i runs from 1 to 2 (two iterations).
  2. Step 2: Calculate inner loop outputs per i

    For i=1, inner loop j=1 to 3 outputs 1*1=1, 1*2=2, 1*3=3. For i=2, inner loop outputs 2*1=2, 2*2=4, 2*3=6.
  3. Final Answer:

    1 2 3 2 4 6 -> Option B
  4. Quick Check:

    Nested loops multiply i and j = output lines [OK]
Hint: Multiply outer and inner loop counters for output [OK]
Common Mistakes:
  • Confusing loop ranges
  • Printing without spaces or newlines
  • Mixing up i and j in multiplication
4. Identify the error in this nested loop code:
for(int i = 0; i < 3; i++) {
  for(int j = 0; j < 2; j++)
    Console.WriteLine(i + j);
  }
}
medium
A. Extra closing brace '}' after the inner loop.
B. Missing semicolon after Console.WriteLine statement.
C. Incorrect loop condition in the outer loop.
D. Variable j is not declared.

Solution

  1. Step 1: Check braces matching

    There is an extra closing brace '}' after the inner loop which causes a syntax error.
  2. Step 2: Verify other syntax elements

    Console.WriteLine has correct syntax, loops have valid conditions, and variables are declared properly.
  3. Final Answer:

    Extra closing brace '}' after the inner loop. -> Option A
  4. Quick Check:

    Brace mismatch = syntax error [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
  • Ignoring extra or missing braces
  • Assuming missing semicolon error
  • Confusing variable declaration errors
5. You want to print a multiplication table from 1 to 3 using nested loops in C#. Which code correctly prints the table with rows and columns aligned?
hard
A. for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { Console.Write(i*j); } Console.WriteLine(); }
B. for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { Console.WriteLine(i*j); } }
C. for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { Console.Write($"{i*j,4}"); } Console.WriteLine(); }
D. for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { Console.WriteLine($"{i}*{j}={i*j}"); } Console.WriteLine(); }

Solution

  1. Step 1: Understand formatting for aligned output

    Using Console.Write with format specifier {value,4} aligns numbers in columns with 4 spaces width.
  2. Step 2: Check nested loop and output structure

    for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { Console.Write($"{i*j,4}"); } Console.WriteLine(); } prints products in a grid with spaces, then moves to next line after inner loop finishes, creating a table.
  3. Final Answer:

    for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { Console.Write($"{i*j,4}"); } Console.WriteLine(); } -> Option C
  4. Quick Check:

    Formatted nested loops print aligned multiplication table [OK]
Hint: Use Console.Write with alignment specifier for columns [OK]
Common Mistakes:
  • Using Console.WriteLine inside inner loop causing new lines
  • Not formatting output for alignment
  • Missing Console.WriteLine after inner loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard