C — If Else
Basic if statement
Execute code only when a condition is true:
#include <stdio.h>
int main() {
int temperature = 28;
if (temperature > 25) {
printf("It's warm outside!\n");
}
return 0;
}
if-else
Provide an alternative path:
#include <stdio.h>
int main() {
int hour = 14;
if (hour < 12) {
printf("Good morning!\n");
} else {
printf("Good afternoon!\n");
}
return 0;
}
if-else if-else
Check multiple conditions:
#include <stdio.h>
int main() {
int score = 85;
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;
}
Important: Evaluate conditions from most specific to least specific. Once a condition matches, subsequent conditions are skipped.
Nested if
An if inside another if:
#include <stdio.h>
int main() {
int age = 25;
int hasID = 1;
if (age >= 18) {
if (hasID) {
printf("Entry granted.\n");
} else {
printf("Please show your ID.\n");
}
} else {
printf("Sorry, you must be 18+.\n");
}
return 0;
}
Switch statement
Match a value against multiple cases:
#include <stdio.h>
int main() {
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:
case 7:
printf("Weekend!\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0;
}
Always use break after each case. Without it, execution "falls through" to the next case:
#include <stdio.h>
int main() {
int month = 8;
int days;
switch (month) {
case 2:
days = 28; // Assume non-leap year
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
default:
days = 31;
break;
}
printf("Month %d has %d days\n", month, days);
return 0;
}
Switch limitations
Switch only works with integer-compatible types:
#include <stdio.h>
int main() {
// Valid switch types
int i = 5;
char c = 'A';
enum Color { RED, GREEN, BLUE } color = GREEN;
switch (i) { /* OK */ }
switch (c) { /* OK — char promotes to int */ }
switch (color) { /* OK — enum promotes to int */ }
// Invalid switch types
// float f = 3.14;
// switch (f) { } // Error: float not allowed
// const char *s = "hello";
// switch (s) { } // Error: pointer not allowed
return 0;
}
Ternary operator
Compact if-else for simple expressions:
#include <stdio.h>
int main() {
int age = 20;
const char *label = (age >= 18) ? "Adult" : "Minor";
printf("Label: %s\n", label);
int a = 5, b = 10;
int max = (a > b) ? a : b;
printf("Max: %d\n", max);
return 0;
}
Combining conditions
Use logical operators for compound conditions:
#include <stdio.h>
int main() {
int age = 25;
int income = 50000;
// AND: both must be true
if (age >= 18 && income >= 30000) {
printf("Qualifies for premium card.\n");
}
// OR: at least one must be true
if (age < 12 || age > 65) {
printf("Discounted ticket.\n");
}
// NOT: inverts the condition
int isBanned = 0;
if (!isBanned) {
printf("Access granted.\n");
}
return 0;
}
Best practices
#include <stdio.h>
// GOOD: Early returns reduce nesting
const char *getDiscount(int age, int isMember) {
if (age < 0) return "Invalid age";
if (isMember) return "20% off";
if (age < 12) return "50% off";
if (age > 65) return "15% off";
return "No discount";
}
int main() {
printf("Discount: %s\n", getDiscount(25, 1));
// GOOD: Use switch for multiple related conditions
int code = 200;
const char *message;
switch (code) {
case 200: message = "OK"; break;
case 404: message = "Not Found"; break;
case 500: message = "Server Error"; break;
default: message = "Unknown"; break;
}
printf("%d: %s\n", code, message);
return 0;
}
Mini Practice
Write C code that:
- Uses if-else to check if a number is positive, negative, or zero
- Uses switch to map a grade (A-F) to a description
- Combines conditions with
&&and|| - Uses the ternary operator for a simple conditional
Up Next
In the next lesson, you'll learn about Loops — for, while, and do-while in C.
Related Topics
Frequently Asked Questions about If Else
What is If Else in C?
If Else is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn If Else?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn If Else.
Why is If Else important in C?
If Else is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.