C++ OR Logical Operator

C++ OR Logical Operator is used to combine two or more logical conditions to form a compound condition. || is the symbol used for C++ OR Operator.

C++ OR Operator takes two boolean values as operands and returns a boolean value.

operand_1 || operand_2

Truth Table

Following is the truth table of C++ OR Logical Operator.

Operand 1Operand 2Returns
truetrue1
truefalse1
falsetrue1
falsefalse0

C++ OR returns true even if one of the operand is true.

C++ OR Example

The following example demonstrates the usage of OR logical operator (&&) with different boolean values.

main.cpp

#include <iostream>
using namespace std;

int main() {
   cout << (true || true) << endl;
   cout << (true || false) << endl;
   cout << (false || true) << endl;
   cout << (false || false) << endl;
}

Output

1
1
1
0

C++ OR combining Multiple Conditions

The following example demonstrates the usage of OR logical operator (||) in combining boolean conditions and forming a compound condition.

main.cpp

#include <iostream>
using namespace std;

int main() {
   int a = 7;

   if ((a < 10) || (a%2 == 0)) {
      cout << "a is even or less than 10." << endl;
   }
}

Output

a is even or less than 10.

In the above example, a<10 is a condition that checks if a is less than 10 and a%2==0 is another condition that checks if a is even number.

If we would like to check if either of the conditions a<10 or a%2==0 is true, we use C++ OR logical operator.

Conclusion

In this C++ Tutorial, we learned what C++ OR Logical Operator is, and how to use it with conditional expressions.