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

Nested if Statement in C++

A nested if statement means writing one if statement inside another if statement. It is useful when a second condition needs to be checked only after the first condition is true.

Note: In a nested if statement, the inner if is checked only when the outer if condition allows program execution to reach it.

1. What is a Nested if?

When an if statement is placed inside another if statement, it is called a nested if.

if (condition1) {

    if (condition2) {
        // statements
    }

}

The inner condition is checked only when the outer condition is true.

2. Basic Syntax

The general syntax of a nested if statement is:

if (condition1) {

    // Outer if

    if (condition2) {

        // Inner if

    }

}

3. Simple Nested if Example

int age = 20;

if (age >= 18) {

    if (age <= 60) {
        std::cout << "Adult";
    }

}

First, the program checks whether the age is at least 18. If that is true, it checks whether the age is 60 or below.

4. How Nested if Works

A nested if statement works in two stages:

  1. The outer condition is checked first.
  2. If the outer condition is true, the inner condition is checked.
  3. If the inner condition is also true, its block executes.
  4. If the outer condition is false, the inner if is skipped.

5. Nested if with Marks

int marks = 80;

if (marks >= 40) {

    if (marks >= 75) {
        std::cout << "Distinction";
    }

}

The inner condition is checked only because the student has passed.

6. Nested if with Age

int age = 25;

if (age >= 18) {

    if (age < 60) {
        std::cout << "Adult";
    }

}

Both conditions must be true for the message to be displayed.

7. Outer if is False

int age = 15;

if (age >= 18) {

    if (age < 60) {
        std::cout << "Adult";
    }

}

Here the outer condition is false, so the inner if statement is never checked.

8. Inner if is False

int age = 65;

if (age >= 18) {

    if (age < 60) {
        std::cout << "Adult";
    }

}

The outer condition is true, but the inner condition is false. Therefore, the message is not displayed.

9. Nested if with Boolean Variable

bool loggedIn = true;
bool isAdmin = true;

if (loggedIn) {

    if (isAdmin) {
        std::cout << "Admin access";
    }

}

The user must be logged in before the program checks whether the user is an administrator.

10. Nested if with Login

#include <iostream>
#include <string>

int main() {

    std::string username = "admin";
    std::string password = "1234";

    if (username == "admin") {

        if (password == "1234") {
            std::cout << "Login successful";
        }

    }

    return 0;
}

11. Nested if-else

An inner if can also have its own else block.

int age = 20;

if (age >= 18) {

    if (age >= 21) {
        std::cout << "Age is 21 or above";
    }
    else {
        std::cout << "Age is between 18 and 20";
    }

}

12. Outer if-else with Inner if

int marks = 85;

if (marks >= 40) {

    if (marks >= 75) {
        std::cout << "Good result";
    }
    else {
        std::cout << "Passed";
    }

}
else {

    std::cout << "Failed";

}

13. Nested if with Logical Conditions

int age = 25;
bool hasID = true;

if (age >= 18) {

    if (hasID && age <= 60) {
        std::cout << "Access allowed";
    }

}

Logical operators can also be used inside the nested condition.

14. Checking Student Eligibility

int marks = 70;
int attendance = 80;

if (marks >= 40) {

    if (attendance >= 75) {
        std::cout << "Eligible for examination";
    }

}

The student must satisfy both the marks and attendance requirements.

15. Checking Driving Eligibility

int age = 20;
bool hasLicense = true;

if (age >= 18) {

    if (hasLicense) {
        std::cout << "Can drive";
    }

}

16. Nested if with User Input

#include <iostream>

int main() {

    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    if (age >= 18) {

        if (age <= 60) {
            std::cout << "You are an adult";
        }

    }

    return 0;
}

17. Nested if for Even Positive Number

int number = 20;

if (number > 0) {

    if (number % 2 == 0) {
        std::cout << "Positive even number";
    }

}

The program first checks whether the number is positive and then checks whether it is even.

18. Nested if for Positive Even or Odd

int number = 15;

if (number > 0) {

    if (number % 2 == 0) {
        std::cout << "Positive even number";
    }
    else {
        std::cout << "Positive odd number";
    }

}
else {

    std::cout << "Number is not positive";

}

19. Nested if for Password Verification

#include <iostream>
#include <string>

int main() {

    std::string username;
    std::string password;

    std::cout << "Username: ";
    std::cin >> username;

    std::cout << "Password: ";
    std::cin >> password;

    if (username == "admin") {

        if (password == "1234") {
            std::cout << "Login successful";
        }
        else {
            std::cout << "Wrong password";
        }

    }
    else {

        std::cout << "Wrong username";

    }

    return 0;
}

20. Nested if for Discount

double amount = 6000;
bool member = true;

if (amount >= 5000) {

    if (member) {
        std::cout << "Member discount available";
    }

}

The customer must meet the purchase requirement before membership is checked.

21. Three-Level Nested if

An if statement can contain another if, and that inner if can contain another if. This creates multiple levels of nesting.

bool loggedIn = true;
bool verified = true;
bool isAdmin = true;

if (loggedIn) {

    if (verified) {

        if (isAdmin) {
            std::cout << "Admin access";
        }

    }

}

Although possible, too many nesting levels can make code harder to read.

22. Nested if with Character

char grade = 'A';

if (grade == 'A') {

    if (grade == 'A') {
        std::cout << "Excellent grade";
    }

}

The example demonstrates the structure of a nested condition. In real programs, redundant conditions like this should be avoided.

23. Nested if for Exam Result

int marks = 82;
int attendance = 80;

if (marks >= 40) {

    if (attendance >= 75) {

        if (marks >= 75) {
            std::cout << "Passed with good marks";
        }
        else {
            std::cout << "Passed";
        }

    }
    else {

        std::cout << "Attendance is too low";

    }

}
else {

    std::cout << "Failed";

}

24. Nested if vs Logical AND

Sometimes a nested if can be simplified by using the logical && operator.

Nested if:

if (age >= 18) {

    if (hasID) {
        std::cout << "Allowed";
    }

}

Using AND:

if (age >= 18 && hasID) {
    std::cout << "Allowed";
}

Both approaches can represent the same simple requirement. Choose the clearer structure for the situation.

25. Nested if with else-if

An inner condition can also contain an else-if ladder.

int marks = 85;

if (marks >= 40) {

    if (marks >= 90) {
        std::cout << "Grade A";
    }
    else if (marks >= 75) {
        std::cout << "Grade B";
    }
    else {
        std::cout << "Grade C";
    }

}
else {

    std::cout << "Fail";

}

26. Common Nested if Mistakes

  • Forgetting the closing curly brace of the inner if.
  • Forgetting the closing curly brace of the outer if.
  • Using incorrect indentation.
  • Creating unnecessary levels of nesting.
  • Checking the inner condition without understanding the outer condition.
  • Using = instead of == for comparison.
  • Writing complex nested conditions when a simple logical expression would be clearer.

Good indentation makes nested conditions much easier to understand.

27. Practical Admission Example

#include <iostream>

int main() {

    int marks;
    int age;

    std::cout << "Enter age: ";
    std::cin >> age;

    std::cout << "Enter marks: ";
    std::cin >> marks;

    if (age >= 18) {

        if (marks >= 40) {
            std::cout << "Eligible for admission";
        }
        else {
            std::cout << "Marks are too low";
        }

    }
    else {

        std::cout << "Age is below the required level";

    }

    return 0;
}

28. Practical Bank Access Example

#include <iostream>

int main() {

    bool loggedIn = true;
    bool accountActive = true;
    double balance = 5000;

    if (loggedIn) {

        if (accountActive) {

            if (balance > 0) {
                std::cout << "Account is ready";
            }
            else {
                std::cout << "Insufficient balance";
            }

        }
        else {

            std::cout << "Account is inactive";

        }

    }
    else {

        std::cout << "Please login";

    }

    return 0;
}

29. Best Practices for Nested if

  • Use clear indentation for every nested level.
  • Keep nesting levels as small as practical.
  • Use meaningful variable names.
  • Check the most important condition first.
  • Consider logical operators when they make the code simpler.
  • Use braces to make the structure clear.
  • Test both true and false paths.
if (condition1) {

    if (condition2) {
        // clear and readable code
    }

}

30. Nested if – Final Summary

Concept Meaning
Outer if The first condition that controls access to the inner if.
Inner if A condition placed inside another if statement.
Nested if-else An inner if can have its own else block.
Nested levels Multiple levels are possible, but excessive nesting can reduce readability.
Logical operators Can sometimes simplify nested conditions.
if (condition1) {

    if (condition2) {
        // statements
    }

}
else {

    // outer else

}

📌 Key Points

  • A nested if is an if statement inside another if statement.
  • The outer condition is checked first.
  • The inner condition is checked only when the outer condition is true.
  • An inner if can have its own else or else-if statements.
  • Nested if statements are useful for dependent conditions.
  • Logical operators can sometimes replace simple nested conditions.
  • Too many nested levels can make code difficult to read.
  • Proper indentation is important for nested statements.
  • Always test both the outer and inner conditions.
  • Use nested if when the second decision depends on the first decision.

🧠 Quick Quiz

Question: When is the inner if statement checked in a nested if?