< Return to Video

PseudoCode (Video Version)

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

English subtitles

Revisions