In this C++ tutorial, you will learn about OR Logical operator, and how to use OR logical operator with boolean values, and how to combine simple conditions and form compound conditions, with example programs.
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 1 | Operand 2 | Returns |
true | true | 1 |
true | false | 1 |
false | true | 1 |
false | false | 0 |
C++ OR returns true even if one of the operand is true.
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
OR Operator to form compound condition
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.