In this C++ tutorial, you will learn about AND Logical operator, and how to use AND logical operator with boolean values, and how to combine simple conditions and form compound conditions, with example programs.
C++ AND Logical Operator
C++ AND Logical Operator is used to combine two or more logical conditions to form a compound condition. &&
is the symbol used for C++ AND Operator.
C++ AND 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++ AND logical operator.
Operand 1 | Operand 2 | Returns |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
C++ AND returns true only if both the operands are true.
Example
Following example demonstrates the usage of AND logical operator (&&) with different boolean values.
C++ Program
#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
0
0
0
AND Operator to form compound conditions
Following example demonstrates the usage of AND logical operator (&&) in combining boolean conditions and forming a compound condition.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 10;
if ((a < 100) && (a%2 == 0)) {
cout << "a is even and less than 100." << endl;
}
}
Output
a is even and less than 100.
In the above example, a<100
is a condition that checks if a
is less than 100
and a%2==0
is another condition that checks if a
is even number.
If we would like to check if the condition both the conditions a<100
and a%2==0
is true, we use C++ AND logical operator.
Conclusion
In this C++ Tutorial, we learned what C++ AND Logical Operator is, and how to use it with conditional expressions.