What are n and i after int i = 4, n = 2; while (i++ <= 15) n++; printf("n = %d, i = %d", n, i);?
Control statements decide which statements run and how many times: if/else and switch choose a path, while/do-while/for repeat, and break, continue, goto and return jump. Tracing them exactly — fall-through, dangling else, loop counts — is a PSC staple.
while checks the signal before each lap, a do-while only after the lap. break is an emergency exit off the circle, and continue skips the rest of this lap and starts the next.if (n) means if (n != 0).while (…);.for (init; cond; update) are optional; an empty condition is true, so for (;;) loops forever.; after if (…), while (…) or for (…) makes an empty body.A function body is a sequence of statements executed one after another — sequential flow. An expression statement is an expression followed by ; (x = 5;, printf("hi");), and a lone ; is a null statement that does nothing. A compound statement, or block, is a group of statements inside braces { }; it counts as a single statement wherever C expects one.
Control statements change that top-to-bottom order. They form three families: selection (decision) statements choose a path, iteration (loop) statements repeat a block, and jump statements transfer control unconditionally. The Böhm–Jacopini theorem (1966) showed that sequence, selection and iteration are enough to express any algorithm — the foundation of structured programming.
| Family | Statements | Purpose |
|---|---|---|
| Selection | if, if-else, else-if ladder, switch | choose one of several paths |
| Iteration | while, do-while, for | repeat a statement or block |
| Jump | break, continue, goto, return | transfer control immediately |
if (n) means if (n != 0), while (*s) loops until it meets the character '\0', and if (-1) is true. Relational and logical operators themselves produce 1 or 0.The simple if runs a statement only when its condition is true: if (marks >= 40) printf("Pass");. if-else chooses between two statements. An else-if ladder tests conditions from the top and runs the first branch whose condition is true. To control more than one statement, wrap them in braces — without braces only the first statement after the condition belongs to the if.
#include <stdio.h>
int main(void)
{
int marks = 72, a = 4, b = 9, c = 7;
/* else-if ladder: tested top-down, the first true condition wins */
if (marks >= 80)
printf("Grade A\n");
else if (marks >= 60)
printf("Grade B\n"); /* printed, because 72 >= 60 */
else if (marks >= 40)
printf("Grade C\n");
else
printf("Fail\n");
/* nested if: largest of three numbers */
if (a > b) {
if (a > c) printf("%d\n", a);
else printf("%d\n", c);
} else {
if (b > c) printf("%d\n", b); /* printed: 9 */
else printf("%d\n", c);
}
return 0;
}When ifs are nested without braces, which if owns an else? C's rule is fixed: an else belongs to the nearest preceding if that does not already have an else, within the same block. The compiler ignores indentation completely. This ambiguity for the reader is called the dangling-else problem.
int a = 5, b = 10;
if (a > 10)
if (b > 5)
printf("X");
else /* looks as if it belongs to the outer if ... */
printf("Y"); /* ... but it pairs with if (b > 5) */
/* a > 10 is false, so the inner if-else is never reached: no output */if (x = 5) assigns 5, and the value of that assignment is 5 — always true. if (x = 0) is always false. Use == to compare (some programmers write 5 == x, so that the typo 5 = x becomes a compile error). (2) if (x > 0); — the semicolon is an empty statement that forms the entire body, so the next line runs unconditionally.Two more condition pitfalls: if (a < b < c) is legal but compares (a < b) — which is 0 or 1 — with c; write a < b && b < c. And comparing floating-point values with == is unreliable (0.1 + 0.2 == 0.3 is false), so test whether fabs(x - y) is below a small tolerance instead.
A switch evaluates an integer expression once and jumps to the case label with the matching value; if no label matches it jumps to default, or past the whole switch when there is no default. For testing one variable against many constant values it is clearer than a long else-if ladder, and compilers can turn it into a fast jump table.
#include <stdio.h>
int main(void)
{
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B': /* no statements, no break: falls into 'C' */
case 'C':
printf("Good\n"); /* printed for both 'B' and 'C' */
break;
case 'D':
printf("Pass\n");
break;
default:
printf("Invalid grade\n");
}
return 0;
}int, char, short, long or an enum. A float, a double or a string cannot be switched on.case 3:, case 'x':, case 2 + 1: and case RED: (an enum constant) are fine; case n: with a variable, case 1.5: and case "yes": are errors. Duplicate values are also an error.break ends the switch. Without it, execution falls through into the next case's statements, whatever that case's value is.default is optional, allowed at most once, and may be placed anywhere — it is still chosen only when no case matches.case 1 ... 5: are a GCC extension, not standard C.switch (i) { case 10: … case 25: printf("25 "); case 30: printf("30 "); Default: printf("default"); } with i = 25, Default: (capital D) is not the keyword default — C is case-sensitive, so it compiles as an ordinary goto label. Control enters at case 25 and falls through every following statement, printing 25, 30 and then 'default', because labels never stop the flow.x > 10&&, floats, strcmp resultsA loop repeats its body while a condition holds. Every loop needs three ingredients: initialisation of a control variable, a test, and an update that eventually makes the test false. Forget the update and the loop runs forever. C has three loop statements, and any of them can express any loop; they differ in where the test sits.
#include <stdio.h>
int main(void)
{
int i;
i = 1; /* while: test first (entry-controlled) */
while (i <= 5) {
printf("%d ", i);
i++;
}
i = 1; /* do-while: body first (exit-controlled) */
do {
printf("%d ", i);
i++;
} while (i <= 5); /* the semicolon is required */
for (i = 1; i <= 5; i++) /* for: init; test; update in one header */
printf("%d ", i);
printf("\n"); /* 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 */
return 0;
}while (cond) statement — no semicolon after the conditiondo statement while (cond); — the semicolon is part of the syntaxIn for (init; condition; update) the init runs once, the condition is tested before every iteration, and the update runs after every iteration — including one cut short by continue. Every part is optional: a missing condition counts as true, so for (;;) is an infinite loop, just like while (1). The comma operator handles two variables at once, as in for (i = 0, j = n - 1; i < j; i++, j--), and since C99 the counter may be declared in the header (for (int i = 0; i < n; i++)), limiting its scope to the loop.
for (init; cond; update) body behaves like init; while (cond) { body; update; } with one exception: a continue inside the while version jumps straight to the test and skips the update. That single difference causes many accidental infinite loops.break immediately leaves the innermost enclosing loop or switch, and execution resumes at the statement after it; in nested loops it exits only the loop it is written in. continue abandons the rest of the current iteration: in while and do-while control goes to the condition test, in for it goes to the update and then the test. continue is not allowed in a switch on its own — a compile error, unlike break — but inside a switch that sits in a loop, it continues the loop.
#include <stdio.h>
int main(void)
{
int i, sum = 0;
for (i = 1; i <= 10; i++) {
if (i % 3 == 0)
continue; /* skip 3, 6, 9 */
if (i > 8)
break; /* leave the loop when i reaches 10 */
sum += i; /* adds 1, 2, 4, 5, 7, 8 */
}
printf("sum = %d, i = %d\n", sum, i); /* sum = 27, i = 10 */
return 0;
}goto label; transfers control to the statement marked label: anywhere in the same function — labels have function scope and their own name space, so a label may share a name with a variable. A goto can jump forwards or backwards but never into another function. Edsger Dijkstra's 1968 letter 'Go To Statement Considered Harmful' argued that free jumping makes programs hard to follow; today goto is used sparingly, mainly to escape deeply nested loops or to reach shared clean-up code.
#include <stdio.h>
int main(void)
{
int grid[3][3] = {{1, 2, 3}, {4, -5, 6}, {7, 8, 9}};
int r, c;
for (r = 0; r < 3; r++)
for (c = 0; c < 3; c++)
if (grid[r][c] < 0)
goto found;
printf("no negative value\n");
return 0;
found:
printf("negative at [%d][%d]\n", r, c); /* negative at [1][1] */
return 0;
}return ends the current function, optionally handing back a value; returning from main ends the program, and the value becomes the exit status. The library function exit(status) from <stdlib.h> ends the program from any depth after flushing open streams; EXIT_SUCCESS and EXIT_FAILURE are the portable status values.
A loop inside another loop runs to completion for every iteration of the outer loop. If the outer loop runs m times and the inner n times, the inner body executes m × n times. When the inner range depends on the outer variable — for (j = i; j < n; j++) — the count becomes a sum such as n + (n − 1) + … + 1 = n(n + 1)/2.
while (i++ <= 15) the comparison uses the old i, and the increment happens even on the failing test.#include <stdio.h>
int main(void)
{
int n = 4, i, j;
for (i = 1; i <= n; i++) { /* row number */
for (j = 1; j <= i; j++) /* row i has i stars */
printf("*");
printf("\n");
}
/* output: rows of 1, 2, 3 and 4 stars — 10 stars in all = n(n + 1)/2 */
return 0;
}#include <stdio.h>
int main(void)
{
int n = 4096, rev = 0, sum = 0;
while (n > 0) {
int d = n % 10; /* last digit */
rev = rev * 10 + d; /* append it to the reverse */
sum += d;
n /= 10; /* drop the last digit */
}
printf("%d %d\n", rev, sum); /* 6904 19 */
int p = 97, k, prime = 1;
for (k = 2; k * k <= p; k++) /* divisors up to the square root suffice */
if (p % k == 0) { prime = 0; break; }
printf("%d is %s\n", p, prime ? "prime" : "not prime"); /* 97 is prime */
return 0;
}while (j <= 255); has an empty body; j never changes, so the loop never ends and nothing after it runs (a KPSC question). for (i = 0; i < 5; i++); runs the empty body five times, then executes the intended body once with i = 5.while (i < 5) printf("%d", i); i++; only the printf is inside the loop, so i never changes — an infinite loop.for (unsigned u = 5; u >= 0; u--) never ends, because an unsigned value is always ≥ 0 and 0 − 1 wraps to UINT_MAX. for (char c = 0; c < 200; c++) never ends when char is signed (its maximum is 127).for (i = 10; i != 0; i -= 3) visits 10, 7, 4, 1, −2, … and never equals 0.for (x = 0.1; x != 1.0; x += 0.1) may never hit 1.0 exactly; use x < 1.0 or an integer counter.while (i < 10) { if (i % 2) continue; i++; } the first odd i is never incremented again.#include <stdio.h>
int main(void)
{
int i, count = 0;
for (i = 0; i < 5; i++); /* stray ';' : the loop body is empty */
printf("i = %d\n", i); /* not in the loop: runs once, i = 5 */
i = 0;
while (i++ < 3) /* tests 0, 1, 2 (true) and 3 (false) */
count++;
printf("count = %d, i = %d\n", count, i); /* count = 3, i = 4 */
return 0;
}while (i++ < n) the increment happens on every test — including the final, failing one — so after the loop i is one past the value that failed. Treat the comparison and the increment as two separate steps when you trace, and count the successful tests to get the number of iterations.Try the problem first, then reveal the working one step at a time.
What are n and i after int i = 4, n = 2; while (i++ <= 15) n++; printf("n = %d, i = %d", n, i);?
What does this print? int i = 25; switch (i) { case 10: printf("In 10 "); case 25: printf("In 25 "); case 30: printf("In 30 "); Default: printf("In default"); }
How many times is "Kerala" printed? for (i = 0; i < 20; i++) for (j = 0; j < 5; j++) { if (i > 2) continue; printf("Kerala\n"); }
Predict the output: int a = 5, b = 10; if (a > 10) if (b > 5) printf("X"); else printf("Y"); if (b != 10); printf("Z");
Predict the output: int i; for (i = 1; i <= 10; i += 3) printf("%d ", i); printf("| %d", i);
Compare int n = 0; while (n > 0) { printf("%d ", n); n--; } with int n = 0; do { printf("%d ", n); n--; } while (n > 0);. What does each print, and why?
How many times does count++ execute in each fragment? (a) for (i = 0; i < 10; i++) for (j = i; j < 10; j++) count++; (b) for (i = 1; i < 100; i *= 2) count++; (c) for (i = 0; i < 5; i++) for (j = 0; j < 4; j++) if (j == 2) break; else count++;
Which loops terminate, and after how many iterations? (a) for (i = 9; i; i -= 3) (b) for (i = 10; i; i -= 3) (c) int k = 0; while (k < 5); k++; (d) for (unsigned u = 3; u >= 0; u--)
Write a program that reads a non-negative integer and reports whether it is a palindrome (reads the same reversed). Trace it for 1221 and 1231.
if (x = 5) is an assignment and always true; if (x = 0) is always false. Compilers only warn — use ==.; right after if (…), while (…) or for (…) is an empty body: while (j <= 255); hangs forever, and the 'body' after for (…); runs just once.default (Default:, defualt:) silently becomes a goto label.continue in a for loop still runs the update; in a while loop it jumps straight to the test, skipping any update written at the bottom of the body.10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
int k = 4, m = 2;
while (k++ <= 15)
m++;
printf("m = %d, k = %d", m, k);