C++ not_eq Keyword
The not_eq keyword in C++ is an alternative representation of the inequality operator (!=). It is part of the set of alternative tokens introduced in the C++ Standard to improve readability and accessibility for programmers using different keyboard layouts or preferences.
The not_eq keyword functions identically to !=, evaluating to true when the operands are not equal and false when they are equal.
Syntax
</>
Copy
expression1 not_eq expression2
- not_eq
- Represents the inequality operator.
- expression1
- The first operand to compare.
- expression2
- The second operand to compare.
Examples
Example 1: Basic Use of not_eq
This example demonstrates the equivalence of not_eq and !=.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 10;
cout << "Using != operator: " << (a != b) << endl;
cout << "Using not_eq keyword: " << (a not_eq b) << endl;
return 0;
}
Output:
Using != operator: 1
Using not_eq keyword: 1
Explanation:
- The variables
aandbhave different values. - The expressions
a != banda not_eq bboth evaluate totrue, indicating inequality. - The output confirms that
not_eqis functionally identical to!=.
Example 2: Using not_eq in Conditional Statements
The not_eq keyword can be used in conditional statements to evaluate inequality.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int x = 3;
int y = 7;
if (x not_eq y) {
cout << "x and y are not equal." << endl;
} else {
cout << "x and y are equal." << endl;
}
return 0;
}
Output:
x and y are not equal.
Explanation:
- The variables
xandyhave different values. - The condition
x not_eq yevaluates totrue, so theifblock executes. - The output indicates that
xandyare not equal.
Example 3: Using not_eq with Strings
The not_eq keyword can be used with string comparisons to evaluate inequality.
</>
Copy
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = "World";
if (str1 not_eq str2) {
cout << "The strings are not equal." << endl;
} else {
cout << "The strings are equal." << endl;
}
return 0;
}
Output:
The strings are not equal.
Explanation:
- The strings
str1andstr2have different content. - The condition
str1 not_eq str2evaluates totrue, so theifblock executes. - The output indicates that the strings are not equal.
Key Points to Remember about not_eq Keyword
not_eqis an alternative representation of the inequality operator (!=).- It evaluates to
truewhen the operands are not equal andfalseotherwise. - It is functionally identical to
!=and can be used interchangeably. - It is part of the alternative tokens in C++ to improve code readability and accessibility.
- Using
not_eqis optional and a matter of coding style or preference.
