Return to Video

If/Else Part 2 (Video Version)

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

more » « less
Video Language:
English
Duration:
05:53

English subtitles

Revisions