C++ Code Example: nested if-else statement

When an if-else statement is executed in the body of another if-else statement, it is called nested.

Code Example

The program first declares a variable number and assigns it a value of 61. The code then checks whether the value of number is less than or equal to 100. If it is, the program checks whether the value is less than 50, equal to 50, or greater than 50 and less than or equal to 100, and outputs a corresponding message. If the value of number is greater than 100, the program outputs a message indicating that the value is greater than 100.

#include <iostream>
using namespace std;

int main() {
    int number = 61;
    
    if (number <= 100) {
        if (number < 50) {
            cout << "Value is smaller than 50";
        } else if (number == 50) {
            cout << "Value is 50";
        } else {
            cout << "Value is between 51 und  100";
        }
    } else {
        cout << "Value is greater than 100";
    }

    return 0;
}
Output
Value is between 51 und 100