C++ Not Equal (!=) Operator - Tutorial Kart
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
</> Copy x != yThe 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
</> Copy #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: 0Since 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
</> Copy #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: 0Since 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.
Tag » C Not Equal Operator
-
Operators In C | Set 2 (Relational And Logical Operators)
-
C - Operators
-
Equality Operators: == And != | Microsoft Docs
-
C Relational And Equality Operators - Microsoft Docs
-
C Programming Operators - Programiz
-
Operators In C And C++ - Wikipedia
-
C | Not Equal To: != | Easy Language Reference
-
C Operators - W3Schools
-
Logical Operators - How Can We Write "not Equal" In C? - Stack Overflow
-
Comparison Operators
-
C Operators
-
Python Not Equal – Does Not Equal Operator Tutorial - FreeCodeCamp
-
If Statements In C
-
Operators - C++ Tutorials