Every interesting program needs to make decisions — do this if a condition holds, do something else otherwise. C provides two main tools for this: the if family of statements and the switch statement. This is Post 5, closing out the Unit I series on C fundamentals.
The if Statement
if (condition) {
// executes only if condition is true (non-zero)
}
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
return 0;
}
if-else
if (condition) {
// runs if true
} else {
// runs if false
}
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
if-else Ladder (else if)
For more than two possibilities, chain multiple conditions:
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 60) {
printf("Grade: C\n");
} else if (marks >= 40) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Sample Output
Enter marks (0-100): 82
Grade: B
How the ladder actually evaluates: C checks each condition top to bottom and stops at the FIRST one that’s true — the rest are skipped entirely. This is why order matters: marks >= 75 is only even checked if marks >= 90 already failed, which is why 82 correctly lands in the “B” branch rather than incorrectly matching some later condition too.
Nested if
An if statement can contain another if inside it, for checking a condition only once an outer condition is already satisfied:
#include <stdio.h>
int main() {
int age;
char hasLicense;
printf("Enter age: ");
scanf("%d", &age);
printf("Do you have a license? (y/n): ");
scanf(" %c", &hasLicense);
if (age >= 18) {
if (hasLicense == 'y') {
printf("You may drive.\n");
} else {
printf("You need a license first.\n");
}
} else {
printf("You are not old enough to drive.\n");
}
return 0;
}
The dangling else problem: When if statements are nested without braces, an else always binds to the NEAREST unmatched if — which is not always the one a beginner intends. Always use { } braces around if/else bodies, even single-statement ones, to remove any ambiguity about which if an else belongs to.
The switch Statement
When you’re checking one variable against many specific, discrete values, a switch is often cleaner than a long if-else ladder:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}
Example: Day of the Week
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
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 number.\n");
}
return 0;
}
Sample Output
Enter day number (1-7): 3
Wednesday
The #1 switch bug: forgetting break: Without break, execution doesn’t stop after a matching case — it “falls through” and keeps executing every subsequent case’s code too, until it hits a break or the end of the switch. E.g. removing break from case 3 above would print both “Wednesday” AND “Thursday” for input 3. This fall-through behavior is occasionally used intentionally (to group cases with shared logic, as we saw with the days-in-month example in the earlier lab post on switch statements), but it’s the single most common switch bug for beginners.
switch vs. if-else: When to Use Which
- Use switch when checking one variable against several exact constant values (integers or characters) — menu selections, day/month numbers, grade letters.
- Use if-else when conditions involve ranges (
marks >= 90), multiple variables, or logical combinations (&&,||) — things aswitchsimply cannot express, since it only tests for exact equality.
Summary
ifruns code conditionally;if-elseadds an alternative path; anif-elseladder (else if) checks several conditions in order and stops at the first match.- Always brace
if/elsebodies to avoid the dangling-else ambiguity. switchis a clean alternative to a long ladder when comparing one value against several exact constants — but never forgetbreak, or execution falls through into the next case.
Further Reading
- Conditionals: if-else, Nested if and else-if (video embedded above)
- Conditionals: switch Statement (second video embedded above)
- GeeksforGeeks: switch Statement in C
This wraps up Unit I: Introduction to C Language. Up next: Unit II, covering loops (while, for, do-while), break/continue/goto, and arrays.