Programming is truly the field of testing your skills and teasing your brain. The deeper you’ll go into programming, the more you realize that programming’s all about applying logic, developing interesting algorithms, teasing your mind by encountering new challenges and situations, and gaining next-level confidence by overcoming them and getting a robust solution.

Logic is an essential part of programming. It starts with thinking which emerges with an idea, and then the programmer expresses and elaborates the idea in the LOC (Lines of code) to implement the idea into reality.

But here we are talking about the logical operators, which are the base of any logical operation within our programming. They are easy to learn and understand, and when you get used to them, your programming will get far better as your core concepts got a new companion on their side.

There are **three types** of logical operators in C++ programming.

- && (AND)
- || (OR)
- ! (NOT)

## Logical Operators: Quick Overview

- In Programming, we use logical operators to analyze a situation and get a boolean value (true/false) out of it as a result/output.
- It depends on the operator we use to obtain the result, as all three operators work differently.
- We use them to set logic between variables to build the correct flow of the program. It allows us to control the program’s execution how we want; thus, it provides control of the whole program.
- In programming, we refer to the value “0” as false and the value “1” as true.

## AND Operator

We use && “and” operator when we want to execute the flow of code while **all the situations are true**. It will ** only return the value as “true”** if

**encountered by the operator**

__all the situations__**. If any**

__are true__**, the**

__one of the situations goes false__**.**

__final result will be false__#### Syntax

`&&`

We also have the concept of Truth Table to make the concept more understandable and clear.

### Truth Table of && Operator

Cases | a | b | a&&b |
---|---|---|---|

Case 1 | 0 | 0 | 0 |

Case 2 | 0 | 1 | 0 |

Case 3 | 1 | 0 | 0 |

Case 4 | 1 | 1 | 1 |

As we know, in programming, the boolean value **“0” means false**, and **“1” signifies the value as true**.

Here in the truth table, we assume that there are two variables: “**a”** and “**b**,” and we have the result of the operator. Both variables bear the boolean value, and the operator evaluates the result based on its functionality.

#### Case-wise Working

- In the
**First Case**, as both**a**and**b**are**false**, the result of**a&&b**will be false. - In the
**Second Case**, the value of**a**is**false**, but the value of**b**is**true**; as we know in the “**and**” operator, we need**both operators to be true to obtain the result as true**; therefore, the**result of a&&b will be false**. - In the
**Third Case**, the value of**a**is**true**, but the value of**b**is**false**; again, as we know in the “**and**” operator, we need**both operators to be true to obtain the result as true**; therefore, the**result of a&&b will be false**. - Finally, in the
**Fourth****case**, the value of**a**is**true**. Therefore, the**result of a&&b will be true**.

#### Example

```
#include <iostream>
using namespace std;
int main()
{
int g = 8;
int p = 12;
cout << ((g == 0) && (g > p)) << endl;
cout << ((g == 0) && (g < p)) << endl;
cout << ((g == 8) && (g > p)) << endl;
cout << ((g == 8) && (g < p)) << endl;
return 0;
}
```

#### Output

```
0
0
0
1
```

## OR Operator

We use the || “or” operator when we want to execute the flow of code while **any of the situations are true**. It will ** return the value as “true”** if

**encountered by the operator**

__any of the situations__**. If**

__is true__**are**

__all situations__**, the**

__false__**.**

__final result will be false__#### Syntax

`||`

We also have the concept of Truth Table to make the concept more understandable and clear.

### Truth Table of || Operator

Cases | a | b | a | | b |
---|---|---|---|

Case 1 | 0 | 0 | 0 |

Case 2 | 0 | 1 | 1 |

Case 3 | 1 | 0 | 1 |

Case 4 | 1 | 1 | 1 |

As we know, in programming, the boolean value **“0” means false**, and **“1” signifies the value as true**.

Here in the truth table, we assume that there are two variables: “**a”** and “**b**,” and we have the result of the operator. Both variables bear the boolean value, and the operator evaluates the result based on its functionality.

### Case-wise Working

- In the
**First Case**, as both**a**and**b**are**false**, the result of**a||b**will be false. - In the
**Second Case**, the value of**a**is**false**, but the value of**b**is**true**; as we know in the “**or**” operator, we need**any operator to be**; therefore, the__true__to obtain the result as__true__**result of a||b will be**.__true__ - In the
**Third Case**, the value of**a**is**true**, but the value of**b**is**false**; again, as we know in the “**or**” operator, we need**any operator to be**__true__**to obtain the result as**; therefore, the__true__**result of a||b will be**.__true__ - Finally, in the
**Fourth****case**, the value of**a**is**true**. Therefore, the**result of a||b will be true**.

#### Example

```
#include <iostream>
using namespace std;
int main(){
int g = 8;
int p = 12;
cout<< ((g == 0) || (g > p))<<endl;
cout<< ((g == 0) || (g < p))<<endl;
cout<< ((g == 8) || (g > p))<<endl;
cout<< ((g == 8) || (g < p))<<endl;
return 0;
}
```

#### Output

```
0
1
1
1
```

## Not Operator

Not operator is quite different from the other two.

We use the ! “not” operator when we want to reverse the result.

We usually use, ! (not) operator to obtain the true value when the condition is not satisfied and the false value when the condition is satisfied successfully.

Syntax

`!`

We also have the concept of Truth Table to make the concept more understandable and clear.

### Truth Table of ! Operator

Cases | a | !a |
---|---|---|

Case 1 | 1 | 0 |

Case 2 | 0 | 1 |

### Case-wise Working

- In the
**First Case**, as both**a**is**true**, therefore result of**!a**will be false. - In the
**Second Case**, the value of**a**is**false**; therefore the value of**!a**will be**true.**

#### Example

```
#include <iostream>
using namespace std;
int main()
{
int g = 8;
int p = 12;
// Reversing Basic Value
bool a = true;
cout << !a << endl;
// Reversing AND Outputs
cout << !((g == 0) && (g > p)) << endl;
cout << !((g == 0) && (g < p)) << endl;
cout << !((g == 8) && (g > p)) << endl;
cout << !((g == 8) && (g < p)) << endl;
// Reversing OR Outputs
cout << !((g == 0) || (g > p)) << endl;
cout << !((g == 0) || (g < p)) << endl;
cout << !((g == 8) || (g > p)) << endl;
cout << !((g == 8) || (g < p)) << endl;
return 0;
}
```

#### Output

```
0 //Basic Value
1
1
1
0
1
0
0
0
```

## Conclusion

We’ve studied the basics of the logical operators in C++. It’s an achievement for the students and students shall appreciate themselves to be able to study and comprehend till here, also special thanks to the students who were able to keep up till here. But don’t stay down! It’s just the beginning; we have a long way to go and crack something great that will brew fantastic coding skills. Until then, keep learning. Stay Smart. Here’s an extra step to help you learn more effectively.

You must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂