# Lesson 11 - Variables and Operations Continued

Tutorial Series: Introduction to Unity with C# Series

## Transcript

You can perform the calculation and store the result all in the same line as well. You can go like this: 2 + 2 goes into x; the result of 2 + 2 goes into variable x. Then, just input x as the argument for Debug.Log(). Again, just to show there is nothing up my sleeve or I am wrong, which is distinctly possible, there you go, 4 just as we saw before. We call a variable a variable because the information it stores is variable.

Let's demonstrate this. I don't think we have to comment this out because it is... We will just write in some new stuff. So, int x = 2, x = 5, x = 22. The question is, what value does x hold? Well, execution occurs in order, so it holds 2, then on the next line it holds 5, then finally it holds 22. Let's actually output this to see what we get when we put in input x for Debug.Log(). What do you think it is going to be? 22. Were you expecting that? If you were expecting that, it is probably because you realize that when this method is called, which again, it is called in the Start() method which happens when you first run your game, it is executing this entire code block in sequence, the MyMethod() code instruction block. When that executes in full, we are only outputting the value that is stored in x right after it is being assigned 22. In memory, we are assigning 2 to the x variable, and we are assigning 5, and then we are assigning 22; one right after the other, almost immediately. There is no real space between those assignments. It is happening so fast. It then outputs to debug log only as 22. It no longer holds those previous values. You will also notice that we don't have to declare the type more than once. We only declare it once. X is an int. It doesn't change. All we have changed is the assignment.

Let's look at few other mathematical operations that are common; that you are used to seeing, and you will be used to using in Unity. Let's comment that out, and do this, actually keep this. We will declare it, not assign it. Actually, we will assign it; assign it 2, and then we will do this: x times x. x divided by x. There you go. Let's run that and see what we get. There we are, 4 is output first, then 1, and then 0. Is that what we expect? Well, x = 2; x times x, 2 times 2 is equal to 4, so we first get 4. Then, that is divided by, so 2 divided by 2 is 1, and 2 take away 2 is 0. This makes sense. You get a little sneak preview on the code reuse we get from calling methods multiple times. We are using that same instruction set one after the other, not having to redefine a totally different instruction set. That is just a quick sneak preview on some of the values of using methods, and being able to reuse a code, but that not really what we are interested in right now.

That is multiplication, division, subtraction, and we also saw addition previously. You maybe wonder about mathematical order of operation. What occurs first, multiplication, division, subtraction, addition? The order in which these things occur alter the results. It is the same as in mathematics, multiplication and division before subtraction or addition. To be explicit, you could create your own precedence, and sometimes it is necessary. Often, I prefer to wrap up my operations in parenthesis, even when it is obvious that the correct operation will occur; multiplication will occur before addition. We can do this here; we will comment this out. I don't know let's just give this... For whatever reason, I want to change that to 5. We will write in x*x. We want to do that first, then minus x. In this case I don't have to wrap it up in parenthesis, because multiplication will always occur before subtraction.

The result will be same even without parenthesis, but sometimes when you are coding, adding those parenthesis, it could either make your code more readable or more ugly depending on your opinion on such matters. Here now when we run that...Sorry, I forgot. That was silly of me. I didn't put it in the Debug.Log(). I assigned that calculation to x, but I didn't put it in the Debug.Log(). There we go. Now we can actually see it, which is useful. The result of that calculation is 20. So, 5 times 5 is 25, minus 5 is 20. It doesn't matter if we use those parenthesis or not. If I run this now, yep still get 20. It made it maybe look a little bit cleaner. What if you wanted to do the subtraction first? That will render a very different result. 0, right? Because 5 take away 5 is 0, and now 5 times 0 is of course going to be 0. That is how you can use the parenthesis as creating your own order of operation.

You can use the same principle throughout your code. It doesn't just have to be mathematical operations. You will see them in conditionals, and "if/then" statements, and so on when we work with those later. This code might look a little bit confusing. You might be wondering, “What is the value of "x in this calculation at any given time?” X is the value, in the calculation portion, the right part of the, on the right side of the equals sign. It is the value before it is assigned back to x. We know that the value is 5, then this calculation performs given that value. Then, at the end of that calculation, it gets assigned back into x; x then becomes that value. You can often, and I often look at variables from the right to the left. I kind of read them from the right to the left. I read their value or whatever sort of operation is being performed on the right side of the equals sign, and then as I said earlier, it goes into, sometimes I have heard people refer to it not as the equals sign, but as the ‘goes into operator.’ It goes into that container, x in this case.

That is the basics of working with ints. You will use them whenever you need simply a whole number, and nothing more. You might recall we used ints for counters in our prototype project. Ints are great for basic counters. You will often be using them just for that. Sometimes you want to have a fraction. You want to have, in other words, a number with a decimal place. To represent a numerical value with a decimal place we need a different type altogether. We will actually be using the float type predominantly, probably exclusively, in our coding with Unity, to represent decimal places. We will go into that in the next video. See you there.