Operators

17 minutes
Share the link to this page
Copied
  Completed
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provide the following types of operators − Arithmetic Operators. Relational Operators.

Transcript

Hello in this video we are going to look at the different operators with in Java. So we have arithmetic operators such as plus minus, we have relational operators bitwise logical assignment and we have some extra operators as well, we won't cover every single one we'll cover one or two, you know brief forms of each category and I'll provide a link with this video we can see a extensive list so for the purpose of this video, we are simply going to be printing stuff out the apprentice intend them like so. And in here, we will do something so if let's say I put the number of Toys R Us variable for this. So if I put int no one equals five equals true and informative do well. Number two, I think you can guess what the result will be. And you will be right it is eight.

So five plus three is eight. So this is a very basic arithmetic operator for lift operator. Number one, you can also do minus, but we could do instead. So if I run this, so we do five times three, which is 15. Their division as well, let me just quickly show you some of the other ones. So there is something called the modulus operator which just gets the remainder, which in this case should be two.

So it's two right there and say, we also do one more operator and the other ones are just multiply which isn't there. It is. I mean just divide and minus and you know, the other operator is called plus plus or increment operator. And what this does is add one to no one. And if I do nothing to the minus minus, which just takes away one, so if we run it Oh, sorry, it does it. Okay, so we've plus plus or minus minus the increment in the decrement.

Operator, there we go preamble line like so. And if I just print out number one, you know, it's incremented. It is added one to it. What this plus plus means is, it means do everything on this one first and then add one to the number. So when we have here, what you're saying is do system dot printed line. printer number one wants that.

Then I'd want to enhance while the change wasn't getting reflected. So if I were, if I were to just get rid of this, run it again. But if I were to just duplicate this, on the next line, you'll have six, by the end of the line, no one is seven. So if you want to do it in line, the way you do the increment, or decrement, if I print plus plus before, and now this will, you know, take away one if it's this decrement operator, this one here, or add one if it's the increment operator. So this increment or decrement first then does the rest of the line. Otherwise it does it after the rest of the line is executed.

So those are the arithmetic operators. Let's have a look at the relational operators. Operators So, in terms of relational operators, this is basically determining my relationships between you know, values, for example, and one of the most obvious one is system. So, again, you could put this in any form and maintain the result out. And you probably have these when you do stuff like if statements, which comes on to the form of decision making conditional statements, which we'll cover later on in this series. But for the number one, and to check if something is equal to something else, you do double equals no to for single equals in assignment double equals is comparison for the prints are false because you know slide does not equal free.

But if I were to change this to five now so long is still printing To the false, ah, cook obtains the numbers here. So I'll comment out this line. It's true. If I were to change this back to free it is four to five different fruit. So the number operator, which is the opposite of this, which is not equal exclamation equals I say is number one not equal to num two, if that is correct your return true. So, you see returns false for the first line plus sign is no more than equal to num two, five is not equal to three.

Second line is not one not equal to num two. So as long as they are different than what they are, this will return a value of true. So five does not equal three, therefore it returns true. There are a few others there's greater than, less than, greater than or equal to, less than or equal to or cover greater than and greater than or equal to and are the next Task only to look at less than or less than or equal to. So if I do, so greater than is like, so. And again, greater than or equal to is like this.

So if I run it again, so that true true. So what this is saying is number one greater than num free, five is greater than free, is number one, greater than or equal to num three or num two, five is greater than or equal to num three, so it's fine. If I were to change this to five, however, we have false and true is saying is five greater than five? No, they're the same. So that does not invalidate as true is number one greater than or equal to num two. So 10 is five greater than or equal to num two is not greater than or equal to, so this will return a value true See if you can see the right the less than and less than or equal to well, so now let's have a look at some of the bitwise operators.

So what this does, it gets the binary equivalent of what you're doing and it will do a binary operation. So bit wise operator and the pipeline was upright decisively system dot print ln. And for the number one and number two for this we'll do the end operation on the binary equivalence and for number one and number two, so for run this, it returns a value of five. And that's because you know that they are actually the same. Go to the free for example, The return a value of one so this is on a binary level check in, though trying to do the and operation which does if one and one actually I'll just quickly just show you what the equivalent is. So, I will blonde.

So if we have if you have an eight bit number so 128 6432 16 two months is binary. So, for us to have the number five we would have 0001014 number phrase of identical but this is zero now and this is the one so when you do the and operation more it basically does is it goes through it. It does I will also put a line here to distinguish this. So it does one and one was one and one. It's one. So I'll actually actually do from the left you generally do from the right is going to be easy to write on here.

So 01000000000100 again 01001 and zero is still zero because the AND operator required both of them to be on. Again, zero and one. So the resulting value from five and free is one and that's what we get printed out. And then other operating wallet or operator which only requires one of the bits in each number, when you're doing the operation to be you know, on so if we go for these would result in want the would also result in one as well. Know that the variety of bitwise operators not said I'll provide a link you can check them out. Okay, so let's cover logical operators, logical operators the logical operator is double ampersand and this is checking if two things are true and if they are it will return a value of true.

So let me create some more variables I will do Boolean know ball won equals true equals true as well. And in here if we were to do vol one and row two is saying is vol one and and war two was result to that. So the double ampersand mean both values need to be true, which you know they are. So you'll return true for change one of them both however, and, and so the AND operator requires all the logical and I should say because it's slightly different to what this does. This is changing on a binary level. This is more of just checking if both values not equate to true.

So this printed false. Another type of operator you can do is the logical OR operator, which is just two pipes. And this will return a value of true because the only requires one of them to be on the next task. Change vol one to false. So both of them are false, and see what the result is. There's another operator called the logical NOT operator, which just basically checks.

If it's true, you're reverse the result. Again only to have a go at that yourself. I'll provide a link with this video to cover all of the operators that we covered. Okay, so assignment operator. So let's take a look at the assignment operator. We've already done one, which is equal, what it does is assigned what's on the right to this variable on the left.

I'll cover a couple more. So I'm on top right now okay, lowercase. Okay, so for this system, dot print ln. Okay, so what we can do is single equals does fine. Boy, you can also do so if you have no one here. So I'll create another number because it's gonna be hard to keep track of what's been happening.

So in num three equals four. What you can do if you want to add, let's say eight to num free for what it is currently you just do num three plus equals eight. And if I were to put green to here, run it. There we go, it prints out. Wow. So this is the equivalent of doing num three equals itself plus eight, both of them are valid.

This is just a shorthand. See if you can figure out how to do the shorthand version of, you know, subtracting a number from the current number. multiplying and dividing often you can get it pretty similar. You just have to change one of the operators same with the modulars as well. So you can also use some of the bitwise operators in a similar way and I will We'll show you or I'll provide a link where you can check all of them out. So we've covered, you know, a variety of operators, there's one operator we will cover, which is the conditional operator.

And let's take a look at that one. So the conditional operator is allows you to check if a condition is true, a bit more advanced than what basically this is a sign of both of them true and providing the vote but we can, you know, do a bit more than that. And we can provide an alternative if one of them or if it's false. So if we just have a look at the conditional operator, honestly, I'm not a big fan of the conditional operator. This thing can be very confusing looking at it, but just know what it is. conditional operator and if I do this Create a variable called in norm four equals six.

Oh do another note 64 make the number of it big I don't actually matter for non five equals 65 as well. And what we can do is say if we do result equals So this is the conditional part. So we do brackets that we put the condition inside of here. So we're gonna say if num four is equal to Long live so the conditions are the relational operators that we've learned and the other ones that are covered in the link along with the video can all be provided here, though saying does num four equal to num five? If it does, you have to put a question mark. If it does, say assign the value of 34 Doesn't you assign the value of 12 so if we run this actually won't do anything that will system the current result and see what we get.

So we get a result of 34 because num for daddy cannot fly, therefore it Princess 34 but this has a fallback. So, if he isn't true this condition it will provide the value of 12 and assign that to result. So, all of this side is the same, you know, variable or mandate type variable name, assignment operator, but all of this is the format of the conditional operator. So first we have the condition itself. Question mark. First value is if this condition complex is true, second value after the colon is the validated return if the condition itself so far, do 63 For example, 65 is not equal to 63 therefore, it provides a value 12 or do a different value other than, you know 12 because I've got that printing it might look a bit confusing, so it prints out 19.

So we've covered a lot of operators. There are a few more. Like I said, I'll provide a link with this video. We can check them all out. If you have any questions. As usual, feel free to reach out and I will help you and I look forward to seeing you in the next awesome Java video.

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.