C Programming • C Basics, Operators, Loops
C Programming / Control Structures: nested if

Control Structures: nested if

Notes 2 C Basics, Operators, Loops

Definition: A nested if statement is an if statement placed inside another if statement. It is used when a second condition needs to be checked only after the first condition is true.

Notes

Nested if Statement in C

Definition: A nested if statement is an if statement placed inside another if statement. It is used when a second condition needs to be checked only after the first condition is true.

💡 In simple words: First, C checks the outer if condition. If it is true, the inner if condition is checked.

Syntax

if (condition1)
{
    if (condition2)
    {
        // statements
    }
}

How Nested if Works

In a nested if, one condition is placed inside another condition. The inner condition is checked only when the outer condition is true.

💡 Example: Suppose we want to check whether a student has passed and then check whether the student has scored more than 75 marks.

Example

#include <stdio.h>

int main()
{
    int marks = 80;

    if (marks >= 40)
    {
        if (marks >= 75)
        {
            printf("Student passed with distinction.");
        }
    }

    return 0;
}

Output

Student passed with distinction.

Nested if for Multiple Conditions

Nested if statements can be used when more than one condition must be checked step by step.

💡 Example: To find the largest among three numbers, first compare the first two numbers. Then compare the larger number with the third number.

Practical Example — Largest of Three Numbers

#include <stdio.h>

int main()
{
    int a, b, c, largest;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a > b)
    {
        if (a > c)
            largest = a;
        else
            largest = c;
    }
    else
    {
        if (b > c)
            largest = b;
        else
            largest = c;
    }

    printf("Largest number = %d", largest);

    return 0;
}

Output

Enter three numbers: 25 40 15
Largest number = 40

Important Points — Nested if

  • A nested if means an if statement inside another if statement.
  • The inner if is checked only when the outer condition is true.
  • Nested if is useful for checking conditions in a step-by-step manner.
  • Multiple levels of nested if statements can be used when required.
  • Proper indentation should be used to make nested conditions easy to understand.

Nested if vs if-else

Nested if if-else
An if statement is placed inside another if statement. Provides two alternative paths based on a condition.
Useful for dependent or step-by-step conditions. Useful when one of two alternatives must be selected.
Inner condition depends on the outer condition. The else block executes when the if condition is false.

Exam Tip

🎯 Remember: In nested if, the inner condition does not execute independently. The program reaches the inner if only when the outer if condition is satisfied.

Quick Revision

Nested if → Outer condition → Inner condition → Statement