if-else is a two-way branch: one block runs when the condition is true, and a different block runs when it is false. Exactly one path executes—never both, never neither.
Remember
if (condition) {
/* true path */
} else {
/* false path */
}
true → if body only
false → else body only
use { } on both sides
need 3+ choices → else-if / switch
Simulate if-else choices in the browser. Same ideas as the C examples below (compile those with gcc).
Browser simulation of if-else (same idea as the C examples below).
Result
n = 8
if (n % 2 == 0) → true
→ print "even"
(else skipped)
Trace
Worked Walkthrough — Even or Odd
Trace if (n % 2 == 0) … else … with two values. Same pattern as Example 1.
Trace
Case A: n = 8
8 % 2 == 0? true → if body → "even"
else skipped
Case B: n = 7
7 % 2 == 0? false → else body → "odd"
if body skipped
One branch only — always.
Code
C Examples
Compile every example with gcc file.c -std=c11 -o out. Review if; next try else-if for more than two outcomes.
Example 1 — Even or Odd
Classic two-way branch: remainder 0 means even; otherwise odd.
C
#include <stdio.h>
int main(void) {
int n = 8;
if (n % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
}
return 0;
}
Output
even
How It Works
Set a value.n = 8 is what we test.
Remainder.n % 2 is 0 for even numbers.
True path. Condition is true → print even.
False path. For n = 7, the else branch would print odd.
Example 2 — Pass or Fail
Use a threshold: score at or above 40 passes; otherwise fail.
C
#include <stdio.h>
int main(void) {
int score = 55;
if (score >= 40) {
printf("pass\n");
} else {
printf("fail\n");
}
return 0;
}
Output
pass
How It Works
Threshold. Passing means score >= 40.
True path. 55 meets the bar → print pass.
False path. A score like 30 would take the else and print fail.
Exclusive. Only one message prints—never both.
Example 3 — Positive or Not
else covers every case that is not positive (zero and negative). For three labels (positive / zero / negative), use else-if.
C
#include <stdio.h>
int main(void) {
int n = -2;
if (n > 0) {
printf("positive\n");
} else {
printf("not positive\n");
}
return 0;
}
Output
not positive
How It Works
Test.n > 0 is false for -2.
Skip if. The true branch does not run.
Run else. Prints not positive (covers zero and negatives).
Limit. Two labels only—split further with else if when needed.
Edge Cases & Pitfalls
Check these before relying on if-else in real projects.
dangling
Dangling else
Without braces, else pairs with the nearest if—often not the one you meant. Always brace both bodies.
assign
= instead of ==
if (x = 1) assigns and takes the true branch. Compare with ==; compile with -Wall.
two-way
Only two outcomes
else means “everything else.” For positive / zero / negative, use else-if, not a single if-else.
empty
Stray semicolon
if (ok); else … makes an empty if. The following else may not match what you expect—avoid the semicolon after the condition.
An if-else is cheap. Prefer the form that reads clearest. For many constant cases, switch can be clearer and sometimes faster.
Remember
Key Takeaways
Role: exactly one of two branches runs.
vs if:else covers the false case explicitly.
Style: brace both sides; avoid dangling else.
More paths: use else-if or switch.
One line:if-else always picks exactly one path—true or false—brace both bodies.
Frequently Asked Questions
if-else runs one block when the condition is true and a different block when it is false. Syntax: if (condition) { ... } else { ... }. Exactly one of the two branches executes—never both, never neither.
if alone does nothing when the condition is false. if-else always handles both outcomes—use it when you need an explicit alternative path, such as printing pass or fail.
No. else must immediately follow an if (or else-if) block. A standalone else is a compile error.
else if chains more than two conditions: if, else if, else if, ..., else. A plain if-else handles only true vs false. See the else-if tutorial for multi-way chains.
Without braces, else binds to the nearest if. In if (a) if (b) x++; else y++; the else pairs with the inner if, not the outer one. Always use braces so intent is clear.
C allows single statements without braces, but use { } on both if and else bodies in real code for readability and to avoid dangling-else bugs.