02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Deeper Look: Math Operators

Things are starting to fall into place: let's revisit the math operators and add a few new tricks and shortcuts. After taking a deeper look at defining and assigning variables, now it's time to go for Math Operators. Learning various types of math operators is necessary for anyone who wants to develop as a programmer. We have seen all types of arithmetic operators such as +, -, *, /, but we need to know about one more very important operator to use in a number of programs. it's defined as modulo and is abbreviated with the percentile symbol %.

Percentile (%) Operator

If we use this symbol for a and b: a%b; it divides a by b to the closest integer quotient. The remainder is left over to be part of modulo. Hence, we can say that % is the operator which gives the reminder as output provided the quotient is an integer. For example, if we have 13%2, here the best possible division that can be performed keeping the quotient to an integer value is (13/2)=(6+remainder.) We can see that the quotient can't be 6.5 because it isn't an integer value, so it's limited to 6 with a remainder of 1. Therefore, 1 is considered to be the modulus of the equation 13%2. Your turn! What is 9%4? (9/4 = 2 with a remainder of 1: the answer is 1.)

Consider another example where the dividend is an even number: 12%2;. Here, division can be performed to the fullest extent and still the quotient is an integer. Therefore, this leaves a remainder of 0, which is the final value of 12%2. This operator is very useful not only for finding the remainder but also in various programs for finding Fibonacci series, whether a number is odd or even, etc. You will understand the use of this operator more as you move forward and start utilizing it while coding.

Incrementing and decrementing variables

Since we learned all the basic operators and their uses, we need to focus on how these operators are used in a program. We can use these operators and write the equations in the same way as we write in mathematical algebra. But there are a few shortcuts for writing math equations in programming which makes life easier for the user. Mostly we use math operators for incrementing or decrementing variables and their values, for example: i=i+1.; This will increment the value of i to i+1 before moving to the next line. When we start writing the code, we can minimize this equation and simply write i++; which has the same meaning. This is applicable for all types of operators such as i**; i- -; . Your turn! How would you easily write code to divide I by 3?

If you answered I/=3; you win that one! These are also very useful while writing the conditions for controlled loops where we can either decrement the value using i-- or increment the value using i++.

Video Transcript

00:00Now that we got the basics of variables kind of sorted out
00:03I wanted to revisit numbers and kind of operators for a second and kind of and show you all the basic all
00:12the basic
00:14mathematical
00:15Options you have in flash which we've seen plus we've seen minus we've seen division. We've seen duplication. So let's just run over these for
00:24quite quickly, so we could we could say that I
00:28equals I times
00:301 and we could say I equals I
00:34Minus 1 you could say I equals I
00:38plus 1 and
00:41We could say as well I equals I divide by 1
00:46Okay, and all these equations are updating our I every time so if in the first line our I was was three in
00:54The end of this line it still is 3 cuz 3 times 1 equals 3 and we're putting 3 back into I now in the next line
01:02We're taking I which equals 3 minus 1 which equals 2 and we're putting that into our I so from now on I equals 2
01:10And the next line we're actually
01:13Overriding our I because we didn't even put I in this line
01:16We're just saying that 1 plus 1 equals I which is actually by coincidence that equals
01:212 as well or if we want we wanted to we could have placed your I and our I right now equals 2 from the previous
01:27Line so I plus 1 would equal 3 and
01:31in the next line we're taking I
01:34Dividing it by 1 and now I remains 3 and if we would trace it out
01:38We should be getting the value the value of I again
01:42And if you run our application one more time, we're gonna see that I equals 3 and that's exactly what we expected it to do
01:54Very good. So let me just comment out our trace so it doesn't interrupt us
01:59Let's move on to another group of
02:04Another group that we've talked about we've talked about
02:07brackets inside of mathematical equations, so if I wanted to I could have I could have said that I equals I
02:15plus T
02:18times I
02:22Minus T which is completely legitimate again
02:26So what I'm doing here is I'm take um were because brackets come before duplication and we put before plus and minus basic math
02:34I plus T
02:36equals 6 I
02:39Minus T equals 0 because I and T currently are equal
02:43So 6 times 0 equals 0 so our I should equal 0 and if you run our application
02:54We'll see that I equals 0
02:57Okay, so we've talked about
03:00Times division plus minus we've talked about brackets with numbers
03:05Let's get another another another way of adding numbers together
03:10Okay, so we could add numbers together in a few different ways
03:14we could out there's a few shortcuts for example in all these scenarios we actually took I and
03:20Added to I or divided I for that
03:23There's a little shortcut in flash that enables you to add a value directly to a variable without saying
03:29Without typing that variable again, and the way you do that is a short and shorthand way of doing this line
03:34And that's we're gonna do we're gonna kind of comment this line out and see how could we in a shorthand do the exact same thing?
03:42So let's look at this so we could say we wanted to make I times 1 so we could say I
03:48times equals 1
03:51And what we've said right here is basically I equals I plus 1 times I it's exact same thing
03:59Okay
04:00Now if you wanted to do that for a minus equation, it would be the same way and but instead that would be I minus
04:061
04:08And we're always ending it with a semicolon. And if we wanted to do a plus we could have done I
04:14plus equals 1
04:17Okay, now same thing goes to if we wanted to do a division exact same thing
04:22So we've seen our basic plus minus times and division one more item that we didn't talk about that. We skipped was
04:33Moldu which is basically the left over
04:36So if we take a if we take a full
04:39If we take a number, let's say ten
04:41Let's say we take the number ten and we divide it by three the value that we would get would be three dot three
04:49three three three three three
04:53but
04:54What if we just wanted to get?
04:57The left over to find out what the left over is to do that we could use the moldu
05:02Which is a percentage sign and what would happen now is it would it would divide ten by the most amount of times it could
05:10Extract three out of it and return us the leftovers that we have out of ten
05:16Which would be one
05:18Because three times three equals nine and then there's one left
05:23Now if our if our number was not an integer if it was a floating number such as ten dot five
05:31Then we would get the value of one to five. It's completely the same

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!