Operator and Expressions: Operators

Relational Operators

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.

relational operators

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

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

Types of Relational Operators

There are a total of 6 different types of Relational Operators in computer science programming languages. They are as follows –

  1. 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

  1. 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

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

  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

  1. 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?

  1. Arithmetic Operators
  2. Relational Operators
  3. Assignment Operators
  4. Logical Operators

Answer. Option B

Q2. What value is returned if the comparison of two variables is False?

  1. 1
  2. 2
  3. 4
  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;

  1. a is greater
  2. 20 > 10
  3. b is greater
  4. 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. 1
  2. 30
  3. 25
  4. Syntax Error

Answer. Option A

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.