Conditionals: if, else & ternary
10 min lesson
- A conditional lets your program choose between different paths, running one block of code or another based on a test.
- Conditionals matter because real programs must react to data: pass or fail, adult or child, discount or no discount.
- Think of a conditional as a fork in a road. You check a sign (the condition) and then take the left road or the right road.
- An if statement runs a block only when a condition is true: if (score >= 60) { ... }.
- An else block runs when the if condition is false, and else if lets you chain several tests in order.
- A condition is any expression that produces a value; zero means false and any nonzero value means true.
- The ternary operator ?: is a compact if-else that produces a value: result = (a > b) ? a : b;.
- The computer evaluates the condition first, then jumps to the matching block and skips the others.
- The syntax is: if (condition) { ... } else if (other) { ... } else { ... }.
- A realistic use: giving a letter grade based on a numeric score, or deciding whether a customer gets free shipping.
- A surprising detail: using = instead of == inside an if silently assigns instead of comparing, and the condition often becomes always true.
- The example program decides a letter grade from a score using if, else if and else, then prints the larger of two numbers with the ternary operator.
- Always use == to compare and = only to assign.
- Put braces { } around every block, even one-liners, to avoid subtle bugs later.
- Order your else if tests from most specific to most general.
- The ternary operator is great for tiny choices but avoid nesting it deeply.
- Conditionals are the brain of your program. You are doing great!
#include <stdio.h>
int main(void) {
// A student's score out of 100
int score = 85;
// Check each range from highest to lowest
if (score >= 90) {
// This runs only when the score is 90 or more
printf("Grade: A\n");
} else if (score >= 80) {
// This runs when the first test failed but score is 80 or more
printf("Grade: B\n");
} else if (score >= 70) {
// This runs when the score is 70 or more
printf("Grade: C\n");
} else {
// This runs when none of the above were true
printf("Grade: F\n");
}
// Two numbers to compare with the ternary operator
int a = 12;
int b = 30;
// The ternary picks a if a > b, otherwise picks b
int larger = (a > b) ? a : b;
// Print the larger value
printf("Larger: %d\n", larger);
return 0;
}What happens: Grade: B Larger: 30
Breaking it down
First if
if (score >= 90)Tests the highest range first. If the score is 90 or more, this block runs and every later test is skipped.
else if chain
else if (score >= 80)Only checked when the previous test failed. This lets you test ranges in order without overlapping.
Final else
else { ... }Runs when none of the earlier conditions were true. It acts as the catch-all path.
Ternary condition
(a > b) ? a : bEvaluates a > b. If true it produces a, otherwise b. The result is stored in larger.
Printing the result
printf("Larger: %d\n", larger);Shows the chosen value with a clear label so the reader knows what the number means.
Comparison operators in conditions
| Operator | Meaning | Example | True when |
|---|---|---|---|
| == | Equal to | x == 5 | x is exactly 5 |
| != | Not equal | x != 5 | x is anything but 5 |
| < | Less than | x < 5 | x is below 5 |
| > | Greater than | x > 5 | x is above 5 |
| <= | Less or equal | x <= 5 | x is 5 or below |
| >= | Greater or equal | x >= 5 | x is 5 or above |
A helpful analogy
A conditional is like a fork in a road with a sign. You read the sign (the condition) and take the left road if it matches, or the right road if it does not. Only one road is taken, never both.
Nesting rules
- 1.Each else if is only tested when all earlier conditions were false, so order matters.
- 2.Use braces { } around every block, even a single line, to prevent future mistakes.
- 3.Zero is false and any nonzero value is true, so if (x) runs whenever x is not zero.
- 4.The ternary operator produces a value; use it in an expression, not as a replacement for multi-branch logic.
Common mistakes
Using = instead of ==
Avoid
if (x = 5) { ... } // always trueUse instead
if (x == 5) { ... }The single = assigns 5 to x, and the result is 5 (true). The block always runs. Turn on compiler warnings to catch this.
Forgetting braces on a multi-line block
Avoid
if (x > 0)
printf("pos");
printf("done");Use instead
if (x > 0) {
printf("pos");
printf("done");
}Only the first statement is controlled by the if; the second runs always. Braces make the intended block explicit.
Testing ranges in the wrong order
Avoid
if (score >= 70) { ... }
else if (score >= 90) { ... }Use instead
if (score >= 90) { ... }
else if (score >= 70) { ... }The first matching branch wins. A 95 would match the 70 branch first and never reach the 90 branch.
Quick tips
- •Read an if-else chain top to bottom; the first true test wins.
- •Use parentheses around complex conditions so the intent is clear.
- •Keep the ternary operator short; if it needs more than one condition, use if-else.
- •Try changing score in the example to 95, 75 and 50 to see each branch run.
- •Enable -Wall and -Wextra; they warn about suspicious conditions like assignment in if.