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.

KnowledgeGate Team

Exam prep & CS education

Updated 27 Oct 20266 min read

You may recognise the syntax of for, while, and do-while, yet still lose marks when a question asks for the output, the exact iteration count, or whether the body runs at all. The reliable way through is to trace four checkpoints in order: initialise the state, test the condition, execute the body, and update the state. The method applies to concrete values, nested loops, and statements that change normal flow. Use a repeatable process to solve common loop questions confidently without blind guessing.

Loops and iteration in C: the state-change model

Iteration means repeatedly executing a statement or block while program state permits it. If i is the control variable, initialise it, test the Boolean condition, execute the body when that test is true, update i, and return to the test. Termination requires relevant state to change eventually.

Both for and while are entry-controlled loops because they test before executing the body. Their bodies may therefore run zero times. A do-while loop is exit-controlled: it executes the body first and checks afterward, so the body runs at least once.

These ideas support later work with arrays and algorithms. Coding & DSA Courses for Placements places them within a wider programming foundation. The earlier Control Flow in C Programming: If-Else, Loops, Switch and Jump Statements with Worked Examples supplies the full selection-iteration-jump map; loop-specific work concentrates on state changes, exact counts and termination.

for, while, and do-while loops: when each form fits

Form

Order

Minimum body executions

Best fit

for

initialise, test, body, update

0

Clear control-variable progression

while

test, body, explicit update

0

Condition-controlled repetition

do-while

body, explicit update, test

1

One required execution before testing

for (i = 3; i >= 1; i--) printf("%d ", i); is equivalent to i = 3; while (i >= 1) { printf("%d ", i); i--; }. Each tests 3, 2, 1, 0, prints 3 2 1, and stops when i = 0 makes the condition false.

Zero-entry behaviour exposes the key difference. int x = 0; while (x > 0) { printf("%d", x); x--; } prints nothing. In contrast, int x = 0; do { printf("%d", x); x--; } while (x > 0); prints 0 once and finishes with x = -1. The semicolon after the do-while condition is required.

Loop tracing worked example: sum of squares from 1 to 5

Consider this complete example:

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

The initial state is i = 1, sum = 0. The loop continues while i <= 5, and i++ performs the update after each body execution.

Iteration

i before body

i*i

sum after body

1

1

1

1

2

2

4

5

3

3

9

14

4

4

16

30

5

5

25

55

At i = 1, the program adds 1, giving sum = 1. At i = 2, it adds 4 for sum = 5; at i = 3, it adds 9 for sum = 14; at i = 4, it adds 16 for sum = 30; and at i = 5, it adds 25 for sum = 55. The update makes i = 6. The test 6 <= 5 is false, so the program prints 55. That failed check is not a sixth iteration.

Trace table for the sum-of-squares loop showing i, i squared, and the running sum reaching 55.

Nested loops: count inner executions before naming complexity

Now trace a nested loop whose inner bound depends on the outer variable:

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

The inner body runs once for i = 1, then two, three, and four times for i = 2, 3, and 4. The total is 1 + 2 + 3 + 4 = 10, so the program finishes with count = 10.

For an upper bound n, the same count is 1 + 2 + ... + n = n(n + 1)/2, which grows as Theta(n^2). Nested loops are not automatically quadratic because their bounds and updates determine the count. Also keep body executions separate from condition checks: the failed inner test after each row does not increment count.

Triangular grid of inner-loop (i, j) pairs for i = 1 to 4, totalling 10 executions.

break, continue, and non-termination in C loops

Trace this loop in execution order:

c
int total = 0;
for (i = 1; i <= 10; i++) {
    if (i % 3 == 0) continue;
    total += i;
    if (total > 20) break;
    printf("%d ", i);
}

It prints 1 2 4 5 7. At i = 3 and i = 6, continue skips the rest of the body, yet the for update still advances i. At i = 8 the running total reaches 27, so break exits before the print, and the loop never tests i = 9.

break exits only the nearest loop. continue starts its next iteration. In a while loop, an update below continue can be skipped, so update the control state first or restructure the condition.

In i = 1; while (i <= 5) { printf("%d ", i); }, i remains 1, so the condition stays true. Some infinite loops are intentional, but every trace must explain termination or non-termination.

Loop traps: bounds, semicolons, and hidden state changes

An inclusive bound can cause an off-by-one error. for (i = 0; i <= 5; i++) executes for 0, 1, 2, 3, 4, 5, meaning six body executions. Using < 5 gives five, from 0 through 4. For unit steps from a through b inclusive, the count is b - a + 1 when a <= b.

A stray semicolon creates an empty body: for (i = 0; i < 3; i++); printf("%d", i);. The empty loop finishes with i = 3, then the later printf runs once and outputs 3. Remove the semicolon and use braces around the intended body.

Finally, trace every place that changes the control variable. In for (i = 0; i < 6; i++) { i++; }, the body-entry values are 0, 2, 4. The body increment and loop update together add 2, so there are three body executions and the loop ends at i = 6.

Loops and iteration exam patterns: a five-step method

Output prediction, exact iteration counts, equivalent loop forms, zero-versus-one execution, and nested-loop growth are common CS exam question forms.

For a multiplicative update, consider count = 0; for (i = 1; i <= 16; i *= 2) count++;. The successful values are i = 1, 2, 4, 8, 16, so count = 5. The next update produces i = 32; 32 <= 16 is false. Multiplicative updates usually produce logarithmic iteration counts, a connection developed further in Time Complexity & Asymptotic Notation: Big-O.

Use this five-step solving method every time:

  1. Write the initial values.

  2. List each successful condition check.

  3. Record every change made inside the body.

  4. Apply the loop update in its correct position.

  5. Evaluate the first failed condition separately.

For mixed programs that combine loops with arrays, recursion, undefined behaviour and complexity, use Coding Questions for GATE CS: Trace Outputs and Complexity Step by Step. Loop-only tracing isolates checkpoint order and exact iteration counts.

Loops and iteration in C: the short version and next step

Trace loops as initialise, test, body, and update. Remember that do-while executes once before testing, count actual body executions rather than failed checks, and follow each state change caused by break, continue, or a second update.

If you need the language sequence and practice around this topic, continue with the C Programming course.