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