- Comparison operators are used to compare for equality
or for relative value.
This is working dot CPP from chapter four
of the exercise files.
These operators are called relational operators
and they work like this.
We'll set up a couple of integer variables
And an if statement, if X is equal to Y,
we'll put true,
and else false.
- Now, when I build and run this,
you notice it says false because X is not equal to Y.
So, the double equal sign is a comparison for equality.
It's comparing these two values to see if they are equal
or not.
It's not checking to see if they're the same variable.
It doesn't check the address of the variable.
All it does is it checks if the values are the same,
one is not greater and not less than the other.
We can check for inequality by using the bang sign
instead of the first equal sign.
So, this says not equal,
it's checking if they're not equal.
And so, it should return true.
We can check for greater than or less than,
if X is greater than Y, which is not true.
Or if X is less than Y, which is true.
And we also, of course, have less than or equal
and greater than or equal.
And so, these are the comparison operators
or the relational operators.
And they return a logical value.
So, instead of this, if I were to say print off
and I put that logical expression over here,
I'm going to get a one or a zero every time.
So in this case, it's a zero.
And if I say not equal, now it's a one.
And so, as we've learned earlier,
a zero is considered false
in the context of a conditional expression
and anything that is not a zero is considered true.
And so, these relational operators,
they return a one for true and a zero for false.
So, the comparison or relational operators
are used to compare for relative value or for equality.
These are simple and effective operators
that work on any scalar type.