The else if Statement - C++

In C++, the if, else if, and else statements are used for conditional execution, allowing the program to make decisions based on specified conditions.

if Statement

The if statement is used to execute a block of code if a specified condition is true.

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

The condition can be any Boolean expression. If the condition evaluates to true, the block of code inside the if statement is executed. Otherwise, the block of code is skipped.

Example
int number = 10; if (number > 5) { std::cout << "Number is greater than 5." << std::endl; }

else if Statement

The else if statement is used to provide an alternative condition to test if the initial if condition is false.

Synatx
else if (condition2) { // Code to be executed if condition1 is false and condition2 is true }

The condition2 can be any Boolean expression. If the condition2 evaluates to true and the condition1 evaluated to false, the block of code inside the else if statement is executed. Otherwise, the block of code is skipped.

Example
int number = 3; if (number > 5) { std::cout << "Number is greater than 5." << std::endl; } else if (number > 0) { std::cout << "Number is positive but not greater than 5." << std::endl; }

else Statement

The else statement is used to execute a block of code when none of the previous conditions in the if and else if statements is true.

Syntax
else { // Code to be executed if all previous conditions evaluated to false }

The block of code inside the else statement is executed only if all of the previous conditions evaluated to false.

Example
int number = -2; if (number > 5) { std::cout << "Number is greater than 5." << std::endl; } else if (number > 0) { std::cout << "Number is positive but not greater than 5." << std::endl; } else { std::cout << "Number is non-positive." << std::endl; }

These statements allow you to create complex decision-making structures. In the examples, the program evaluates conditions and executes the corresponding block of code based on whether those conditions are met.

Full Source

The following example shows how to use the if, else if, and else statements to classify a number as even or odd:

int main() { int number; std::cout << "Enter a number: "; std::cin >> number; // Check if the number is even if (number % 2 == 0) { std::cout << "The number is even." << std::endl; } else if (number % 2 == 1) { std::cout << "The number is odd." << std::endl; } else { std::cout << "The number is invalid." << std::endl; } return 0; }
//Output: Enter a number: 10 The number is even.

You can have multiple else if blocks to test multiple conditions, and if none of the conditions are true, the code within the else block will be executed as a default action.

Conclusion

The if, else if, and else statements are used for conditional execution, allowing the program to make decisions based on specified conditions. The if statement is used to execute code when a condition is true, the else if statement provides alternative conditions, and the else statement defines what to do when none of the conditions are met. These constructs are fundamental for creating branching logic in C++ programs.