ACompressed recap
🧾
if/else choose by any non-zero condition; an else pairs with the nearest unmatched if; braces decide what belongs to a branch. switch jumps to the matching integer-constant case (or default) and falls through until break.
Loops: while tests first, do-while tests last (at least one run), for (init; test; update) is a packaged while. break leaves the innermost loop; continue skips to the update (for) or the test (while). Count iterations with the step formula, ⌊log₂ n⌋ + 1 for doubling counters, and the Phase 0 series for nested loops.
BMerged rule card
1.2.1 if / else if / else; nested conditions; switch and fall-through
- if (cond) S1; else S2; — cond non-zero → S1.
- Braces group statements; without them one statement belongs to the if.
- else ladder: first true branch runs, rest skipped.
- Dangling else binds to the nearest unmatched if.
- switch: evaluate once, jump to matching case/default, run down until break or }.
- Case labels: distinct integer constants (chars allowed).
- if (x = 5) assigns; compare with ==.
1.2.2 for, while, do-while; break and continue; tracing loop output and counting iterations
- 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.
CInterleaved GATE-style questions
Q1MCQ1 markOutput
What does this print?
int i, j;
for (i = 0, j = 10; i < j; i += 2, j -= 3)
printf("(%d,%d) ", i, j);
printf("\n");
- (A)
(0,10) (2,7) - (B)
(0,10) (2,7) (4,4) - (C)
(0,10) (2,7) (4,4) (6,1) - (D)
(0,10)
Q2NAT1 markIterations
How many times does the body of for (i = 3; i < 40; i += 5) run?
Numerical answer: ________
Q3MCQ2 marksOutput
What does this print?
int k = 10;
do {
printf("%d ", k);
k++;
} while (k < 5);
printf("| k = %d\n", k);
- (A)
10 11 12 13 14 | k = 15 - (B) nothing; the loop never runs
- (C)
| k = 10 - (D)
10 | k = 11
Q4MCQ1 markif
What is printed by int x = 0; if (x == 0); printf("A"); printf("B\n");?
- (A)
AB - (B)
B - (C)
A - (D) nothing
Q5NAT2 marksSwitch
With int n = 3, s = 0; what is s after switch (n) { case 1: s += 1; case 3: s += 3; case 5: s += 5; break; default: s += 100; }?
Numerical answer: ________
Q6NAT2 marksNested loops
How many times does the innermost statement run in for (i = 1; i <= 10; i++) for (j = 1; j <= 10; j++) if (j >= i) count++;?
Numerical answer: ________
Q7MCQ1 markbreak
In a for loop nested inside another for loop, a break in the inner loop:
- (A) skips to the next inner iteration
- (B) ends both loops
- (C) ends the program
- (D) ends only the inner loop
Q8MCQ2 marksOutput
What does this print?
int i;
for (i = 1; i <= 10; i++) {
if (i % 3 == 0) continue;
if (i == 8) break;
printf("%d ", i);
}
printf("\n");
- (A)
1 2 4 5 7 - (B)
1 2 4 5 7 8 - (C)
1 2 4 5 7 10 - (D)
1 2 3 4 5 6 7
Q9MSQ2 marksControl flow
Which are true?
- (A) In a while loop, continue jumps straight to the condition test.
- (B) An else pairs with the nearest unmatched if.
- (C) A switch with no matching case and no default runs no statements.
- (D)
case x:where x is an int variable is allowed.
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (A)1 mark
After (2,7): i = 4, j = 4, and 4 < 4 is false.
Output
(0,10) (2,7)
- (B) is wrong: 4 < 4 fails, so (4,4) is not printed.
- (C) is wrong: The loop ends at i = j = 4.
- (D) is wrong: 2 < 7 holds for the second pass.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q2 Answer: 81 mark
i = 3, 8, 13, 18, 23, 28, 33, 38: ⌈37/5⌉ = 8.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q3 Answer: (D)2 marks
do-while runs once: prints 10, k = 11, 11 < 5 is false.
Output
10 | k = 11
- (A) is wrong: The test fails after the first pass.
- (B) is wrong: do-while always runs at least once.
- (C) is wrong: The body runs before the test.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q4 Answer: (A)1 mark
The ; after the if is an empty statement; both printf calls always run.
Output
AB
- (B) is wrong: printf("A") is not controlled by the if.
- (C) is wrong: B is always printed.
- (D) is wrong: Both statements run.
Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through
Q5 Answer: 82 marks
Enter at case 3: s = 3, fall into case 5: s = 8, break.
Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through
Q6 Answer: 552 marks
For each i, j = i..10 counts: 10 + 9 + … + 1 = 55.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q7 Answer: (D)1 mark
break leaves only the innermost enclosing loop.
- (A) is wrong: That is continue.
- (B) is wrong: The outer loop continues.
- (C) is wrong: Only the loop is left.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q8 Answer: (A)2 marks
Multiples of 3 are skipped by continue; at i = 8, break ends the loop before printing.
Output
1 2 4 5 7
- (B) is wrong: break happens before printing 8.
- (C) is wrong: The loop ends at 8.
- (D) is wrong: 3 and 6 are skipped.
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q9 Answer: (A) (B) (C)2 marks
Case labels must be constants.
- (D) is wrong: Labels must be constant expressions.
Re-read: Lesson 1.2.1 — if / else if / else; nested conditions; switch and fall-through