Return to Video

More Variables (Video Version)

  • 0:02 - 0:04
    So, we already made
    this super cool variable
  • 0:04 - 0:06
    for Winston's eye size,
    which has been really helpful
  • 0:06 - 0:09
    because it saves us a lot of typing
    when we want to change
  • 0:09 - 0:10
    the size of Winston's eyes.
  • 0:10 - 0:13
    Remember: before, we had to
    type the same number four times
  • 0:13 - 0:16
    whenever we wanted
    to change the size of his eyes.
  • 0:16 - 0:18
    But, can we use variables
    to replace numbers
  • 0:18 - 0:19
    when they're not all the same?
  • 0:19 - 0:22
    Like, what if I wanted to
    move Winston over a little bit?
  • 0:22 - 0:24
    I could change
    each of these x coordinates,
  • 0:24 - 0:26
    but they're not all the same anyways,
  • 0:26 - 0:28
    so I can't just replace them all
    with some variable.
  • 0:28 - 0:29
    But wait!
  • 0:29 - 0:32
    Once I assign a number to a variable,
    I can use that variable
  • 0:32 - 0:34
    exactly as if it were that number,
  • 0:34 - 0:37
    which means I can use variables
    and math expressions.
  • 0:37 - 0:39
    So, how about this:
    let's make a variable
  • 0:39 - 0:41
    for the x position of Winston.
  • 0:41 - 0:43
    Call it var x.
  • 0:43 - 0:45
    Simple enough.
    Okay, now let's give it
  • 0:45 - 0:48
    the value of the x coordinate
    of Winston's face.
  • 0:48 - 0:51
    His face is being drawn
    at x coordinate 200.
  • 0:51 - 0:54
    So, we're going to say var x gets 200.
  • 0:55 - 0:59
    Now I can replace this number
    with my new variable x,
  • 0:59 - 1:02
    and when I change the value of x,
    it moves Winston's face,
  • 1:02 - 1:04
    but not his eyes or his mouth.
  • 1:05 - 1:08
    So, to fix that, we're going to define
  • 1:08 - 1:10
    the position of his eyes and his mouth,
  • 1:10 - 1:14
    relative to the position of his face.
  • 1:14 - 1:17
    The x coordinates
    are going to be the centers
  • 1:17 - 1:20
    of all the other ellipses,
    so his face is being drawn at--
  • 1:20 - 1:25
    that's 200,
    and let's do his left eye first.
  • 1:25 - 1:26
    So, this ellipse is his left eye,
    and we can see
  • 1:26 - 1:30
    that it's being drawn at 150.
  • 1:30 - 1:33
    Right here, that is 150,
  • 1:33 - 1:38
    and I know that 150 is 50 less than 200.
  • 1:38 - 1:42
    So, his left eye should be
    wherever his face is, minus 50.
  • 1:42 - 1:48
    It's going to be x,
    which is the position of his face, - 50.
  • 1:48 - 1:50
    And now, if I change the value
    of this x coordinate,
  • 1:50 - 1:53
    his eye moves along with his face.
  • 1:53 - 1:55
    Let's do the other ones.
  • 1:55 - 1:57
    So, his right eye is being drawn at 300,
  • 1:57 - 2:01
    I can see, so this over here is 300.
  • 2:01 - 2:06
    And 300, I know, is 100 more than 200.
  • 2:06 - 2:10
    So, his right eye should be
    wherever his face is plus 100.
  • 2:10 - 2:13
    That's x + 100.
  • 2:13 - 2:14
    And now we just have his mouth.
  • 2:14 - 2:17
    His mouth is being drawn at 250.
  • 2:17 - 2:19
    That's over here, we've got 250,
  • 2:19 - 2:23
    and 250 is 200 plus 50.
  • 2:23 - 2:26
    So, it's going to be wherever--
    ooh, that's a 5--
  • 2:26 - 2:31
    wherever his face is,
    x + 50.
  • 2:31 - 2:33
    And now--
    I'm going to get rid of these scribbles--
  • 2:33 - 2:38
    whenever I change the value of x,
    it moves his whole face.
  • 2:38 - 2:40
    Yay!
  • 2:40 - 2:43
    Now before you go, you should know
    a couple more things about variables.
  • 2:43 - 2:46
    Once you create a variable using var x,
  • 2:46 - 2:48
    you can assign a value to it
    as many times as you want.
  • 2:48 - 2:52
    So, after giving x the value of 198,
  • 2:52 - 2:55
    I could say x gets 300,
  • 2:55 - 2:59
    or x gets 150.
  • 2:59 - 3:01
    And from then on,
    the computer would treat x
  • 3:01 - 3:03
    as if it were
    whatever we last assigned it.
  • 3:03 - 3:05
    So, in this case, 150.
  • 3:05 - 3:08
    But, you only need to type var
    once per variable.
  • 3:08 - 3:13
    If, over here, I had typed
    var x gets 150 instead,
  • 3:13 - 3:16
    that would have made
    a brand new variable named x,
  • 3:16 - 3:18
    completely destroying
    the old variable x in the process,
  • 3:18 - 3:22
    so all these variables
    are gone, in the dumpster.
  • 3:22 - 3:25
    And then, afterwards, the computer
    would only use the new variable.
  • 3:25 - 3:28
    Sometimes you can do this kind of thing
    without horrible things happening to you,
  • 3:28 - 3:30
    but I wouldn't advise it.
  • 3:30 - 3:33
    For now, I'm just going to do it once:
    var x gets 150.
  • 3:33 - 3:37
    And now, conveniently,
    I can move all of Winston off the screen,
  • 3:37 - 3:40
    so I can talk about my next point.
  • 3:40 - 3:45
    Whenever you have
    a variable equals something,
  • 3:45 - 3:47
    that variable is in a happy, safe place.
  • 3:47 - 3:51
    In fact, to the left of the equal sign
    is the only place
  • 3:51 - 3:54
    where a variable
    can truly feel like itself,
  • 3:54 - 3:56
    because that is the only place
    where the computer
  • 3:56 - 3:57
    will consider it to be a variable.
  • 3:57 - 4:00
    Anywhere else, the computer
    just pretends it's a value.
  • 4:00 - 4:02
    So even if we do something like this:
  • 4:02 - 4:06
    var x gets 10
  • 4:06 - 4:11
    and then x gets x.
  • 4:11 - 4:12
    Woo, confusing.
  • 4:12 - 4:15
    Well, over on the left,
    we've got x,
  • 4:15 - 4:17
    which is a free-thinking,
    independent variable;
  • 4:17 - 4:20
    one that can grow, can change!
  • 4:20 - 4:25
    But, on the right, it's just,
    well it's just 10.
  • 4:25 - 4:27
    So sad!
    But that means
  • 4:27 - 4:29
    we can do really useful stuff like this:
  • 4:29 - 4:34
    I can say x gets x + 1
  • 4:34 - 4:37
    So, on the left, we've got our variable
    in a happy, safe place,
  • 4:37 - 4:40
    and on the right, we have this variable,
  • 4:40 - 4:43
    which we know is basically just a number,
  • 4:43 - 4:44
    so it's just 10.
  • 4:44 - 4:47
    So, we can mentally replace
    this with x gets 10 plus 1,
  • 4:47 - 4:51
    Which just means x gets 11.
  • 4:51 - 4:54
    So, this whole line is basically
    just saying x gets 11,
  • 4:54 - 4:58
    so from now on the value of x is 11.
  • 4:58 - 5:01
    Let's see what happens if we
    bring another variable into the equation.
  • 5:01 - 5:06
    I'm going to do var x gets 10,
  • 5:06 - 5:10
    var y gets 20,
  • 5:10 - 5:12
    x gets y --
  • 5:12 - 5:14
    oh, my god--
  • 5:14 - 5:17
    and y gets 7.
  • 5:17 - 5:19
    Okay, and what I want to know is,
  • 5:19 - 5:22
    what is the value of x?
  • 5:22 - 5:24
    Let's figure this out.
  • 5:24 - 5:27
    So, at this line here,
    we're making a new variable x,
  • 5:27 - 5:29
    and assigning it the value of 10.
  • 5:29 - 5:32
    So the value of x is 10.
  • 5:32 - 5:34
    At this line, we're making
    a new variable y,
  • 5:34 - 5:36
    and giving it the value of 20.
  • 5:36 - 5:37
    We don't do anything to x,
  • 5:37 - 5:39
    so the value of x is still 10.
  • 5:39 - 5:42
    Over here, we're saying x gets y.
  • 5:42 - 5:44
    Well, that's pretty confusing,
    but if you think about it,
  • 5:44 - 5:46
    we know that only one of these variables
  • 5:46 - 5:48
    is in the happy, safe place,
  • 5:48 - 5:50
    and the other variable is being treated
  • 5:50 - 5:54
    exactly as if it were a number.
  • 5:54 - 5:56
    So over here, we can mentally replace
  • 5:56 - 5:58
    this y with its value, which is 20,
  • 5:58 - 6:02
    and we know that what this line
    really means is x gets 20.
  • 6:02 - 6:05
    So, the value of x here is 20.
  • 6:05 - 6:09
    And then, over here, we've got y
    in the happy safe place this time,
  • 6:09 - 6:10
    but it's not really that confusing,
  • 6:10 - 6:13
    because we've just got a number
    on the other side.
  • 6:13 - 6:17
    So, here's the big question:
    when I change the value of y here,
  • 6:17 - 6:21
    does that change the value of x up here?
  • 6:21 - 6:26
    And the answer:
    No! No, no, no, no, no!
  • 6:26 - 6:29
    You've got to remember, up here,
  • 6:29 - 6:32
    y is just being treated like a number.
  • 6:32 - 6:35
    The computer's going to completely ignore
    that it's a variable,
  • 6:35 - 6:36
    forget the fact that it can change,
  • 6:36 - 6:39
    and it's just going to say,
  • 6:39 - 6:42
    "Oh, y? What you really mean is 20."
  • 6:42 - 6:45
    So right here,
    we're just assigning a number to x,
  • 6:45 - 6:47
    we're not assigning a variable.
  • 6:47 - 6:50
    We're assigning the value of y,
    which is 20,
  • 6:50 - 6:54
    and that means that
    the value of x stays 20,
  • 6:54 - 6:58
    even after we change the value
    of this y variable from the last line.
  • 7:00 - 7:02
    Phew! Capisce?
  • 7:02 - 7:05
    Well, now you guys are
    total experts on variables.
  • 7:05 - 7:07
    Congratulations!
Title:
More Variables (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:
07:10

English subtitles

Revisions