Saturday 5 January 2013

Relational and equality operators ( ==, !=, >, <, >=, <= )

In order to evaluate a comparison between two expressions we can use the relational and equality operators. The
result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result.
We may want to compare two expressions, for example, to know if they are equal or if one is greater than the
other is. Here is a list of the relational and equality operators that can be used in C++:




Here there are some examples:



Of course, instead of using only numeric constants,we can use any valid expression, including variables. Suppose
that a=2, b=3and c=6, 


 

Be careful! The operator =(one equal sign) is not the same as the operator ==(two equal signs), the first one is an
assignment operator (assigns the value at its right to the variable at its left) and the other one (==) is the equality
operator that compares whether both expressions in the two sides of it are equal to each other. Thus, in the last
expression ((b=2) == a), we first assigned the value 2to band then we compared it to a, that also stores the
value 2, so the result of the operation is true.

No comments:

Post a Comment