Control Flow Statements
1. What is Control Flow?
Normally, a program runs line by line, from top to bottom.
But we often need to:
• Make decisions (if marks ≥ 50 → pass, else fail).
• Repeat tasks (print “Hello” 10 times).
• Stop or skip certain instructions.
This is controlled using control flow statements.
2. Categories of Control Flow Statements
(a) Decision-Making (Selection)
i. if statement
Executes a block only if the condition is true.
int age = 20;
if (age >= 18) {
printf("You are an adult\n");
ii. if-else statement
Executes one block if true, another if false.
int marks = 45;
if (marks >= 50) {
printf("Pass\n");
} else {
printf("Fail\n");
}
iii. if-else if-else ladder
Used when checking multiple conditions.
int grade = 75;
if (grade >= 80) {
printf("A\n");
} else if (grade >= 70) {
printf("B\n");
} else if (grade >= 60) {
printf("C\n");
} else {
printf("Fail\n");
iv. switch-case
Better when testing one variable against many values.
int day = 3;
switch(day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Invalid day\n");
}
(b) Iteration (Loops)
i. for loop (count-controlled loop)
Executes a block a fixed number of times.
for (int i = 1; i <= 5; i++) {
printf("Hello %d\n", i);
ii. while loop (condition-controlled)
Executes as long as the condition is true.
int num = 1;
while (num <= 5) {
printf("%d\n", num);
num++;
iii. do-while loop (exit-controlled)
Executes at least once, then checks condition.
int x = 1;
do {
printf("%d\n", x);
x++;
} while (x <= 5);
(c) Jump Statements
i. break – exits a loop immediately
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
printf("%d\n", i);
Output:
1234
ii. continue – skips the current iteration
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d\n", i);
Output:
1245
iii. return – exits a function, sending a value back
int square(int x) {
return x * x;
int main() {
printf("%d\n", square(5)); // Output: 25
return 0;
}
3. Real-Life Analogies
• if-else → If it rains, take an umbrella, else take sunglasses.
• for loop → Eat 3 meals a day (repeat fixed times).
• while loop → Keep brushing until teeth are clean.
• do-while → At least taste food once, then decide if you want more.
• break → Stop eating when full, even if food is left.
• continue → Skip one dish if you dislike it, continue with the others.