C++ Logical Operators

Logical Operators are used to perform boolean operations like AND, OR, and NOT.

In this tutorial, we will learn about the different Logical Operators available in C++ programming language and go through each of these Logical Operators with examples.

C+ Logical Operators

The following table specifies symbol, example, and description for each of the Logical Operator in C++.

OperatorSymbolLogicalOperationExampleDescription
&&ANDa && bReturns boolean AND of a and b.
||ORa && bReturns boolean OR of a and b.
!NOT!aReturns Negation or boolean NOT of a.

C++ AND

The following is the truth table for AND operation.

Operand 1Operand 2Returns
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

main.cpp

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

More about C++ AND.

C++ OR

The following is truth table for OR operation.

Operand 1Operand 2Returns
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

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.

More about C++ OR.

C++ NOT

The following is truth table for NOT operation.

OperandReturns
truefalse
falsetrue

main.cpp

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

More about C++ NOT.

Conclusion

In this C++ Tutorial, we have learned what Logical operators are, which logical operators are available in C++, and how to use them, with the help of examples programs.