Relational Operators

Learn the Basic Java Concepts Working with variables and operators
9 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to the Java development course. So, last time we went over arthritic operators and you had some work to do, your job was to take the program that we created last time, which got two numbers from a user, and then just added and subtracted them and printed the results. And your job was to add multiplication, division and modulus as well. So here's essentially what should have done. So right here I have what we did together, and the right here is what should have added so multiply equals first Val time second Val divided equals first of all divided by second Val. And then modulus.

I mean, I didn't really know how how to use modulus in you know, to assign it something. But in this case, I just I think modulus could could be a word, it's a word now because I used it anyway. modulus equals First of all, and then modulus second. And here's what I did. So first value times second value equals multiplied divided by second value divided first value modulus second value in module list. So now if you run this application, and we do 150 There we go.

100 Plus 50 is 150 100 minus 50 is 50 100 times 50 is 5000 100 divided by 50 is two and 100 modulus 50 is zero. So there we go, that is essentially what should have done. Okay, so we've got that that's very good. If not, don't worry about it really trying to understand what this code does. So get on your machine, get it working Trentham, try some stuff, you know, maybe change some stuff, add some stuff, you know, get creative with the software, because that's really how you learn. And you're good to go.

So without further ado, then let's get into today's lesson. Alright, so today we're going to be going over comparative operators or relational operators depending on how you want to call it. So let's call it relational operators for now. So we're going to create a new class, new class, relational operators. There we go. relational operators.

Now let's go to main as always, and finish. There we go. Alright, so how do relational operators work? So what if we want to check if one is more than two? Well, we can do that we can do Boolean. One is more than two equals one is more than two.

And then we can print system dot out, dot print ln. One is more than two equals and then plus. And then this one is more than two Boolean right here. There we go. Alright, so now let's go and run it. Alright, so one is more than two is equal to false, which makes sense.

So this is the first relational operators, there's more than two integers or actually two numbers. And then just more than we will tell you, if not in return a Boolean. So we actually have a couple relational operators. So we have More than less than let's see. Okay, I think that's all of them. So now we're going to do is first of all, let's go for less than, so we have less than pretty self explanatory more than or equal to two, we're going to have more than or equal to two There we go.

Then we have is more than is less than or equal to two less than or equal equal to there we go we have less than or equal to, there we go okay, then we have not eaten our weightless for so do equal to equal to, and that's going to be just two equal signs and then we have not to This is just going to be a quick question mark and equal sign. There we go. Okay, so those are the relational operators. I think I got them all. I'm pretty sure I got them all. Let me actually check.

Okay, yeah, I got them all. I mean, I've been developing job applications for a really long time, but you never know when you might forget something. Anyway, I got them all. And let's go over exactly what they do. So really, the relational operators should be pretty self explanatory. It's how really they work and how we can use them for something that can really get a little bit confusing, you know, speaking later on, and we're going to go over decision making where these relational operators will start to make a lot of sense and become very useful.

But at this point, it may seem a little bit you know, useless, since I mean, yeah, okay, we can print that one is more than two, but what's the point, you know? Let's go on and do the same thing for all of these operators. So one is more than two equals false and then do the same thing one is less than two equals one is less than To one is more than or equal to two. One is that more than or equal to, and then just Same thing for less than less than or equal to two is one is less than or equal to two. All right. So now if you if you run this, there we go.

So we get one is more than two equals false. One is less than two equals true, this right here, one less than two, one less than two, it's true. And then one is more than or equal to two is false. And then one is less than or equal to two is true. And then finally we have the equal to let's do that as well. Oops, Okay, there we go.

Okay, so one is equal to two equals equal to. And then one is not equal to two will be this not equal to, there we go. All right, so one is equal to two equals false. And then one is not equal to two equals true, which makes sense. All right? And so those are the relational operator.

So we have more than less than, more than or equal to less than or equal to equal to in the not equal to. So yeah, oh, pretty simple stuff. Now let's go over a program together that we will then extend for homework, just like we did in the last class. So I'm going to actually comment this out. All right, there we go. Okay, so we're also going to use the utilize the scanner class as we already have.

So we're gonna do scanner, SC equals new scanner. system.in There we go. All right, as always remember to input in Port scanner. There we go. Okay, now we have int a equals mc dot next int, and then int b equals SC dot next int. Okay?

And then together we're going to check whether or not these values are equal. Okay, so we're going to do Ah, int, no Boolean equal equals A is equal to b. Now if you're wondering why we don't simply have a equals b, so why do we have two equal signs? Well, that's because we want equal signs is for assigning a value to a variable. So that's all you have to equal signs for comparing two variables. So okay, so equal and then we'll just do a system dot out.

Out dot print ln A equals A equals B equals plus equals, there we go. All right, let's run this application. Okay, now we can do five, six and eight equals b equals false. Actually, no, we could actually just do a plus equals plus B plus equals four. There we go such though just make it a little bit more understanding. 56 equals equals six equals false.

There we go. All right, now we're also going to add the not equal to as well. Let's go ahead and do it. So Boolean Boolean, not equal equals A is not equal to b. And then we're going to do a not equal to b equals plus, not equal. All right?

There we go. Now let's test this out. We're going to have this number and then whoops. For string, Okay, I see the problem. Yeah, this number is too large, it can't fit inside an integer. There we go.

So let's say 56. And then 67. There we go. So 56 equals equals 67 equals false. And then 56 is not equal to 67 equals true. So there we go.

That is essentially how we work with relational operators. All right, let's look at some homework. All right. So for homework, your job is to extend this application and add in more than, less than, more than or equal to and less than or equal to as well. So pretty simple, just like last time, just you know, working with these values, you know, getting some some results here and yeah, so without further ado, then I wish you luck and I'll see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.