Boolean Operators in C++

Boolean Operators in C++
Techiio-author
Written by Sagar RabidasFebruary 7, 2022
9 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Today start the discussion on Boolean Operators in C++

Boolean Operators in C++

Boolean operators are used for appearing boolean operations, to validate the connection among the operands and go back to both 0 or 1. This 0 or 1 output is equivalent to false or authentic return cost respectively. In C++, for acting the boolean operations at the side of the item-orientated principles programming, 3 sorts of boolean operators are used. They are ‘&&’ for and operation that effects in actual cost whilst both the enter is authentic results in authentic when at least one input is actual, and ‘!’ for now, not an operation that consequences in precise opposite cost from that of the input price.

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 way of “&&” collectively in the c++ programming language and it is also known as an ampersand. This operator has conditions in both aspects. So it takes the left fee from the operators after which the proper price from the operator if both values fit it returns real otherwise it returns a false cost. In less difficult phrases, we will say that in regular English writing we best use and whilst we want each thing or each the conditions are essential. It means if each condition is authentic then the simplest output will be actual otherwise for the rest of the conditions it will be false. “if an expression desires to be proved authentic then both the situations must be authentic.”

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;
}

Example #2 – OR Boolean Operator!

This boolean operator is represented by using” collectively within the c++ programming language and it's also referred to as logical or. This operator additionally has situations in both aspects. But it’s now not as an operator as it's far or operators because of this if even an unmarried circumstance of any aspect is proper then it's going to return a true fee. In easier words, we will say that in regular English writing we only use or while we've got the choice out of two alternatives that even though different isn't always exceptional you will, in any case, select the left one. The simplest situation is important. In the manner, if any unmarried situations are real then the only output might be authentic in any other case for the relaxation of the conditions it'll be fake. “if an expression desires to be proved true then most effective one situation needs to be genuine.”

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;
}

Example #3 – NOT Boolean Operator!

This boolean operator is represented using “!” in the c++ programming language and it is also known as logical no longer operating. This operator has no such conditions on both sides. It has the simplest one aim to invert the cost of the given boolean expression as the simplest one unmarried expression can be prefixed to it. In less complicated phrases, we can say that during everyday English writing we simplest use now not while we don’t want something or we can say that no longer in choose like opposition. “if an expression wishes to be proved false or authentic relying upon the expression prefixed to it constantly use no longer operational.”

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;
}

C
C ++
Boolean Operators
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance