Hi, in this video we're going to introduce comparison operators. So what are comparison operators? They let us compare two values. So for example, say we have int x is equal to 15 and int y is equal to 10 and then we create a boolean called z which is equal to x is greater than y and so that will be getting the value true because it is true that x which is 15 is greater than y which is 10. So what are our comparison operators?
How can we compare things? Well we can ask if things are equal using equals equals. We can ask if things are not equal using the exclamation point and then equals. We can ask if something is less than another thing using the less than sign. We can ask if something is greater than.
We can ask if something is less than or equal to using the less than and then the equal to sign or greater than or equal to. So those are our comparison operators. And you should note that we can use these comparison operators to compare the primitive types like int and double and boolean and char.
We're not going to use these on strings because that gets a little bit more complicated. We'll talk about that later, but for now we'll use these on the primitive types. So let's say we want to find out if we can ride a roller coaster. So we'll get from the user, we'll ask int height in inches is equal to readint what is your height and then we'll say boolean can ride is equal to Height in inches is greater than or equal to 50. And what this program is really doing is saying that whether or not you can ride, that's true or false.
If you're at least 50 inches tall, then it's true. Otherwise, it's false. So let's say we want to figure out your grade.
And we read in the grade using int grade equals read int, what was your grade. And then we make a Boolean called got b. And got b is equal to grade is greater than or equal to 80 and grade is less than 90. And this means that it's true if your grade is between 80 and 90. So let's go into our code editor and look at this problem. Okay, so first let's ask the user for their grade. We'll say int grade is equal to readint, what was your grade?
And then we'll make a Boolean, which is whether or not they got a B. And that's equal to grade being greater than or equal to 80, and grade is less than 90. And then we'll print out, got a B plus got B. And so let's enter in some values, test out this program and see how that works.
Okay, so let's say I got an 85. That should be a B, because that's right in the middle of the range. Now let's say I got an 80. Yep, that's also B because grade is greater than or equal to 80. Now let's try something on the other end of the range. What if I got a 90? Well, that's false because 90 is not less than 90. Let's try one more test.
What if I got a 74? That's also not a B. And so here you can see what comparison operators are letting us do is compare that value from the user grade to another value.
Right now it's the number 80. And we're combining that using the AND logical operator. So now you'll get to use comparison operators in your programs.