You might have come around two types of people in your life till now. One always talks about equality and the other who thinks that not everyone is equal in whatever manner. Since C++ is also not so much different from our real-life scenarios, the two operators like the thinking exist. These come under the Equality Operator. So, let us go into detail about the Equality Operators.
Equality Operator
The equality operator strictly checks the equality or inequality among two conditions or operands. The symbols we use to check the equality are” == ” and inequality is ” != “.
The Equality operators are of the type bool, which means that if the condition specifies that the operand on the left side should be equal to the operand on the right side ( a == b ), and the condition is satisfied, then the result will be true ( 1 ); else false ( 0 ).
In case the condition specifies that the operand on the left side should not be equal to the operand on the right side ( a != b ), and the condition is satisfied, then the result will be true ( 1 ) else false ( 0 ).
Example of equal operator
// Using Equal to Operator
// Condition is true
#include <iostream>
using namespace std;
int main()
{
int a = 3, b = 3;
if (a == b)
{
cout << "a is Equal to b ( Condition True ) ";
}
else
{
cout << "a is Not Equal to b ( Condition False ) ";
}
return 0;
}
Output
a is Equal to b ( Condition True )
Example
// Using Equal to Operator
// Condition is false
#include <iostream>
using namespace std;
int main()
{
int a = 3, b = 4;
if (a == b)
{
cout << "a is Equal to b ( Condition True ) ";
}
else
{
cout << "a is Not Equal to b ( Condition False ) ";
}
return 0;
}
Output
a is Not Equal to b ( Condition False )
Example OF UNEQUAL OPERATOR
// Using Not Equal to operator
// Condition is true
#include <iostream>
using namespace std;
int main()
{
int a = 3, b = 4;
if (a != b)
{
cout << "a is Not Equal to b ( Condition True ) ";
}
else
{
cout << "a is Equal to b ( Condition False ) ";
}
return 0;
}
OUTPUT
a is Not Equal to b ( Condition True )
Example
// Using Not Equal to operator
// condition is false
#include <iostream>
using namespace std;
int main()
{
int a = 3, b = 3;
if (a != b)
{
cout << "a is Not Equal to b ( Condition True ) ";
}
else
{
cout << "a is Equal to b ( Condition False ) ";
}
return 0;
}
OUTPUT
a is Equal to b ( Condition False )
CONCLUSION
In this blog, we’ve learned about another category of Operators known as Equality Operators and have taken another step in our journey of learning C++ programming. We got to know precisely what these operators are and how they make checking the equality factor among two operands super easy—hoping to learn many more related topics shortly.
As you all know, 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 🙂