1. Introduction
The if-else construct is C's primary tool for conditional execution — running one block of code when a condition is true and, optionally, a different block when it is false. Almost every non-trivial C program uses if-else to branch its logic based on input, computed values, or state.
Cricket analogy: A captain's decision to bowl first or bat first based on pitch conditions is an if-else branch — 'if pitch is damp, bowl first, else bat first' — exactly how C code branches its logic based on a condition.
C supports a plain if, an if-else pair, nested if statements, and the else-if ladder for testing a sequence of mutually exclusive conditions. Any expression that evaluates to a non-zero value is treated as true; zero is treated as false.
Cricket analogy: A DRS review system has a plain 'if' (ball tracking shows hitting stumps, out), an else-if ladder for umpire's call thresholds, and nested checks for no-ball first — and any non-zero delivery outcome (a wicket recorded as 1) counts as 'true' just like C.
2. Syntax
// Simple if
if (condition) {
statement(s);
}
// if-else
if (condition) {
statement(s);
} else {
statement(s);
}
// else-if ladder
if (condition1) {
statement(s);
} else if (condition2) {
statement(s);
} else {
statement(s); // runs if none of the above matched
}
// Nested if
if (condition1) {
if (condition2) {
statement(s);
}
}3. Explanation
The condition in an if statement must be an expression that C can evaluate to an integer; any non-zero result is treated as true and 0 as false. Braces { } are optional when a branch has exactly one statement, but omitting them for multi-line intent is a common source of bugs, so most style guides require braces even for single statements.
Cricket analogy: A third umpire's decision reduces to a single integer signal (1 for out, 0 for not out) — like C's condition evaluation — and skipping the full replay review for an obvious single-frame call is risky, just as omitting braces invites bugs.
The else-if ladder tests conditions top to bottom and executes only the first block whose condition is true, skipping the rest — this makes it suitable for mutually exclusive range checks such as grading logic (marks >= 90, else >= 80, else >= 70, ...).
Cricket analogy: An umpire's out-decision ladder checks 'if caught, out; else if bowled, out; else if lbw, out; else not out' top to bottom, stopping at the first true condition — just like C's else-if ladder for grading marks >=90, >=80, etc.
Nested if-else means placing an if-else construct entirely inside the body of another if or else. This is different from an else-if ladder in indentation and intent, though the two can look similar; nesting is used when the inner check only makes sense given the outer condition (e.g., check if a number is positive, and only then check if it's even or odd).
Cricket analogy: Checking 'if the ball beat the bat, then check if it hit the stumps or missed' is a nested if — the inner 'stumps or miss' check only makes sense once we already know the ball beat the bat, unlike a flat else-if ladder of unrelated conditions.
The 'dangling else' problem: when an if without an else is itself nested inside another if, C's rule is that an else always binds to the NEAREST unmatched if, not necessarily the one the indentation suggests. For example:
if (a > 0)
if (b > 0)
printf("both positive");
else
printf("a positive, b not");
Here the else binds to if (b > 0), even though indentation might mislead a reader into thinking it belongs to if (a > 0). Always use explicit braces to remove ambiguity: if (a > 0) { if (b > 0) { ... } else { ... } }.
Because any non-zero value is truthy, if (x) is a common idiom for 'if x is non-zero' or, for pointers, 'if x is not NULL'. Writing if (x != 0) or if (ptr != NULL) explicitly is more readable and is generally recommended in production and exam-quality code.
4. Example
#include <stdio.h>
int main(void) {
int marks = 76;
if (marks >= 90) {
printf("Grade: A+\n");
} else if (marks >= 80) {
printf("Grade: A\n");
} else if (marks >= 70) {
printf("Grade: B\n");
} else if (marks >= 60) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
int num = -4;
if (num != 0) {
if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
if (num > 0) {
printf("%d is positive\n", num);
} else {
printf("%d is negative\n", num);
}
} else {
printf("num is zero\n");
}
return 0;
}5. Output
Grade: B
-4 is even
-4 is negative6. Key Takeaways
- Any non-zero condition value is treated as true; 0 is treated as false.
- An else-if ladder tests conditions in order and executes only the first true branch, skipping the rest.
- An else always binds to the nearest unmatched if — this is the 'dangling else' rule; use braces to avoid ambiguity.
- Nested if-else lets an inner decision depend on an outer condition already being true.
- Braces are optional for single-statement branches but strongly recommended for clarity and to avoid bugs when adding statements later.
- Prefer explicit comparisons like
if (x != 0)orif (ptr != NULL)over relying on implicit truthiness.
Practice what you learned
1. In C, which value(s) are treated as false in an if condition?
2. Consider: if (a > 0) if (b > 0) printf("X"); else printf("Y"); If a = 5 and b = -1, what is printed?
3. What happens in an else-if ladder once one condition evaluates to true?
4. Why are braces recommended even for single-statement if bodies?
5. What is the output of `if (x = 0) printf("true"); else printf("false");` (note the single =)?
Was this page helpful?
You May Also Like
switch Statement in C
Understand the C switch statement — syntax, fall-through, break, and default rules — with a correct example and common exam traps.
Loops in C (for, while, do-while)
Complete guide to C loops — for, while, and do-while — with syntax, execution order, examples, and exam-focused comparisons.
Operators in C
Learn C operators — arithmetic, relational, logical, assignment, and increment/decrement — with syntax, examples, and exam-ready MCQs.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics