Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

What are relational operators in C++?

Onyejiaku Theophilus Chidalu

In order to evaluate a comparison between two expressions, a relational operator can be used. The result of a relational operation is a Boolean value that can only be true or false.

Relational operators

In C++, there are six relational operators. They include:

  • Equal to ==
  • Not equal to !=
  • Greater than >
  • Less than <
  • Greater than or equal to >=
  • Less than or equal to <=

They are called relational because they evaluate how two expressions on either side of the operator relate to each other.

For example, the relation 5>105>10 produces the integer value 0, meaning false. The descriptions, as well as examples of the six relational operators, are represented in the table below. Assume variable A holds 1 and variable B holds 2, then:

Relational operators

Operator

Description

Example

==

Helps check if the values of two operand are equal or not, if they are equal then condition becomes true

A == B is not true because A is not equal to B

!=

Helps check if the values of two operands are equal or not, if the values are not equal then condition becomes true

A != B is true because A and B are not equal

>

Helps check if the value of left operand is greater than the value of right operand, if A is greater than B, then condition becomes true

A > B is not true because A is not greater than B

<

Helps check if the value of left operand is less than the value of right operand, if A is less than B, then the condition becomes true

A < B is true because A is less than B

>=

Helps check if the value of left operand is greater than or equal to the value of right operand, if the value of A is either greater or equal to the value of B, then the condition becomes true

A >= B is not true because A is neither greater nor equal to B

<=

Helps check if the value of left operand is less than or equal to the value of right operand, if A is either less or equal to B, then the condition becomes true


A <= B is true because A is at least greater than B

Now, let’s see a very simple example using the relational operator greater than >.

Code

#include <iostream>
using namespace std;

int main() {
  int x,y;
  cout<<"Enter two integers:";
  cin>>x>>y;
  if(x>y)cout<<x<<endl;
  else cout<<y<<endl;
  return 0;
}

Sample output

Enter two integers: 10, 20
20

Explanation

In the program above, the condition given is (x>y). If x is greater than y, the condition is true and evaluates to 1; otherwise, the condition is false and evaluates to 0. So, x is printed precisely when it is greater than y.

Using relational operators

Below, the example includes all relational operators in the example below.

Code

In the code below, a=2a = 2 and b=1b = 1.

#include <iostream>
using namespace std;

int main()
{
int a=2;
int b=1;

if(a==b)
{
cout << "Line 1: a is equal to b"<<endl;
}
else
{
cout << "Line 1: a is not equal to b"<<endl;  
}
if(a<b)
{
cout << "Line 2: a is less than b"<<endl;  
}
else
{
cout << "Line 2: a is not less than b"<<endl;
}
if(a>b)
{
cout << "Line 3: a is greater than b"<<endl;
}
else
{
cout << "Line 3: a is not greater than b"<<endl;
}
/* lets change the values of a and b */
a=5;
b=20;
if(a<=b)
{
cout << "Line 4: a is either less than or equal to b"<<endl;
}
if(b>=a)
{
cout << "Line 5: a is not greater than or equal to b"<<endl;
}
return 0;
}

RELATED TAGS

c++
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring