Loops and Iteration Interview Questions in C: 14 Model Answers with Exact Output Traces
Can you predict a C loop after continue moves an update or unsigned arithmetic crosses zero? Work through 14 interview-ready answers, exact traces and termination checks.
KnowledgeGate Team
Exam prep & CS education

Candidates can name for, while and do-while. Insert continue, move an increment or make the counter unsigned, and memorised definitions fail.
A reliable loop answer follows state, test, body and update, then proves the stop. The broader C Programming Interview Questions for Freshers set covers the wider interview syllabus. For dependent bounds, growth classes and asymptotic counting, see Iterative Loops Interview Questions and Answers; this page stays with C control-flow semantics, integer boundaries and exact output traces.
for, while and do-while: three answers before any output puzzle
Question 1: What is iteration in C?
Iteration repeats while its controlling expression is nonzero. Name the initial state, condition, body and state change. C treats zero as false and nonzero as true; the expression need not be Boolean.
Question 2: When should I use for, while or do-while?
for groups counter-oriented initialisation, test and update; while uses prior state. do-while tests after its body, so this leaves attempts = 1:
int attempts=0;
do {++attempts;} while(attempts<0);These are semantic choices, not rules.
Question 3: In what order does a for loop execute?
For for(int i=0;i<4;++i), initialisation runs once. Tests at i = 0, 1, 2, 3 succeed, each followed by body and update; i = 4 fails. Thus condition, body and update run 5, 4 and 4 times. This i is loop-scoped. Since the update value is discarded, i++ and ++i both add one, with no universal performance difference. The same order applies in any C loop.
Fully worked trace: continue, break and the update that does not run
Question 4: What are the exact output, final i and final sum?
int i;
int sum = 0;
for (i = 1; i <= 6; ++i) {
if (i == 3) continue;
sum += i;
if (sum >= 10) break;
printf("%d ", sum);
}
printf("| %d %d\n", i, sum);
| Action |
| Printed | Next step |
|---|---|---|---|---|
1 | Add 1 | 1 |
| Update to 2 |
2 | Add 2 | 3 |
| Update to 3 |
3 |
| 3 | Nothing | Update to 4 |
4 | Add 4 | 7 |
| Update to 5 |
5 | Add 5, then | 12 | Nothing | Exit with |
Exact output: 1 3 7 | 5 12. There is no i = 6 row because break skips its update.

Question 5: What exactly do continue and break do in a for loop?
At i = 3, continue proceeds to update and the next test. At i = 5, break exits the nearest loop, skipping its remaining body and update.
Why moving one increment can change termination
Question 6: Is every for loop mechanically equivalent to a while loop?
This for loop totals 0 + 1 + 3 + 4 = 8:
int total=0;
for(int i=0; i<5; ++i){ if(i==2) continue; total+=i; }A naive rewrite stalls:
int i=0;
while(i<5){ if(i==2) continue; total+=i; ++i; }At i = 2, continue skips ++i, so progress stops. Put the update on every continuing path:
while(i<5){ if(i!=2) total+=i; ++i; }From i = 0, total = 0, the repair ends at i = 5, total = 8.
Question 7: How do while (i--) and while (--i) differ?
With signed int i=3; while(i--) printf("%d ",i);, the old value is tested; it prints 2 1 0 and ends at i = -1. Separately, int j=3; while(--j) printf("%d ",j); decrements first, prints 2 1 and ends at j = 0. A for update's value is discarded.
Boundary questions: off-by-one errors and unsigned reverse loops
Question 8: Why is < length usually correct for array traversal?
int a[5]={4,7,1,9,2}, total=0;
for(int i=0; i<5; ++i) total+=a[i];Indices 0, 1, 2, 3, 4 form [0, 5), and 4 + 7 + 1 + 9 + 2 = 23. i <= 5 attempts out-of-bounds a[5], causing undefined behaviour. It promises neither garbage nor zero.
Question 9: Why can for (unsigned i = 3; i >= 0; --i) run forever?
Every unsigned value is at least zero, so the condition stays true. A print body begins 3, 2, 1, 0; decrementing zero gives UINT_MAX by defined arithmetic, 4,294,967,295 for an illustrative 32-bit unsigned int.
for(size_t i=4;i>0;--i) printf("%zu ",i-1); prints 3 2 1 0 and exits at i = 0. Test a quantity that can cross the boundary.
Nested loops: nearest break, exact counts and time complexity
Question 10: Does break exit all nested loops?
int visits=0;
for(int i=1;i<=3;++i)
for(int j=1;j<=4;++j){ if(j==3) break; ++visits; }For each outer i, the inner body counts j = 1, 2, then breaks at j = 3. The outer loop continues, so visits = 2 + 2 + 2 = 6. Plain break exits only the nearest loop; use a flag, function return or clear redesign to leave both.
Question 11: How do you count a triangular nested loop?
int count=0;
for(int i=1;i<=4;++i)
for(int j=1;j<=i;++j) ++count;Inner counts 1, 2, 3, 4 give count = 1 + 2 + 3 + 4 = 10. For n, total is n(n + 1) / 2, hence Theta(n^2). Bounds matter: nested loops are not automatically quadratic; an n by n rectangle is.

The deeper answer: invariant, progress and proof of termination
Question 12: What is a loop invariant?
A loop invariant is true before every iteration and supports correctness. For int sum=0; for(int i=1;i<=4;++i) sum+=i;, before body value i, let sum equal 1 + ... + (i - 1). States are (1, 0), (2, 1), (3, 3), (4, 6). Initialisation establishes it, addition maintains it, and exit at i = 5 gives sum = 10.
Question 13: How do you prove that a loop terminates?
Choose a bounded measure moving towards exit on every path. Here, 5 - i is 4, 3, 2, 1, then the i = 5 test fails. Question 6 stalls at i = 2 because its measure stops changing. Check continue, error and early-exit paths.
One practical mock: filtering, accumulation and early exit
Question 14: Scan readings, ignore negatives and stop once the accepted total reaches 15. What does the loop report?
int readings[6] = {4, -2, 7, 5, -1, 9};
int i;
int total = 0;
int accepted = 0;
for (i = 0; i < 6; ++i) {
if (readings[i] < 0) continue;
total += readings[i];
++accepted;
if (total >= 15) break;
}
printf("%d %d %d\n", i, total, accepted);Trace: i=0,value=4 -> total=4,accepted=1; i=1,value=-2 -> continue; i=2,value=7 -> total=11,accepted=2; i=3,value=5 -> total=16,accepted=3 -> break. Exact output: 3 16 3. Four elements are inspected; indices 4 and 5 are not. Testing after addition enforces the threshold.
A fundamentals-first screen may ask for output and definitions. A depth-oriented follow-up may ask about the invariant, path progress, inspected count or a moved threshold test.
Answer as state -> test -> body branch -> update -> exit. Additional output practice is available in Loops & Iteration MCQs in C. Use the examples here to rehearse exact state transitions before you generalise them.
Thirty-second loops recap and the next practice step
Keep this checksum speakable:
fororders initialisation, test, body and update.whiletests before the body.do-whiletests after one execution.continuein aforproceeds to its update.breakexits the nearest loop and skips that update.Array bounds are half-open.
Unsigned counters need a condition that can become false.
Sum actual inner counts for nested-loop complexity.
Without notes, reproduce 1 3 7 | 5 12; postfix from 3 prints 2 1 0 and ends at -1; safe reverse prints 3 2 1 0; the triangle counts 10; readings print 3 16 3. Explain each transition.
Use the C Language Course for structured C practice, or browse Placement Preparation & Coding for broader multi-language work.
Keep learning
Loops and Iteration in C: for, while, do-while, Worked Traces and Exam Patterns
Learn a four-checkpoint method for tracing C loops, then apply it to sums, nested loops, break, continue, off-by-one errors and exam-style iteration counts.
Switch and Jump Statements Interview Questions: C Model Answers and Output Traces
Prepare for C control-flow interviews with precise definitions, a complete score trace, common switch traps and a practical multi-loop goto example.
C Data Types Interview Questions and Answers: 12 Model Answers with Output Traces
Prepare C fundamentals with speakable answers, portable type rules and step-by-step output traces for integer division, promotions and unsigned conversion.
Loops & Iteration MCQs in C: 12 Solved Questions with Explanations
Solve 12 previous-year C loop MCQs by checking conditions, tracing each pass and recognising the mathematics hidden inside short code fragments.





