Module 3 · Decisions and Loops — topic 1 of 5
Conditional Statements (if, else if, else)
if statements let a program choose what to do based on a condition. Learn if, if-else, else-if ladders, nesting, and how C decides what counts as true.
Learning objectives
- Write if, if-else and else-if ladders
- Explain how C treats any non-zero value as true
- Avoid the dangling-else and missing-brace bugs
Definition
A selection statement evaluates a scalar controlling expression; if the value compares unequal to zero the associated statement executes, otherwise the optional else statement executes.Introduction
Real programs react to data: a passing mark prints “Pass”, a wrong password is rejected, a low balance blocks a withdrawal. The if statement is how a program makes those decisions.
C has no special boolean type in conditions: any expression can be a condition. Zero means false and every other value means true. Comparisons like marks >= 40 simply produce 1 or 0.
Why it matters
Selection is one of the three building blocks of every algorithm. Grading systems, menu-driven programs, input validation and game logic all depend on correct conditions.
Off-by-one conditions (> instead of >=) and missing braces are among the most common bugs in lab exams. Writing conditions carefully is worth the effort.
Detailed explanation
if (condition) statement runs the statement only when the condition is non-zero. Add else statement to run an alternative when it is zero.
An else-if ladder tests conditions in order and runs the first branch whose condition is true; the final else handles everything else. Order matters: put the most specific or highest thresholds first.
Without braces, if controls exactly one statement. A block { ... } groups several statements into one. Using braces always avoids the classic bug where an indented second line is not actually inside the if.
Dangling else: an else attaches to the nearest unmatched if, regardless of indentation. Braces make the intended pairing explicit.
Syntax
- Parentheses around the condition are required.
else ifandelseare optional.- Use braces even for single statements.
Code examples
Even or odd
17 is odd
Any non-zero value is true
Cart has 3 items !0 is 1
More examples
Grade calculator with an else-if ladder
95 -> O 80 -> A 79 -> B 40 -> P 39 -> F 105 -> ?
Step-by-step explanation
- The first
ifrejects invalid marks before any grading happens (input validation). - The ladder checks thresholds from highest to lowest, so 95 matches
>= 90and stops. - 80 fails
>= 90but matches>= 80, showing the boundary is inclusive. - 39 falls through every test to the final
elseand gets 'F'. - The test array deliberately includes boundary values — the best way to test conditions.
Expected output
Running “Grade calculator with an else-if ladder” prints exactly:
Practical applications
- ATM software checks PIN, balance and daily limit before dispensing cash.
- Compilers decide which code to generate from the types of operands.
- Login systems branch on whether credentials are valid, locked or expired.
Best practices
- Always use braces for
ifandelsebodies. - Test boundary values (exactly 40, 39, 100, 0) for every condition.
- Validate input at the top of a function and return early.
Common mistakes
Missing braces around two statements
Avoid
Prefer
Only the first line belongs to the if. balance = 0; always runs, whatever the indentation suggests.
Semicolon after the condition
Avoid
Prefer
The semicolon is an empty statement that becomes the if body, so the printf always runs.
Ladder in the wrong order
Avoid
Prefer
A mark of 95 matches >= 40 first, so the higher grade is unreachable.
Important notes
if (x = 0)assigns and is always false;-Wallwarns about assignments used as conditions.- Comparing floating-point values for exact equality inside conditions is unreliable; compare with a tolerance.
Key takeaways
ifruns code when the condition is non-zero;elsehandles the zero case.- An else-if ladder runs only the first true branch, so order thresholds carefully.
- Braces prevent the missing-brace and dangling-else bugs.
Try it yourself
- 1Read a year and print whether it is a leap year (divisible by 4 and not by 100, or divisible by 400).
- 2Read three numbers and print the largest using nested
ifstatements, then again using&&.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is the bug? `if (x > 10); printf("big");`