In C++, the logical AND operator && combines conditions and produces true only when every required condition is true. In this tutorial, you will learn how && works with boolean values, how to use it in if statements, how short-circuit evaluation works, and how && differs from the bitwise AND operator &.
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
The expression evaluates to true only when both operands evaluate to true. If either operand is false, the result is false.
C++ && Operator 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.
C++ && Operator with Boolean Values
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
By default, std::cout prints a boolean value as 1 for true and 0 for false. Therefore, only true && true produces 1.
C++ AND Operator in an if Statement
The && operator is commonly used in an if statement when more than one condition must be satisfied before a block of code should run.
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 checks whether a is less than 100, while a%2==0 checks whether a is even.
The body of the if statement runs only when both conditions are true. For a = 10, the number is less than 100 and is divisible by 2, so the compound condition evaluates to true.
Combining Three or More Conditions with && in C++
You can chain multiple && operators when several requirements must all be satisfied. The complete expression becomes true only if every condition is true.
condition1 && condition2 && condition3
For example, the following program checks that an age is at least 18, no more than 60, and that permission has been granted.
#include <iostream>
using namespace std;
int main() {
int age = 25;
bool hasPermission = true;
if (age >= 18 && age <= 60 && hasPermission) {
cout << "All conditions are satisfied.";
}
return 0;
}
All conditions are satisfied.
If any one of these three conditions becomes false, the message is not printed.
Short-Circuit Evaluation of && in C++
The logical AND operator uses short-circuit evaluation. C++ evaluates the left operand first. If that operand is false, the entire && expression must already be false, so the right operand is not evaluated.
This behavior is useful when the second condition is safe or meaningful only after the first condition succeeds. A common example is checking a pointer before dereferencing it.
#include <iostream>
using namespace std;
int main() {
int value = 20;
int* ptr = &value;
if (ptr != nullptr && *ptr > 10) {
cout << "Pointer is valid and the value is greater than 10.";
}
return 0;
}
Here, *ptr > 10 is evaluated only if ptr != nullptr is true. If ptr were null, evaluation would stop after the first condition.
Difference Between && and & in C++ Conditions
&& and & are different C++ operators. The && operator is the logical AND operator discussed in this tutorial. The single & operator is primarily the bitwise AND operator when used with integral values.
| Operator | Purpose | Short-circuits? |
|---|---|---|
&& | Logical AND of conditions | Yes |
& | Bitwise AND of integral operands; can also be applied to boolean operands | No |
When & is applied to boolean expressions, both sides are evaluated. With &&, evaluation of the right side is skipped when the left side is false. Therefore, && is normally the appropriate operator when combining conditions in an if statement.
C++ && Versus || Logical Operators
The logical AND operator && requires both operands to be true. The logical OR operator || requires only one operand to be true.
| Expression | Meaning |
|---|---|
a && b | True only when both a and b are true |
a || b | True when at least one of a or b is true |
For example, use && when a value must satisfy both a lower and an upper limit. Use || when either of two alternatives is acceptable.
The Alternative and Operator Token in C++
C++ also provides the keyword and as an alternative token for &&. The two forms have the same logical meaning and operator precedence.
condition1 and condition2
For example, a > 0 and a < 10 is equivalent to a > 0 && a < 10. The symbolic && form is more commonly seen in C++ code, but both are valid language tokens.
Operator Precedence When Using && in C++
Comparison operators such as <, >, and == have higher precedence than logical AND. Therefore, an expression such as the following is interpreted as two comparisons combined with &&:
age >= 18 && age <= 60
Parentheses are not required around each comparison in this case, although they may be added when they make a complex expression easier to read.
Common Mistakes with the C++ && Operator
- Using
&instead of&&: these operators have different semantics, particularly because&&short-circuits. - Expecting AND to succeed when only one condition is true: every operand joined with
&&must evaluate to true. - Writing a chained mathematical comparison: write
0 < x && x < 10, rather than trying to express the range as0 < x < 10. - Depending on the right operand to always run: it may be skipped because of short-circuit evaluation.
- Confusing logical AND with rvalue-reference syntax:
&&also appears in C++ declarations for rvalue references, but that is a different language feature determined by context.
Key Rules for the C++ Logical AND Operator
- Use
&&when all combined conditions must be true. true && trueevaluates to true; every other two-operand combination evaluates to false.- C++ evaluates
&&from left to right and short-circuits when a false operand determines the result. - Use
&&, not&, for ordinary logical condition checks when short-circuit behavior is intended. - The keyword
andis a valid alternative token for&&.
C++ AND Operator Tutorial Summary
In this C++ Tutorial, we learned what C++ AND Logical Operator is, and how to use it with conditional expressions.
TutorialKart.com