Conditional Statements
Conditional statements are like traffic lights at an intersection -- when the condition is met, you take one path; otherwise, you take another. Programs gain intelligence through decision-making.
The if Statement
if is the most fundamental conditional. When the condition is true (non-zero), the code inside the braces executes; when false (zero), it is skipped.
#include <stdio.h>
int main(void) {
int score = 95;
if (score >= 90) {
printf("Excellent!\n");
}
printf("Program continues\n");
return 0;
}
Excellent!
Program continues
if, the braces can be omitted. However, it is strongly recommended to always use braces to avoid logic errors when adding statements later and forgetting to add braces.
The if-else Statement
if-else provides two branches: if the condition is true, the if block executes; if false, the else block executes. One of the two will always run.
#include <stdio.h>
int main(void) {
int age = 16;
if (age >= 18) {
printf("You are an adult\n");
} else {
printf("You are a minor\n");
}
return 0;
}
You are a minor
The Dangling else Problem
When multiple if statements are nested, else pairs with the nearest if:
#include <stdio.h>
int main(void) {
int a = 5, b = 0;
if (a > 10)
if (b > 0)
printf("A\n");
else
printf("B\n");
return 0;
}
It looks like the else pairs with the first if, but it actually pairs with the second if, so nothing is printed. Adding braces makes the pairing clear:
if (a > 10) {
if (b > 0) {
printf("A\n");
} else {
printf("B\n");
}
}
The if-else if-else Chain
When multiple conditions need to be checked in sequence, use else if to chain multiple branches:
#include <stdio.h>
int main(void) {
int score = 78;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Grade: C
else if conditions are evaluated from top to bottom. Once a condition is satisfied, all subsequent branches are skipped. The order of conditions matters.
Nested if Statements
An if can be nested inside another if for more refined conditional logic:
#include <stdio.h>
int main(void) {
int age = 25;
char gender = 'M';
if (age >= 18) {
if (gender == 'M') {
printf("Adult male\n");
} else {
printf("Adult female\n");
}
} else {
printf("Minor\n");
}
return 0;
}
Adult male
else if.
The switch-case Statement
switch jumps to a matching case branch based on the value of an integer expression. It is ideal for branching on discrete values.
Basic Syntax
switch (expression) {
case constant1:
statement;
break;
case constant2:
statement;
break;
default:
statement;
break;
}
#include <stdio.h>
int main(void) {
int day = 3;
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid day\n"); break;
}
return 0;
}
Wednesday
The Importance of break
After switch matches a case, it executes all statements from that point onward until it encounters a break or the end of the switch. This is called "fall-through".
#include <stdio.h>
int main(void) {
int num = 2;
switch (num) {
case 1: printf("One\n");
case 2: printf("Two\n");
case 3: printf("Three\n");
default: printf("Default\n");
}
return 0;
}
Two
Three
Default
break is the most common switch mistake. Unless intentionally using fall-through, always add break at the end of each case.
Using Fall-through to Merge Branches
Sometimes multiple case labels need to execute the same logic. You can deliberately omit break:
#include <stdio.h>
int main(void) {
char grade = 'B';
switch (grade) {
case 'A':
case 'B':
case 'C':
printf("Pass\n"); break;
case 'D':
printf("Retake\n"); break;
case 'F':
printf("Fail\n"); break;
default:
printf("Invalid grade\n"); break;
}
return 0;
}
Pass
Limitations of switch
- The expression must be an integer type (
int,char,enum, etc.); floating-point numbers and strings are not allowed - The value after
casemust be a compile-time constant, not a variable casevalues must not be duplicated
The Conditional Operator (Ternary)
The conditional operator ?: can replace simple if-else statements. It was introduced in lesson 6; here are more usage examples:
#include <stdio.h>
int main(void) {
int a = -5;
int abs_a = a >= 0 ? a : -a;
printf("|%d| = %d\n", a, abs_a);
int x = 10, y = 10;
printf("%s\n", x == y ? "Equal" : "Not equal");
return 0;
}
|-5| = 5
Equal
if-else instead.
Example
Determine body type based on BMI, demonstrating multi-branch conditional logic:
#include <stdio.h>
int main(void) {
double weight = 70.0;
double height = 1.75;
double bmi = weight / (height * height);
printf("BMI: %.1f\n", bmi);
if (bmi < 18.5) {
printf("Underweight\n");
} else if (bmi < 24.0) {
printf("Normal\n");
} else if (bmi < 28.0) {
printf("Overweight\n");
} else {
printf("Obese\n");
}
return 0;
}
BMI: 22.9
Normal
Example
Implement a simple menu selection using switch:
#include <stdio.h>
int main(void) {
int choice = 2;
printf("===== Menu =====\n");
printf("1. New Game\n");
printf("2. Continue\n");
printf("3. Settings\n");
printf("4. Exit\n");
printf("================\n");
printf("Your choice: %d\n", choice);
switch (choice) {
case 1: printf("Starting new game!\n"); break;
case 2: printf("Loading save...\n"); break;
case 3: printf("Opening settings\n"); break;
case 4: printf("Exiting game\n"); break;
default: printf("Invalid choice\n"); break;
}
return 0;
}
===== Menu =====
1. New Game
2. Continue
3. Settings
4. Exit
================
Your choice: 2
Loading save...
❓ FAQ
if?if/else, but it is recommended to always use them to avoid bugs from forgetting to add braces when adding more statements later.switch evaluate floating-point numbers or strings?switch only accepts integer expressions. Neither floating-point numbers nor strings work. To compare strings, use an if-else if chain.default branch be omitted?else if and nested if?else if is a flat multi-branch structure that is easier to read; nested if is hierarchical and suitable when conditions depend on each other. They are interchangeable -- choose whichever is more readable.📖 Summary
ifhandles single conditions,if-elsechooses between two, andelse ifchooses among multiple branches- Always use braces to avoid dangling else issues and future maintenance problems
switchis ideal for branching on discrete integer values; do not forgetbreakfor eachcase- Fall-through in
switchcan merge branches, but forgettingbreakis a common bug source - The ternary operator
?:is suitable for simple two-way choices; useif-elsefor complex logic
📝 Exercises
- Write a program that reads an integer and determines whether it is positive, negative, or zero, then prints the result.
- Write a program that reads a month number (1-12) and uses
switchto output the number of days in that month (assume a non-leap year, so February has 28 days). - Write a program that reads a character and determines whether it is an uppercase letter, a lowercase letter, a digit, or some other character.



