# Logical Operators in C/C++

C/C++ programming Logical Operators: In this tutorial, we are going to learn about the various logical operators with their usages, syntaxes and examples.
Submitted by IncludeHelp, on June 03, 2020

## Logical Operators

Logical operators are used to check the combinations of the two conditional expressions.

The following are the types of logical operators.

1. Logical AND (&&) Operator
2. Logical OR (||) Operator
3. Logical NOT (!) Operator

### 1) Logical AND (&&) Operator

Logical AND operator represented by the symbols "&&", it works with two operands and returns 1 if both operands are true (non-zero); 0, otherwise.

Note: Operands can be values, conditions, expressions, etc.

Syntax:

```operand1 && operand2
```

Truth table:

operand1 operand2 operand1 && operand2
Non-zero Non-zero 1
Non-zero 0 0
0 Non-zero 0
0 0 0

C++ program to demonstrate the example of logical AND (&&) operator

```#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 20;

// printing the values
cout << "a : " << a << endl;
cout << "b : " << b << endl;
cout << endl;

// Logical AND operations
cout << "(a && b) : " << (a && b) << endl;
cout << "(a && 0) : " << (a && 0) << endl;
cout << "(0 && b) : " << (0 && b) << endl;
cout << "(0 && 0) : " << (0 && 0) << endl;
cout << endl;

cout << "(a >= 10 && b <= 30) : " << (a >= 10 && b <= 30) << endl;
cout << "(a == 10 && b == 20) : " << (a == 10 && b == 20) << endl;
cout << "(a >= 10 && b == 30) : " << (a >= 10 && b == 30) << endl;
cout << "(a < 10 && b < 20)   : " << (a < 10 && b < 20) << endl;

return 0;
}
```

Output:

```a : 10
b : 20

(a && b) : 1
(a && 0) : 0
(0 && b) : 0
(0 && 0) : 0

(a >= 10 && b <= 30) : 1
(a == 10 && b == 20) : 1
(a >= 10 && b == 30) : 0
(a < 10 && b < 20)   : 0
```

### 2) Logical OR (||) Operator

Logical OR operator represented with the symbols "||", it works with two operands and returns 1 if one (or both) operands are true (non-zero); 0, otherwise.

Syntax:

```operand1 || operand2
```

Truth table:

operand1 operand2 operand1 && operand2
Non-zero Non-zero 1
Non-zero 0 1
0 Non-zero 1
0 0 0

C++ program to demonstrate the example of logical OR (||) operator

```#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 20;

// printing the values
cout << "a : " << a << endl;
cout << "b : " << b << endl;
cout << endl;

// Logical OR operations
cout << "(a || b) : " << (a || b) << endl;
cout << "(a || 0) : " << (a || 0) << endl;
cout << "(0 || b) : " << (0 || b) << endl;
cout << "(0 || 0) : " << (0 || 0) << endl;
cout << endl;

cout << "(a >= 10 || b <= 30) : " << (a >= 10 || b <= 30) << endl;
cout << "(a == 10 || b == 20) : " << (a == 10 || b == 20) << endl;
cout << "(a >= 10 || b == 30) : " << (a >= 10 || b == 30) << endl;
cout << "(a < 10 || b < 20)   : " << (a < 10 || b < 20) << endl;

return 0;
}
```

Output:

```a : 10
b : 20

(a || b) : 1
(a || 0) : 1
(0 || b) : 1
(0 || 0) : 0

(a >= 10 || b <= 30) : 1
(a == 10 || b == 20) : 1
(a >= 10 || b == 30) : 1
(a < 10 || b < 20)   : 0
```

### 3) Logical NOT (!) Operator

Logical NOT operator represented by the symbols "!", it works with one operand and returns 1 if the operand is zero;0, otherwise.

Syntax:

```!operand
```

Truth table:

operand !operand
Non-zero 0
0 1

C++ program to demonstrate the example of logical NOT (!) operator

```#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 0;

// printing the values
cout << "a : " << a << endl;
cout << "b : " << b << endl;
cout << endl;

cout << "!a : " << !a << endl;
cout << "!b : " << !b << endl;
cout << endl;

// Logical NOT operations
cout << "!(a || b) : " << !(a || b) << endl;
cout << "!(a || 0) : " << !(a || 0) << endl;
cout << "!(0 || b) : " << !(0 || b) << endl;
cout << "!(0 || 0) : " << !(0 || 0) << endl;
cout << endl;

cout << "!(a >= 10 || b <= 30) : " << !(a >= 10 || b <= 30) << endl;
cout << "!(a == 10 || b == 20) : " << !(a == 10 || b == 20) << endl;
cout << "!(a >= 10 || b == 30) : " << !(a >= 10 || b == 30) << endl;
cout << "!(a < 10 || b < 20)   : " << !(a < 10 || b < 20) << endl;

return 0;
}
```

Output:

```a : 10
b : 0

!a : 0
!b : 1

!(a || b) : 0
!(a || 0) : 0
!(0 || b) : 1
!(0 || 0) : 1

!(a >= 10 || b <= 30) : 0
!(a == 10 || b == 20) : 0
!(a >= 10 || b == 30) : 0
!(a < 10 || b < 20)   : 0
```