In this tutorial, you shall learn about Not-Equal Relational Operator in C++ programming language, its syntax, and how to use this operator with the help of examples.

C++ Not-Equal Operator

In C++, Not Equal Relational Operator is used to check if left operand is not equal to second operand.

The syntax to check if x does not equal y using Not Equal Operator is

x != y

The operator returns a boolean value of true if x is not equal to y, or false if not.

Examples

1. Check if two numbers are not equal

In the following example, we take two integer values in x and y, and check if these two are not equal, using Not Equal Operator.

main.cpp

#include <iostream>
using namespace std;

int main() {
    int x = 1;
    int y = 6;
    
    if (x != y) {
        cout << "x and y are not equal." << endl;
    } else {
        cout << "x and y are equal." << endl;
    }
}

Output

x and y are not equal.
Program ended with exit code: 0

Since values in x and y are not equal, x != y returned true.

2. Check if two strings are not equal

Now, let us take two strings, and check if they are not equal using Not Equal Operator.

main.cpp

#include <iostream>
using namespace std;

int main() {
    string x = "apple";
    string y = "apple";
    
    if (x != y) {
        cout << "x and y are not equal." << endl;
    } else {
        cout << "x and y are equal." << endl;
    }
}

Output

x and y are equal.
Program ended with exit code: 0

Since values in x and y are equal, x != y returned false.

Conclusion

In this C++ Tutorial, we learned about Not Equal Operator in C++, with examples.