In C++, Relational Operators are used for comparing two or more numerical values. C++ has different types of Operators which are used for carrying out numerous functions in the program. One such type used is the Relational Operators. We use Relational Operators for the decision-making process. This article contains information about the different types of Relational Operators, their uses, and their program examples.
Definition
- In computer science, Relational Operators are a concept where they are used to define the relation between 2 or more entities in a program.
- Relational Operators are used for the purpose of comparison of two or more numerical values stored in an operand.
Relational Operators
There arises a need where we want a comparison between the operands and their values. In such cases, Arithmetic or Unary operators are of no use. For such a case, special operators called Relational Operators are used.
Relational Operators try to find out if there exists any relation or connection between two or more operands/entities.
Relational Operators are used for finding out the numerical equality or the numerical inequalities between the operands.
SyntaxÂ
operand_1 operator operand_2 ;
where,
operand_1 and operand_2 are either variable_name or a numerical value
operator = symbol used in various relational operators
After comparison, the result returned by the Relational Operator is a Boolean data type value. Boolean Datatype values contain only 2 values, either 0 or 1.
In Programming languages like Java, Pascal, these operators return the value of True or False, depending on the conditionalities between the operands. In C, C++ programming languages, Relational operators return an integer value of 0 or 1, where 0 indicates False and 1 stands for True.
Example
int main()
{
int a = 10 ;
int b = 5 ;
cout << (a > b) << endl;Â Â Â Â Â Â Â Â Â Â // is a greater than b?
cout << (a < b) << endl;Â Â Â Â Â Â Â Â Â Â // is a less than b?
return 0;
}
Output
1
0
Relational Operator symbols might vary from programming language to language. Most of the programming languages use the following operator symbols –
Operator | Context |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
Browse all the Topics Under Operator and Expressions: Operators
- Arithmetic Operators
- Assignment Operator
- C++ Shorthands
- Unary Operator
- Increment and Decrement Operators
- Logical Operators
Types of Relational Operators
There are a total of 6 different types of Relational Operators in computer science programming languages. They are as follows –
-
Greater than (>)
This relational operator is used to find out which operand is greater than the other operand in comparison. The symbol used for comparison is ‘>’.
If the condition is True then it will give 1 as the output and if the condition is False then it will return 0 as the output.
Example
int main()
{
int x = 15 ;
int y = 10 ;
cout << (x > y) << endl;
return 0;
}
Output
1
-
Less than (<)
This relational operator is used to find out which operand is lesser than the other operand in comparison. The symbol used for comparison is ‘<’.
If the condition is True then it will give 1 as the output and if the condition is False then it will return 0 as the output.
Example
int main()
{
int x = 15 ;
int y = 10 ;
cout << (x < y) << endl;
return 0;
}
Output
0
-
Greater than or equal to (>=)Â
Using this relational operator, we find out which operand is greater than or equal to the other operand in comparison. The symbol used for comparison is ‘>=’.
If the condition is True then it will give 1 as the output and if the condition is False then it will return 0 as the output.
Example
int main()
{
int x = 10 ;
int y = 10 ;
cout << (x > y) << endl;
cout << (x >= y) << endl;
return 0;
}
Output
0
1
Less than or equal to (<=)
In this relational operator, we find out which operand is lesser than or equal to the other operand in comparison. The symbol used for comparison is ‘<=’.
If the condition is True then it will give 1 as the output and if the condition is False then it will return 0 as the output.
Example
int main()
{
int x = 10 ;
int y = 10 ;
cout << (x < y) << endl;
cout << (x <= y) << endl;
return 0;
}
Output
0
1
-
Equal to (=)
Equal to Relational Operator is used to compare the operands and check if the values are the same. The symbol used for comparison is ‘==’.
Note – In mathematics we always use = for equating, but in Computer Science we use the Double Equal ‘==’ for checking the equality condition.
Example
int main()
{
int x = 10 ;
int y = 10 ;
int z = 15 ;
cout << (x == y) << endl;
cout << (x == z) << endl;
return 0;
}
Output
1Â Â Â Â Â Â Â Â Â // since x is equal to y, Condition is True
0Â Â Â Â Â Â Â Â Â // since x is not equal to z, Condition is False
-
Not equal to (!=)
Not Equal to Relational Operator is used to compare the operands and check if the values are different. The symbol used for comparison is ‘!=’.
Example
int main()
{
int x = 10 ;
int y = 10 ;
int z = 15 ;
cout << (x != y) << endl;
cout << (x != z) << endl;
return 0;
}
Output
0Â Â Â Â Â Â Â Â Â // since x is equal to y, Condition is False
1Â Â Â Â Â Â Â Â Â // since x is not equal to z, Condition is True
FAQs on Relational Operators
Q1. Operators, which specify the relation between two variables by comparing them are called as?
- Arithmetic Operators
- Relational Operators
- Assignment Operators
- Logical Operators
Answer. Option B
Q2. What value is returned if the comparison of two variables is False?
- 1
- 2
- 4
- 0
Answer. Option D
Q3. What will be the output of the following code?
int a=10,b=20;
if(a>b)
cout<<"a is greater"<<endl;
else
cout<<"b is greater"<<endl;
- a is greater
- 20 > 10
- b is greater
- None of these
Answer. Option C
Q4. What will be the final output of the following code?
int main()
{
int a = 30, b = 25, c = 5;
int d;
d = b + c == a;
cout << d << endl;
}
- 1
- 30
- 25
- Syntax Error
Answer. Option A
Leave a Reply