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

Updated 14 Nov 20265 min read
Iterative Loops in C for GATE: Code Traces, Worked Examples and Exam Traps

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:

c
i = 2;
while (i <= 8) {
    /* body */
    i += 2;
}

The equivalence holds only if the update runs on every path through the body.

Flowchart of the loop from i = 2 to 8 in steps of 2, with a trace table showing sum reaching 20 and the test failing at i = 10.

For, while and do-while: trace exact values before predicting output

Trace this loop by recording the state after its body and update:

c
int i, sum = 0;
for (i = 2; i <= 8; i += 2) {
    sum += i;
}

Point

State (i, sum)

Test or action

Initial

(2, 0)

2 <= 8, true

After iteration 1

(4, 2)

added 2, updated i

After iteration 2

(6, 6)

added 4, updated i

After iteration 3

(8, 12)

added 6, updated i

After iteration 4

(10, 20)

added 8, updated i

Exit

(10, 20)

10 <= 8, false

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:

c
int count = 0;
for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= i; j++)
        count += i + j;

i

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.

Triangular nested-loop trace for rows i = 1 to 4 showing the i + j values, row totals 2, 7, 15, 26 and 10 body executions.

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:

c
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 continue or semicolon

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.