In this video, we'll go over the c++ equality operators.
These operators compare the values of two expressions
and evaluate to a Boolean that's a true false value.
The operators are the equals which is represented by two equal signs
and the not equals operator,
which is represented by the exclamation point or bang symbol,
followed immediately by an equal symbol.
Be extra careful when you're starting. It's super common for beginning c++
programmers to use a single equal sign. That's assignment, not comparison.
These simple operators, along with the operators we'll learn about in the next several videos,
give us so much power in programming.
We use these operators all the time in control flow statements that we'll learn shortly.
So suppose we want to do something when x is equal to 100. We can do that.
We can also test for two expressions not being equal.
So if my password doesn't match the password on file then
you want to do something specific.
The following code snippets only show the equality expressions.
They're always used within statements.
The first example evaluate to true if the value of expression 1
is equal to the value of expression 2.
These two expressions could be very complex or very simple.
The second example will evaluate to true if the value of expression 1
is not equal to the value of expression 2.
The third example will always evaluate defaults since 100 will never equal 200.
We don't usually write expressions like these but it serves to illustrate how this operator works.
The final example evaluates to true if num1 is not equal to num2.
Let's go over a few more examples that use statements.
First, we're declaring a variable named result that's of type Boolean,
and we're initializing it to false.
Then we compare 100 and 50 plus 50 for equality.
This will result in a Boolean, that's a true false value which we assign to result.
In the next example we compare num1 and num2.
And if they're not equal, we store true in result. Otherwise, false.
Notice how we can use Boolean expressions and output statements.
In the first output statement, we're comparing num1 and num2.
And we're displaying one if they're equal or zero if they're not equal.
Sometimes it's handy to display the words true and false
rather than 1 and 0 in the output statements.
We can do that using the boolalpha stream manipulator.
Boolalpha and noboolalpha are located in the standard namespace.
Once you use them, all Boolean output to the stream will result in the words true and
false being displayed, that can be pretty handy.
If you want to go back to the default of zero and one, just use no bool alpha.
Let's head over to CodeLite and see some examples.
So I'm in CodeLite, I'm in the section 8 workspace, the equality
operators project.
Let's run over a few examples of using these equality operators.
These are going to be really, really straightforward examples. I don't think you'll have any issues understanding
these but you may see a couple of surprises. So stay tuned for that.
So here we go. We've got a Boolean called equal result,
which I'm initializing to false, and another Boolean called not equal result,
which I'm also initializing to false.
And I've got two integers here num1 and num2. And we'll just play around with this a little bit.
I've got my cout bool alpha here commented out. I'll uncomment that
out in a moment, so you can see the difference between the behavior when you do that.
So here, I'm prompting the user to enter two integers separated by a space.
I'm reading in those two integers on line 15.
I'm comparing those two integers for equality, right here on line 16.
So I'm saying, is num1 equal to num2
That result is going to be stored in equal results.
So it's either going to be true if they're equal or not true if they're not equal.
Then I'm going to compare the same two numbers using the not equal operator.
So is num1 not equal to num2. In this case if they are not equal,
then I'm going to store true into the not equal result.
Otherwise false. Okay and then I'm just going to print out those two results.
So let's run this piece of the code right now.
And you'll see what happens. Let's enter two integers.
In this case, let's enter two integers that are equal to one another.
So I'll type in 10 and 10 and press enter.
And you can see here, the comparison result for equals is one which is true
and for not equals it's zero because they obviously are equal.
Okay. Now notice I have a one and a zero being outputted here.
When I uncomment out the
bool alpha stream manipulator and recompile and run,
now I can say, let's say, 2020
and instead of zero one, we see true false. So that's pretty handy.
So in this case they are true. So the equals will be true and the not equals will be false.
Now let's use an example where they are not equal.
And let's say we're trying to do 10 and 20 this time.
So equals is false and not equals is true, just like what we'd expect.
Okay. So like I said, that's pretty straightforward.
I'm going to comment out this piece of code right here.
And now we'll do the same thing except we'll do it with characters.
Remember, we can do this with any type. We can compare any type.
So in this case, I've declared
two characters,
char1 and char2 and initialize them both to empty to 0.
And I'm asking the user to enter two characters separated by space.
I'm reading in the characters, then I'm doing exactly the same thing. I'm comparing them for equality,
and I'm comparing them to see if they're not equal.
So let's run this, and the first test we'll do is
we'll say lowercase a and uppercase a.
Those are different characters. I'll press enter,
are they equals? Nope, that's false.
Are they not equals? True, that's true. So that's exactly what we expect.
And let's test it one more time with, let's say, a capital z
and another capital z. In this case, they are equal.
So that's true and the not equals is false.
Okay. So you can understand the logic that's going on here.
I'll comment that out,
and I will uncomment the next section, which is dealing with doubles.
And so again, exactly the same thing except I'm using doubles. I'll run this,
and we'll type in just say two doubles that are the same
12.1 and 12.1,
the comparison result is they are equal. Exactly what we'd expect again.
And let's do an example where they are not equal.
So let's say 12.5 and 13.2,
the equals is false because they're not equals. And the not equals is true because they are not equals.
Okay. Now this is the example that I wanted to run to show you
something that may not be real obvious. Let's say we want to enter two doubles separated by a space.
So I'm going to type in 12.0
and 11 point a lot of nines.
Now to us those are not the same number.
But remember, the way the computer stores floating point numbers,
it stores approximations. So
that might be surprising. As far as the computer is concerned,
those numbers are equal because the representation that it's using to store them behind the scenes is equal.
Okay. So it's important to understand that
if you're dealing with applications that need this kind of precision,
you know scientific application,
medical applications, many many safety critical type applications
you, would not use these built-in doubles.
We would use specific specialized libraries
that really help us deal with this.
So that 12.0 is not equal to 11.99999.
Okay. So it's important to understand that. But the way that it stores it is in approximation.
So as far as the computer is concerned, these are the same.
So let's do one more, and I'll leave that double
declaration there. And I'll uncomment this last piece of code here.
And here, I'm just grabbing an integer and a double.
So we're doing some mixed mode comparison here.
I'll run it and it says give me an integer and a double separated by a space.
In this case, I could say 10 and 10.0.
Remember, what happens. It's using an operator to compare two different types.
It doesn't want to do that. Those types have to be the same.
So the 10 will be promoted to a 10.0.
And then the comparison happens. That's the mixed mode expressions.
So what we're doing here is we're eventually comparing 10.0 and 10.0.
So we expect this to be true. And it is right there.
And you get the same sort of issue here if i say 10
and 9.999, a whole bunch of nines.
What's going to happen again is the 10 will be promoted to 10.0.
10.0 and nine point a bunch of nines as far as the
internal representation is concerned are equal.
So there's your true.
Okay. So that's it, that finishes off this video.
And that's it. I'll see you in the next video.