Statement Categories
C programs consist of several statement types:
- Expression statements
- Function call statements
- Control flow statements
- Compound statements (blocks)
- Null statements
Control flow statements include decision-making (branching) and iteration (looping) constructs. Branching relies on if and switch, while loops are implemented via while, do-while, and for.
Branching with if
The if keyword evaluates a condition. In C, zero represents false, and any non-zero value represenst true.
if (condition)
single_statement;
if (condition)
true_branch;
else
false_branch;
// Multiple conditions
if (cond1)
action1;
else if (cond2)
action2;
else
default_action;
Example of boolean evaluaiton:
#include <stdio.h>
int main() {
if (0) printf("Visible?\n"); // Skipped
if (1) printf("Always prints\n"); // Executed
int x = 10, y = 20;
if (x > y) {
printf("x is larger\n");
} else {
printf("y is larger or equal\n");
}
return 0;
}
Logical operators:
&&(AND): Result is true only if both operands are true.||(OR): Result is true if at least one operand is true.
Example using input and logic:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if (num > 0 && num < 50) {
if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
}
return 0;
}
switch Statement
The switch statement allows multi-way branching based on an integer value. The break keyword exits the switch block; without it, execution falls through to subsequent cases. The default label handles unmatched values.
#include <stdio.h>
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Option A\n");
break;
case 2:
printf("Option B\n");
break;
case 3:
printf("Option C\n");
break;
default:
printf("Invalid selection\n");
break;
}
return 0;
}
Nested switch example:
int m = 1, n = 5;
switch (m) {
case 1:
n += 2; // n becomes 7
// no break, fall through
case 2:
m += 1; // m becomes 2
switch (m) {
case 2:
n++; // n becomes 8
m++; // m becomes 3
break;
}
// no break
case 3:
n += 1; // n becomes 9
break;
}
// Final: m = 3, n = 9
Loop Constructs
while Loop
while (condition) {
// code block
}
break: Exits the loop immediately.continue: Skips the rest of the current iteration and re-evaluates the condition.
Printing even numbers up to 20:
int val = 0;
while (val <= 20) {
if (val % 2 == 0) {
printf("%d ", val);
}
val++;
}
for Loop
for (initialization; test_condition; update) {
// loop body
}
- Initialization: Executed once at the start.
- Test condition: Checked before each iteration.
- Update: Executed after each iteration.
Nested loop pitfall (missing re-initialization):
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("Hi ");
}
// j remains 3 here; if the inner loop lacked initialization,
// it would not run again after the first outer iteration.
}
do-while Loop
Executes the body at least once before checking the condition.
int counter = 1;
do {
if (counter == 3) {
counter++;
continue; // Skips printf for 3
}
printf("%d ", counter);
counter++;
} while (counter <= 5);
Using break to exit early:
int k = 0;
do {
if (k == 4) break;
printf("%d ", k);
k++;
} while (k < 10);