Overview of Boolean Operators in C++
Boolean operators are used for performing boolean operations, in order to validate the relationship between the operands and it return either 0 or 1. This 0 or 1 output is equivalent to false or true return value respectively. In C++, for performing the boolean operations along with the object oriented concepts programming, three types of boolean operators are used. They are ‘&&’ for AND operation that results in true value when both the input is true, ‘||’ for OR operation that results in true when at least one input is true, and ‘!’ for NOT operation that results in exact opposite value from that of the input value.
Examples of Boolean Operators in C++
The examples of boolean operators in c++ are explained below.
Example # 1 – AND Boolean Operator!
This Boolean operator is represented by “&&” together in C++ programming language and it is also known as an ampersand. This operator has conditions on both sides. So it takes left value from the operators and then the right value from the operator if both values match it returns true otherwise it returns a false value. In a simpler word, we can say that in regular English writing we only use and when we need both the things or both the conditions are necessary. It means if both conditions are true then the only output will be true otherwise for the rest of the conditions it will be false. “If an expression needs to be proved true then both the conditions must be true.”
Here is the C++ code to demonstrate AND Boolean operator by checking age between the given range for medical insurance:
Code:
#include <iostream>
using namespace std;
int main ()
{
int your_age;
cout << " Please enter your age here: " ;
cin >> your_age;
if ( your_age >= 20 && your_age <= 70 )
{
cout << " Congratulations ! You're between 20 and 70 and you can save money on your medical insurance!" << endl;
}
else
{
cout << " We are so sorry, we don't have any deals for you for this month! " << endl;
}
return 0;
}
Output:
Example #2 – OR Boolean Operator!
This Boolean operator is represented by “||” together in C++ programming language and it is also known as logical OR. This operator also has conditions on both sides. But it’s not like AND operator as it is OR operators which means if even a single condition of any side is true then it will return a true value. In a simpler word, we can say that in regular English writing we only use or when we have the choice out of two options that even if other is not fine you will anyhow pick the left one. Only one condition is necessary. It means if any single conditions are true then the only output will be true otherwise for the rest of the conditions it will be false. “If an expression needs to be proved true then only one condition needs to be true.”
Here is the C++ code to demonstrate OR Boolean operator by checking age between the given range:
Code:
#include <iostream>
using namespace std;
int main ()
{
int age;
cout << " Please enter your age here: " ;
cin >> age;
if ( age < 0 || age > 125 )
{
cout << " Ha Ha Ha You're lying - you CANNOT be that age. Impossible " << endl;
}
else
{
cout << " Great! Thanks for providing your age ! " << endl;
}
return 0;
}
Output:
Example #3 – NOT Boolean Operator!
This Boolean operator is represented by “!” in C++ programming language and it is also known as logical NOT operator. This operator has no such conditions on both sides. In fact, it has only one aim to invert the value of the given Boolean expression as only one single expression can be prefixed to it. In a simpler word, we can say that in regular English writing we only use not when we don’t want something or we can say that not in favor like opposition. “If an expression needs to be proved false or true depending upon the expression prefixed to it always use NOT operator.”
Here is the C++ code to demonstrate NOT Boolean operator by checking age between the given range:
Code:
#include <iostream>
using namespace std;
int main ()
{
bool initiate;
cout << " Hey ! Do you really want to initialise the application ? [0: No] [1: Yes] " << endl;
cin >> initiate ; // 0 input is false', and 1 is 'true'
if ( !initiate )
{
cout << " Then why would you open the application ? Haha Funny, too bad, I'm starting now anyway. " << endl;
}
cout << " Application initialized. " << endl;
// you can continue writing the main program code here
return 0;
}
Output:
Conclusion
Above all the three codes are separately demonstrated using three Boolean C + + operators such as AND, OR and NOT. Every single operator has its own specialty which can be used depending upon features and operations you want to implement in software or programs. AND operator is used when you want both of the given conditions to be the same or satisfied to proceed with the code. OR operators are used when you want only one of the given conditions to be true to proceed with the code.
NOT operator is used when you want to give one statement that can be used to handle two statements simultaneously. In Conclusion, Boolean operators in the C + + programming language are very useful in programming as it helps to solve complex operations in bits of time without occupying any memory space. Boolean operators are widely used in database management because it helps in narrowing and broadening the search based on a given query.
Recommended Articles
This is a guide to Boolean Operators in C++. Here we discuss the overview and examples of boolean operators in c++ along with the code implementation. You may also look at the following articles to learn more-