- How while, do-while, and for loops work in C
- When the condition is checked in each loop type
- The role of initialization, condition, and increment
- How break and continue change loop behaviour
- How nested loops execute (and why inner loops matter)
- How to trace output of loops step by step
- Common pitfalls like infinite loops and undefined behaviour
This page has 40 multiple choice questions on loops in C. You'll find questions on while, do-while, and for loops, plus a set of nested-loop and output-tracing questions that test how well you can read code. Each question comes with the correct answer and a short explanation in plain English, so you understand why an option is right and not just what it is. These MCQs are useful for beginners preparing for college exams, interviews, or any C programming test. Some questions involve running code in your head — take them slowly, one line at a time.
while Loop Basics
Q1. Which one is the syntax of a while loop in C?
Answer: D) while(condition) { }
The condition goes inside parentheses, and the loop body is enclosed in curly braces. Option B is missing the body, and options A and C use curly braces around the condition, which is wrong. Getting the syntax right the first time saves you from confusing compiler errors.
Q2. How many times does the following loop execute?
int p = 0;
while(p < 15) {
p++;
}
Answer: B) 15
p starts at 0 and increases by 1 each time. The loop runs while p is less than 15 — that is, for p = 0, 1, 2, ..., 14. That's exactly 15 iterations. When p becomes 15, the condition p < 15 is false and the loop ends.
Q3. Identify the correct way to exit a while loop normally (before its condition becomes false) in C.
Answer: D) break;
The break statement exits the loop and jumps to the next statement after it. return exits the whole function, and exit(0) ends the program — both are heavier than just exiting the loop. continue is the opposite of break: it skips the rest of the current iteration but keeps the loop running.
Q4. What happens if the condition in a while loop is always true?
Answer: A) The loop runs indefinitely
If the condition never becomes false, the loop keeps running forever — this is called an infinite loop. The program doesn't crash automatically; it just freezes. To stop it, you'd need break, return, exit(), or for the operating system to kill the process. That's why it's important to make sure your loop condition will eventually become false.
Q5. What is the purpose of a while loop in C?
Answer: D) To check a condition before executing the loop body
A while loop is a "pre-test" loop: the condition is tested first, and only if it's true does the body run. If the condition is false from the start, the body never runs at all. This is different from do-while, which runs the body at least once regardless.
Q6. Which of the following is an infinite loop in C?
Answer: D) while(1) {}
In C, any non-zero value is true, and 1 is always non-zero. So while(1) is always true and the loop never ends. Options A, B, and C depend on the value of i — they will end when i reaches a certain value. This makes while(1) the standard way to write an intentional infinite loop.
Q7. What is the initializer in a while loop in C?
Answer: A) To initialize variables before the loop starts
A while loop doesn't have a formal "initializer" — you write any setup code before the loop yourself. For example, int i = 0; appears before the while, not inside it. That line is the initializer. The for loop is different: it has a dedicated initialization section inside the for(...) itself.
Q8. What is the purpose of the increment (++)/decrement (--) statement in a while loop?
Answer: D) To change the loop variable's value each iteration so the loop eventually ends
Without changing the loop variable, the condition would never become false, and the loop would run forever. Incrementing or decrementing moves the variable towards the exit condition. This is why forgetting i++ inside a while loop is one of the most common beginner bugs — instant infinite loop.
Q9. What is the importance of the condition in a while loop?
Answer: C) To decide whether to execute the loop or not
The condition is the gatekeeper of the while loop. Every iteration, before running the body, the condition is checked. If it's true, the body runs; if it's false, the loop exits. Without a correct condition, the loop either won't run at all or won't stop — both are problems.
while Loop Output Questions
Q10. What is the output of the following code snippet?
int x = 1;
while(x < 13) {
printf("%d ", x);
x += 2;
}
Answer: C) 1 3 5 7 9 11
x starts at 1 and increases by 2 each time. So it takes the values 1, 3, 5, 7, 9, 11 — all less than 13. Each value gets printed. When x becomes 13, the condition x < 13 is false and the loop ends. Notice that 13 is not printed because the check happens before the print.
Q11. Find the output of the following code.
int p = 5;
while(p > 2) {
printf("%d ", p);
p--;
}
Answer: A) 5 4 3
p starts at 5 and decreases by 1 each time. The loop continues while p > 2, so it prints 5, 4, 3. When p becomes 2, the condition p > 2 is false and the loop stops. The number 2 itself is never printed.
Q12. Find the value of x after the program finishes.
int x = 5;
while(x > 0) {
x -= 5;
}
Answer: D) 0
x starts at 5. The first iteration subtracts 5, so x becomes 0. Then the condition x > 0 is checked: 0 > 0 is false, so the loop ends. The final value of x is 0. Note that x does not go negative — the loop exits exactly when x hits 0.
Q13. Trace the output of the following code.
int p = 10;
while(p > 0) {
printf("%d ", p);
p -= 2;
}
Answer: A) 10 8 6 4 2
p starts at 10 and decreases by 2 each time: 10, 8, 6, 4, 2. When p becomes 0, the condition p > 0 is false and the loop ends. So the printed sequence is 10 8 6 4 2 — all even numbers down to 2, and 0 is not printed.
Q14. Find the value of tot after the code finishes running.
int tot = 0;
int i = 1;
while(i <= 10) {
tot += i;
i++;
}
Answer: B) 55
This is the classic sum-of-first-N-numbers pattern. It adds 1 + 2 + 3 + ... + 10. The formula is n(n+1)/2 = 10×11/2 = 55. So tot becomes 55. This pattern appears in almost every C course — memorise it.
do-while Loop
Q15. Which is the syntax for a do-while loop in C?
Answer: C) do { } while(condition);
The do-while loop starts with the keyword do, then the body in braces, then the keyword while followed by the condition in parentheses — and critically, a semicolon at the end. Forgetting that trailing semicolon is a common beginner mistake and a compiler error.
Q16. In a do-while loop, the condition is checked:
Answer: C) after the loop body is executed
That's the whole point of a do-while loop — it's a "post-test" loop. The body runs first, then the condition is checked. If the condition is true, the loop repeats. That's why the body always runs at least once, even when the condition is false from the very beginning.
Q17. What will happen if the condition in a do-while loop is never true?
Answer: B) The loop runs at least once
This is the defining feature of a do-while loop. The body runs first, and only then is the condition checked. So even if the condition is false from the start, the body has already executed once. Use this when you want to guarantee at least one pass — like reading a user's menu choice.
Q18. Which of the following is not strictly necessary in a do-while loop?
Answer: A) Initialization
A do-while loop must have a body and a condition — those are part of its syntax. Initialization and increment are not part of the syntax; they are written before and inside the loop by the programmer. That's why the syntax of a do-while is do { ... } while(condition); — no initializer or increment section like a for loop has.
Q19. How many times will the following do-while loop execute?
int i = 0;
do {
i++;
} while (i < 0);
Answer: B) 1
The body runs first, so i++ makes i = 1. Then the condition i < 0 is checked: 1 < 0 is false, so the loop ends. Result: the body ran exactly once. This is a classic do-while trap — even when the condition is false from the start, the body runs once.
Q20. Which of the following statements executes at least once?
Answer: C) do-while loop
Only the do-while loop is guaranteed to run its body at least once. while, for, and if all check the condition first — if it's false on the very first check, the body never runs. This is a favourite exam question, so remember: do-while = "at least once."
Q21. What does the following code snippet do?
int p = 9;
do {
printf("%d ", p);
p--;
} while (p > 5);
Answer: A) Prints numbers from 9 to 5
p starts at 9. Each iteration prints p, then decrements by 1, then checks p > 5. So it prints 9, then p becomes 8; prints 8, then p becomes 7; and so on — until p becomes 5. The final check: 5 > 5 is false, so the loop ends. Output: 9 8 7 6 5.
Q22. Which of the following is true about the do-while loop in C?
Answer: D) It is a post-test loop
"Post-test" means the condition is tested after the body runs. The opposite is "pre-test" — while and for loops are pre-test loops. This naming makes it easy to remember: pre-test = test first (may run 0 times), post-test = test last (runs at least once).
Q23. What is the biggest difference between a do-while loop and a while loop?
Answer: C) In a do-while loop, the loop body is executed at least once
This is the only guaranteed difference. Both check the condition eventually — the difference is when. In a while loop, the condition is checked first, so the body may never run. In a do-while loop, the body runs first, so it always runs at least once, then the condition is checked.
Q24. In which situation would you prefer a do-while loop over a while loop?
Answer: C) When the loop body needs to be executed at least once
Common scenarios: reading a menu choice from a user (you must show the menu at least once), validating input (you must ask the first time), and retry loops (the first attempt must happen regardless). All of these fit the do-while pattern.
Q25. What will happen when the condition in a do-while loop always remains true?
Answer: D) The loop executes indefinitely
If the condition is always true, the loop never exits — same as an infinite while loop. Just like with a while loop, you'd need break, return, or exit() to escape. The do-while structure doesn't protect you from infinite loops; it only guarantees at least one iteration.
Q26. How can a variable be incremented inside a while or do-while loop?
Answer: D) Only A and B are correct
Both i += 1 and i++ correctly increment the variable by 1. Option C (i = i++) is undefined behaviour in C — it modifies i twice between sequence points, which the C standard says can produce any result. Never write code like i = i++; it's a well-known trap and can behave differently on different compilers.
for Loop
Q27. How many times will the following loop execute?
for(int i = 20; i >= 10; i -= 2) {
// Some code
}
Answer: B) 6 times
The variable i takes the values 20, 18, 16, 14, 12, 10 — all greater than or equal to 10. That's 6 values, so 6 iterations. When i becomes 8, the condition i >= 10 is false and the loop ends. Quick check: (20 − 10) / 2 + 1 = 5 + 1 = 6.
Q28. What will be the output of the following loop?
for(int x = 5; x > 0; x -= 1) {
printf("%d ", x);
}
Answer: A) 5 4 3 2 1
x starts at 5 and decreases by 1 each time, and the loop runs while x > 0. So it prints 5, 4, 3, 2, 1. When x becomes 0, the condition is false and the loop ends. This is the classic "countdown" pattern — the simplest use of a for loop.
Q29. What will be the output of the following loop?
int j = 0;
while(j < 5) {
printf("%d ", j * 2);
j += 1;
}
Answer: B) 0 2 4 6 8
j goes from 0 to 4 (five values, all less than 5). Each iteration prints j × 2. So it prints 0, 2, 4, 6, 8. When j becomes 5, the condition j < 5 is false and the loop ends. The value 10 is never printed because the loop exits before that iteration.
Q30. Which of the following is the correct nested for loop in C?
Answer: D) for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { // code } }
Every for loop needs all three parts: initialization, condition, and increment. Option A is missing the inner increment, B is missing a closing brace, and C is missing the outer increment. Only D has correct syntax for both loops and matching braces.
Q31. How do you create an infinite loop in C?
Answer: D) All of the above
C gives you several ways to write an intentional infinite loop. while(1) and do-while(1) both test a non-zero (true) condition. for(;;) is a for loop with no condition at all — C treats the missing condition as always true. All three are valid and used in real code (like event loops or servers).
Nested Loops
Q32. What will be the output of the following nested loop?
for(int x = 0; x < 4; x++) {
for(int y = 0; y < 2; y++) {
if(y == 1) {
break;
}
printf("%d %d ", x, y);
}
}
Answer: C) 0 0 1 0 2 0 3 0
The inner loop runs y = 0, then y = 1. When y == 1, break exits the inner loop immediately — so printf never runs for y = 1. Only the y = 0 print happens each outer iteration. The outer loop runs x = 0, 1, 2, 3, printing "0 0", "1 0", "2 0", "3 0". That's option C.
Q33. What happens if the break statement is used inside a nested loop?
Answer: C) Only the innermost loop is exited
In a nested loop, break only affects the loop it's directly inside — that is, the innermost one. The outer loops continue as if nothing happened. If you need to break out of multiple loops, you'd use a flag variable or restructure the code into a function with a return.
break, continue & Loop Keywords
Q34. Which keyword is used to skip the current iteration of a loop?
Answer: B) continue;
continue skips whatever is left in the current iteration and jumps straight to the next iteration's condition check (or the increment step, in a for loop). break, by contrast, exits the loop entirely. pass and skip are not C keywords — pass is Python, and skip doesn't exist in C.
Q35. Which keyword is used to exit a loop in C?
Answer: A) break;
break is the dedicated keyword for exiting a loop. return exits the entire function (which may contain other code after the loop), and exit() ends the whole program — both are heavier. continue does the opposite of exit — it keeps the loop going but skips one iteration.
Q36. What is the difference between break and continue in a loop?
Answer: C) break exits the loop entirely, continue skips only the current iteration
This is a must-remember distinction. break = "I'm done, exit the loop." continue = "Skip the rest of this iteration, but keep looping." Many beginners mix these up because they sound similar. If you want to stop looping, use break. If you want to skip one bad value and move on, use continue.
Q37. Which loop is best when the number of iterations is known in advance?
Answer: C) for loop
The for loop packs all three pieces into one line: initialization, condition, and increment. When you know exactly how many times to run (like "repeat 10 times" or "for each element in the array"), the for loop is the cleanest choice. For unknown iteration counts, use while. For at-least-once scenarios, use do-while.
Q38. What is the output of the following do-while loop that increments a counter and prints it, starting at 0, until counter < 3?
int counter = 0;
do {
counter++;
printf("%d ", counter);
} while (counter < 3);
Answer: B) 1 2 3
Notice the order: increment, then print, then check. So the first iteration increments counter to 1, prints 1, checks 1 < 3 (true). Second iteration: 2, prints 2, checks (true). Third iteration: 3, prints 3, checks 3 < 3 (false) — loop ends. Output: 1 2 3. The initial 0 is never printed because the increment happens before the print.
Q39. In a nested loop with two levels, if the outer loop runs m times and the inner runs n times per outer iteration, how many total iterations occur?
Answer: B) m × n
Each outer iteration runs the inner loop n times, and there are m outer iterations. So total inner-body executions = m × n. For example, a 3-row, 4-column table loop runs the inner body 12 times. This is exactly how nested loops print 2D grids, multiplication tables, and star patterns.
Q40. What is the output of the following loop?
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
printf("%d ", i);
}
Answer: B) 1 2 4 5
The loop increments i first, then checks if i == 3. When i becomes 3, continue skips the printf for that iteration — but the loop keeps running. So 3 is never printed, and the output is 1 2 4 5. This is a classic "skip a specific value" pattern using continue.
Frequently Asked Questions
What is the difference between while, do-while, and for loops in C?
A while loop checks the condition before running the body. A do-while loop runs the body at least once, then checks the condition. A for loop is best when you know how many times to run — it combines initialization, condition, and increment in one line. All three can express the same logic; choose the one that reads most clearly for the situation.
When should I use a do-while loop instead of a while loop?
Use do-while when the body must run at least once regardless of the condition. Common cases are menu-driven programs (you must show the menu first), input validation (you must ask at least once), and retry loops. If the very first check could be false and that should skip the body entirely, use a while loop instead.
What does the break statement do inside a nested loop?
break only exits the innermost loop where it appears. The outer loops continue running as normal. If you need to exit multiple loops at once, use a flag variable (int done = 0; check it in the outer condition), restructure the code into a function with a return statement, or use goto (rarely recommended for readability reasons).
Why is i = i++ considered undefined behaviour in C?
i = i++ modifies i twice between sequence points — once by the post-increment, and once by the assignment. The C standard says such an expression has undefined behaviour, which means the compiler can produce literally any result (sometimes 1, sometimes the old value, sometimes a crash). Never write code like i = i++. Use i++ or i += 1 on its own line, or i = i + 1 if you prefer explicit style.
How do I create an infinite loop in C, and how do I break out of it?
The most common ways to write an infinite loop are while(1) { }, for(;;) { }, and do { } while(1). All three are valid and used in real code. To exit such a loop, place a break; inside the body where you want to stop, or use return to leave the enclosing function, or exit() to end the program entirely.
💡 Tip: Loops are the most-tested topic in C exams after operators. Practise tracing output by running small loops in your head, line by line. Pay special attention to the order of operations — whether i++ happens before or after a printf can change the answer completely.