< Return to Video

If Statements (Video Version)

  • 0:03 - 0:04
    Whoo hoo! Another animation.
  • 0:04 - 0:06
    This time we've got a ball
    moving across the screen,
  • 0:06 - 0:10
    and you guys know how this is done.
  • 0:10 - 0:11
    We've got a variable, "x,"
    that tells us the position of the ball,
  • 0:11 - 0:14
    a variable "speed" that tells us
    how far the ball moves every time,
  • 0:14 - 0:15
    and a familiar draw loop
  • 0:15 - 0:17
    where we're redrawing
    the background every time
  • 0:17 - 0:23
    setting fill colors and drawing
    the ellipse at position x,
  • 0:23 - 0:24
    and every single time,
  • 0:24 - 0:26
    we're going to change "x"
    to be the old value "x,"
  • 0:26 - 0:30
    plus the variable "speed."
  • 0:30 - 0:34
    So if I make "speed" smaller,
    you can see it moves slower.
  • 0:34 - 0:39
    I can make it negative,
    so we move backwords,
  • 0:39 - 0:39
    or I can make it zero,
    and the ball would stop moving.
  • 0:39 - 0:40
    But as long as speed is not zero,
  • 0:41 - 0:44
    eventually the ball
    is going to go off the screen.
  • 0:44 - 0:46
    And I can always press
    the restart button to bring it back,
  • 0:46 - 0:48
    but you know, after awhile,
    that gets old.
  • 0:48 - 0:53
    You restart, and restart...
  • 0:53 - 0:56
    So, how about this? When the ball reaches
    the right edge of the screen,
  • 0:56 - 0:59
    instead of going off the edge
    of the screen like it's doing now,
  • 0:59 - 1:00
    I want it to turn around.
  • 1:00 - 1:05
    And, I know how to turn the ball around,
    I can just say, "speed" gets negative 5.
  • 1:05 - 1:10
    If I make the speed negative,
    then the ball would go backwards.
  • 1:10 - 1:13
    But here is the problem.
  • 1:13 - 1:18
    I only want to change the speed
    if the ball has reached the right edge.
  • 1:18 - 1:21
    Hmmm... so I think I already said it.
  • 1:21 - 1:26
    I only want to change the speed,
    if the ball has reached the right edge.
  • 1:26 - 1:30
    I think this calls for an "if" statement.
  • 1:30 - 1:31
    Up until now, we've only been giving
  • 1:31 - 1:33
    the computer commands
    to run no matter what.
  • 1:33 - 1:35
    If statements are a way to say,
  • 1:35 - 1:37
    "Hey dude, I want you to run this code,
  • 1:37 - 1:40
    but only under these
    specific circumstances."
  • 1:40 - 1:43
    "So only change the speed
    if the ball has reached the right edge."
  • 1:43 - 1:46
    And here's how it looks in code.
  • 1:46 - 1:47
    All you do is type "IF"
  • 1:47 - 1:51
    and then a pair of parentheses,
    and then a pair of brackets.
  • 1:51 - 1:54
    Inside the parentheses
    we're going to put the condition.
  • 1:54 - 1:57
    Inside the brackets
    we're going to put the code to run.
  • 1:57 - 2:00
    So the way it works is,
    "If this condition is true,
  • 2:00 - 2:03
    then run this code;
    otherwise, don't bother."
  • 2:03 - 2:07
    So in our case, the condition
    is the ball reaching the right edge.
  • 2:07 - 2:10
    How do we know if the ball
    has reached the right edge?
  • 2:10 - 2:16
    We've got this variable "x"
    that tells us where the ball is,
  • 2:16 - 2:23
    and I know that the edge of the canvas
    x position 400, so let's see.
  • 2:23 - 2:25
    When "x" is greater than 400,
  • 2:25 - 2:30
    then we know that the ball has gone
    past the right edge a little bit.
  • 2:30 - 2:33
    So let's see how that works.
  • 2:33 - 2:39
    And the code to run, we already said
    before, we're just going to change speed.
  • 2:39 - 2:45
    Speed gets negative five. We're going
    to press restart and see what happens.
  • 2:45 - 2:48
    So this time, when the ball reaches
    the right edge, it bounces! Yaaaay!
  • 2:48 - 2:51
    And then it keeps going off the screen.
  • 2:51 - 2:52
    But that's OK, because we can keep doing
    the same thing on the other side.
  • 2:52 - 2:53
    So this time, we want to check
    if the ball has reached the left edge.
  • 2:53 - 2:58
    And that's when x is less than zero,
    and what we want to do
  • 2:58 - 3:03
    is make speed positive again,
    so speed gets 5.
  • 3:03 - 3:08
    Alright, and then we're going
    to press restart, and this time...
  • 3:08 - 3:12
    boing...
  • 3:12 - 3:14
    boing... boing...
  • 3:14 - 3:17
    yay! It works.
  • 3:17 - 3:19
    And I know we're checking to see
    if the ball has gone past the edges
  • 3:19 - 3:21
    but it feels like it's going
    a little too far past the edges.
  • 3:22 - 3:24
    And if you remember,
    these two parameters control
  • 3:24 - 3:25
    where the center of the ellipse is drawn.
  • 3:26 - 3:32
    So right now, by the time
    the center reaches the edge,
  • 3:32 - 3:37
    half the ellipse has already gone
    past the edge.
  • 3:37 - 3:42
    So if we want to fix that,
    we just stop the ellipse a little sooner.
  • 3:42 - 3:47
    So if our edge is here, at 400 and we want
    to stop our ellipse when it gets here,
  • 3:47 - 3:53
    and we can see from the function
    call that the ellipse has width 50,
  • 3:53 - 3:59
    so that means from the center
    to the edge, that's going to be 25.
  • 3:59 - 4:05
    So we want to stop it
    when the center reaches 375,
  • 4:05 - 4:07
    that's 400 minus 25.
  • 4:07 - 4:09
    So instead of checking
    for x greater than 400,
  • 4:09 - 4:11
    we're going to check for
    x greater than 375.
  • 4:11 - 4:13
    Instead of checking for x less than 0.
  • 4:13 - 4:16
    I'll check for x less than 25.
  • 4:16 - 4:20
    And now it's perfect! Yay!
    Look at at that ball bounce.
Title:
If Statements (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:
04:23

English subtitles

Revisions