How to Use the Not-Equal (!=) Operator in C++?
Last Updated :
08 Feb, 2024
The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++.
Not-Equal (!=) Operator in C++
The not-equal operator(!=) evaluates two values and returns a boolean value.
- It returns true when both values are not equal.
- It returns false if they are equal
The not equal to the operator is a binary operator so it is used with the two operands:
operand1 != operand2
It only allows for branching when both values are different and can be used to make decisions based on a given condition.
How to Use the Not-Equal (!=) Operator in C++?
The following examples show how can we use the not-equal operator in our C++ programs:
Example 1
The below example demonstrates the use of the not-equal operator in conditional statements.
C++
// C++ program to demonstrate the use of the not-equal
// operator in conditional statements.
#include <iostream>
using namespace std;
int main()
{
// creating two integer numbers
int num1 = 10;
int num2 = 20;
// Check if the two numbers are not equal
if (num1 != num2) {
cout << num1 << " is not equal to " << num2 << endl;
}
else {
cout << num1 << " is equal to " << num2 << endl;
}
return 0;
}
Output10 is not equal to 20
Explanation: In above example, we are using Not-Equal Operator (!=) that checks whether the num1 and num2 are equal or not. As 10 and 20 are not equal so the block of code inside if statement is executed and prints " 10 is not equal to 20 ".
Example 2
The below example demonstrates the use of not-equal operator in loops.
C++
// C++ program to demonstrate the use of not-equal operator
// in loops.
#include <iostream>
using namespace std;
#include <iostream>
int main()
{
int arr[] = { 2, 4, 6, 8, 10 }; // Sample array
int target = 6; // Number to find
bool found
= false; // Flag to indicate if the number is found
int i = 0;
int n = sizeof(arr)
/ sizeof(
arr[0]); // Calculate the size of the array
// Loop through the array using the not-equal operator
while (i != n) {
if (arr[i] == target) {
found = true;
break; // Exit the loop if the number is found
}
i++;
}
if (found) {
cout << "Number " << target
<< " is present in the array." << endl;
}
else {
cout << "Number " << target << " is not present in the array." << endl;
}
return 0;
}
OutputThe number 6 is not present in the array.
Explanation: In above example, we are checking that a target is present in array or not by using a not equal operator to loop in array until iterator i is not equal to size of array.
Similar Reads
How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as
2 min read
std::not_equal_to in C++ with Examples The std::not_equal_to is a functional object class for non-equality comparison and binary function object class. It returns a boolean value depending upon the condition whether the two arguments are not equal or not.Header File: #include <functional.h> Template Class: template struct not_equal
3 min read
Relational Operators on STL Array in C++ The article illustrates the working of the different relational operator on the array STL. The equality comparison ( == ) is performed by comparing the elements sequentially using operator ( == ), stopping at the first mismatch. The less-than comparison ( < ) or greater-than comparison ( > ) b
3 min read
std::equal_to in C++ with Examples The std::equal_to allows the equality comparison to be used as a function, which means that it can be passed as an argument to templates and functions. This is not possible with the equality operator == since operators cannot be passed as parameters.Header File: #include <functional.h> Templat
3 min read
How to Compare Two Lists in C++ STL? In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Inpu
2 min read