🇯🇵 日本語 | 🇺🇸 English

Quiz (Loops)

Test your understanding of for, while, break, and nested loops.

Question 1 — Iteration count of a for loop

int sum = 0;
for (int i = 1; i <= 5; i++) {
    sum += i;
}
printf("sum = %d\n", sum);

What does this print?

sum = 5
sum = 15
sum = 10
sum = 14
Explanation: The loop runs 5 times with i=1,2,3,4,5.
1+2+3+4+5 = 15.

Question 2 — while loop termination

int x = 10;
while (x > 0) {
    x -= 3;
}
printf("x = %d\n", x);

What does this print?

x = 0
x = 1
x = -2
x = 3
Explanation: x progresses: 10 → 7 → 4 → 1 → -2.
When x=-2, x > 0 is false and the loop ends. Because x -= 3 subtracts 3 each time, the loop can overshoot zero.

Question 3 — break behavior

for (int i = 0; i < 10; i++) {
    if (i == 3) break;
    printf("%d ", i);
}

What does this print?

0 1 2 3
0 1 2
0 1 2 3 4 5 6 7 8 9
3
Explanation: When i==3, break exits the loop immediately. The break runs before printf, so 3 is never printed.
The output is just 0 1 2 (i=0, 1, 2).

Question 4 — Nested loop iterations

int count = 0;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        count++;
    }
}
printf("count = %d\n", count);

What does this print?

count = 7
count = 12
count = 3
count = 4
Explanation: Outer loop 3 times × inner loop 4 times = 12 iterations.
For nested loops, the total iteration count is outer × inner.

Question 5 — The difference between < and <=

int count = 0;
for (int i = 1; i <= 5; i++) {
    count++;
}
printf("count = %d\n", count);

What does this print?

count = 4
count = 5
count = 6
count = 1
Explanation: i <= 5 includes i=5, so the loop runs for i=1, 2, 3, 4, 5 — 5 iterations.
  • for (i=1; i<=5; i++) → 5 iterations (i=1..5)
  • for (i=1; i<5; i++) → 4 iterations (i=1..4)
Switching between < and <= shifts the count by one — a classic off-by-one error.

Question 6 — How continue behaves

for (int i = 0; i < 5; i++) {
    if (i == 2) continue;
    printf("%d ", i);
}

What does this print?

0 1
0 1 2 3 4
0 1 3 4
2
Explanation: continue skips only the current iteration and jumps to the next one.
  • break: exit the entire loop
  • continue: skip the rest of this iteration and move on
When i=2, the printf is skipped, so the output is 0 1 3 4.

Question 7 — do-while runs at least once

int i = 10;
do {
    printf("%d ", i);
    i++;
} while (i < 5);

What does this print?

(nothing)
10
10 11 12 13 14
Infinite loop
Explanation: In a do-while loop, the condition is checked at the end of each iteration.
  • The body always runs at least once
  • The first iteration prints i=10, then i becomes 11
  • 11 < 5 is false, so the loop ends
A plain while loop would never enter the body because the condition is false from the start. A do-while always runs at least once.

Question 8 — Which is an infinite loop?

// Which of the following is an infinite loop?
// A) for (int i = 0; i < 10; i++) { ... }
// B) while (1) { ... }  // no break
// C) for (int i = 0; i < 10; ) { i++; }
// D) int i = 0; while (i < 10) { i++; }

Which one is an infinite loop?

A
B
C
D
Explanation: The condition in while (1) is always true, so it never ends unless you break out.
  • A: exits once i reaches 10 (10 iterations)
  • B: always true → infinite loop
  • C: the body increments i, so it terminates at 10
  • D: i++ advances i, so it terminates
Infinite loops are used intentionally for things like event loops — just make sure there's always an exit condition (a break).

Question 9 — break inside nested loops

int count = 0;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 2) break;
        count++;
    }
}
printf("count = %d\n", count);

What does this print?

count = 9
count = 6
count = 3
count = 0
Explanation: break only exits the innermost enclosing loop; the outer loop keeps going.
  • Inner loop: count++ runs for j=0 and j=1, then break at j=2
  • So each inner pass does count++ twice
  • Outer loop 3 times × inner 2 times = 6
To break out of the outer loop too, use a flag variable, a goto, or extract the code into a function and return.

Question 10 — Counting down with i--

Putting i-- in the update step of a for loop lets you count down.
for (int i = 5; i >= 0; i--) {
    printf("%d ", i);
}

What does this print?

0 1 2 3 4 5
5 4 3 2 1 0
5 4 3 2 1
Infinite loop
Explanation: Breaking down the three parts of the for:
  • Init: i = 5 (start at 5)
  • Condition: i >= 0 (keep going while ≥ 0)
  • Update: i-- (decrement by 1 each iteration)
i is printed as 5, 4, 3, 2, 1, 0. When i becomes -1 the condition is false → the loop ends → 5 4 3 2 1 0.
When to use it: walking through an array from back to front, countdown displays, reversing data, and so on.

Question 11 — for(;;) as an infinite loop

All three parts of a for loop can be omitted. What happens when they are?
int count = 0;
for (;;) {
    count++;
    if (count == 3) break;
}
printf("count = %d\n", count);

What does this print?

count = 0 (loop doesn't run)
Compile error
count = 3
Infinite loop, never stops
Explanation: for (;;) has all three parts omitted:
  • No initialization
  • No condition → treated as always true
  • No update
It's the standard idiom for an infinite loop (equivalent to while (1)).
Here, the body's if (count == 3) break; provides the exit. Once count reaches 3 the loop ends and "count = 3" is printed.
When to use it: menu wait loops, event waits — anything where the exit condition fits most naturally inside the body.

Question 12 — Stepping by 2 with i += 2

Changing the update step from i++ to something else lets you change the stride.
for (int i = 0; i <= 10; i += 2) {
    printf("%d ", i);
}

What does this print?

0 1 2 3 4 5 6 7 8 9 10
1 3 5 7 9
0 2 4 6 8 10
2 4 6 8 10
Explanation: The three parts of the for loop:
  • Init: i = 0
  • Condition: i <= 10 (up to and including 10)
  • Update: i += 2 (add 2 each iteration)
i advances 0, 2, 4, 6, 8, 10. The next value (12) fails the condition → the loop ends → 0 2 4 6 8 10.
Note: Because we use <= 10, 10 is included. Switch to < 10 and it's excluded, giving "0 2 4 6 8".
When to use it: processing only even numbers, sampling every third pixel, or stepping backward with i -= 3.

Result

Answer all the questions to see your score.
Home