C++ Syntax: Equality: == !=

Description

The operator == compares two values and returns 1 (true) if the are equal or 0 otherwise. The operator != checks for inequality in the same way.

See operator precedence

Other Uses for == !=

None.

Usage Notes

Take care, a popular mistake of a novice (and sometimes not so novice) is:- if ( a = b ) { which is perfectly good C++. The point is the assignment operator =, like all other operators, returns a value (its the value of the right hand side). The fact that it also assigns it to the right hand side makes no difference. So the above means assign the value of b to a and execute the conditional statement if b is not 0 (in C++ any non-zero value is considered to be true).
Go Back to the The C++ Crib Top Page


If you have any comments about this page please send them to Nick West