Equality operators: == and != (2024)

Syntax

expression == expression
expression != expression

Remarks

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 if both operands have the same value; otherwise, it returns false. The not-equal-to operator (!=) returns true if the operands don't have the same value; otherwise, it returns false.

Operator keyword for !=

C++ specifies not_eq as an alternative spelling for !=. (There's no alternative spelling for ==.) In C, the alternative spelling is provided as a macro in the <iso646.h> header. In C++, the alternative spelling is a keyword; use of <iso646.h> or the C++ equivalent <ciso646> is deprecated. In Microsoft C++, the /permissive- or /Za compiler option is required to enable the alternative spelling.

Example

// expre_Equality_Operators.cpp// compile with: /EHsc#include <iostream>using namespace std;int main() { cout << boolalpha << "The true expression 3 != 2 yields: " << (3 != 2) << endl << "The false expression 20 == 10 yields: " << (20 == 10) << endl;}

Equality operators can compare pointers to members of the same type. In such a comparison, pointer-to-member conversions are performed. Pointers to members can also be compared to a constant expression that evaluates to 0.

See also

Expressions with binary operators
C++ built-in operators, precedence; and associativity
C relational and equality operators

Equality operators: == and != (2024)

FAQs

Equality operators: == and !=? ›

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

What is the == equality operator? ›

The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions. The result is TRUE if the expressions are equal and FALSE otherwise.

Which symbol is used to test equality in R == != ?=? ›

Equality Operator ==

You can check whether two objects are equal (equality) by using a double equals sign == . We can see if the logical value of TRUE equals the logical value of TRUE by using this query TRUE == TRUE .

Why do we prefer === and !== Over == and != In JavaScript? ›

Return type: boolean

The === operator compares operands and returns true if both operands are of the same data type and have some value, otherwise, it returns false. The !== operator compares operands and returns true if both operands are of different data types or are of the same data type but have different values.

Which operator is used to test if values are equal !> ==? ›

The == (equality) and != (inequality) operators check if their operands are equal or not. Value types are equal when their contents are equal. Reference types are equal when the two variables refer to the same storage.

What is the use of == operator? ›

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

What does the symbol != Mean? ›

The inequality ( != ) operator checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types.

What does the operator == mean in R? ›

R has the following comparison operators: Equal, == , which returns TRUE if two values are equal. Not equal, != , which returns TRUE if two values are not equal. Less than, < , which returns TRUE if left value is less than right value.

What does == mean in Python? ›

The “==” operator is known as the equality operator. The operator will return “true” if both the operands are equal. However, it should not be confused with the “=” operator or the “is” operator. “=” works as an assignment operator. It assigns values to the variables.

What is the meaning of the == symbol in R? ›

== A boolean (meaning there are two possible values, true or false) operator which ensures that the values on the left side are the exact same as the values on the right, i.e., 5==5 would be true and 5==9 would be false.

Should I use === or == in JavaScript? ›

Type coercion in JavaScript can sometimes lead to unexpected results, so it's mostly recommended to use the strict equality operator === instead of the loose equality operator == .

Should I ever use == in JavaScript? ›

The only time you should use double equal is where you know in advance that you definitely do want to use javascript's implicit type conversion. This might be the case for example if you want to check the numeric value of an input field (which would be string).

Which is faster == or === in JavaScript? ›

It turns out that there is little practical performance difference between == and === . While the strict operator is marginally faster (roughly 10%) in most browsers when combined with explicit type conversion, such as a === +b , the only real performance gains will come from avoiding type conversion entirely.

What does !== Mean in JavaScript? ›

!== means "not equal value or not equal type". It is a comparison operator in JavaScript that checks if the two values being compared are not equal in either value or data type. For example, 5 !== "5" would return true because although the values are the same, the data types are not.

Is the == operator used to test for inequality? ›

Equality and inequality operators == !=
OperatorUsage
==Indicates whether the value of the left operand is equal to the value of the right operand.
!=Indicates whether the value of the left operand is not equal to the value of the right operand.

Why use equals instead of == Java? ›

The == operator can't compare conflicting objects, so at that time the compiler surrenders the compile-time error. The equals() method can compare conflicting objects utilizing the equals() method and returns “false”.

What is the equality operator in Python? ›

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=

What is equality operator in Java? ›

Java has 6 relational operators. == is the equality operator. This returns true if both the operands are referring to the same object, otherwise false.

What is the === in C++? ›

The short answer is that “==” is the equality comparison operator and “===” isn't a valid token in the language. Meaning, there's no “===” operator in C++. In dynamically typed languages, “===” serves as the 'strict equality' comparison operator, meaning it returns true if both the type and the value match.

What are the two equality operators? ›

For the equality ( == ) and inequality ( != ) operators, the result of the comparison indicates whether the two pointers address the same memory location.

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6470

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.