Return to Video

Using Math Expressions (Video Version)

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

more » « less
Video Language:
English
Duration:
02:56

English subtitles

Revisions