CS Atlas GATE 2027
PDS course
0 / 103 complete

PDS/Phase 1 · Programming in C

Lesson 1.2.2 · Item 15 of 103

for, while, do-while; break and continue; tracing loop output and counting iterations

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

  • In a switch with no break, after the matching case runs, what happens? from Lesson 1.2.1
  • What is 1 + 2 + … + 20? from Lesson 0.2.2
  • After int a = 3; int b = a; a = 7;, what is b? from Lesson 0.1.2

1 · The Hook

🎣

A shop prints a receipt line for each of 120 items. Writing 120 printf statements is absurd, and the number of items changes every day. A loop repeats a block of statements while a condition holds, so one short piece of code handles any count.

2 · Predict First

🤔

What does this print?

int i;
for (i = 0; i < 5; i++);
printf("%d\n", i);

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

💡

Predict First — the answer

Output

5

The semicolon right after for (…) is an empty body. The loop just counts i up to 5 and stops; then printf runs once and prints 5.

while

while (condition)
    body;          /* test first; repeat while non-zero */

The condition is tested before each pass. If it is false at the start, the body never runs. Each run of the body is one iteration.

for

for (init; condition; update)
    body;

is the same as init; while (condition) { body; update; }. Any part may be empty; for (;;) loops forever. The comma operator lets you update two variables: for (i = 0, j = 10; i < j; i += 2, j -= 3).

do-while

do {
    body;
} while (condition);   /* note the semicolon */

The body runs first, then the condition is tested, so it always runs at least once.

break and continue

break leaves the innermost loop (or switch) at once. continue skips the rest of this iteration: in a for it jumps to the update, in a while or do-while straight to the condition test.

int i;
for (i = 1; i <= 10; i++) {
    if (i % 3 == 0) continue;
    if (i == 8) break;
    printf("%d ", i);
}
printf("\n");

Output

1 2 4 5 7

Counting iterations

  • for (i = a; i < b; i += s) with a < b runs ⌈(b − a)/s⌉ times; with <= it runs ⌊(b − a)/s⌋ + 1 times.
  • for (i = 1; i <= n; i *= 2) runs ⌊log₂ n⌋ + 1 times (i = 1, 2, 4, …).
  • Nested loops multiply, or add up like the series of Lesson 0.2.2: for i in 1..n, for j in 1..i gives 1 + 2 + … + n = n(n + 1)/2.
  • while (n--) runs n times and leaves n at −1.

Tracing a loop

Make a trace table with one row per iteration: the values when the condition is tested, what the body prints, and the values after the update.

int i, sum = 0;
for (i = 1; i <= 4; i++) {
    sum = sum + i;
    printf("i=%d sum=%d\n", i, sum);
}

Output

i=1 sum=1
i=2 sum=3
i=3 sum=6
i=4 sum=10

4 · Visual

Diagram: init · i = 1 · test · i <= 4 ? · true · body · sum += i · update · i++

A for loop: init once, then test → body → update, round and round; when the test is false the loop ends.

Trace of: for (i = 1; i <= n; i *= 2) for (j = 1; j <= i; j++) count++;   with n = 16

 i  | inner runs (j = 1..i) | count after
----+-----------------------+------------
 1  |  1                    |   1
 2  |  2                    |   3
 4  |  4                    |   7
 8  |  8                    |  15
 16 | 16                    |  31
 32 | (test 32 <= 16 fails: loop ends)

 count = 1 + 2 + 4 + 8 + 16 = 2^5 - 1 = 31

One row per outer iteration; the inner loop’s work is summed with the doubling series of Lesson 0.2.2.

5 · Worked Examples

Example 1 Easy

What does this print?

int n = 3;
while (n--)
    printf("%d ", n);
printf("| n = %d\n", n);

Solution

n-- tests the old value, then decrements. Tests: 3 (true, n becomes 2, prints 2), 2 (prints 1), 1 (prints 0), 0 (false, but n still becomes −1).

Output

2 1 0 | n = -1

Example 2 Medium

What value of c is printed?

int n = 6, i, j, c = 0;
for (i = 1; i <= n; i++)
    for (j = i; j <= n; j++)
        c++;
printf("%d\n", c);

Solution

For each i, j runs from i to 6: 6 − i + 1 times. Total 6 + 5 + 4 + 3 + 2 + 1 = 21 = 6 × 7/2.

Output

21

Example 3 GATE

What is printed?

int n = 16, i, j, count = 0;
for (i = 1; i <= n; i *= 2)
    for (j = 1; j <= i; j++)
        count++;
printf("%d\n", count);

Solution

i takes 1, 2, 4, 8, 16. The inner loop runs i times, so count = 1 + 2 + 4 + 8 + 16 = 31 (see the trace table above).

Output

31

6 · GATE Traps

⚠️

  • A semicolon after for (…) or while (…) makes an empty body.
  • do-while runs at least once even if the condition is false at the start.
  • continue in a for still runs the update; in a while it may skip your increment and loop forever.
  • After for (i = 0; i < n; i++) ends, i equals n, not n − 1.
  • while (n--) leaves n = −1, not 0.
  • break leaves only the innermost loop.

8 · Memory Hook

🧠

“Test first (while), test last (do-while), all-in-one (for).” continue = “next please”; break = “everybody out”.

9 · Connect the Dots

Lesson 1.2.1 chose between paths; loops repeat them, and counting their iterations uses the series and logarithms of Phase 0. Programs are now getting long. Next lesson answers: how can we package a piece of work under a name and reuse it with different inputs?

10 · Self-Test

GATE format. Try every question before opening the vault.

Q1MCQ1 markOutput

What does this print?

int x = 5;
do {
    x -= 2;
    printf("%d ", x);
} while (x > 0);
printf("\n");
  • (A) 3 1 -1 -3
  • (B) 3 1
  • (C) 3 1 -1
  • (D) 5 3 1

Q2NAT1 markIterations

How many times does c++ run?

int i, c = 0;
for (i = 1; i < 100; i *= 3)
    c++;
printf("%d\n", c);

Numerical answer: ________

Q3NAT2 markscontinue

What does this print?

int i = 0, s = 0;
while (i < 10) {
    i++;
    if (i % 2) continue;
    s += i;
}
printf("%d\n", s);

Numerical answer: ________

Q4NAT2 marksNested loops

What does this print?

int i, j, c = 0;
for (i = 0; i < 10; i++)
    for (j = 0; j < i; j++)
        c++;
printf("%d\n", c);

Numerical answer: ________

Q5MSQ1 markLoops

Which statements are true?

  • (A) A do-while body runs at least once.
  • (B) for (;;) is an infinite loop unless something breaks out.
  • (C) After for (i = 0; i < 10; i++);, i is 10.
  • (D) continue inside a for loop skips the update expression.

11 · Rule Card

  • while: test then body; do-while: body then test (≥ 1 run); for (init; test; update).
  • for ≡ init; while (test) { body; update; }
  • break leaves the innermost loop/switch; continue skips to update (for) or test (while).
  • i = a..b−1 step s: ⌈(b − a)/s⌉ runs; i *= 2 up to n: ⌊log₂ n⌋ + 1 runs.
  • Nested dependent loops: add the series (n(n + 1)/2, 2ᵏ⁺¹ − 1 …).
  • Stray ; after for/while = empty body; while (n--) ends with n = −1.

12 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

loopiterationwhilefordo-whilebreak (in loops)continueinfinite loopiteration count

13 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Recall Warm-up

  • Execution falls through into the following cases until a break or the end.
  • 3: assignment copies the value; later changes to a do not affect b.

Self-Test

Q1 Answer: (C)1 mark

x: 5 → 3 (print, 3 > 0) → 1 (print, true) → −1 (print, false, stop).

Output

3 1 -1
  • (A) is wrong: The loop stops after −1 fails the test.
  • (B) is wrong: The body runs before the test, so −1 is printed.
  • (D) is wrong: 5 is never printed: x is reduced first.

Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations

Q2 Answer: 51 mark

i = 1, 3, 9, 27, 81; next 243 fails: 5.

Output

5

Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations

Q3 Answer: 302 marks

i runs 1..10; odd i hits continue; s = 2 + 4 + 6 + 8 + 10 = 30.

Output

30

Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations

Q4 Answer: 452 marks

Inner loop runs i times for i = 0..9: 0 + 1 + … + 9 = 45.

Output

45

Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations

Q5 Answer: (A) (B) (C)1 mark

continue in a for jumps to the update, which still runs.

  • (D) is wrong: The update still runs after continue.

Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.