C++ Reference ยท Operators
C++ logical operators
Reference for the C++ logical operators &&, || and !: short-circuit evaluation, truthiness of values, and, or, not alternatives, and overloading pitfalls.
The logical operators combine or invert boolean conditions. a && b is true when both operands are true, a || b is true when at least one is, and !a is true when a is false. Operands are converted to bool first, so numbers (zero is false), pointers (null is false) and objects with an explicit operator bool all work.
The binary operators short-circuit: the right operand is evaluated only if the left one does not already decide the result. That is what makes checks such as p != nullptr && p->ready() safe.
if (index < size && data[index] == target) { /* safe */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
a && b // logical AND (alternative token: and) a || b // logical OR (alternative token: or) !a // logical NOT (alternative token: not) bool operator&&(const T&, const T&); // overloading is possible but discouraged
| Name | Description |
|---|---|
a && b |
True if both operands are true. b is evaluated only if a is true. |
a || b |
True if at least one operand is true. b is evaluated only if a is false. |
!a |
True if a is false. |
Returns: a bool prvalue (type bool, value true or false).
Errors / undefined behavior: none for the operators themselves. A dereference placed in the skipped operand never runs, but the same dereference in a non-short-circuit position (for instance after &) does, and may be undefined behavior.
Usage notes
- Sequencing: the left operand is fully evaluated, with all side effects, before the right operand starts. This is one of the few ordering guarantees C++ gives within an expression.
- Precedence:
!binds tightest, then&&, then||. Parenthesize mixed&&/||chains: compilers warn about the unparenthesized form. - Do not overload
&&or||: an overloaded operator is an ordinary function call, so both operands are always evaluated and the short-circuit guarantee is lost. Provideexplicit operator boolinstead. explicit operator bool: contextual conversion (inif,while,!,&&,||) works with explicit conversion operators, which is why smart pointers and streams can be tested directly.&versus&&:&is bitwise and evaluates both sides. Writingflag & otherfor booleans compiles but is usually a typo.- Comparisons chain badly:
a < b < cdoes not test an interval; writea < b && b < c. !on non-bool objects:!ptrand!streamwork through the same contextual conversion.- Constant conditions: the compiler removes dead branches, so
if (DEBUG && check())costs nothing whenDEBUGisfalse.