In this video, we'll look at c++ relational operators.
In addition to the equality operators, c++ also has several
other relational or comparison operators.
These are greater than, greater than or equal to,
less than and less than or equal to.
And in c++20, there'll be a really neat three-way comparison operator.
This operator compares two expressions and evaluates to zero if they're equal,
less than zero if the left-hand side is greater than the right-hand side
and greater than zero if the right-hand side is greater than the left-hand side.
Like the equality operators, these operators are very intuitive,
easy to use and understand. Let's see a few examples in CodeLite.
I'm in CodeLite. I'm in the section 8 workspace.
And this is the relational operators project. In this project,
I'm just using some really simple examples to test some of these
relational operators.
As you can see, I've got two numbers declared num1 and num2.
This is where I'm going to read some integers into.
And I'm asking the user to enter two integers separated by space
and I'm reading those two integers from the user.
Then what I'm doing in this output statement right here
is suppose that the user entered 10 and 20 here.
I'm just saying 10 greater than,
I'm just explicitly printing out that greater than sign, 20
and then I'm evaluating whether num1 is greater than num2 and that's going to say true or false.
So it's going to do that for all four operators, greater than, greater than or equal to,
less than and less than or equal to.
And then we could try out a couple of examples with this program.
So let's run it.
And let's enter two integers. Okay. So let's enter 10 and 20.
So you could see here, 10 greater than 20 is false.
10 greater than or equal to 20 is false.
10 less than 20 is true. And 10 less than or equal to 20 is also true.
Okay. So now let's try some other ones,
a couple of other test cases here. Let's try 20 and 20.
In this case, you can see that 20 is greater -- 20 is not greater than 20.
20 is, is greater than or equal to 20.
20 is not less than 20. And 20 is less than or equal to 20.
Just what we'd expect, so real simple example.
What I'll do now is I'll comment out those output statements that we just did here.
And I'll declare a couple of constants here.
And I'll uncomment this piece of code out and I'll walk you through it.
So in this case, I'm declaring two constants, lower and upper
to be 10 and 20.
And. I'm asking the user to enter an integer that's greater than lower.
So in this case, I'm asking them to enter an integer that's greater than 10.
And I left those as constants so you can modify them here and
play around with this program yourself. So in this case, enter an integer that's greater than 10.
I'm reading that integer into num1.
Then I'm comparing to see if that number is greater than
the lower limit which is 10 in this case.
Then I'm doing the same thing. I'm saying enter an integer that's less than or equal to the upper limit.
So let's run this.
And first thing you'll see is it says enter --
let me get rid of that
statement at the beginning here.
And we try that again.
Here we go. So enter an integer that's greater than 10. So let's write 12.
And it says 12 greater than 10 is true. That's what we expect.
Now it says enter an integer that's less than or equal to 20.
Let's put in 12. And it says 12
less than or equal to 20 is true.
Okay so let's run this one more time with a different test case.
So enter an integer that's greater than 10. Let's put in 8 this time.
And it says 8 greater than 10 is false. Obviously, I didn't do what i was told to do.
And here enter an integer that's less than or equal to 20. Let's put in 25.
And we can say 25 is less than or equal to 20 is also false.
Okay. You can see where we're going with these operators.
Once we get to control flow statements,
you'll have all this knowledge under your belt
and you'll really be able to write some really powerful programs that solve problems.