If/Else Part 2 (Video Version)
-
0:01 - 0:04Here's where we left off
with our coin-flipping game. -
0:04 - 0:07We started out by generating
a random number between 0 and 1, -
0:07 - 0:09and then rounding that number
to the nearest integer. -
0:09 - 0:12So this gave us a number
that was either 0 or 1. -
0:12 - 0:16And then we illustrated
our super-realistic coin with this ellipse -
0:16 - 0:19that was either purple or yellow,
depending on what the number was. -
0:19 - 0:21And here's the code where we did that.
-
0:21 - 0:24So we said if integer is equal to 0
set the fill code to be purple, -
0:24 - 0:26otherwise, set it to yellow.
-
0:26 - 0:28And then we draw the coin down here.
-
0:28 - 0:31But now I've decided
that actually my coin has 3 sides. -
0:31 - 0:32Yep, a 3-sided coin.
-
0:32 - 0:34So let's add that third side.
-
0:34 - 0:37I'm going to start by making
a number between 0 and 2, -
0:37 - 0:42so after we round that, it will give me
an integer that's either 0, 1, or 2. -
0:42 - 0:46But if we look at our drawing code,
we're only accounting for 2 cases here. -
0:46 - 0:49So if integer is 0, make it purple,
otherwise, make it yellow. -
0:49 - 0:52But now, otherwise can mean
either 1 or 2. -
0:52 - 0:55But wait, what if I did the same thing
that we did before, -
0:55 - 0:57but inside this else block?
-
0:57 - 1:01So I'm just going to say:
if integer is equal to 1, -
1:01 - 1:05then set the fill color to be yellow,
like it was before, -
1:06 - 1:10else, we will set the fill color
to be red. -
1:10 - 1:11Red's pretty good.
-
1:11 - 1:13So what this means is,
-
1:13 - 1:15if integer is equal to 0,
set the color to be purple, -
1:15 - 1:19otherwise, if it's 1,
we will make it yellow; -
1:19 - 1:20otherwise, that is,
-
1:20 - 1:25if it wasn't 0, it wasn't 1,
it must be 2, we'll make the color red. -
1:25 - 1:29I press restart a bunch of times.
It works! Woohoo! -
1:29 - 1:31So then if I wanted
to add more sides to my coin, -
1:31 - 1:36I can go up here, make it 0 to 3,
and then go deeper into this else block -
1:36 - 1:38and add more if-else's
and so on and so forth, -
1:38 - 1:40until I have a bazillion nested blocks.
-
1:40 - 1:45And only then will I realize this code
is disgusting! I mean gross! -
1:45 - 1:49Code is supposed to be easy to read
and pretty, not as ugly as possible. -
1:49 - 1:51So here's what we're going to do:
-
1:51 - 1:53Whenever you have
just a single if statement -
1:53 - 1:56or an if-else statement
inside an else block, -
1:56 - 1:59so that means we're not doing
anything else outside of these blocks, -
1:59 - 2:01we're not setting the stroke color,
-
2:01 - 2:05we don't have anymore
if statements, nothing. -
2:05 - 2:08All we have is that one if statement,
and maybe it comes with an else block. -
2:08 - 2:13Then we can actually combine
this condition with the line before -
2:13 - 2:17and say, else if integer is equal to 1,
then set the fill color to yellow. -
2:17 - 2:20And then this last else block
isn't nested anywhere, -
2:20 - 2:22just comes at the very end all by itself.
-
2:22 - 2:25Great! So now what this means is
-
2:25 - 2:27if integer is equal to 0,
set the color to purple, -
2:27 - 2:30otherwise, if integer is equal to 1,
set it to yellow, -
2:30 - 2:34otherwise, so if both of these were false,
then set it to red. -
2:34 - 2:38I press restart a bunch of times,
you can see it still works. -
2:38 - 2:42Great! And the cool thing about this is
we can have as many else-if's as we want, -
2:42 - 2:45which makes it really easy for me
to add more sides to my coin. -
2:45 - 2:48So let's do that now: I'm going to make
a number between 0 and 3, -
2:48 - 2:52and then just add one more else block
-
2:52 - 2:56that says else if integer is equal to 2
-
2:56 - 3:00we'll set the fill color to... well,
it's red before so we can keep it red. -
3:00 - 3:03And then this last else block will be
for when an integer is equal to 3, -
3:03 - 3:08it will make it blue. Great!
-
3:08 - 3:10All right.
-
3:10 - 3:13So in order to do this, you always have
to start with an if statement, -
3:13 - 3:17and then you can have
as many else-if's as you want, -
3:17 - 3:19and then this last guy is optional.
-
3:19 - 3:20We can actually do without it,
-
3:20 - 3:22and then it's possible
that all of these will be false, -
3:22 - 3:24so none of these blocks gets executed.
-
3:24 - 3:27But as long as we have that there,
-
3:27 - 3:30then exactly one of these blocks
will be run. -
3:30 - 3:34Cool? Now this is a case
where it doesn't really matter -
3:34 - 3:37if you say if else-if else-if else,
-
3:37 - 3:42or just use plain old if statements
like we had in the beginning, -
3:42 - 3:46so if integer is equal to 3.
-
3:46 - 3:48And that's because it's never possible
-
3:48 - 3:51for integer to equal 0,
and then also 1, 2, or 3. -
3:51 - 3:54Great. So these conditions
are all mutually exclusive. -
3:54 - 3:57Only one of these blocks
will ever get run anyway. -
3:57 - 3:58But that's not always the case.
-
3:58 - 4:02What if we try to make a game
like this without rounding to an integer? -
4:02 - 4:04So I'm going to get rid of this part
where we round it, -
4:04 - 4:07and the other part
where we draw it to the screen. -
4:07 - 4:09And let's make up
some new rules to my game. -
4:09 - 4:13So I'm still generating
a number between 0 and 3, -
4:13 - 4:15put some more tick marks
on our number line. -
4:17 - 4:23So let's say that if the number falls
between 0 and 1, we will make it purple. -
4:23 - 4:27Oh, that doesn't look like a 'p' at all.
You get the idea. OK. -
4:27 - 4:31And then if it's between 1 and 2,
we can make it yellow. -
4:31 - 4:36And if it's between 2 and 3,
we will make it red. -
4:36 - 4:39Great. So let's see how we can do that
with if and else-if's. -
4:39 - 4:44So I can start out by saying
if number is less than 1, -
4:44 - 4:46so if it's less than 1,
-
4:46 - 4:50and I know it's between 0 and 3,
then it must be in this range, -
4:51 - 4:53then I'll set the fill color to be purple.
-
4:56 - 5:00Otherwise, if the number is less than 2,
-
5:01 - 5:05I can set the fill color
to be yellow, 255... -
5:07 - 5:12And otherwise,
I'll set the fill to be red. -
5:13 - 5:16Great! And it works just as intended.
-
5:16 - 5:20So if the number was less than 1,
then again, it's in this range. -
5:20 - 5:24Otherwise it's greater than or equal to 1,
but it's also less than 2, -
5:24 - 5:27so that puts it in this range,
so we're going to set the color to yellow. -
5:27 - 5:30Otherwise, it must be greater
than or equal to 2, -
5:30 - 5:32so it's going to fall in that range.
-
5:32 - 5:33And here's a case
-
5:33 - 5:36where we couldn't just say
if number is less than 2, -
5:36 - 5:40and if number is less than 3,
-
5:40 - 5:43because if number is less than 1,
then it's definitely is less than 2 -
5:43 - 5:45and it's also going to be less than 3.
-
5:45 - 5:47So our coin
is always going to end up red. -
5:47 - 5:51And that's why in this case,
it's really helpful to have else-if.
- Title:
- If/Else Part 2 (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:
- 05:53
Denise RQ edited English subtitles for If/Else Part 2 (Video Version) | ||
Denise RQ edited English subtitles for If/Else Part 2 (Video Version) | ||
Denise RQ edited English subtitles for If/Else Part 2 (Video Version) | ||
Denise RQ edited English subtitles for If/Else Part 2 (Video Version) | ||
Josephine McGuire edited English subtitles for If/Else Part 2 (Video Version) |