Definition
📌 Conditions
Conditional statements let your program make decisions — running different code depending on whether a condition is true or false.C offers if, else if, else, and the switch statement for choosing between many fixed options..
Syntax
syntax
if (condition) {
// code
} else if (condition2) {
// code
} else {
// code
}Example
c
#include <stdio.h>
int main() {
int marks = 72;
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 60) {
printf("Grade: B\n");
} else {
printf("Grade: C\n");
}
return 0;
}Output
outputGrade: B
Important Points
- Only the first matching branch executes — the rest are skipped automatically.
- Curly braces
{}can be omitted for a single statement, but it's safer to always use them. switchis a clean alternative when checking one variable against many exact values.- Don't forget
break;inside eachswitchcase, or execution will "fall through" into the next case.
Common Mistakes
- Putting a semicolon right after
if (condition);— this creates an empty if-body bug. - Forgetting
break;in aswitchstatement. - Using
=instead of==inside a condition.
Interview Tip
🎯 Before your interview
"What happens if you forget break in a switch?" — execution falls through into the next case, which is a favorite interview trap.
