Conditionals (if/else)
Make decisions in C using if, else if, and else. Learn the switch statement too.
25 min•By Priygop Team•Last updated: Feb 2026
C Conditionals
C uses if, else if, and else for decision making. The condition must be in parentheses. The code block is wrapped in curly braces {}. In C, 0 is false and any non-zero value is true. The switch statement is a cleaner alternative when checking one variable against multiple values.
Conditional Syntax
- if (condition) { } — Runs if condition is true (non-zero)
- else { } — Runs if condition is false (zero)
- else if (condition) { } — Checks another condition
- switch (variable) { case value: break; } — Multiple cases
- Conditions use: == != > < >= <=
- Logical: && (AND), || (OR), ! (NOT)
- 0 = false, any non-zero = true in C
Conditionals Example
Example
#include <stdio.h>
int main() {
int age = 18;
// Basic if/else
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
// else if chain
int score = 75;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
// switch statement
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("Other day\n");
}
return 0;
}Try It Yourself: Grade Calculator
Try It Yourself: Grade CalculatorC
C Editor
✓ ValidTab = 2 spaces
C|29 lines|625 chars|✓ Valid syntax
UTF-8