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

Nested loop execution in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly after the outer loop finishes
BOnly once before the outer loop starts
COnce for each iteration of the outer loop
DRandomly during the outer loop
If the outer loop runs 5 times and the inner loop runs 2 times, how many total iterations happen?
A10
B5
C7
D2
Which of these is a valid nested loop structure in C#?
Afor inside for
BAll of the above
Cwhile inside for
Dforeach inside while
What happens if the inner loop has zero iterations?
AThe outer loop stops immediately
BThe inner loop runs once anyway
CThe program crashes
DThe inner loop does not run, but the outer loop continues
Why should you be careful with nested loops?
AThey can slow down the program if too many iterations happen
BThey always cause errors
CThey are not allowed in C#
DThey only work with for loops
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.