C++ Nested if...else

Nested if...else statements in C++ are used to add more complex conditions and decision-making logic to a program. They allow you to have multiple levels of conditions and execute different blocks of code based on those conditions. Here is an example of a nested if...else statement in C++:

#include <iostream>
using namespace std;

int main() {
    int num = 10;

    if (num > 0) {
        cout << "The number is positive." << endl;

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

    return 0;
}

In this example, we have a variable num that is initialized with a value of 10. We start with the outer if statement if (num > 0) to check if num is greater than 0. If it is, we enter the if block and print "The number is positive.".

Inside the if block, we have another if...else statement if (num % 2 == 0) to check if num is divisible by 2 (i.e., if it is even). If it is, we enter the if block and print "The number is even.". If it's not divisible by 2, we enter the else block and print "The number is odd.".

If the condition in the outer if statement (num > 0) is not true, we skip the if block and directly execute the else block, which prints "The number is negative.".

In this way, the nested if...else statements allow us to have multiple levels of conditions and execute different blocks of code based on those conditions.