Relational Operators in C++

In this tutorial, we are going to discuss another type of operator in C++, which is the relational operator. These are also called comparison operators. As the name says, we can understand that here, we are checking the relationship between two operands.

Let’s mention some of the relational operators, and then we will try to use them in the programs, so that we can get the concept, and then you can use it as and when required in the programs.

For the sake of understanding, let’s consider a variable ‘a’ with a value of 6.

Relational Operators in C++

OperatorDescriptionExample
==This operator checks whether or not the value in some variable equals another variable or another value.a ==5
!=Checks if values are not equal.a!=2
>Checks whether one value is greater than another value.a>0
<Checks whether one value is lesser than another value.a<10
>=Checks if one value is greater than or equal to another value.a>=6
<=Checks if one value is lesser than or equal to another value.a<=10

As you can see from the above table, we have listed some relational operators, so that you can have a look at them, and you can use them as and when required in your C++ programs.

Let’s have a look at a simple C++ program, in which, we try to use some of these operator symbols.

#include <iostream>

int main()
{
    int a = 10, b = 2;
    std::cout << “a is equal to 10: “<< (a == 10) << std::endl;
    std::cout << “a is not equal to b: “<< (a != b) << std::endl;
    std::cout << “a is greater than or equal to 2: “<< (a >= 2) << std::endl;
    std::cout << “a is less than or equal to 10: “<< (a <= 10) << std::endl;
    std::cout << “a is greater than b: “<< (a > b) << std::endl;
    std::cout << “a is less than 4: “<< (a < 4) << std::endl;
    return 0;
}

As you can see from the above program, we have used some of the relational operators. Let’s have a look at the output as well.

a is equal to 10: 1
a is not equal to b: 1
a is greater than or equal to 2: 1
a is less than or equal to 10: 1
a is greater than b: 1
a is less than 4: 0

In the output, where you see 1, it is true, and where you see 0, it is basically false. So, at times, in our programs, we are going to need to use these relational operators, and once you begin using them in your programs, you would become familiar with them and will be comfortable using them.

If you find this tutorial to be interesting, you can explore our other tutorials related to C++ programming language to learn more about C++ programming language. You can also explore our other courses, related to Python, Machine learning, and Data Science, through which, you can upskill yourself.

FAQs related to relational operators in C++

Q: What are relational operators in C++?

Ans: Relational operators or comparison operators are used for performing comparisons between the operands. We can check for various things, like greater than, equals, etc.

Q: What is the difference between = and == in C++?

Ans: When it comes to =, this is an assignment operator, which simply means that by using it, we can assign some value to some variable, and == is a relational operator, which means that by using it, we can compare two operands, if they are equal.

For example, a = 5 means we are assigning the value 5 to variable ‘a’, and a == 5 means we are comparing if the value in ‘a’ is equal to 5.