C++ Relational and Equality Operators


In C Programming, the values hold on in 2 variables will be compared exploitation following operators and relation between them will be determined. These operators are called relational operators. Various C++ relational operators available are-

Operators
Description
>
Greater than
>=
Greater than or equal to
<=
Less than or equal to
<
Less than


You can use these operators for checking the relationship between the operands. These operators are mostly used in conditional statements and loops to find a relation between 2 operands and act accordingly. For example,

Example

#include<iostream>
using namespace std;

int main() {
   int a = 3, b = 2;

   if(a < b) {
      cout<< a << " is less than " << b;
   }
   else if(a > b) {
      cout<< a << " is greater than " << b;
   }
   return 0;
}

Output

This will give the output −

3 is greater than 2

The equality operators in C++ are is equal to(==) and is not equal to(!=). They do the task as they are named. The binary equality operators compare their operands for strict equality or inequality. The equality operators, equal to (==) and not equal to (!=), have lower precedence than the relational operators, but they behave similarly. The result type for these operators is bool.

The equal-to operator (==) returns true (1) if both operands have the same value; otherwise, it returns false (0). The not-equal-to operator (!=) returns true if the operands do not have the same value; otherwise, it returns false.

Example

#include <iostream>  
using namespace std;  

int main() {  
   cout  << boolalpha  // For printing true and false as true and false in case of a bool result
   << "The true expression 3 != 2 yields: "  
   << (3 != 2) << endl  
   << "The false expression 20 == 10 yields: "  
   << (20 == 10) << endl;  
}

Output

This gives the output −

The true expression 3 != 2 yields: true
The false expression 20 == 10 yields: false


Updated on: 11-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements