C++ if-else Statement

Introduction

Conditional statements allow you to control the flow of your program by executing certain code blocks based on specified conditions. In C++, the if-else statement is a primary way to perform conditional logic. This chapter will cover the if statement, if-else statement, nested if statements, and the if-else-if ladder.

if Statement

The if statement executes a block of code if a specified condition is true. If the condition is false, the block of code is skipped.

Syntax

if (condition) {
    // code to be executed if condition is true
}

Example

#include <iostream>
using namespace std;

int main() {
    int number = 10;

    if (number > 5) {
        cout << "The number is greater than 5." << endl;
    }

    return 0;
}

Output

The number is greater than 5.

if-else Statement

The if-else statement executes one block of code if the condition is true and another block if the condition is false.

Syntax

if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Example

#include <iostream>
using namespace std;

int main() {
    int number = 3;

    if (number > 5) {
        cout << "The number is greater than 5." << endl;
    } else {
        cout << "The number is not greater than 5." << endl;
    }

    return 0;
}

Output

The number is not greater than 5.

Nested if Statement

A nested if statement is an if statement inside another if statement. This allows you to check multiple conditions in a hierarchical manner.

Syntax

if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if condition1 and condition2 are true
    }
}

Example

#include <iostream>
using namespace std;

int main() {
    int number = 10;

    if (number > 5) {
        cout << "The number is greater than 5." << endl;

        if (number > 8) {
            cout << "The number is also greater than 8." << endl;
        }
    }

    return 0;
}

Output

The number is greater than 5.
The number is also greater than 8.

if-else-if Ladder

The if-else-if ladder allows you to check multiple conditions sequentially. Once a condition is true, the corresponding block of code is executed, and the rest are skipped.

Syntax

if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else if (condition3) {
    // code to be executed if condition3 is true
} else {
    // code to be executed if all conditions are false
}

Example

#include <iostream>
using namespace std;

int main() {
    int number = 8;

    if (number < 5) {
        cout << "The number is less than 5." << endl;
    } else if (number == 5) {
        cout << "The number is equal to 5." << endl;
    } else if (number > 5 && number < 10) {
        cout << "The number is greater than 5 but less than 10." << endl;
    } else {
        cout << "The number is 10 or greater." << endl;
    }

    return 0;
}

Output

The number is greater than 5 but less than 10.

Conclusion

The if-else statement is a fundamental control flow structure in C++ that allows you to execute different code blocks based on conditions. This chapter covered the if statement, if-else statement, nested if statements, and the if-else-if ladder with simple examples and their outputs. Understanding these constructs will help you write more logical and efficient programs by making decisions based on different conditions. In the next chapter, we will explore the switch statement in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top