C++ if else if else statement


The if-else if-else statement in C++ is used for multi-way decision-making when there are several possible conditions to evaluate. It is an extension of the basic if statement that allows you to check multiple conditions in sequence and execute the corresponding block of code when the first matching condition is found.

Syntax

if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition1 is false and condition2 is true } else if (condition3) { // Code to execute if condition1 and condition2 are false and condition3 is true } else { // Code to execute if none of the above conditions are true }

How It Works

  • The if-else if-else structure starts by evaluating condition1.
    • If condition1 is true, the code block associated with it is executed, and the rest of the conditions are ignored.
    • If condition1 is false, the program checks condition2. If condition2 is true, the corresponding code block runs.
    • This process continues until one of the conditions is found to be true or the else block is reached.
    • If none of the conditions are true, the else block is executed as a default.

Example

Here is an example that demonstrates the use of if-else if-else to determine a grade based on a score:

#include <iostream> int main() { int score; std::cout << "Enter your score: "; std::cin >> score; if (score >= 90) { std::cout << "Grade: A" << std::endl; } else if (score >= 80) { std::cout << "Grade: B" << std::endl; } else if (score >= 70) { std::cout << "Grade: C" << std::endl; } else if (score >= 60) { std::cout << "Grade: D" << std::endl; } else { std::cout << "Grade: F" << std::endl; } return 0; }

Explanation

  • In the above code:
    • The program takes a score input from the user.
    • It first checks if the score is >= 90. If it is, it prints "Grade: A".
    • If the score is not >= 90, it checks if it is >= 80. If true, it prints "Grade: B".
    • This continues for each else if condition.
    • If the score is less than 60, the else block is executed, and "Grade: F" is printed.

Key Points

  1. Order Matters: The conditions are evaluated from top to bottom. The first condition that evaluates to true will execute its block of code, and the rest will be skipped.
  2. Only One Block Executes: In an if-else if-else chain, only one block of code can execute, even if multiple conditions could potentially be true. The first true condition wins.
  3. The else Block: The else block is optional and acts as a default for when none of the if or else if conditions are true.

Example Without else

The else block is not mandatory. If you do not include it, and none of the if or else if conditions are met, no action will be taken.

int number = 15; if (number < 10) { std::cout << "Number is less than 10" << std::endl; } else if (number > 10 && number <= 20) { std::cout << "Number is between 11 and 20" << std::endl; // Output: Number is between 11 and 20 } // No `else` block here, so if the number doesn't match any condition, nothing happens.

Summary

  • if-else if-else is useful for handling multiple possible conditions.
  • The conditions are evaluated in sequence, and only the block corresponding to the first true condition will be executed.
  • The else block is optional but can be used to handle cases where none of the preceding conditions are met.

The if-else if-else statement makes decision-making in C++ more flexible and allows the program to respond to various scenarios effectively.