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 in this code?<br>
for(int i=0; i<3; i++) {
for(int j=0; j<2; j++) {
// inner loop body
}
}The inner loop runs 2 times for each of the 3 outer loop runs, so 3 * 2 = 6 times total.
Click to reveal answer
beginner
Why use nested loops? Give a real-life example.
Nested loops help when you need to repeat actions in layers. For example, printing a multiplication table: outer loop for rows, inner loop for columns.
Click to reveal answer
intermediate
What happens if the inner loop depends on the outer loop variable?
The inner loop can change how many times it runs based on the outer loop variable, allowing patterns like printing a triangle of stars.
Click to reveal answer
beginner
What is the output of this code?<br>
for(int i=1; i<=2; i++) {
for(int j=1; j<=3; j++) {
printf("%d,%d\n", i, j);
}
}The output is:<br>1,1<br>1,2<br>1,3<br>2,1<br>2,2<br>2,3<br>This shows the inner loop runs fully for each outer loop value.
Click to reveal answer
In nested loops, which loop runs first?
✗ Incorrect
The outer loop starts first, and for each iteration of the outer loop, the inner loop runs fully.
How many times will the inner loop run in this code?<br>
for(int i=0; i<4; i++) {
for(int j=0; j<5; j++) {
// code
}
}✗ Incorrect
The inner loop runs 5 times for each of the 4 outer loop runs, so 4 * 5 = 20 times.
What is a common use of nested loops?
✗ Incorrect
Nested loops are often used to work with multi-dimensional data such as rows and columns in tables.
What will this code print?<br>
for(int i=1; i<=3; i++) {
for(int j=1; j<=i; j++) {
printf("*");
}
printf("\n");
}✗ Incorrect
The inner loop runs up to the current outer loop count, printing a triangle shape.
If the outer loop runs 3 times and the inner loop runs 4 times, how many total iterations happen?
✗ Incorrect
Total iterations = outer loop count * inner loop count = 3 * 4 = 12.
Explain what nested loops are and how they work in C.
Think about loops inside loops and how many times each runs.
You got /3 concepts.
Describe a real-life scenario where nested loops would be useful.
Imagine doing something for each item in a list, and for each item, doing something else multiple times.
You got /3 concepts.