Recall & Review
beginner
What is a nested loop in C#?
A nested loop is a loop inside another loop. The inner loop runs completely every time the outer loop runs once.
Click to reveal answer
beginner
How many times will the inner loop run if the outer loop runs 3 times and the inner loop runs 4 times each time?
The inner loop will run 3 × 4 = 12 times in total.
Click to reveal answer
beginner
What is the output of this nested loop?<br>
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
Console.WriteLine($"i={i}, j={j}");
}
}The output will be:<br>i=1, j=1<br>i=1, j=2<br>i=1, j=3<br>i=2, j=1<br>i=2, j=2<br>i=2, j=3
Click to reveal answer
intermediate
Why do nested loops often increase the total number of executions quickly?
Because the inner loop runs completely for each iteration of the outer loop, the total executions multiply, causing a rapid increase.
Click to reveal answer
intermediate
Can nested loops be used with different types of loops in C#?
Yes, you can nest any type of loops inside each other, like for inside while, while inside for, or foreach inside for.
Click to reveal answer
In a nested loop, when does the inner loop run?
✗ Incorrect
The inner loop runs completely every time the outer loop runs once.
If the outer loop runs 5 times and the inner loop runs 2 times, how many total iterations happen?
✗ Incorrect
Total iterations = outer loop count × inner loop count = 5 × 2 = 10.
Which of these is a valid nested loop structure in C#?
✗ Incorrect
All these loop types can be nested inside each other in C#.
What happens if the inner loop has zero iterations?
✗ Incorrect
If the inner loop has zero iterations, it does not run, but the outer loop continues its iterations.
Why should you be careful with nested loops?
✗ Incorrect
Nested loops multiply the number of executions, which can slow down the program if too large.
Explain how nested loops work and why the inner loop runs multiple times.
Think about a clock: the minute hand (inner loop) moves fully for each hour (outer loop).
You got /3 concepts.
Describe a real-life example where nested loops could be useful.
Imagine checking every seat in every row of a theater.
You got /3 concepts.