0
0
Cprogramming~5 mins

Nested loops in 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 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?
AThe outer loop runs first, then the inner loop runs completely each time.
BThe inner loop runs first, then the outer loop runs completely each time.
CBoth loops run at the same time.
DOnly the outer loop runs.
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
  }
}
A20 times
B5 times
C4 times
D9 times
What is a common use of nested loops?
ATo repeat a task once
BTo handle multi-dimensional data like grids or tables
CTo avoid repetition
DTo run code only once
What will this code print?<br>
for(int i=1; i<=3; i++) {
  for(int j=1; j<=i; j++) {
    printf("*");
  }
  printf("\n");
}
ANo output
BA square of stars
CA triangle of stars
DAn error
If the outer loop runs 3 times and the inner loop runs 4 times, how many total iterations happen?
A3
B4
C7
D12
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.