Nested loops let you repeat actions inside other repeated actions. This helps when you work with tables or grids.
When you want to print a grid of stars or numbers.
When you need to check every pair of items in two lists.
When you want to create a multiplication table.
When you process rows and columns in a spreadsheet.
When you want to compare elements inside a matrix.
Syntax
C Sharp (C#)
for (int i = 0; i < outerLimit; i++) {
for (int j = 0; j < innerLimit; j++) {
// code to repeat
}
}
The outer loop runs first, then the inner loop runs completely for each outer loop step.
Use different variable names for each loop to avoid confusion.
Examples
This prints pairs of i and j values, showing how inner loop runs fully for each i.
C Sharp (C#)
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
Console.WriteLine($"i={i}, j={j}");
}
}
This prints 2 rows of 3 stars each, like a small rectangle.
C Sharp (C#)
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
Console.Write("*");
}
Console.WriteLine();
}
Sample Program
This program prints a 3x3 multiplication table. The outer loop controls rows, the inner loop controls columns.
C Sharp (C#)
using System;
class Program {
staticvoid Main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
Console.Write($"{i}x{j}={i*j} ");
}
Console.WriteLine();
}
}
}
OutputSuccess
Important Notes
Nested loops can slow down your program if they run many times, so use them carefully.
Indent your code inside loops to keep it clear which code belongs to which loop.
Summary
Nested loops run one loop inside another to repeat actions multiple times.
The inner loop finishes all its steps for each step of the outer loop.
They are useful for working with tables, grids, or pairs of items.
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
Step 1: Understand nested loop structure
In nested loops, the outer loop controls how many times the inner loop runs fully.
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.
Final Answer:
The inner loop runs completely for each iteration of the outer loop. -> Option D
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
Step 1: Check C# for loop syntax
The correct for loop uses parentheses with initialization, condition, and increment separated by semicolons.
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.
Final Answer:
for(int i = 0; i < 3; i++) { for(int j = 0; j < 2; j++) { Console.WriteLine(i + j); } } -> Option A
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]
B. Missing semicolon after Console.WriteLine statement.
C. Incorrect loop condition in the outer loop.
D. Variable j is not declared.
Solution
Step 1: Check braces matching
There is an extra closing brace '}' after the inner loop which causes a syntax error.
Step 2: Verify other syntax elements
Console.WriteLine has correct syntax, loops have valid conditions, and variables are declared properly.
Final Answer:
Extra closing brace '}' after the inner loop. -> Option A
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
Step 1: Understand formatting for aligned output
Using Console.Write with format specifier {value,4} aligns numbers in columns with 4 spaces width.
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.
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