Nested if Statement in C++

 


A nested if statement is an if statement inside another if statement. It is used when we need to check multiple conditions in a hierarchical manner.


1️⃣ Syntax of Nested if Statement

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

📌 The inner if executes only if the outer if condition is true.


2️⃣ Example: Checking Voting Eligibility with Nested if

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;

    if (age >= 18) {  
        if (age >= 60) {
            cout << "You are a senior citizen." << endl;
        } else {
            cout << "You are eligible to vote." << endl;
        }
    } else {
        cout << "You are not eligible to vote." << endl;
    }

    return 0;
}

Output 1 (if input is 25):

You are eligible to vote.

Output 2 (if input is 65):

You are a senior citizen.

Output 3 (if input is 15):

You are not eligible to vote.

📌 The inner if runs only if age >= 18.


3️⃣ Example: Checking a Number's Type (Positive, Negative, Even, Odd)

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (num > 0) {
        if (num % 2 == 0) {
            cout << "The number is positive and even." << endl;
        } else {
            cout << "The number is positive and odd." << endl;
        }
    } else if (num < 0) {
        if (num % 2 == 0) {
            cout << "The number is negative and even." << endl;
        } else {
            cout << "The number is negative and odd." << endl;
        }
    } else {
        cout << "The number is zero." << endl;
    }

    return 0;
}

Output (if input is -7):

The number is negative and odd.

4️⃣ Example: Checking Admission Eligibility (Marks + Age Criteria)

#include <iostream>
using namespace std;

int main() {
    int marks, age;
    cout << "Enter your marks: ";
    cin >> marks;
    cout << "Enter your age: ";
    cin >> age;

    if (marks >= 75) {
        if (age >= 18) {
            cout << "You are eligible for admission." << endl;
        } else {
            cout << "You need to be at least 18 years old." << endl;
        }
    } else {
        cout << "You need at least 75 marks for admission." << endl;
    }

    return 0;
}

Output 1 (if marks = 80, age = 19):

You are eligible for admission.

Output 2 (if marks = 80, age = 16):

You need to be at least 18 years old.

Output 3 (if marks = 70, age = 20):

You need at least 75 marks for admission.

5️⃣ Example: Checking Employee Salary Based on Department

#include <iostream>
using namespace std;

int main() {
    string department;
    int experience;
    
    cout << "Enter department (IT/HR): ";
    cin >> department;
    cout << "Enter years of experience: ";
    cin >> experience;

    if (department == "IT") {
        if (experience >= 5) {
            cout << "Salary: $80,000" << endl;
        } else {
            cout << "Salary: $50,000" << endl;
        }
    } else if (department == "HR") {
        if (experience >= 5) {
            cout << "Salary: $60,000" << endl;
        } else {
            cout << "Salary: $40,000" << endl;
        }
    } else {
        cout << "Invalid department entered." << endl;
    }

    return 0;
}

Output (if department = "IT", experience = 6):

Salary: $80,000

Output (if department = "HR", experience = 3):

Salary: $40,000

6️⃣ Nested if with Logical Operators

We can combine conditions using logical operators (&&, ||) inside if statements.

#include <iostream>
using namespace std;

int main() {
    int age, salary;
    cout << "Enter your age: ";
    cin >> age;
    cout << "Enter your salary: ";
    cin >> salary;

    if (age >= 18) {
        if (salary >= 50000 && salary <= 100000) {
            cout << "You are eligible for a bank loan." << endl;
        } else {
            cout << "Your salary is not within the required range." << endl;
        }
    } else {
        cout << "You must be at least 18 years old to apply." << endl;
    }

    return 0;
}

Output 1 (if age = 25, salary = 60000):

You are eligible for a bank loan.

Output 2 (if age = 25, salary = 40000):

Your salary is not within the required range.

Output 3 (if age = 16, salary = 70000):

You must be at least 18 years old to apply.

7️⃣ Key Points About Nested if

Inner if runs only if outer if is true.
✅ Can be used for multi-level decision making.
✅ Avoid too many nested if statements to keep code readable.
✅ Use logical operators (&&, ||) to simplify conditions.

Would you like an example with user input validation (e.g., preventing non-numeric input)? 🚀

Post a Comment

0 Comments