C++ logical operators combine or reverse conditions that evaluate as true or false. The three logical operators are logical AND (&&), logical OR (||), and logical NOT (!). This tutorial explains their truth tables, syntax, short-circuit behavior, precedence, and use in C++ programs.

C++ Logical Operators

Logical operators are commonly used in if, while, and other conditional expressions. The operands are interpreted as boolean conditions, and the result of a logical operation is a value of type bool: either true or false.

C++ Logical Operator Tutorials

C++ Logical Operators List and Syntax

The following table lists the logical operators available in C++, with their symbols, basic syntax, and meaning.

Operator
Symbol
Logical
Operation
ExampleDescription
&&ANDa && bReturns true only when both operands are true.
||ORa || bReturns true when at least one operand is true.
!NOT!aReverses the boolean value of the operand.

For example, if a and b are boolean expressions, the basic forms are:

</>
Copy
a && b
a || b
!a

1. C++ Logical AND (&&)

The logical AND operator returns true only if both operands are true. If either operand is false, the result is false.

The following is the truth table for AND operation.

Operand 1Operand 2Returns
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

In the following program, both conditions must be true: a must be less than 100 and it must be even.

main.cpp

</>
Copy
#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.

Here, a < 100 is true and a % 2 == 0 is also true, so the complete && expression evaluates to true and the statement inside the if block runs.

2. C++ Logical OR (||)

The logical OR operator returns true when at least one operand is true. It returns false only when both operands are false.

The following is truth table for OR operation.

Operand 1Operand 2Returns
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

In this program, the if block runs if a is less than 10, or if a is even, or if both conditions are true.

main.cpp

</>
Copy
#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.

For a = 7, the expression a < 10 is true while a % 2 == 0 is false. Because one operand is true, the || expression evaluates to true.

3. C++ Logical NOT (!)

The logical NOT operator is a unary operator. It reverses a condition: !true becomes false, and !false becomes true.

The following is truth table for NOT operation.

OperandReturns
truefalse
falsetrue

The following condition checks whether a is not even. The inner expression is false for a = 7, and ! changes that false value to true.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int a = 7;

   if (!(a%2 == 0)) {
      cout << "a is not even." << endl;
   }
}

Output

a is not even.

Short-Circuit Evaluation with && and || in C++

C++ evaluates && and || from left to right and uses short-circuit evaluation. For &&, if the left operand is false, the right operand is not evaluated because the whole expression must already be false. For ||, if the left operand is true, the right operand is not evaluated because the whole expression must already be true.

This behavior is useful when the right-hand condition should only be evaluated after a safety check succeeds. For example:

</>
Copy
int* ptr = nullptr;

if (ptr != nullptr && *ptr > 0) {
    cout << "Positive value";
}

Because ptr != nullptr is false, *ptr > 0 is not evaluated. The program therefore does not dereference the null pointer in this condition.

Precedence of !, &&, and || in C++ Expressions

Among these three logical operators, logical NOT has the highest precedence, followed by logical AND, then logical OR. Therefore, an expression such as !a || b && c is grouped as (!a) || (b && c).

OperatorRelative precedence
!Highest of the three
&&Middle
||Lowest of the three

Parentheses are still useful when a condition contains several comparisons because they make the intended grouping easier to read.

Logical Operators vs Comparison, Conditional, and Bitwise Operators

Logical operators are sometimes confused with other C++ operators. The following distinctions are useful when reading conditions.

OperatorCategoryPurpose
&&, ||, !LogicalCombine or negate boolean conditions.
==, !=, <, >, <=, >=ComparisonCompare values and produce a boolean result.
?:ConditionalSelect one of two expressions based on a condition.
&, |, ^, ~BitwiseOperate on individual bits of integral values.
++IncrementIncrease a numeric value by one.

For example, == is not a logical operator. It is an equality comparison operator. A comparison such as a == b produces a boolean result, which can then be combined with another condition using && or ||.

Using Non-Boolean Values with C++ Logical Operators

Operands of logical operators are contextually converted to bool. For integral values, zero is treated as false and nonzero values are treated as true. Even though this is valid C++, explicit comparisons are often clearer when the code is meant to express a numeric condition.

</>
Copy
int x = 5;
int y = 0;

bool result = x && y;

Here, x is treated as true and y is treated as false, so result becomes false.

Alternative C++ Keywords for Logical Operators

C++ also provides alternative operator keywords: and for &&, or for ||, and not for !. They have the same meaning as the symbolic operators, although the symbolic forms are more common in C++ code.

</>
Copy
a and b
a or b
not a

C++ Logical Operators Summary

In this C++ Tutorial, we have learned how &&, ||, and ! work, how their truth tables determine results, how && and || short-circuit, and how logical operators differ from comparison, conditional, and bitwise operators.