Incrementing shortcuts | Computer Programming | Khan Academy

Incrementing shortcuts | Computer Programming | Khan Academy


do you remember the steps of making an animation let’s review them to see how this ball animation works so first out of our outside of our draw function we declare variables whatever variables you want to use in animation in this program we declare four variables XY ball width and ball height then inside our draw function we use those variables and change them in some way in this program we use the variables to draw an ellipse passing a variable for every parameter and since we aren’t passing a variable for every parameter that means actually that we can animate every aspect of this ellipse and that’s just what we’re doing look at that so how are we changing those numbers that’s what we’re doing with these lines of code here on this line we add one to X which means that the ball moves one pixel to the right in every frame on this line we subtract two from Y which means that our ball moves two pixels up in every frame see that now on these lines we’re using some math operators that you may not have seen before this star operator is how we multiply in JavaScript and the slash operator is how we divide so this expression here is taking the variable ball width and multiplying it by the fraction 99 over a hundred basically it’s making the width be 99 percent of what it was before C and we’re doing the same thing on the next line but with ball height so each time ball height becomes 99 percent of what it was before that means you know just to show you the math if ball height starts off at 200 then on the second frame it’ll be 198 because that’s 99 percent of 200 see alright so you can see that this is the actual effect of our animation now if you look at these four lines of code you’ll notice they all follow a pattern we have a variable than an equal sign then that same variable again then some math operator and then some number and you can see that in all four lines of code this pattern is common in programming because we often take a variable and do some math operation to it and store the result in the same variable now programmers we don’t like to do more work than necessary so you know a long time ago programmers decided hey since we use this pattern so much shouldn’t there be an easier way to type it so they invented shortcuts I’ll show you instead of writing x equals x plus 1 we can write x plus equals 1 and I’ll do the same thing instead of this y equals y minus 2 we can write Y minus equals 2 same thing here we can replace this with x equal and the same thing on the next line and when we restart we can see our animation is precisely the same all we’ve done is use the shortcut to write the same expression in less code there’s also a divide equals or cut if you wanted to write instead oh look at that that’s weird alright so now that you know the shortcuts you can write less code for the same beautiful result and that is what we programmers love

6 thoughts to “Incrementing shortcuts | Computer Programming | Khan Academy”

Leave a Reply

Your email address will not be published. Required fields are marked *