What is Nested if Statement in C++?

The nested if statement means using an if statement inside another if statement. It is used when we need to check multiple conditions step by step.

In this, the inner if statement executes only when the outer condition is true.

Syntax of Nested if Statement:

if (condition1) {

    if (condition2) {
        // code if both conditions are true
    } else {
        // code if condition1 is true but condition2 is false
    }

} else {
    // code if condition1 is false
}

Explanation:

  • First, condition1 is checked.
  • If it is true, then condition2 is checked.
  • If both are true, inner if block executes.
  • If condition2 is false, inner else block executes.
  • If condition1 is false, outer else block executes.

Example 1:

#include <iostream>
using namespace std;

int main() {
    int marks = 75;

    if (marks >= 40) {
        if (marks >= 75) {
            cout << "Passed with Distinction";
        } else {
            cout << "Passed";
        }
    } else {
        cout << "Failed";
    }

    return 0;
}

Output:

Passed with Distinction

Explanation:

  • First, marks >= 40 is checked → true.
  • Then, marks >= 75 is checked → true.
  • So, inner if block executes → "Passed with Distinction".

Example 2:

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    bool hasLicense = true;

    if (age >= 18) {
        if (hasLicense) {
            cout << "You can drive";
        } else {
            cout << "You need a driving license";
        }
    } else {
        cout << "You are underage";
    }

    return 0;
}

Output:

You can drive

Explanation:

  • First, age >= 18 is checked → true.
  • Then, hasLicense is checked → true.
  • Both conditions are true, so "You can drive" is printed.

Summary:

  • Nested if means an if inside another if.
  • It is used to check conditions step by step.
  • Inner condition runs only if outer condition is true.
  • It helps in writing detailed decision logic.
  • Nested if is used for multiple condition checking.
  • Too many nested ifs can make code complex.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar