Skip to 0 minutes and 4 seconds[VIDEO GAME MUSIC PLAYING]

Skip to 0 minutes and 13 secondsVariables aren't worth a lot if they're just standing on their own. It's just, after all, a reference to something and a value. What we want to do is change the value and do things to the values. For that, we use operators. We know some operators from maths-- multiplication, addition, subtraction, division-- that sort of thing. In programming, we have many more. We'll look at some of those in this video. We will use these to create more advanced features, adding them together, and create new statements. In the last video, we actually saw the use of operators already. For instance, when we were dividing mCanvasWidth with 2, we'll use the operator divide to find the middle of the screen.

Skip to 1 minute and 8 secondsAnd then assigning that value to mBallX and mBallY so that the ball would be positioned in the middle of the screen. We use the fact from maths that if we divide something with 2, we get to the middle of it. And we use that. Quite a simple mathematical term, but still giving us the result that we want.

Skip to 1 minute and 36 secondsMay be a little bit of a surprise is that actually the assignment, the equal sign, is also an operator. In the world of Java, it's not an equal sign like in normal maths where we say something is equal to something else. It's actually assigning. So what the equal sign says here is that everything to the right of it will be assigned to mBallX so that the value becomes the same.

Skip to 2 minutes and 8 secondsSo that's a little bit of a tricky one, and sometimes it takes a little bit of getting used to because sometimes you see, for instance, x equals x plus 1 and you're thinking that can never happen because x can never be equal to its own value plus 1. But in the world of assignment, you can, of course, because you're only saying that the x will now be the value that is 1 higher than itself. And after doing it, x has grown by 1. So that gets a little bit of getting used to, but try and think about that. If you're looking at the code as a whole, you'll notice that there's hardly any lines without any operators in them.

Skip to 2 minutes and 53 secondsThey are everywhere. And that's because it is such a fundamental concept in programming. We are applying math. We're using computer to do math and math thinks for us. And we use it in a way that it does the things that we want. OK, let's go in and look at a few examples here. So if you go into actionOnTouch for instance, what we're doing here is we want to change the speed so that if we are pressing the screen far from the ball, the ball will start moving towards where we pressed, and the further away, the faster it would go.

Skip to 3 minutes and 31 secondsAnd the very easy way of doing that is just to say, OK, x and y here, that's where we press the screen. And if we minus the x value with the current position of the balls in x and do the same to y, well, then we'll actually get that feature. The ball will start moving in the direction towards where we click. So if you don't understand that, try maybe with a pen and paper to draw it up because you'll see that that's actually going to happen. It will start moving in that direction and the further away, the faster the speed.

Skip to 4 minutes and 14 secondsThis is actually a quite simple example. If you go into updateGame for instance, we'll see a line which is slightly longer. We got more operators. And when we have more operators, of course, there is a question in which order are they going to be performed. And there's quite an elaborate system for that. You can find a table on the step. It's called the operator precedence. And it gives us the priority of when they will be performed. In this case, the multiplication will be performed before the addition, which will be performed before the assignment. To change that, we can change the order, and then we'll have to use parentheses. That will always change the order for precedence.

Skip to 5 minutes and 2 secondsSo for instance, if you want to do the addition first, we could do that by adding these two parentheses. And now the addition would happen first. But of course, if we did that, we would get a result that is wrong because what we do want in here is, of course, to change the mBallX to the same position as what it was before but with the change in speed depending on how many seconds has gone. So we calculate the distance that it should move at each update. The updateGame is called just before we draw the ball on the screen.

Skip to 5 minutes and 54 secondsSo this means that on the screen, the ball will move as much as it should for the amount of milliseconds that has happened since we updated the game last. And this is actually how we're getting the ball to move. We also got logical operators and these are used to create logical rules. We only have one of them at this moment in the code. And we'll look much more into them later on when we introduce the if statement. There's one if statement here. You don't need to understand at this time what an if statement does. We'll look at that in a later week. So more than that, but the equals equals, that's action also an operator.

Skip to 6 minutes and 49 secondsAnd it is just checking if canvas is equals to null. So this is the equal that we are normally used to from math. We're checking if canvas is equal to something-- in this case, no. But as I said, we will look much more into logical operators in another week when we work on if statements.

# Introduction to operators

Operators are used to perform functions on the data in our variables. In this video, you will learn about some of the ways operators can be used, see examples of operators in the game code, and find out how you can use the IDE to implement and test operators in the code.

In this activity we’ll be looking at some of the most commonly used operators. These fall into four main groups:

### Assignment operator

The assignment operator is used to assign a value.

### Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction and division.

### Unary operator

We’re only going to cover one unary operator in this course: `!`

which is used to invert a Boolean value.

### Equality, conditional and relational operators

These operators are used in conditional statements, which we will learn more about in Week 3.

We’ll cover each of these in more depth in the next few steps, as well as looking at operator precedence (the rules that govern the order in which operators are evaluated).

© University of Reading