Nested if-else in C
Some decisions happen in steps. First check the username; only if it is correct, check the password. First check if a number is positive; only then check if it is even.
Putting an if inside another if is called a nested if. In this lesson you will learn how it works and write the classic exam program: largest of three numbers.
What You Will Learn
- What a nested if-else is.
- Its syntax and flowchart.
- Largest of three numbers using nested if-else.
- A two-step login check.
- When to use nesting and when to use
&&.
Nested if-else in C Explained
Exam definition: A nested if-else is an if or if-else statement placed inside another if or else block. The inner condition is checked only when the outer condition allows it.
Memory trick: Nested if is like airport security: you reach the second check (boarding pass) only if you pass the first check (ID). Fail the first, and the second is never asked.
Syntax of Nested if-else
if (condition1)
{
if (condition2)
{
/* condition1 true AND condition2 true */
}
else
{
/* condition1 true, condition2 false */
}
}
else
{
/* condition1 false */
}
Flowchart of nested if-else
< condition1? >
/ \
Yes No
| |
< condition2? > [ outer else ]
/ \ |
Yes No |
| | |
[ block A ] [ block B ] |
\ | /
[ next statement ]
Example 1: Largest of Three Numbers Using Nested if-else
#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b)
{
if (a >= c)
printf("Largest = %d\n", a);
else
printf("Largest = %d\n", c);
}
else
{
if (b >= c)
printf("Largest = %d\n", b);
else
printf("Largest = %d\n", c);
}
return 0;
}
Output
Enter three numbers: 14 9 22 Largest = 22
Code Explanation
- Outer if: is a ≥ b? 14 ≥ 9 is true, so we enter the first block. Now we know b is not the largest.
- Inner if: is a ≥ c? 14 ≥ 22 is false, so the inner else runs and prints c = 22.
- The outer else block is skipped completely.
- Using
>=instead of>makes the program correct even when two numbers are equal.
How Nested if-else Works
- C checks the outer condition first.
- It enters the matching outer block (if or else) and then checks the inner condition there.
- Inner conditions in the block that was not entered are never checked.
- Each
elsepairs with the nearest unmatched if, so use braces to keep the pairing clear and avoid the dangling else bug.
Example 2: Two-Step Login Check
#include <stdio.h>
int main(void)
{
int user_id, pin;
printf("User ID: ");
scanf("%d", &user_id);
if (user_id == 101)
{
printf("PIN: ");
scanf("%d", &pin);
if (pin == 4321)
printf("Login successful. Welcome!\n");
else
printf("Wrong PIN.\n");
}
else
{
printf("User not found.\n");
}
return 0;
}
Output
User ID: 101 PIN: 4321 Login successful. Welcome!
The program asks for the PIN only if the user ID is correct. This step-by-step behaviour is something && alone cannot do.
Important Points to Remember
- Nested if = an if inside another if or else block.
- The inner condition is checked only when the outer path reaches it.
- Each else pairs with the nearest unmatched if.
- Use braces to keep nesting clear.
- Don't nest too deep (more than 3 levels is hard to read).
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| No braces in nested if-else | else attaches to the wrong if | Always use { } |
Using > for largest with equal numbers | Wrong answer when a == b | Use >= |
| Nesting 4–5 levels deep | Code is hard to read and debug | Combine with && or use else-if |
| Bad indentation | Looks like the wrong structure | Indent each level consistently |
Nested if vs Logical Operators
| Nested if | Logical AND (&&) |
|---|---|
| Can do different things at each step | One combined condition, one result |
| Good when the second check depends on the first | Good when both checks are simple |
| More lines | Shorter |
Nested if-else Practice Questions
- What is a nested if-else statement?
- When is the inner if of a nested statement checked?
- Write the logic for the largest of three numbers.
- Which if does an else belong to in nested code?
- When would you use nested if instead of
&&?
Show answers
- An if or if-else statement inside another if or else block.
- Only when the outer condition leads into the block that contains it.
- If a ≥ b: the largest is a if a ≥ c, else c. Otherwise: the largest is b if b ≥ c, else c.
- The nearest unmatched if above it.
- When the second check depends on the first, or different messages are needed at each step.
Coding Exercise
Read a number. If it is positive, print whether it is even or odd. If it is not positive, print "Not a positive number".
Show solution
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0)
{
if (n % 2 == 0)
printf("Positive and even\n");
else
printf("Positive and odd\n");
}
else
{
printf("Not a positive number\n");
}
return 0;
}
Enter a number: 12 Positive and even
Quick Quiz: Nested if-else in C (MCQ)
-
A nested if is:
A) An if after another if B) An if inside another if C) An if without else D) A loop
-
If the outer condition is false, the inner if in the outer if-block is:
A) Checked B) Skipped C) An error D) Checked twice
-
With a = 5, b = 5, c = 3, the program above prints:
A) Largest = 5 B) Largest = 3 C) Nothing D) Error
-
An else belongs to:
A) The outermost if B) The nearest unmatched if C) The first if D) No if
-
Best way to keep nested ifs clear:
A) Write everything on one line B) Use braces and indentation C) Avoid else D) Use goto
Show answers
- B: an if inside another if.
- B: skipped.
- A: Largest = 5.
- B: the nearest unmatched if.
- B: braces and indentation.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Nested if | if inside another if/else block |
|---|---|
| Inner condition checked | Only if the outer path reaches it |
| else pairs with | Nearest unmatched if |
| Largest of 3 | Compare a, b first; then winner with c |
| Equal numbers | Use >= |
Frequently asked questions
- What is a nested if statement?
- An if statement inside another if or else, checked only when the outer path allows.
- How do you find the largest of three numbers?
- Compare a and b, then compare the bigger one with c.
- Why use braces in nested if-else?
- To avoid the dangling else problem and make the structure clear.
- Can nested if be replaced by &&?
- Sometimes, when both checks are simple; not when each step needs its own action.
Remember
- Nested if = airport security: pass check 1 to reach check 2.
- Braces + indentation always.
Next Topic
Next lesson: switch Statement in C. You will learn a cleaner way to choose from many fixed options: the switch statement.