Intro To Variables (Video Version)
-
0:01 - 0:04So I've got my good friend Winston here
to help us out with this one. -
0:04 - 0:06And I know he already looks
impossibly awesome, -
0:06 - 0:09but I think I want to change
his eye size a little bit. -
0:09 - 0:11So here is where I draw his eyes.
-
0:11 - 0:13You can see we set the fill color,
and then drop two ellipses. -
0:13 - 0:15And if I want to make
this ellipse smaller, -
0:15 - 0:18I can change the width,
but then I also want it to be round, -
0:18 - 0:20so I'm going to change the height
to be the same thing. -
0:20 - 0:23And, well I want the eyes
to be the same size, -
0:23 - 0:25so we should change the width
and the height of this eye. -
0:25 - 0:28No, now the eyes are a little too small,
so I should change them back. -
0:29 - 0:31Wouldn't I be great if I could
just change one number, -
0:31 - 0:33and have both eyes change?
-
0:33 - 0:36And here is where variables come in.
-
0:37 - 0:41Now, a variable is just like a name
or a placeholder for something else. -
0:41 - 0:45I like to think of a variable
as a big bucket with a name on it. -
0:45 - 0:46You can put anything
you want in the bucket, -
0:46 - 0:48but the name stays the same.
-
0:48 - 0:52We call the thing inside the bucket
the value of the variable. -
0:54 - 0:56So let's start out by actually
making a variable. -
0:56 - 0:58I'm going to do it all the way up here.
-
0:58 - 1:00All you do is type var,
which stands for variable, -
1:00 - 1:02Space, and then
the name of the variable, -
1:02 - 1:04which should describe
what the variable will hold. -
1:04 - 1:06So I'm going to call this one eyeSize.
-
1:06 - 1:08And you can pretty much
name it whatever you want, -
1:08 - 1:10but you're not allowed to have
spaces between words. -
1:10 - 1:12And of course,
don't forget that semicolon! -
1:12 - 1:14So now I've made
a variable called eyeSize, -
1:14 - 1:17because it's going to hold
the size of Winston's eyes. -
1:17 - 1:20But so far this variable
doesn't have a value yet. -
1:20 - 1:23To give it one, we just say eyeSize,
-
1:23 - 1:27and then an =, and then 20; .
-
1:28 - 1:29Okay, now see that equal sign?
-
1:29 - 1:32Forget whatever your math teacher
taught you about it. -
1:32 - 1:35That equal sign does not mean "equals,"
it means assignment. -
1:35 - 1:41This means we're assigning
the value 20 to the variable eyeSize. -
1:42 - 1:44In other words,
we're putting the number 20 -
1:44 - 1:46into the bucket called, eyeSize.
-
1:46 - 1:48And remember how in math class
you can say stuff like, -
1:48 - 1:52x = 3, and 3 = x,
-
1:52 - 1:54and it all means the same thing,
because duuh, they're equal? -
1:54 - 1:56Well, you can't do that here, either.
-
1:56 - 1:58The thing on the left-hand side
of the equal sign -
1:58 - 2:00is always the variable.
-
2:00 - 2:03And the thing on the right-hand side
of the equal sign is always the value -
2:03 - 2:05that you are assigning to the variable.
-
2:06 - 2:08A good way to help you
remember which side is what -
2:08 - 2:10is while you're coding
and talking out loud to yourself, -
2:10 - 2:11like every cool programmer does,
-
2:11 - 2:15if you hit an equal sign
don't say "equals," say "gets." -
2:15 - 2:19So this becomes, eyeSize gets 20.
-
2:20 - 2:22And now, whenever I use
eyeSize in my program, -
2:22 - 2:25the computer is going to be like,
"Oh yeah, that's a variable, -
2:25 - 2:28I know what she really means
is this value 20." -
2:28 - 2:31So check it out--
I'm just going to copy this, -
2:31 - 2:34and then replace these four numbers
-
2:35 - 2:37with my new variable eyeSize,
-
2:38 - 2:40and Voila!
-
2:40 - 2:44Winston's eyes are now both
perfectly round and the same size, -
2:44 - 2:47and if I want to change
the value of both eyes-- -
2:47 - 2:48or the size of both eyes,
-
2:48 - 2:51I can just change the value
of this one variable. -
2:51 - 2:53Aah that's so cool!
-
2:54 - 2:56Okay, couple of last notes.
-
2:56 - 2:59Up here we made
a new variable called eyeSize. -
2:59 - 3:01And here we gave it a value of 33.
-
3:01 - 3:03We can actually do that all in one step
-
3:03 - 3:06by saying var eyeSize,
that's the first step, -
3:06 - 3:09gets 33,
that's the second step. -
3:09 - 3:11And if I delete these two lines,
-
3:11 - 3:13you can see everything still works.
-
3:13 - 3:17Also remember that the computer
reads your code from top to bottom -
3:17 - 3:19so the only reason it knew
what eyeSize was down here, -
3:19 - 3:22was because we already
defined it up here. -
3:22 - 3:24If I had put it down here instead,
-
3:25 - 3:28then once we got to this line of code,
the computer's going to be like, -
3:28 - 3:31"eyeSize, what the heck is eyeSize?
I don't know what that is." -
3:31 - 3:34In fact, here we get an error that says
eyeSize is not defined. -
3:34 - 3:36And maybe you're thinking to yourself,
-
3:36 - 3:37"Yes, I did define it; it's right here!"
-
3:37 - 3:40But the computer's not smart,
and doesn't get that. -
3:40 - 3:43So let's just move it back up to the top.
-
3:44 - 3:46And you've got to always make sure
-
3:46 - 3:48that you define your variable
before you try to use it. -
3:49 - 3:52And now you know
about variables! Yaaay!
- Title:
- Intro To 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/ - Video Language:
- English
- Duration:
- 03:53
Josh Kline edited English subtitles for Intro To Variables (Video Version) | ||
James Irwin edited English subtitles for Intro To Variables (Video Version) | ||
James Irwin edited English subtitles for Intro To Variables (Video Version) | ||
James Irwin edited English subtitles for Intro To Variables (Video Version) | ||
James Irwin edited English subtitles for Intro To Variables (Video Version) | ||
digitaluniverse edited English subtitles for Intro To Variables (Video Version) | ||
Mary Beth Strawn edited English subtitles for Intro To Variables (Video Version) | ||
Retired user edited English subtitles for Intro To Variables (Video Version) |