Equality Operators: == and != in C++


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() {  
   // For printing true and false as true and false in case of a bool result
   cout  << boolalpha  
   << "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

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements