Here’s a function you might not know about: random. It takes in two parameters: a lower bound and an upper bound and it gives you back a random number somewhere between those two bounds. So here this variable number will be somewhere between zero and one. And then we’re going to draw that number to the canvas using this text function. Those last two parameters are for the x and y coordinates of the text and we use textSize and fill to set the fill and the color of the text. So if I press restart a couple of times you can see the random numbers being generated. And you’ll see that the precision of these numbers is to three decimal places. So here’s a question: What if I only wanted to generate a number that’s either zero or one? Well, we can use this other function called round, and this takes in a number that can have as many decimals as you want, and it rounds it to the nearest integer. So I’m just going to go ahead and make a new variable called “integer” and assign it whatever round gives us. And we can also draw that “integer” to the screen with our handy-dandy text function. So text “integer”…we’ll put it maybe at 160 and 350. Nice. So this shows us that round of 0.2314 rounds down to zero. And if I put in something like 4.6, that would round me up to 5. Neat. So if I wanted to randomly generate a 0 or a 1, I could take this random decimal that we’re generating that falls between 0 and 1 and stick it right into that round function. So just like this: I’m going to grab this number and plop it down here. And now you can see that whenever we generate a number that is less than 0.5 it gets rounded down to 0 And whenever we generate a number that is greater than or equal to 0.5 it gets rounded up to 1. And maybe you can start to see the beginning of some sort of coin-flip game here where if you flip a zero, your friend gives you a dollar. And if you flip a one your friend gives you ten dollars. Great game, right? In fact, let’s go ahead and illustrate this coin-flipping game with some super realistic coins that just happen to look like really boring ellipses. Just like this: I’m going to draw an ellipse in the middle of our canvas and that’s going to be our coin. Ooh it’s covering the text, let’s scoot that up a bit. Great. And, I have this idea where if I flip a zero, I’m going to show the purple side of the coin, so to make the coin purple I can just fill it with some purple. And if I flip a one, I’ll show the…umm…yellow side of the coin. So it’ll be a purple and yellow-sided coin. And luckily, with our impressive knowledge of if-statements this is super easy. So we can just say if “integer” equals zero, so remember we use three equals signs to check for equality, then we will fill the ellipse purple. And then if “integer” is equal to one, we have a different fill function and we’ll make that one yellow. Great. And it works! Woo hoo! But let’s think about this for a second. Integer here will only ever be zero or one, right? We designed it that way. So that means that either this statement will be true Or this statement will be true Always. We’ve covered every possible case here, which means that we can start thinking about our decision making a little differently. That is, if “integer” is equal to zero, we will fill it purple, otherwise, we’ll fill yellow. So do you see how we don’t have to say anything about “integer” being one in that second case? All we have to say is if “integer” is zero, do this; otherwise, do that. And in programming, the way we say “otherwise” is “else.” So watch this: I’m just going to replace this second if-condition with the word “else” And what this means is that if the stuff inside these parenthesis is true, then run the code in these brackets. Otherwise, run the code in these brackets. And sometimes we’ll even put the “else” on the same line as that closing bracket just to remind ourselves that these two blocks of code are very, very connected. You can’t have an “else” block unless you just had an “if” block. Got it? And this will also help you remember to not put something in between the two blocks like “var y equals 0,” and that would just break everything! So don’t do that. Great. So now we know about if-else which is really good when we are deciding between two possible things to do. But what if we have more? What if I generated an integer between zero and two and then I had three possibilities: zero, one, or two? What then? Duh duh duh! To be continued.

If/Else – Part 1 | Computer Programming | Khan Academy
Tagged on:         

One thought on “If/Else – Part 1 | Computer Programming | Khan Academy

  • February 4, 2019 at 10:37 pm

    Cool!!! 😀


Leave a Reply

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