If/Else Part 1 (Video Version)
-
0:01 - 0:04Here's a function
you might not know about: random. -
0:04 - 0:07It takes in two parameters:
a lower bound and an upper bound, -
0:07 - 0:11and it gives you back a random number
somewhere between those two bounds. -
0:11 - 0:14So here, this variable number will be
somewhere between zero and one. -
0:14 - 0:18Then we're going to draw that number
to the canvas using this text function. -
0:18 - 0:22Those last two parameters are
for the x and y coordinates of the text -
0:22 - 0:26and we use textSize and fill
to set the fill and the color of the text. -
0:26 - 0:28So if I press Restart a couple of times,
-
0:28 - 0:30you can see the random numbers
being generated, -
0:30 - 0:34and you'll see the precision of
these numbers is to three decimal places. -
0:34 - 0:35So here's a question--
-
0:35 - 0:38what if I only wanted to generate
a number that's either zero or one? -
0:38 - 0:42Well, we can use
this other function called round, -
0:42 - 0:46and this takes in a number that can
have as many decimals as you want, -
0:46 - 0:48and it rounds it to the nearest integer.
-
0:48 - 0:52So I'm just going to go ahead
and make a new variable called integer -
0:52 - 0:54and assign it whatever round gives us.
-
0:54 - 0:56We can also draw that integer
to the screen -
0:56 - 0:58with our handy-dandy text function.
-
0:58 - 1:00So text(integer
-
1:00 - 1:06--we'll put it maybe at 160
and 350. Nice. -
1:06 - 1:12So this shows us that round of 0.2314
rounds down to zero, -
1:12 - 1:18and if I put in something like 4.6,
that would round me up to 5. Neat. -
1:18 - 1:21So if I wanted to randomly generate
a zero or a one, -
1:21 - 1:24I can take this random decimal
that we're generating, -
1:24 - 1:27that falls between zero and one,
-
1:28 - 1:31and stick it right into
that round function. -
1:31 - 1:33So just like this:
I'm going to grab this number -
1:33 - 1:35and plop it down here.
-
1:35 - 1:37And now you can see
-
1:37 - 1:40that whenever we generate
a number that's less than 0.5, -
1:40 - 1:41it gets rounded down to zero,
-
1:41 - 1:45and whenever we generate a number
that is greater than or equal to 0.5, -
1:45 - 1:47it gets rounded up to one.
-
1:48 - 1:51Maybe you can start to see the beginning
of some sort of coin-flip game here -
1:51 - 1:55where if you flip a zero,
your friend gives you a dollar, -
1:55 - 1:58and if you flip a one,
your friend gives you ten dollars. -
1:58 - 1:59Great game, right?
-
1:59 - 2:02In fact, let's go ahead
and illustrate this coin-flipping game -
2:02 - 2:03with some super realistic coins
-
2:03 - 2:06that just happen to look
like really boring ellipses. -
2:06 - 2:11Just like this: I'm going to draw
an ellipse in the middle of our canvas -
2:11 - 2:12and that's going to be our coin.
-
2:12 - 2:15Ooh! It's covering the text.
Let's scoot that up a bit. -
2:17 - 2:21Great, and I have this idea
where, if I flip a zero, -
2:21 - 2:23I'm going to show
the purple side of the coin, -
2:23 - 2:28so to make the coin purple,
I can just fill it with some purple. -
2:29 - 2:33If I flip a 1,
I'll show the yellow side of the coin -
2:33 - 2:35so it'll be a purple
and yellow-sided coin. -
2:35 - 2:38And luckily, with our impressive
knowledge of if-statements -
2:38 - 2:39this is super easy.
-
2:39 - 2:45We can just say if integer equals zero,
-
2:45 - 2:48(remember we use
three equals signs to check for equality), -
2:48 - 2:53then we will fill the ellipse purple.
-
2:54 - 2:58Then, if integer is equal to one,
-
2:59 - 3:01we have a different fill function
-
3:02 - 3:04and we'll make that one yellow.
-
3:06 - 3:09Great. And it works! Woo hoo!
-
3:09 - 3:11But let's think about this for a second--
-
3:11 - 3:14integer here will only ever
be zero or one, right? -
3:14 - 3:16We designed it that way
-
3:16 - 3:19so that means
that either this statement will be true, -
3:19 - 3:21or this statement will be true
-
3:21 - 3:22Always.
-
3:22 - 3:25We've covered every possible case here, which means
-
3:25 - 3:28we can start thinking about
our decision-making a little differently. -
3:28 - 3:32That is, if integer is equal to zero,
we will fill it purple, -
3:33 - 3:35otherwise, we'll fill yellow.
-
3:36 - 3:38So, do you see
how we don't have to say anything -
3:38 - 3:41about integer being one
in that second case? -
3:41 - 3:42All we have to say is,
-
3:42 - 3:45"If integer is zero, do this;
otherwise, do that", -
3:45 - 3:48and, in programming,
the way we say "otherwise" is "else." -
3:48 - 3:49So watch this:
-
3:49 - 3:53I'm just going to replace this second
if-condition with the word "else" -
3:53 - 3:55and what this means is,
-
3:55 - 3:58if the stuff inside these parentheses
is true, -
3:58 - 4:00then run the code in these brackets.
-
4:00 - 4:02Otherwise, run the code in these brackets.
-
4:03 - 4:06Sometimes we'll even put the "else"
on the same line as that closing bracket -
4:06 - 4:08just to remind ourselves
-
4:08 - 4:10that these two blocks of code
are very, very connected. -
4:10 - 4:14You can't have an else block
unless you've just had an if block. -
4:14 - 4:15Got it?
-
4:15 - 4:18This will also help you remember
-
4:18 - 4:23to not put something in between
the two blocks, like var y equals zero, -
4:23 - 4:26and that would just break everything!
So don't do that. -
4:27 - 4:30Great. So now we know about if/else
which is really good -
4:30 - 4:33when we are deciding
between two possible things to do. -
4:33 - 4:34But what if we have more?
-
4:34 - 4:37What if I generated
an integer between zero and two -
4:37 - 4:40and then I had three possibilities:
zero, one, or two? What then? -
4:40 - 4:43Duh duh duh! To be continued!
- Title:
- If/Else Part 1 (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 04:45
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) | ||
JULIE BEAL edited English subtitles for If/Else Part 1 (Video Version) |