Using Math Expressions (Video Version)
-
0:03 - 0:05point() is a function that takes in
-
0:05 - 0:06an x coordinate and a y coordinate
-
0:06 - 0:08and draws a point on the screen.
-
0:08 - 0:11So right now it's drawing a point
that's only one pixel big, -
0:11 - 0:13which you might be able
to see if you squint. -
0:13 - 0:15If you want to make the point bigger,
-
0:15 - 0:17you can call this function strokeWeight()
-
0:17 - 0:20and pass it a size, let's say 10.
-
0:20 - 0:23Aha! That's better. So now let's talk
about some math expressions. -
0:23 - 0:26A math expression is any string
of numbers and symbols -
0:26 - 0:27that evaluates to some other number.
-
0:27 - 0:31For example, 1 + 1 is
a math expression that evaluates to 2. -
0:31 - 0:33A more complicated expression might be
-
0:33 - 0:384 + 3 * 8 / (2 + 2),
-
0:38 - 0:41which evaluates to -- let's see,
-
0:41 - 0:44two... never mind what it evaluates to.
-
0:44 - 0:46The point is, while you're coding,
-
0:46 - 0:48any place where you would normally use
-
0:48 - 0:50a number, you can actually substitute
-
0:50 - 0:52one of these math expressions.
-
0:52 - 0:54So for example, this point() function
-
0:54 - 0:56here accepts two numbers as parameters.
-
0:56 - 0:57Instead of 100, I could just as well say
-
0:57 - 1:02"50 + 50," or maybe "25 * 4,"
-
1:03 - 1:05and the computer would evaluate
those expressions for us -
1:05 - 1:08before drawing the point at that position.
-
1:08 - 1:10The basic order of operations
you learned about in school -
1:10 - 1:12applies to coding as well.
-
1:12 - 1:15Remember PEMDAS?
-
1:16 - 1:18Well, maybe you don't, but this is
-
1:18 - 1:21the acronym I learned
in school and it stands for -
1:21 - 1:23Parentheses, Exponent, Multiplication,
-
1:23 - 1:25Division, Addition, Subtraction.
-
1:25 - 1:28And there's actually no symbol
for exponent in this language, -
1:28 - 1:32so in our case, it's just PMDAS.
-
1:33 - 1:35This means that everything
-
1:35 - 1:37inside parentheses are evaluated first,
-
1:37 - 1:38and then multiplication and division
-
1:38 - 1:40are evaluated next from left to right,
-
1:40 - 1:42and finally we do addition
-
1:42 - 1:44and subtraction, also from left to right.
-
1:44 - 1:47So let's just go through a couple
more examples and see -
1:47 - 1:48what kind of things we can do.
-
1:48 - 1:54Let's draw a rectangle,
maybe at 100 * (4 - 2), -
1:55 - 1:57and then, y coordinate -- give it
-
1:57 - 2:03(300 + 50) / 7 + 80,
-
2:04 - 2:10and maybe a width of 100 + 50,
-
2:11 - 2:15and then a height of 60 * 3,
how about that? -
2:15 - 2:17Whew! That is a fat rectangle.
-
2:17 - 2:21Maybe I wanna set the strokeWeight()
-
2:21 - 2:24to something smaller like 10 - 7, yeah?
-
2:24 - 2:27I can also draw an ellipse()
-
2:27 - 2:32at 130 + 50
-
2:32 - 2:35and 100 + 60,
-
2:35 - 2:38maybe 40 * 3,
-
2:38 - 2:42and let's do 200 - 17, yeah?
-
2:43 - 2:45Oh, and would you look at that!
-
2:45 - 2:50Kinda looks like... an elephant!
-
2:51 - 2:54Well that's it, now you know
about math expressions.
- Title:
- Using Math Expressions (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:
- 02:56
Kirstin Cosper edited English subtitles for Using Math Expressions (Video Version) | ||
Kirstin Cosper edited English subtitles for Using Math Expressions (Video Version) | ||
odduse_of_language edited English subtitles for Using Math Expressions (Video Version) | ||
kramtark edited English subtitles for Using Math Expressions (Video Version) | ||
kramtark edited English subtitles for Using Math Expressions (Video Version) | ||
nirmit.leo edited English subtitles for Using Math Expressions (Video Version) |