Role of relational operators and How is relational operator == different from =?

Q4. What role do the relational operators play? How is relational operator == different from =?

Ans. The relational operators determine the relation among different operands. C++ provides six relational operators for comparing numbers and characters. But they don’t work with strings. If the comparison is true, the relational expression results into the value 1 and to 0, if the comparison is false. The six relational operators are:

<(less than), <=(less than or equal to), ==(equal to), >(greater than), >=(greater than or equal to), and !=(not equal to).

Do not confuse the = and the == operators.

A very common mistake is to use the assignment operators = in place of the relational operator ==. Do not confuse the testing the operator == with the assignment operator(=).

For instance, the expression

value == 3

tests whether value is equal to 3? The expression has the value 1 if comparison is true and 0 if it is false.

But the expression

value = 3

assigns 3 to value. The whole expression, in this case, has the value 3 because that’s the value of the left-hand side.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: