Table of Contents
What is a Nested If Statement in C?
A nested if statement means writing an if statement inside another if block. The inner condition is checked only if the outer condition is true.Syntax:
The second condition runs only when the first condition is satisfied.if (condition1) {
// code
if (condition2) {
// code
}
}
Simple Example of Nested If Statement
Check whether a number is positive and even.
// C program to check if a number
// is positive and even
#include stdio.h
int main()
{
int num = 10;
if (num 0)
{
if (num % 2 == 0)
{
printf("The number is positive and even.");
}
}
return 0;
}
Output:The number is positive and even.
Explanation:
- First, the program checks if the number is greater than zero.
- Only if that condition is true, it checks whether the number is even using % 2 == 0.
- This is exactly how nested logic works step by step.
Example with User Input
Check if a student is eligible for a scholarship:- Marks must be greater than 75
- Attendance must be greater than 80
// C program to check if a student is eligible
// for scholarship or not
#include stdio.h
int main()
{
int marks, attendance;
printf("Enter marks: ");
scanf("%d", &marks);
printf("Enter attendance percentage: ");
scanf("%d", &attendance);
if (marks 75)
{
if (attendance 80)
{
printf("Eligible for scholarship.");
}
else
{
printf("Not eligible due to low attendance.");
}
}
else
{
printf("Not eligible due to low marks.");
}
return 0;
}
Output:
Explanation:Enter marks: 56
Enter attendance percentage: 67
Not eligible due to low marks.
- The outer if checks marks.
- If marks are sufficient, only then the program checks attendance.
- This avoids unnecessary checks and keeps logic clean.
Example with Multiple Nested Conditions
Find the largest among three numbers.
// C program to find the largest
// number among three numbers
#include stdio.h
int main()
{
int a = 10, b = 20, c = 15;
if (a b)
{
if (a c)
{
printf("A is the largest.");
}
else
{
printf("C is the largest.");
}
}
else
{
if (b c)
{
printf("B is the largest.");
}
else
{
printf("C is the largest.");
}
}
return 0;
}
Output:
Explanation:B is the largest.
- The program first compares a with b.
- Based on that result, it compares the larger one with c.
- This step-by-step comparison is a classic use of nested if.
Why Use Nested If Statements?
- Handles dependent conditions: Some conditions only make sense after another is true.
- Improves decision-making: You can break complex problems into smaller logical steps.
- Reduces unnecessary checks: Inner conditions run only when needed.
- Makes logic structured: Your program follows a clear decision flow.
Common Mistakes to Avoid
- Too many levels: Deep nesting can make code hard to read. Try to keep it simple.
- Missing braces: Always use {} even for single statements to avoid confusion.
- Wrong condition order: Always check broader conditions first, then move to specific ones.
- Ignoring readability: If nesting gets too complex, consider using else if instead.
Here are some differences between nested if and else if statements:
| Feature | Nested If | Else If |
|---|---|---|
| Structure | One if inside another | Multiple conditions in sequence |
| Use Case | Dependent conditions | Independent Conditions |
| Readability | Can become complex | Usually easier to read |
| Execution | Inner runs only if outer is true | Checks one by one |
Conclusion
Nested if statements are useful when decisions depend on multiple levels of conditions. They help you control program flow in a structured way. However, overusing them can make code messy, so balance is important. Use nested if where logic truly depends on earlier checks. Once you get comfortable with it, writing complex conditions becomes much easier.Frequently Asked Questions
1. What is a nested if statement in C?2. When should I use nested if instead of else if?It is an if statement placed inside another if block to check multiple dependent conditions.
3. Can I nest multiple if statements?Use nested if when one condition depends on another. Use else if when conditions are independent.
4. Is nested if efficient?Yes, but too many levels can make your code hard to read and maintain.
5. What is the alternative to nested if?Yes, because inner conditions are only checked when required.
You can use else if, switch statements, or logical operators depending on the situation.
0 Comments