If-else syntax

The if-else statement is a fundamental control structure that allows you to execute different blocks of code based on the evaluation of boolean conditions. Here’s a detailed explanation of the syntax and usage of if-else statements in C++:

Basic if Statement

The if statement evaluates a condition and executes a block of code if the condition is true.

Syntax:

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

Example:

int number = 10;
if (number > 0) {
    std::cout << "The number is positive." << std::endl;
}

if-else Statement

The if-else statement provides an alternative block of code to be executed 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:

int number = -5;
if (number > 0) {
    std::cout << "The number is positive." << std::endl;
} else {
    std::cout << "The number is not positive." << std::endl;
}

if-else if-else Ladder

When you need to test multiple conditions, you can use an if-else if-else ladder. This allows for sequential checking of multiple conditions.

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 none of the conditions are true
}

Example:

int number = 0;
if (number > 0) {
    std::cout << "The number is positive." << std::endl;
} else if (number < 0) {
    std::cout << "The number is negative." << std::endl;
} else {
    std::cout << "The number is zero." << std::endl;
}

Nested if-else Statements

You can nest if-else statements within each other to handle more complex conditions.

Syntax:

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

Example:

int number = 10;
if (number > 0) {
    if (number % 2 == 0) {
        std::cout << "The number is positive and even." << std::endl;
    } else {
        std::cout << "The number is positive and odd." << std::endl;
    }
} else {
    std::cout << "The number is not positive." << std::endl;
}

Common Mistakes

Missing Braces: Although braces {} are optional for single statements, it’s good practice to use them to avoid logical errors and improve code readability.

// Correct
if (number > 0) {
    std::cout << "Positive" << std::endl;
}

// Without braces (can lead to errors)
if (number > 0)
    std::cout << "Positive" << std::endl;

Dangling Else: Ensure that each else matches the correct if. Proper indentation helps avoid confusion.

int a = 5, b = 10;
if (a > b) 
    if (a > 0)
        std::cout << "a is greater than b and positive" << std::endl;
    else
        std::cout << "a is not positive" << std::endl; // This else corresponds to the inner if

Logical Errors: Ensure conditions are logically correct to avoid unexpected behavior.

int age = 18;
if (age >= 18) {
    std::cout << "Adult" << std::endl;
} else if (age >= 13) {
    std::cout << "Teenager" << std::endl;
} else {
    std::cout << "Child" << std::endl;
}