Followers

Monday, August 14, 2023

Understanding the if-else Statement in C++ with Examples

 

In C++, the if and else statements are used for conditional execution of code. They allow us to execute certain blocks of code based on whether a specified condition is true or false. The if statement is used to execute code when a given condition is true, and the else statement is used to execute code when the condition is false.

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

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

Here's a simple example to illustrate the usage of the if-else statement:

cpp
#include <iostream> int main() { int number; std::cout << "Enter a number: "; std::cin >> number; if (number > 0) { std::cout << "The number is positive." << std::endl; } else { std::cout << "The number is non-positive." << std::endl; } return 0; }

In this example, the program prompts the user to enter a number. Then, it uses an if-else statement to check whether the entered number is greater than zero. If the condition (number > 0) is true, the code within the if block is executed, and it prints "The number is positive." If the condition is false, the code within the else block is executed, and it prints "The number is non-positive."

We can also have more complex conditions using logical operators like && (logical AND) and || (logical OR) within the if statement.

Remember that the else block is optional. If we only need to execute code when the condition is true, we can use just the if statement without an else block.


Let's dive into more details about the if-else statement in C++.

Basic if-else Structure:

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

Multiple if Statements (if-else if-else chain):

We can have multiple if statements in an if-else chain to test multiple conditions in sequence. The first true condition's block of code will be executed. If none of the conditions is true, the code in the else block (if present) will be executed.

cpp
if (condition1) { // Code to be executed if condition1 is true } else if (condition2) { // Code to be executed if condition2 is true } else { // Code to be executed if neither condition1 nor condition2 is true }

Nested if-else Statements:

We can also nest if-else statements inside each other to handle more complex conditions.

cpp
if (outerCondition) { // Code for outer condition if (innerCondition) { // Code for inner condition } else { // Code if inner condition is false } } else { // Code if outer condition is false }

Ternary Operator (Conditional Operator):

C++ also supports a shorthand form of the if-else statement called the ternary operator. It's used to assign a value based on a condition.

cpp
variable = (condition) ? value_if_true : value_if_false;

Example of Ternary Operator:

cpp
#include <iostream> int main() { int number; std::cout << "Enter a number: "; std::cin >> number; std::string result = (number > 0) ? "Positive" : "Non-positive"; std::cout << "The number is " << result << std::endl; return 0; }

In this example, the ternary operator is used to assign the appropriate label ("Positive" or "Non-positive") to the result variable based on the condition number > 0.

Remember that clear and readable code is important. While nested if-else statements and the ternary operator can be powerful tools, excessive nesting or overly complex conditions can make our code harder to understand.

No comments:

Post a Comment