Iterative Loops in C for GATE: Code Traces, Worked Examples and Exam Traps
Learn a mechanical method for tracing C loops. Work through exact values, invariants, nested-loop counts, complexity, termination bugs, and output traps.
KnowledgeGate Team
Exam prep & CS education

You may recognise for, while, and do-while syntax but still lose the answer while tracking the condition, body, and update together. The solution is a repeatable state-trace method. Use two complete numerical traces and a checklist to solve output, iteration-count, complexity, and bug-finding questions. Use the C Programming Course if you also want a structured refresh of control-flow fundamentals.
Iterative loops in C: read every loop as a state transition
An iteration moves a program from one state to the next. Read each loop in four phases: initialise, test, execute the body, then update.
for and while are entry-controlled, so they test before the body. do-while is exit-controlled, so its body runs before the first test.
For example, for (i = 2; i <= 8; i += 2) is mechanically equivalent to:
i = 2;
while (i <= 8) {
/* body */
i += 2;
}The equivalence holds only if the update runs on every path through the body.

For, while and do-while: trace exact values before predicting output
Trace this loop by recording the state after its body and update:
int i, sum = 0;
for (i = 2; i <= 8; i += 2) {
sum += i;
}Point | State | Test or action |
|---|---|---|
Initial |
|
|
After iteration 1 |
| added 2, updated |
After iteration 2 |
| added 4, updated |
After iteration 3 |
| added 6, updated |
After iteration 4 |
| added 8, updated |
Exit |
|
|
After resetting sum = 0, the equivalent loop is i = 2; while (i <= 8) { sum += i; i += 2; }. Its trace matches phase for phase because it has the same initialisation, test, body, and update.
Now consider int x = 5, count = 0; do { count++; x -= 2; } while (x < 0);. The body runs once, producing x = 3 and count = 1. Only then does 3 < 0 fail, so count = 0 is impossible.
Loop invariants and iteration counts: prove instead of guessing
For the even-number sum, write the top-of-loop value as i = 2k. Before the body, the invariant is sum = k(k - 1). At k = 1, the state is (i, sum) = (2, 0).
Adding 2k changes the sum to k(k - 1) + 2k = k(k + 1). Updating gives i = 2(k + 1), so the same invariant holds for the next state. At exit, k = 5, giving i = 10 and sum = 5(4) = 20.
For int steps = 0; for (i = 1; i <= 20; i *= 2) { steps++; }, the body values are 1, 2, 4, 8, 16. The fifth update makes i = 32, the condition fails, and steps = 5. In general, for positive integer N >= 1, doubling from 1 while i <= N takes floor(log2 N) + 1 iterations.
Nested iterative loops: count executions and accumulated value
Trace this triangular loop row by row:
int count = 0;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= i; j++)
count += i + j;
| Added values | Row total | Cumulative total |
|---|---|---|---|
1 | 2 | 2 | 2 |
2 | 3, 4 | 7 | 9 |
3 | 4, 5, 6 | 15 | 24 |
4 | 5, 6, 7, 8 | 26 | 50 |
The body executes 1 + 2 + 3 + 4 = 10 times, not 16. With an outer limit n, that count is n(n + 1)/2, which has quadratic growth. Keep execution count 10 separate from the accumulated value count = 50. This is how loop counts become a running-cost model in topics such as Sorting Algorithms: Complexity and Comparison.

Iterative-loop exam traps: update paths, semicolons and unsafe expressions
In for (int i = 1; i <= 6; i++) { if (i == 4) continue; s += i; }, starting with s = 0, the included values are 1, 2, 3, 5, 6. Therefore, s = 17. A for loop still performs its update after continue.
The behaviour differs when the update is inside a while body:
int i = 0;
while (i < 5) {
if (i == 2) continue;
i++;
}At i = 2, continue skips i++, so the state remains 2 forever. Update before continue, or restructure the body so every path advances the state.
In int i; for (i = 0; i < 5; i++); printf("%d", i);, the semicolon is the empty loop body. The later statement runs once and prints 5.
Finally, never assign a deterministic result to i = i++. It modifies i through unsequenced operations, which is undefined behaviour in C, not a normal post-increment trace.
GATE-style iterative-loop questions: one checklist for four forms
Question signal | Method | Common trap |
|---|---|---|
Output trace | Record state after body and update | Mixing old and new values |
Exact iteration count | List control values through the failed test | Omitting the exit value |
Time complexity | Sum inner-loop bounds | Multiplying bounds blindly |
Bug or termination | Inspect tests and every update path | Missed |
Also reject undefined expressions instead of inventing an output. The same idea scales from one loop variable to tables of reusable subproblem states in Dynamic Programming Explained: 0/1 Knapsack.
Iterative loops in C: the short version and next step
For revision, write the initial state, mark the loop as entry-controlled or exit-controlled, trace body and update separately, stop at the first failed condition, and verify that every path progresses.
The two anchors are simple: the even-number loop ends with sum = 20 and i = 10; the triangular nested loop executes 10 times and accumulates count = 50. For a broader study plan, use GATE Guidance by Sanchit Sir. The GATE CS Exam Preparation category connects loop tracing to the rest of the syllabus.
Keep learning
Iterative Loops Interview Questions and Answers: Output, Complexity and Code
Learn a repeatable way to trace loop values, count exact work and derive tight bounds. Includes nested-loop sums, control-flow output and ten interview answers.
Time Complexity of Loops MCQs: 12 Solved Questions with Explanations
Solve 12 previous-year loop questions by counting iterations, tracing values, summing dependent loops and separating running time from return values.
C++ Iterators and Invalidation: Container Rules and Safe Erase Loops
Learn to read iterator ranges, match algorithms to categories, and mutate standard containers without using stale handles. Includes two fully traced erase examples.
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.





