A nested if statement means placing one if
statement inside another if statement. It is useful when a
second condition should be checked only after a first condition is true.
if, the inner condition is
checked only when program execution reaches that inner if
statement.
A nested if is an if statement placed inside
another if statement.
if (condition1)
{
if (condition2)
{
// statements
}
}
The inner condition depends on the program reaching the outer
if block.
if (condition1)
{
statement1;
if (condition2)
{
statement2;
}
}
The second if is inside the first if.
int age = 20;
if (age >= 18)
{
if (age <= 60)
{
printf("Adult");
}
}
The inner condition is checked only because age >= 18 is true.
The execution happens step by step:
int age = 25;
int marks = 70;
if (age >= 18)
{
if (marks >= 40)
{
printf("Eligible");
}
}
The student must satisfy the age condition before the marks condition is checked.
int age = 20;
if (age >= 18)
{
if (age >= 21)
{
printf("Age is 21 or above");
}
else
{
printf("Age is between 18 and 20");
}
}
else
{
printf("Below 18");
}
Both outer and inner if-else statements can be combined.
int number = 20;
if (number > 0)
{
if (number % 2 == 0)
{
printf("Positive even number");
}
}
The program checks whether the number is positive first. Only then does it check whether it is even.
int number = 10;
if (number >= 0)
{
if (number == 0)
{
printf("Zero");
}
else
{
printf("Positive");
}
}
else
{
printf("Negative");
}
The outer condition separates non-negative and negative numbers. The inner condition then distinguishes zero from positive numbers.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
if (age <= 60)
{
printf("Adult");
}
}
return 0;
}
The program checks the second condition only for people aged 18 or above.
int age = 20;
int marks = 65;
if (age >= 18)
{
if (marks >= 50)
{
printf("Student is eligible");
}
}
Both conditions must be satisfied for the message to be displayed.
int marks = 75;
if (marks >= 40)
{
if (marks >= 75)
{
printf("Passed with distinction");
}
else
{
printf("Passed");
}
}
else
{
printf("Failed");
}
The inner decision is made only after the student has passed.
int usernameCorrect = 1;
int passwordCorrect = 1;
if (usernameCorrect)
{
if (passwordCorrect)
{
printf("Login successful");
}
else
{
printf("Incorrect password");
}
}
else
{
printf("Incorrect username");
}
The password is checked only after the username condition is satisfied.
int age = 25;
int marks = 80;
if (age >= 18)
{
if (marks >= 60 && marks <= 100)
{
printf("Eligible");
}
}
Logical operators can be used inside the inner condition.
char grade = 'A';
if (grade == 'A')
{
if (grade == 'A')
{
printf("Excellent");
}
}
The example is simple for demonstration. In practical programs, avoid repeating the same condition unnecessarily.
int number = 50;
if (number >= 1)
{
if (number <= 100)
{
printf("Number is between 1 and 100");
}
}
The outer condition checks the lower limit and the inner condition checks the upper limit.
int amount = 5000;
int member = 1;
if (amount >= 3000)
{
if (member)
{
printf("Member discount available");
}
}
The membership condition is checked only when the purchase amount reaches the required amount.
int age = 25;
int marks = 80;
int attendance = 90;
if (age >= 18)
{
if (marks >= 40)
{
if (attendance >= 75)
{
printf("Eligible");
}
}
}
Nested statements can have more than one level, but too many levels can make code difficult to read.
#include <stdio.h>
int main()
{
int age, marks;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter marks: ");
scanf("%d", &marks);
if (age >= 18)
{
if (marks >= 40)
{
printf("Eligible");
}
else
{
printf("Marks are too low");
}
}
else
{
printf("Age is too low");
}
return 0;
}
int a = 30;
int b = 50;
int c = 40;
if (a > b)
{
if (a > c)
{
printf("A is largest");
}
else
{
printf("C is largest");
}
}
else
{
if (b > c)
{
printf("B is largest");
}
else
{
printf("C is largest");
}
}
Nested decisions can be used to compare several values.
int a = 20;
int b = 10;
int c = 30;
if (a < b)
{
if (a < c)
{
printf("A is smallest");
}
else
{
printf("C is smallest");
}
}
else
{
if (b < c)
{
printf("B is smallest");
}
else
{
printf("C is smallest");
}
}
The outer decision narrows down the possible result before the inner decision is made.
In C, an else is associated with the nearest unmatched
if in the same block.
if (a > 0)
{
if (b > 0)
{
printf("Both positive");
}
else
{
printf("B is not positive");
}
}
Here the else belongs to the inner if.
Curly braces make nested decision structures easier to understand.
if (age >= 18)
{
if (marks >= 40)
{
printf("Eligible");
}
else
{
printf("Marks are low");
}
}
else
{
printf("Age is low");
}
Always use braces when nested conditions become complex.
Nested version:
if (age >= 18)
{
if (marks >= 40)
{
printf("Eligible");
}
}
Using logical AND:
if (age >= 18 && marks >= 40)
{
printf("Eligible");
}
Both can express the same basic requirement in this example. Nested
if can be useful when the second decision needs to happen only
after the first decision or when the inner logic has its own branches.
Nested statements become difficult to understand when braces are omitted.
A clearer version is:
if (age >= 18)
{
if (marks >= 40)
{
printf("Eligible");
}
}
Proper indentation and braces make the structure clear.
Use == when comparing values.
Correct:
if (age == 18)
{
if (marks >= 40)
{
printf("Eligible");
}
}
Do not use assignment = when you intend to compare values.
A nested structure can contain an else-if ladder.
int marks = 85;
if (marks >= 40)
{
if (marks >= 90)
{
printf("A+");
}
else if (marks >= 80)
{
printf("A");
}
else
{
printf("Pass");
}
}
else
{
printf("Fail");
}
This separates the overall pass/fail decision from the grade decision.
#include <stdio.h>
int main()
{
int age, marks;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter marks: ");
scanf("%d", &marks);
if (age >= 18)
{
if (marks >= 40)
{
printf("Eligible for admission");
}
else
{
printf("Marks are insufficient");
}
}
else
{
printf("Age requirement not satisfied");
}
return 0;
}
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b)
{
if (a > c)
{
printf("%d is largest", a);
}
else
{
printf("%d is largest", c);
}
}
else
{
if (b > c)
{
printf("%d is largest", b);
}
else
{
printf("%d is largest", c);
}
}
return 0;
}
#include <stdio.h>
int main()
{
int userId, password;
printf("Enter user ID: ");
scanf("%d", &userId);
printf("Enter password: ");
scanf("%d", &password);
if (userId == 100)
{
if (password == 1234)
{
printf("Login successful");
}
else
{
printf("Incorrect password");
}
}
else
{
printf("Incorrect user ID");
}
return 0;
}
This example demonstrates how one condition can be checked only after another condition succeeds.
Practice the following programs:
Start with two levels of nested conditions. Once you understand the structure, practice programs with three or more related decisions.
if means an if statement inside another if statement.if statements are useful for dependent decisions.if statements can contain else and else-if.else belongs to the nearest unmatched if.Question: What is a nested if statement?