# Logical Operators in C++

###### Operators in C++

In this lesson, we will learn what is the Logical Operators and how it works in C++ programming with some examples.

## What is Logical Operators

Logical operators in C++ are used to evaluate two or more conditions. They allow a program to make a decision based on multiple conditions. If the result of the logical operator is true then 1 is returned otherwise 0 is returned.

There are 3 types of logical operators in C++ and they are:

**&&**AND**||**OR**!**NOT

Now let's see the examples of all the logical operators one by one for more understanding.

### && AND Operator

**&&** (AND) operator is used to check if two or more given
conditions are true or not. The output of **&&** (AND) operator is 1 (true) only if
all the given conditions are correct otherwise the output will be 0 (false) if any one
of the given conditions is wrong.

#### Example

```
int x=15, y=2, z=6, k=25, a, b;
a=x>5 && y<6 && k>=25; // output of a is 1
b=x>y && k<25; // output of b is 0
```

In the example above the output of **a** is **1** because all
the three given conditions x>5, y<6 and k>=25 are correct (true). On the other hand the
output of **b** is **0** because out of the two given conditions x>y and k<25 the
second condition k<25 is wrong (false).

### || OR operator

**||** (OR) operator is used to check if any one of the given
conditions is true or not. The output of **||** (OR) operator is 1 (true) if at least
any one of the given conditions is correct otherwise the output will be 0 (false) only
if all the given conditions are wrong.

#### Example

```
int x=15, y=2, z=6, k=25, a, b;
a=x<5 || y<6 || k>=25; // output of a is 1
b=x<y || k<25; // output of b is 0
```

In the example above the output of **a** is **1** because out of
the three given conditions x<5, y<6 and k>=25 the second condition is correct (true). On the
other hand the output of **b** is **0** because out of the two given conditions x<y
and k<25 both of them are wrong (false).

### ! NOT Operator

**!** (NOT) operator negates the value of the condition. If the
condition is false then the output of **!** (NOT) operator becomes true (1). If the
condition is true then the output of **!** (NOT) operator becomes false (0).

#### Example

```
int x=15, y=10, a, b;
a=!(x>5); // output of a is 0
b=!(y<5); // output of b is 1
```

In the example above the output of **a** is **0** because the
given condition x>5 is correct (true) so **!** (NOT) operator changed the output from
1 to 0. On the other hand the output of **b** is **1** because the given condition
y<5 is wrong (false) so **!** (NOT) operator changed the output from 0 to 1.