C++

Relational Operators C++

Operators are signs that are used to carry out different operations on values. C ++ has different kinds of operators used to perform different functions. Relational operators are one of them. These operators are essential for decision making. In C++, we can compare different numerical values by using relational operators. The comparison operator is binary, that is, it takes two operands.

The comparison operator has left-right connectivity. It means that if two operators having the same precedence are together, the leftmost operator is assessed first. Relational operators express the relation between the entities. These operators are utilized to compare two or more numbers that are saved in an operand. In C ++, relational operators give the output in form 1 or 0. Where 0 depicts false and 1 depicts true. This article illustrates different relational operators that are used in C ++. For the implementation of codes in C++, we install the DEV C++ compiler and then run the succeeding codes.

Greater Than Operator (>)

This operator examines whether or not the integral value of the left variable is more than the integral value of the right variable. We use ‘>’ this symbol to represent the greater-than operator.

In the subsequent program, we take integral values of variable ‘a’ and variable ‘b’, then we apply the if-else condition. Here, we see whether ‘a’ holds large value or ‘b’ holds large value. And for this, we use the greater than ‘>’ operator.

#include <iostream>

using namespace std;

int main() {
    int a = 9;
    int b = 18;

    if (a > b) {
cout<< "a is greater than b." <<endl;
    } else {
cout<< "a is not greater than b." <<endl;
    }
}

If the value of ‘a’ is greater than ‘b’, the statement following if is executed. And if that condition is not satisfied then the statement following else is executed and the message will be printed.

Less Than Operator (<)

In C++, for comparison, we also use less than the ‘<’ operator. It checks which comparison operand is smaller than the other one.

#include <iostream>

using namespace std;
int main()
{
int m, n;
cout<>m;
cout<>n;
if(m < n)
{
cout<< "m is less than n";
}
else
{
cout<< "n is less than m";
}
return 0;
}

In this case, we declare two integers ‘m’ and ‘n’. First, the user inputs the value of ‘m’ and that value is stored. After this, the user enters the value of ‘n’. Now, we want to know which value is small. For this, we check if ‘m’ is less than ‘n’ by the use of less than ‘<’ operator.

As the user enters ‘45’ value to ‘n’ and ‘67’ value to ‘m’. So it shows the value of ‘n’ is less than ‘m’.

Greater Than or Equal to the Operator (>=)

We may determine which number is greater or equal to another by using the greater than or equal to operator. For this form of comparison, we use the sign ‘>=’.

#include <iostream>

using namespace std;
int main()

{

int i = 23 ;

int j = 23 ;

cout< j) <<endl;

cout<= j) <<endl;

return 0;

}

In this program, first, we include the header file <iostream>. We take two integers ‘i’ and ‘j’. Hence, after assigning the same values to these variables we perform greater than or equal to (>=) operation to obtain different results. Therefore, if the value of “i” is higher than the value of “j”, we execute the condition. Now on the second line, we apply other conditions. Whether the defined conditions are true we get 1 as a result of defined conditions are false we obtain 0 in output.

The output of the first condition is 0 which shows the condition is false and the output of the second condition is 1 which shows the applied condition is true.

Less Than or Equal to Operator (<=)

This comparison operator shows which operand is smaller than or equivalent to another comparison operand. The sign utilized for comparison is ‘<=’. Now, we see how we utilize this operator in the C++ language.

#include <iostream>

using namespace std;

int main() {
    int u = 6;
    int v = 9;

    if (u <= v) {
cout<< "u is less than or equal to v." <<endl;
    } else {
cout<< "u is not less than or equal to v." <<endl;
    }
}

Inside the body of a main() function, we take two variables ‘u’ and ‘v’. We assigned values to these variables. The data type of these variables is “int”. Further, we use the if-else condition. We apply less than or equal to the operator (<=) on these variables. This operator checks if the value assigned to variable ‘u’ is less than or equal to the value assigned to variable ‘v’. If the condition is fulfilled, the ‘cout’ function prints ‘u is less than or equal to v’ otherwise it displays ‘u is not less than or equal to v’.

As we assigned the ‘6’ value to the variable ‘u’ and ‘9’ value to the variable ‘v’. Hence, we get the output ‘u is less than or equal to ‘v’

Conclusion

This article explains the relational operator and several kinds of relational operators and their examples. The relational operator is utilized to relate the specified operands. These operators give the result as 0 or 1. These operators are used to associate conditions. It compares any two numbers ​​and then prints the output. We talked about four different relational operators along with their detailed examples.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.