Lesson 26 of 60 – Nested if Statement in C
43%

Nested if Statement in C

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.

Note: In a nested if, the inner condition is checked only when program execution reaches that inner if statement.

1. What is a Nested if?

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.

2. Basic Syntax of Nested if

if (condition1)
{
    statement1;

    if (condition2)
    {
        statement2;
    }
}

The second if is inside the first if.

3. Simple Nested if Example

int age = 20;

if (age >= 18)
{
    if (age <= 60)
    {
        printf("Adult");
    }
}

The inner condition is checked only because age >= 18 is true.

4. How Nested if Works

The execution happens step by step:

  1. The outer condition is checked.
  2. If the outer condition is false, the inner block is skipped.
  3. If the outer condition is true, execution enters the outer block.
  4. The inner condition is then checked.
  5. If the inner condition is true, its block executes.

5. Nested if with Two Conditions

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.

6. Nested if with else

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.

7. Checking Positive Number and Even Number

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.

8. Positive and Negative with Nested if

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.

9. Nested if with User Input

#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.

10. Nested if for Student Eligibility

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.

11. Nested if with else

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.

12. Nested if for Login

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.

13. Nested if with Logical Conditions

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.

14. Nested if with Characters

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.

15. Checking Number Range

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.

16. Nested if for Discount

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.

17. Nested if with Multiple Levels

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.

18. Nested if with User Input

#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;
}

19. Nested if for Largest of Three Numbers

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.

20. Nested if for Smallest of Three Numbers

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.

21. else Belongs to the Nearest if

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.

22. Using Braces to Avoid Confusion

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.

23. Nested if vs Logical AND

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.

24. Common Mistake: Missing Braces

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.

25. Common Mistake: Incorrect Comparison

Use == when comparing values.

Correct:

if (age == 18)
{
    if (marks >= 40)
    {
        printf("Eligible");
    }
}

Do not use assignment = when you intend to compare values.

26. Nested if with else-if

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.

27. Complete Eligibility Program

#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;
}

28. Complete Largest Number Program

#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;
}

29. Complete Login Example

#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.

30. Practice Programs Using Nested if

Practice the following programs:

  1. Check whether a number is positive and even.
  2. Check whether a student is eligible based on age and marks.
  3. Find the largest of three numbers using nested if.
  4. Find the smallest of three numbers using nested if.
  5. Create a simple login system.
  6. Check whether a number is within a specified range.
  7. Create a discount eligibility program.
  8. Create a grade system using nested if and else-if.
  9. Check admission eligibility using multiple conditions.
  10. Create a simple menu decision system using nested if.

Start with two levels of nested conditions. Once you understand the structure, practice programs with three or more related decisions.

📌 Key Points

  • A nested if means an if statement inside another if statement.
  • The inner condition is checked only when execution reaches it.
  • Nested if statements are useful for dependent decisions.
  • Nested if statements can contain else and else-if.
  • Curly braces make nested structures easier to understand.
  • An else belongs to the nearest unmatched if.
  • Logical operators can sometimes replace simple nested conditions.
  • Too many nested levels can make code difficult to read.
  • Use meaningful indentation and braces when writing nested conditions.
  • Practice with eligibility, login, number comparison, and grading problems.

🧠 Quick Quiz

Question: What is a nested if statement?