How to Implement the if Statement With Multiple Conditions in C++

Aditya Raj Feb 02, 2024
  1. Implement the if Statement With Multiple Conditions in C++
  2. Implement the if Statement With Multiple Conditions Using the && Logical Operator in C++
  3. Implement the if Statement With Multiple Conditions Using the || Logical Operator in C++
  4. Implement the if Statement With Multiple Conditions Using the && and || Logical Operators in C++
  5. Conclusion
How to Implement the if Statement With Multiple Conditions in C++

In C++, logical operators && (logical “and”) and || (logical “or”) are powerful tools for handling multiple conditions within if statements. This article explores different techniques for implementing if statements with multiple conditions in C++ and provides examples for better understanding.

Implement the if Statement With Multiple Conditions in C++

In C++, you can employ multiple if statements in two ways: through nested if statements or by using multiple if statements in a single program to evaluate different conditions. Let’s explore both approaches in detail.

The following example checks the given condition using a nested if statement.

Code:

// C++ program demonstrating nested if-statement

#include <iostream>
using namespace std;

int main() {
  cout << "Enter a number: ";
  int num;
  cin >> num;

  if (num > 45) {
    if (num < 60) {
      cout << "Entered number is between 45 and 60";
    } else {
      // Displayed when the number is not between 45 and 60
      cout << "Entered number is not in between 45 and 60";
    }
  } else {
    // Displayed when the number is not greater than 45
    cout << "Entered number is not in between 45 and 60";
  }

  return 0;
}

Let’s assume you enter the number 55 as input.

Output:

Enter a number: 55
Entered number is between 45 and 60

Here, we used one if statement for every condition in the form of a nested if statement. In nested if statements, we need to write a longer piece of code.

Instead, we can use multiple logical && operators in a single if statement to make our code simpler and avoid writing unnecessary lines of code.

Implement the if Statement With Multiple Conditions Using the && Logical Operator in C++

The logical “and” operator && is a binary operator. It takes two Boolean values or expressions that evaluate a Boolean value as its operands.

Syntax:

int num_1 = 5;
int num_2 = 12;
int num_3 = 10;

// Check if condition1 (num_1 is less than num_2) AND condition2 (num_2 is
// greater than num_3) are both true.
if ((num_1 < num_2) && (num_2 > num_3)) {
  // This block of code executes if both condition1 and condition2 are true.

  statement_1;  // Execute statement_1
  statement_2;  // Execute statement_2
                // ...
                // Execute statement_n (and so on for all statements)
}

Consider the following example using the logical operator && in an if statement to check whether a number is between 45 and 60.

Code:

// C++ program demonstrating the use of Logical "and" (&&) operator

#include <iostream>
using namespace std;

int main() {
  cout << "Enter a number: ";
  int num;
  cin >> num;

  if (num > 45 && num < 60)
    cout << "Entered number is between 45 and 60";
  else
    cout << "Entered number is not in between 45 and 60";

  return 0;
}

Output:

Entered number is between 45 and 60

In the above example, the logical operator && checks whether the given number is greater than 45 or less than 60. If both operands are true, then the statements under the if condition are executed.

If any of the operands result in false, then the statement under the else section is executed.

We had to check if all the expressions of the if statements were true. However, there can be situations where we only need to check if one of the conditions is true.

For example, consider the dice example in which getting 2 or 5 in a throw of dice wins the match.

Code:

#include <iostream>
using namespace std;

int main() {
  cout << "Enter number on the dice: ";
  int num_1;
  cin >> num_1;

  if (num_1 == 2) {
    cout << "Game is Won";
    return 0;
  }

  if (num_1 == 5) {
    cout << "Game is Won";
    return 0;
  }

  else
    cout << "Game is Lost";

  return 0;
}

Let’s assume you enter the number 2 as input.

Output:

Enter number on the dice: 2
Game is Won

Here, even if either of the conditions (num_1 == 2) and (num_1 == 5) is true, the Game is Won. In such cases, we can use the logical || operators instead of multiple if statements in C++.

Implement the if Statement With Multiple Conditions Using the || Logical Operator in C++

The logical “or” operator (||) is also binary. It takes two Boolean values or expressions that evaluate a Boolean value as its operands.

Syntax:

int num_1;
int num_2;
int num_3;

// Check if any of the conditions inside the parentheses is true using the
// logical OR operator (||).
if ((num_1 == 0) || (num_2 == 0) || (num_3 == 0)) {
  // This block of code executes if any of the conditions inside the parentheses
  // is true.

  statement_1;  // Execute statement_1
  statement_2;  // Execute statement_2
                // ...
                // Execute statement_n (and so on for all statements)
}

Consider the following example explaining using the logical || operator instead of multiple if statements in C++.

Code:

// C++ program demonstrating the use of Logical "or" (||) operator

#include <iostream>
using namespace std;

int main() {
  cout << "Enter number on the dice: ";
  int num_1;
  cin >> num_1;

  if (num_1 == 2 || num_1 == 5)
    cout << "Game is Won";
  else
    cout << "Game is Lost";

  return 0;
}

Output:

Enter number on the dice: // Input 2 or 5 to get the expected output
Game is Won

In this example, the || operator checks whether the given number is 2 or 5. If any operands are true, then the statement under the if condition is executed, and the Game is won.

If all operands are evaluated as false, then the statement under the else section is executed, and the Game is lost.

Implement the if Statement With Multiple Conditions Using the && and || Logical Operators in C++

We can also use && and || in a single if statement to check for multiple conditions in C++. When we use both operators in one if statement to check multiple conditions, the evaluation needs to be done in the correct order.

The operator with the highest precedence is grouped with the operand first in an expression. Then, the next highest operator is grouped with the operand.

The precedence order of logical operators is mentioned in the following table:

Logical Operator Precedence
NOT (!) High
AND (&&) Medium
OR (`
  • The logical ! operator has the highest precedence in the expression and is evaluated first.

  • The logical && operator has the medium precedence in the expression and is evaluated after the ! operator.

  • The logical || operator has the lowest precedence in the expression and is evaluated after the && operator.

For a better understanding, consider the following example:

Code:

#include <iostream>
using namespace std;

int main() {
  cout << "Enter a number: ";
  int num;
  cin >> num;

  if ((num > 45 && num < 100) || (num > 100 && num < 60)) {
    cout << num;  // Display the number if it satisfies the conditions.
  }

  return 0;
}

Output:

Enter a number: 50
50

The input number 50 is displayed because it satisfies the condition specified in the if statement, which checks whether num is greater than 45 and less than 100 or greater than 100 and less than 60.

Conclusion

In this comprehensive guide, we have explored the use of if statements with multiple conditions in C++ by using logical operators, && and ||. Understanding these techniques, including nested if statements, offers you the flexibility to write C++ code.

Author: Aditya Raj
Aditya Raj avatar Aditya Raj avatar

Aditya Raj is a highly skilled technical professional with a background in IT and business, holding an Integrated B.Tech (IT) and MBA (IT) from the Indian Institute of Information Technology Allahabad. With a solid foundation in data analytics, programming languages (C, Java, Python), and software environments, Aditya has excelled in various roles. He has significant experience as a Technical Content Writer for Python on multiple platforms and has interned in data analytics at Apollo Clinics. His projects demonstrate a keen interest in cutting-edge technology and problem-solving, showcasing his proficiency in areas like data mining and software development. Aditya's achievements include securing a top position in a project demonstration competition and gaining certifications in Python, SQL, and digital marketing fundamentals.

GitHub

Related Article - C++ Operator