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