In this blog, we will see the definition of relational operators, types of relational operators, and a detailed explanation of each; if you hadn’t checked the previous blog on operators so, check now and add some precious knowledge to the library of your smart brain. Let’s go
What is the relational operator in c++?
A relational operator checks the relationship between two operands—for example, less than, greater than, equal to, etc.
Types of relational operators
It is subdivided into six parts.
- Equal to operator ‘==’
- Not equal to operator ‘!=’
- Greater than operator ‘>’
- Less than operator ‘<‘
- Greater than or equal to operator ‘>=’
- Less than or equal to operator ‘<=’
The following table briefly introduces the types of relational operators.
Operator | Meaning | Example |
---|---|---|
== | Is Equal to | 5 == 10 gives us false |
!= | Is not equal to | 5 != 10 gives us true |
> | is greater than | 5 > 10 gives us false |
< | is less than | 5 < 10 gives us true |
<= | is greater than or equal to | 5 >= 10 give us false |
>= | is less than or equal to | 5 <= 10 gives us true |
Let’s see them one by one.
1. Equal to operator
The equal to operator(==) checks whether the two given operands are equal or not. It returns
- true – if both the operands are equal or the same
- false – if the operands are unequal
example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = 10;
cout << (a == b) << endl;
cout << (b == c) << endl;
return 0;
}
output
0
1
In the above output, as you know, 0 is false, and 1 is true in terms of programming.
2. Not equal to operator
The not equal to operator(!=) checks whether the two given operands are equal or not. It returns
- true – if both the operands are unequal
- false – if the operands are equal
example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = 10;
cout << (a != b) << endl;
cout << (b != c) << endl;
return 0;
}
output
1
0
In the above output, as you know, 0 is false, and 1 is true in terms of programming.
3. Greater than operator
The greater than operator (>) checks whether the left operand is greater than the right operand or not. It returns
- true – if the left operand is greater than the right
- false – if the left operand is less than the right
example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = 7;
cout << (a > b) << endl;
cout << (b > c) << endl;
return 0;
}
output
0
1
In the above output, as you know, 0 is false, and 1 is true in terms of programming.
4. Less than operator
The greater than operator (<) checks whether the left operand is less than the right operand or not. It returns
- true – if the left operand is less than the right
- false – if the left operand is greater than the right
example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = 20;
cout << (a < b) << endl;
cout << (b < c) << endl;
return 0;
}
output
1
0
5. Greater than or equal to operator
The greater than or equal to operator ( >= ) checks whether the left operand is greater or equal to the right operand or not. It returns
- true – if the left operand is either greater than or equal to the right
- false – if the left operand is less than the right
example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = 20;
int d = 20;
cout << (a >= b) << endl;
cout << (b >= c) << endl;
cout << (c >= b) << endl;
cout << (c >= d) << endl;
return 0;
}
output
0
0
1
1
6. Less than or equal to operator
The less than or equal to operator ( <= ) checks whether the left operand is less or equal to the right operand or not. It returns
- true – if the left operand is either less than or equal to the right
- false – if the left operand is greater than the right
example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = 20;
int d = 20;
cout << (a <= b) << endl;
cout << (b <= c) << endl;
cout << (c <= b) << endl;
cout << (c <= d) << endl;
return 0;
}
output
1
1
0
1
Pheww, so that was all about the relational operators. If you have difficulty understanding this, you can leave a comment below, and don’t forget to subscribe to us for more❣️.
FOLLOW-UP
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 🙂