< Return to Video

Planning with pseudo-code | Intro to JS: Drawing & Animation | Computer Programming | Khan Academy

  • 0:01 - 0:03
    - When we write a program,
    we are figuring out a way
  • 0:03 - 0:06
    to turn the brilliant ideas
    in our head into actual code.
  • 0:07 - 0:09
    Here I want to talk about a technique
  • 0:09 - 0:11
    that many programmers use to do that
  • 0:11 - 0:13
    and it's what we call "pseudocode".
  • 0:14 - 0:17
    Now pseudocode is probably a
    word you've never heard before
  • 0:17 - 0:21
    but basically, it's code
    that looks a lot like English
  • 0:22 - 0:24
    or really, whatever language
    you like to talk in.
  • 0:26 - 0:27
    Well, okay, that may not make sense
  • 0:27 - 0:29
    so let's talk through an actual example.
  • 0:30 - 0:33
    So let's say that I want to
    draw a nice, symmetrical face.
  • 0:34 - 0:36
    So I might start by looking
    at myself in the mirror
  • 0:36 - 0:38
    and maybe sketching it out
  • 0:38 - 0:40
    and seeing, okay well
    I have this oval face,
  • 0:41 - 0:44
    I have two eyes and
    they're about at this level
  • 0:44 - 0:47
    and this is where the
    center of the face is
  • 0:47 - 0:51
    and now I have an idea for what
    I want my face to look like.
  • 0:52 - 0:55
    So, I'll start writing it in pseudocode.
  • 0:56 - 0:58
    So let's see, the first
    thing we want to do is
  • 0:58 - 1:01
    draw the face which is
    an oval in the center.
  • 1:03 - 1:05
    Then we'd want to draw the two eyes,
  • 1:06 - 1:11
    which are two ovals, about
    two thirds up the face
  • 1:12 - 1:15
    and maybe one fifth the size of the face.
  • 1:15 - 1:18
    Not exact math there, just
    looking at my own face.
  • 1:18 - 1:21
    And then we draw the mouth which is a line
  • 1:21 - 1:24
    going halfway across the face
  • 1:24 - 1:28
    and maybe one third of the way up.
  • 1:29 - 1:32
    So, notice how I write
    my pseudocode as comments
  • 1:33 - 1:35
    by starting each line
    with the two slashes here.
  • 1:37 - 1:40
    That way I can write my
    pseudocode in the program itself
  • 1:40 - 1:43
    and not have to worry about
    getting any syntax errors
  • 1:44 - 1:46
    because the program will ignore comments.
  • 1:47 - 1:49
    Now that I've written this in pseudocode,
  • 1:49 - 1:51
    I can spend the time to turn each
  • 1:51 - 1:55
    of these lines of pseudocode
    into actual bits of code.
  • 1:55 - 1:56
    Alright, so let's see.
  • 1:56 - 1:59
    For the face, an oval in the center.
  • 1:59 - 2:01
    Well that I'll use the
    ellipse function for
  • 2:01 - 2:05
    and I'll figure out the
    center of the screen here
  • 2:05 - 2:08
    and figure out a nice size.
  • 2:09 - 2:10
    Okay, that looks good.
  • 2:10 - 2:13
    For the eyes, once again
    those are ellipses.
  • 2:13 - 2:16
    Everything on my face is
    an ellipse, I'm very round.
  • 2:17 - 2:20
    It's going to be, let's
    see, we'll do some math here
  • 2:20 - 2:24
    to get the eyes at a nice place
  • 2:24 - 2:27
    and make them about fit the size.
  • 2:27 - 2:29
    Okay, that looks good for the first eye,
  • 2:29 - 2:31
    I'll just copy and paste
    to make the next eye.
  • 2:32 - 2:33
    Great.
  • 2:33 - 2:35
    Now, I can even leave my pseudocode
  • 2:35 - 2:38
    for a friend to implement
    and they probably could
  • 2:38 - 2:40
    because I've given this
    really nice description.
  • 2:41 - 2:45
    So I'm going to do that here
    because we're friends, right?
  • 2:46 - 2:49
    So you might think this is silly,
  • 2:49 - 2:50
    why are we going through the effort
  • 2:50 - 2:54
    to write our program twice,
    first in human language
  • 2:54 - 2:55
    and then in program language?
  • 2:55 - 2:58
    Well, this example was pretty simple
  • 2:58 - 3:01
    but pretty soon you'll be
    building more complex programs
  • 3:01 - 3:03
    and it may be hard for you
    to keep the whole program
  • 3:03 - 3:06
    in your head before coding it,
  • 3:06 - 3:08
    so what I usually do is
    write the general idea
  • 3:08 - 3:12
    in pseudocode first and
    then I'll spend more time
  • 3:12 - 3:15
    on the details of each part of that idea,
  • 3:15 - 3:19
    converting each line of
    pseudocode into real code.
  • 3:20 - 3:23
    I think you'll find once
    you start using pseudocode,
  • 3:23 - 3:24
    you'll do it more and more.
  • 3:24 - 3:27
    Try it in your next program and see.
Title:
Planning with pseudo-code | Intro to JS: Drawing & Animation | Computer Programming | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
03:27

English subtitles

Revisions