If-Else Statement in C++

The if-else statement is a fundamental control structure in the C++ programming language. It allows you to execute a block of code based on a certain condition. This conditional statement evaluates an expression and executes different sets of instructions depending on whether the condition is true or false.

Here’s the basic syntax of the if-else statement in C++:

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

Let’s dive into an example to illustrate how the if-else statement works:

#include 

int main() {
    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

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

    return 0;
}

In this example, we prompt the user to enter their age. The program then uses the if-else statement to check whether the entered age is greater than or equal to 18. If the condition is true, it displays the message “You are eligible to vote.” If the condition is false, it displays the message “You are not eligible to vote.”

Let’s break down the code:

  • The #include <iostream> directive allows us to use the input/output stream objects in C++.
  • The int age; line declares an integer variable named “age” to store the user’s input.
  • The std::cout << "Enter your age: "; line prints the message “Enter your age: ” to the console.
  • The std::cin >> age; line reads the user’s input and assigns it to the “age” variable.
  • The if (age >= 18) line checks whether the entered age is greater than or equal to 18.
  • If the condition is true, the code block within the if statement is executed, and the message “You are eligible to vote.” is printed to the console using std::cout.
  • If the condition is false, the code block within the else statement is executed, and the message “You are not eligible to vote.” is printed to the console.
  • The return 0; line ends the main() function and returns 0, indicating successful program execution.

The if-else statement can also be nested, allowing for more complex decision-making. Here’s an example:

#include 

int main() {
    int number;

    std::cout << "Enter a number: ";
    std::cin >> number;

    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;
    }

    return 0;
}

In this example, the program checks whether the entered number is positive, negative, or zero. If the number is greater than 0, it prints “The number is positive.” If the number is less than 0, it prints “The number is negative.” If the number is neither greater nor less than 0 (i.e., it is 0), it prints “The number is zero.”

The if-else statement is a powerful tool for controlling the flow of your C++ programs based on different conditions. It allows you to make decisions and execute specific blocks of code accordingly. Understanding how to use if-else statements effectively is essential for writing robust and flexible C++ programs.

Scroll to Top