C++ if, if...else, and Nested if...else


Introduction

In C++, the if statement is used to make decisions in your program. It allows you to execute certain blocks of code based on whether a specified condition is true or false. C++ also provides the if...else and nested if...else statements to handle multiple conditions and complex decision-making processes.

In this blog post, we'll cover:

  • The if statement: A basic conditional statement.
  • The if...else statement: Executes one block of code if the condition is true, and another if it's false.
  • The nested if...else statement: Allows you to test multiple conditions inside other if statements.

1. The if Statement in C++

The if statement is the simplest form of decision-making in C++. It evaluates a condition and, if the condition is true, executes the corresponding block of code.

Syntax:

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

Example:

#include <iostream>
using namespace std;

int main() {
    int a = 10;

    // Check if the number is positive
    if (a > 0) {
        cout << "The number is positive." << endl;
    }

    return 0;
}

Output:

The number is positive.

Explanation:

  • The condition a > 0 is evaluated. Since it is true, the statement inside the if block is executed, printing "The number is positive."

2. The if...else Statement in C++

The if...else statement provides an alternative block of code to be executed if the condition is false. It helps in situations where you need to check a condition and handle both true and false cases.

Syntax:

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

Example:

#include <iostream>
using namespace std;

int main() {
    int a = -5;

    // Check if the number is positive or negative
    if (a > 0) {
        cout << "The number is positive." << endl;
    } else {
        cout << "The number is negative." << endl;
    }

    return 0;
}

Output:

The number is negative.

Explanation:

  • The condition a > 0 is evaluated. Since a is -5, the condition is false, so the code inside the else block is executed, printing "The number is negative."

3. The else if Statement

The else if statement allows you to test multiple conditions. This is useful when you have more than two possibilities and want to check multiple conditions sequentially.

Syntax:

if (condition1) {
    // Code if condition1 is true
} else if (condition2) {
    // Code if condition2 is true
} else {
    // Code if all previous conditions are false
}

Example:

#include <iostream>
using namespace std;

int main() {
    int a = 0;

    // Check if the number is positive, negative, or zero
    if (a > 0) {
        cout << "The number is positive." << endl;
    } else if (a < 0) {
        cout << "The number is negative." << endl;
    } else {
        cout << "The number is zero." << endl;
    }

    return 0;
}

Output:

The number is zero.

Explanation:

  • The first condition (a > 0) is false.
  • The second condition (a < 0) is also false.
  • Therefore, the program executes the code in the else block, printing "The number is zero."

4. Nested if...else Statements in C++

A nested if...else statement is an if or else block inside another if or else block. It is used when you have multiple conditions that need to be checked sequentially and need more than one level of decision-making.

Syntax:

if (condition1) {
    if (condition2) {
        // Code to execute if both condition1 and condition2 are true
    } else {
        // Code to execute if condition1 is true, but condition2 is false
    }
} else {
    // Code to execute if condition1 is false
}

Example:

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    // Check if a is greater than b, and if b is positive
    if (a > b) {
        if (b > 0) {
            cout << "a is greater than b and b is positive." << endl;
        } else {
            cout << "a is greater than b but b is non-positive." << endl;
        }
    } else {
        cout << "a is not greater than b." << endl;
    }

    return 0;
}

Output:

a is greater than b and b is positive.

Explanation:

  • The first if statement checks if a > b. Since this is true, it proceeds to the nested if statement.
  • The nested if checks if b > 0, which is also true, so it prints "a is greater than b and b is positive."

5. Practical Use Cases of if, if...else, and Nested if...else

These conditional statements are commonly used in real-world applications such as:

Example 1: Grade Evaluation

#include <iostream>
using namespace std;

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

    if (marks >= 90) {
        cout << "You got an A grade!" << endl;
    } else if (marks >= 75) {
        cout << "You got a B grade!" << endl;
    } else if (marks >= 50) {
        cout << "You got a C grade!" << endl;
    } else {
        cout << "You failed the exam." << endl;
    }

    return 0;
}

Explanation:

  • Based on the marks, the program uses a chain of if...else if statements to print the grade the user received.

Example 2: Check Voting Eligibility

#include <iostream>
using namespace std;

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

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

    return 0;
}

Explanation:

  • The program checks if the user is 18 or older using a simple if...else statement to determine voting eligibility.