Download "Harvard CS50 – Full Computer Science University Course"

Download this video with UDL Client
installarrow down icon
  • Video mp4 HD+ with sound
  • Mp3 in the best quality
  • Any size files
"videoThumbnail Harvard CS50 – Full Computer Science University Course
play-icon
Please wait
|

Please wait

Video tags
|

Video tags

harvard
cs50
full
computer
science
university
course
Subtitles
|

Subtitles

00:00:04
CS50 is considered by many to be one of the best computer science courses in the world.
00:00:09
This is a Harvard University course taught by Dr. David Malan
00:00:13
and we are proud to bring it to the freeCodeCamp YouTube channel.
00:00:17
Throughout a series of lectures, Dr. Malan will teach you how to think algorithmically and solve problems efficiently.
00:00:23
And make sure to check the description for a lot of extra resources that go along with the course.
00:00:27
[MUSIC PLAYING]
00:01:45
DAVID MALAN: All right, this is CS50, Harvard University's introduction
00:01:50
to the intellectual enterprises of computer science
00:01:52
and the art of programming, back here on campus in beautiful Sanders Theatre
00:01:56
for the first time in quite a while.
00:01:58
So welcome to the class.
00:02:01
My name is David--
00:02:02
OK.
00:02:03
[CHEERING AND APPLAUSE]
00:02:11
So my name is David Malan.
00:02:13
And I took this class myself some time ago, but almost didn't.
00:02:16
It was sophomore fall and I was sitting in on the class.
00:02:20
And I was a little curious but, eh, it didn't really
00:02:22
feel like the field for me.
00:02:24
I was definitely a computer person, but computer science
00:02:26
felt like something altogether.
00:02:28
And I only got up the nerve to take the class,
00:02:30
ultimately, because the professor at the time, Brian Kernighan,
00:02:32
allowed me to take the class pass/fail, initially.
00:02:35
And that is what made all the difference.
00:02:37
I quickly found that computer science is not just
00:02:39
about programming and working in isolation on your computer.
00:02:42
It's really about problem solving more generally.
00:02:45
And there was something about homework, frankly,
00:02:48
that was, like, actually fun for perhaps the first time in, what, 19 years.
00:02:51
And there was something about this ability
00:02:53
that I discovered, along with all of my classmates,
00:02:56
to actually create something and bring a computer to life to solve a problem,
00:03:00
and sort of bring to bear something that I'd been using every day
00:03:03
but didn't really know how to harness, that's been gratifying ever since,
00:03:06
and definitely challenging and frustrating.
00:03:08
Like, to this day, all these years later,
00:03:10
you're going to run up against mistakes, otherwise known as bugs,
00:03:13
in programming, that just drive you nuts.
00:03:15
And you feel like you've hit a wall.
00:03:16
But the trick really is to give it enough time,
00:03:18
to take a step back, take a break when you need to.
00:03:21
And there's nothing better, I daresay, than that sense of gratification
00:03:24
and pride, really, when you get something
00:03:26
to work, and in a class like this, present, ultimately,
00:03:28
at term's end, something like your very own final project.
00:03:32
Now, this isn't to say that I took to it 100% perfectly.
00:03:35
In fact, just this past week, I looked in my old CS50 binder, which I still
00:03:40
have from some 25 years ago, and took a photo
00:03:43
of what was apparently the very first program that I wrote and submitted,
00:03:47
and quickly received minus 2 points on.
00:03:50
But this is a program that we'll soon see in the coming days that
00:03:53
does something quite simply like print "Hello, CS50," in this case,
00:03:57
to the screen.
00:03:58
And to be fair, I technically hadn't really
00:04:00
followed the directions, which is why I lost those couple of points.
00:04:02
But if you just look at this, especially if you've never programmed before,
00:04:05
you might have heard about programming language
00:04:07
but you've never typed something like this out,
00:04:09
undoubtedly it's going to look cryptic.
00:04:11
But unlike human languages, frankly, which
00:04:13
were a lot more sophisticated, a lot more vocabulary, a lot more
00:04:17
grammatical rules, programming, once you start to wrap your mind around what
00:04:21
it is and how it works and what these various languages are, it's so easy,
00:04:24
you'll see, after a few months of a class like this,
00:04:26
to start teaching yourself, subsequently,
00:04:29
other languages, as they may come, in the coming years as well.
00:04:32
So what ultimately matters in this particular course
00:04:36
is not so much where you end up relative to your classmates
00:04:38
but where you end up relative to yourself when you began.
00:04:41
And indeed, you'll begin today.
00:04:43
And the only experience that matters ultimately in this class is your own.
00:04:46
And so, consider where you are today.
00:04:49
Consider, perhaps, just how cryptic something like that
00:04:51
looked a few seconds ago.
00:04:52
And take comfort in knowing just some months from now all of that
00:04:56
will be within your own grasp.
00:04:58
And if you're thinking that, OK, surely the person in front of me, to the left,
00:05:01
to the right, behind me, knows more than me, that's statistically not the case.
00:05:05
2/3 of CS50 students have never taken a CS course before, which is to say,
00:05:10
you're in very good company throughout this whole term.
00:05:14
So then, what is computer science?
00:05:16
I claim that it's problem solving.
00:05:18
And the upside of that is that problem solving is
00:05:20
something we sort of do all the time.
00:05:23
But a computer science class, learning to program,
00:05:25
I think kind of cleans up your thoughts.
00:05:27
It helps you learn how to think more methodically, more carefully, more
00:05:31
correctly, more precisely.
00:05:32
Because, honestly, the computer is not going
00:05:34
to do what you want unless you are correct and precise and methodical.
00:05:37
And so, as such, there's these fringe benefits
00:05:39
of just learning to think like a computer scientist and a programmer.
00:05:42
And it doesn't take all that much to start doing so.
00:05:45
This, for instance, is perhaps the simplest picture of computer science,
00:05:49
sure, but really problem solving in general.
00:05:51
Problems are all about taking input, like the problem you want to solve.
00:05:54
You want to get the solution, a.k.a.
00:05:56
output.
00:05:57
And so, something interesting has got to be happening in here,
00:05:59
in here, when you're trying to get from those inputs to outputs.
00:06:03
Now, in the world of computers specifically,
00:06:05
we need to decide in advance how we represent these inputs and outputs.
00:06:09
We all just need to decide, whether it's Macs or PCs or phones or something
00:06:13
else, that we're all going to speak some common language, irrespective
00:06:16
of our human languages as well.
00:06:18
And you may very well know that computers tend to speak only
00:06:22
what language, so to speak?
00:06:26
Assembly, one, but binary, two, might be your go-to.
00:06:29
And binary, by implying two, means that the world of computers
00:06:32
has just two digits at its disposal, 0 and 1.
00:06:35
And indeed, we humans have many more than that, certainly not just zeros
00:06:40
and ones alone. But a computer indeed only has zeros and ones.
00:06:43
And yet, somehow they can do so much.
00:06:45
They can crunch numbers in Excel, send text messages,
00:06:47
create images and artwork and movies and more.
00:06:51
And so, how do you get from something as simple as a few zeros, a few ones,
00:06:54
to all of the stuff that we're doing today
00:06:56
in our pockets and laptops and desktops?
00:06:58
Well, it turns out that we can start quite simply.
00:07:01
If a computer were to want to do something as simple as count, well,
00:07:05
what could it do?
00:07:06
Well, in our human world, we might count doing this,
00:07:09
like 1, 2, 3, 4, 5, using so-called unitary notation, literally the digits
00:07:13
on your fingers where one finger represents one person in the room,
00:07:16
if I'm, for instance, taking attendance.
00:07:18
Now, we humans would typically actually count 1, 2, 3, 4, 5, 6.
00:07:22
And we'd go past just those five digits and count much higher,
00:07:25
using zeros through nines.
00:07:26
But computers, somehow, only have these zeros and ones.
00:07:29
So if a computer only somehow speaks binary, zeros and ones,
00:07:33
how does it even count past the number 1?
00:07:36
Well, here are 3 zeros, of course.
00:07:38
And if you translate this number in binary, 000,
00:07:42
to a more familiar number in decimal, we would just call this zero.
00:07:46
Enough said.
00:07:47
If we were to represent, with a computer, the number 1,
00:07:49
it would actually be 001, which, not surprisingly,
00:07:52
is exactly the same as we might do in our human world,
00:07:55
but we might not bother writing out the two zeros at the beginning.
00:07:59
But a computer, now, if it wants to count as high as two,
00:08:02
it doesn't have the digit 2.
00:08:03
And so it has to use a different pattern of zeros and ones.
00:08:06
And that happens to be 010.
00:08:08
So this is not 10 with a zero in front of it.
00:08:10
It's indeed zero one zero in the context of binary.
00:08:13
And if we want to count higher now than two,
00:08:15
we're going to have to tweak these zeros and ones further to get 3.
00:08:19
And then if we want 4 or 5 or 6 or 7, we're
00:08:24
just kind of toggling these zeros and ones, a.k.a.
00:08:26
bits, for binary digits that represent, via these different patterns,
00:08:31
different numbers that you and I, as humans, know,
00:08:33
of course, as the so-called decimal system, 0 through 9,
00:08:36
dec implying 10, 10 digits, those zeros through nine.
00:08:40
So why that particular pattern?
00:08:42
And why these particular zeros and ones?
00:08:44
Well, it turns out that representing one thing or the other
00:08:48
is just really simple for a computer.
00:08:50
Why? At the end of the day, they're powered by electricity.
00:08:53
And it's a really simple thing to just either store some electricity
00:08:56
or don't store some electricity.
00:08:57
Like, that's as simple as the world can get, on or off.
00:09:00
1 or 0, so to speak.
00:09:03
So, in fact, inside of a computer, a phone, anything
00:09:05
these days that's electronic, pretty much,
00:09:07
is some number of switches, otherwise known as transistors.
00:09:10
And they're tiny.
00:09:11
You've got thousands, millions of them in your Mac or PC or phone these days.
00:09:14
And these are just tiny little switches that can get turned on and off.
00:09:17
And by turning those things on and off in patterns,
00:09:20
a computer can count from 0 on up to 7, and even higher than that.
00:09:24
And so these switches, really, you can think of being as like switches
00:09:27
like this. Let me just borrow one of our little stage lights here.
00:09:29
Here's a light bulb.
00:09:30
It's currently off.
00:09:32
And so, I could just think of this as representing,
00:09:34
in my laptop, a transistor, a switch, representing 0.
00:09:38
But if I allow some electricity to flow, now I, in fact, have a 1.
00:09:43
Well, how do I count higher than 1?
00:09:44
I, of course, need another light bulb.
00:09:46
So let me grab another one here.
00:09:48
And if I put it in that same kind of pattern, I don't want to just do this.
00:09:53
That's sort of the old finger counting way of unary, just 1, 2.
00:09:57
I want to actually take into account the pattern
00:09:59
of these things being on and off.
00:10:00
So if this was one a moment ago, what I think I did earlier was I turned it off
00:10:06
and let the next one over be on, a.k.a.
00:10:10
010.
00:10:12
And let me get us a third bit, if you will.
00:10:15
And that feels like enough.
00:10:16
Here is that same pattern now, starting at the beginning with 3.
00:10:20
So here is 000.
00:10:22
Here is 001.
00:10:25
Here is 010, a.k.a., in our human world of decimal, 2.
00:10:32
And then we could, of course, keep counting further.
00:10:35
This now would be 3 and dot dot dot.
00:10:37
If this other bulb now goes on, and that switch is turned
00:10:40
and all three stay on-- this, again, was what number?
00:10:43
AUDIENCE: Seven.
00:10:44
DAVID MALAN: OK, so, seven.
00:10:45
So it's just as simple, relatively, as that, if you will.
00:10:49
But how is it that these patterns came to be?
00:10:53
Well, these patterns actually follow something very familiar.
00:10:56
You and I don't really think about it at this level
00:10:58
anymore because we've probably been doing math and numbers since grade
00:11:02
school or whatnot.
00:11:04
But if we consider something in decimal, like the number 123,
00:11:09
I immediately jump to that.
00:11:10
This looks like 123 in decimal.
00:11:12
But why?
00:11:13
It's really just three symbols, a 1, a 2 with a bit of curve, a 3
00:11:17
with a couple of curves, that you and I now instinctively
00:11:20
just assign meaning to.
00:11:22
But if we do rewind a few years, that is one hundred twenty-three
00:11:27
because you're assigning meaning to each of these columns.
00:11:30
The 3 is in the so-called ones place.
00:11:33
The 2 is in the so-called tens place.
00:11:36
And the 1 is in the so-called hundreds place.
00:11:39
And then the math ensues quickly in your head.
00:11:41
This is technically 100 times 1, plus 10 times 2, plus 1 times 3, a.k.a.
00:11:47
100 plus 20 plus 3.
00:11:48
And there we get the sort of mathematical notion we know as 123.
00:11:54
Well, nicely enough, in binary, it's actually the same thing.
00:11:58
It's just these columns mean a little something different.
00:12:01
If you use three digits in decimal, and you have the ones place,
00:12:05
the tens place, and the hundreds place, well, why was that 1, 10, and 100?
00:12:09
They're technically just powers of 10.
00:12:11
So 10 to the 0, 10 to the 1, 10 to the 2.
00:12:13
Why 10?
00:12:14
Decimal system, "dec" meaning 10.
00:12:16
You have 8 and 10 digits, 0 through 9.
00:12:18
In the binary system, if you're going to use three digits,
00:12:21
just change the bases if you're using only zeros and ones.
00:12:24
So now it's powers of 2, 2 to the 0, 2 to the 1, 2 to the 2, a.k.a.
00:12:29
1 and 2 and 4, respectively.
00:12:31
And if you keep going, it's going to be 8s column, 16s column, 32, 64,
00:12:36
and so forth.
00:12:37
So, why did we get these patterns that we did?
00:12:40
Here's your 000 because it's 4 times 0, 2 times 0, 1 times 0, obviously 0.
00:12:46
This is why we got the decimal number 1 in binary.
00:12:49
This is why we got the number 2 in binary, because it's 4 times
00:12:53
0, plus 2 times 1, plus 1 times 0, and now 3, and now 4, and now 5, and now 6,
00:13:01
and now 7. And, of course, if you wanted to count as high as 8, to be clear,
00:13:05
what do you have to do?
00:13:06
What does a computer need to do to count even higher than 7?
00:13:08
AUDIENCE: Add a bit.
00:13:09
DAVID MALAN: Add a bit.
00:13:10
Add another light bulb, another switch.
00:13:12
And, indeed, computers have standardized just how
00:13:14
many zeros and ones, or bits or switches,
00:13:17
they throw at these kinds of problems.
00:13:19
And, in fact, most computers would typically use at least eight at a time.
00:13:23
And even if you're only counting as high as three or seven,
00:13:25
you would still use eight and have a whole bunch of zeros.
00:13:28
But that's OK, because the computers these days certainly
00:13:31
have so many more, thousands, millions of transistors and switches
00:13:35
that that's quite OK.
00:13:37
All right, so, with that said, if we can now count as high as seven
00:13:41
or, frankly, as high as we want, that only
00:13:44
seems to make computers useful for things like Excel,
00:13:46
like number crunching.
00:13:48
But computers, of course, let you send text messages,
00:13:50
write documents, and so much more.
00:13:52
So how would a computer represent something like a letter,
00:13:55
like the letter A of the English alphabet, if, at the end of the day,
00:13:59
all they have is switches?
00:14:02
Any thoughts?
00:14:03
Yeah. AUDIENCE: You can represent letters in numbers.
00:14:05
DAVID MALAN: OK, so we could represent letters using numbers.
00:14:08
OK, so what's a proposal?
00:14:10
What number should represent what?
00:14:11
AUDIENCE: Say if you were starting at the beginning of the alphabet,
00:14:15
you could say 1 is A, 2 is B, 3 is C.
00:14:18
DAVID MALAN: Perfect.
00:14:19
Yeah, we just all have to agree somehow that one number is
00:14:22
going to represent one letter.
00:14:23
So 1 is A, 2 is B, 3 is C, Z is 26, and so forth.
00:14:28
Maybe we can even take into account uppercase and lowercase.
00:14:30
We just have to agree and sort of write it down in some global standard.
00:14:34
And humans, indeed, did just that.
00:14:36
They didn't use 1, 2, 3.
00:14:38
It turns out they started a little higher up.
00:14:40
Capital A has been standardized as the number 65.
00:14:44
And capital B has been standardized as the number 66.
00:14:47
And you can kind of imagine how it goes up from there.
00:14:50
And that's because whatever you're representing,
00:14:53
ultimately, can only be stored, at the end of the day, as zeros and ones.
00:14:57
And so, some humans in a room before, decided that capital A shall be 65,
00:15:01
or, really, this pattern of zeros and ones inside of every computer
00:15:05
in the world, 01000001.
00:15:08
So if that pattern of zeros and ones ever appears in a computer,
00:15:12
it might be interpreted then as indeed a capital letter A, eight of those bits
00:15:17
at a time.
00:15:18
But I worry, just to be clear, we might have now created a problem.
00:15:23
It might seem, if I play this naively, that, OK,
00:15:25
how do I now actually do math with the number 65?
00:15:28
If now Excel displays 65 is an A, let alone Bs and Cs.
00:15:33
So how might a computer do as you've proposed,
00:15:37
have this mapping from numbers to letters, but still support numbers?
00:15:41
It feels like we've given something up.
00:15:43
Yeah?
00:15:44
AUDIENCE: By having a prefix for letters?
00:15:46
DAVID MALAN: By having a prefix?
00:15:47
AUDIENCE: You could have prefixes and suffixes.
00:15:48
DAVID MALAN: OK, so we could perhaps have some kind of prefix,
00:15:51
like some pattern of zeros and ones--
00:15:53
I like this-- that indicates to the computer
00:15:56
here comes another pattern that represents a letter.
00:15:58
Here comes another pattern that represents a number or a letter.
00:16:02
So, not bad.
00:16:03
I like that.
00:16:04
Other thoughts?
00:16:05
How might a computer distinguish these two?
00:16:08
Yeah. AUDIENCE: Have a different file format, so,
00:16:11
like, odd text or just check the graphic or--
00:16:16
DAVID MALAN: Indeed, and that's spot-on.
00:16:17
Nothing wrong with what you suggested, but the world generally does just that.
00:16:20
The reason we have all of these different file formats in the world,
00:16:23
like JPEG and GIF and PNGs and Word documents, .docx,
00:16:28
and Excel files and so forth, is because a bunch of humans got in a room
00:16:32
and decided, well, in the context of this type of file, or really,
00:16:35
more specifically, in the context of this type of program,
00:16:38
Excel versus Photoshop versus Google Docs or the like,
00:16:42
we shall interpret any patterns of zeros and ones as being maybe numbers
00:16:46
for Excel, maybe letters in, like, a text messaging program or Google Docs,
00:16:51
or maybe even colors of the rainbow in something like Photoshop and more.
00:16:54
So it's context dependent.
00:16:55
And we'll see, when we ourselves start programming,
00:16:58
you the programmer will ultimately provide
00:17:00
some hints to the computer that tells the computer, interpret it as follows.
00:17:05
So, similar in spirit to that, but not quite a standardized with these
00:17:08
prefixes.
00:17:09
So this system here actually has a name ASCII, the American Standard
00:17:12
Code for Information Interchange.
00:17:14
And indeed, it began here in the US, and that's
00:17:16
why it's actually a little biased toward A's through Z's
00:17:19
and a bit of punctuation as well.
00:17:21
And that quickly became a problem.
00:17:22
But if we start simply now, in English, the mapping
00:17:26
itself is fairly straightforward.
00:17:28
So if A is 65, B it 66, and dot dot dot, suppose
00:17:33
that you received a text message, an email, from a friend,
00:17:36
and underneath the hood, so to speak, if you kind of
00:17:39
looked inside the computer, what you technically received in this text
00:17:42
or this email happened to be the numbers 72, 73, 33,
00:17:48
or, really, the underlying pattern of zeros and ones.
00:17:50
What might your friend have sent you as a message, if it's 72, 73, 33?
00:17:56
AUDIENCE: Hey.
00:17:58
DAVID MALAN: Hey? Close.
00:17:59
AUDIENCE: Hi. DAVID MALAN: Hi.
00:18:00
It's, indeed, hi.
00:18:01
Why?
00:18:02
Well, apparently, according to this little cheat sheet, H is 72, I is 73.
00:18:06
It's not obvious from this chart what the 33 is,
00:18:09
but indeed, this pattern represents "hi."
00:18:11
And anyone want to guess, or if you know, what 33 is?
00:18:13
AUDIENCE: Exclamation point.
00:18:14
DAVID MALAN: Exclamation point.
00:18:16
And this is, frankly, not the kind of thing most people know.
00:18:18
But it's easily accessible by a nice user-friendly chart like this.
00:18:22
So this is an ASCII chart.
00:18:23
When I said that we just need to write down this mapping earlier,
00:18:26
this is what people did.
00:18:27
They wrote it down in a book or in a chart.
00:18:29
And, for instance, here is our 72 for H, here is our 73 for I,
00:18:33
and here is our 33 for exclamation point.
00:18:37
And computers, Macs, PCs, iPhones, Android devices,
00:18:41
just know this mapping by heart, if you will.
00:18:43
They've been designed to understand those letters.
00:18:46
So here, I might have received "hi."
00:18:48
Technically, what I've received is these patterns of zeros and ones.
00:18:51
But it's important to note that when you get these patterns of zeros and ones
00:18:54
in any format, be it email or text or a file,
00:18:58
they do tend to come in standard lengths,
00:19:00
with a certain number of zeros and ones altogether.
00:19:04
And this happens to be 8 plus 8, plus 8.
00:19:07
So just to get the message "hi, exclamation point,"
00:19:10
you would have received at least, it would seem, some 24 bits.
00:19:15
But frankly, bits are so tiny, literally and mathematically,
00:19:18
that we don't tend to think or talk, generally, in terms of bits.
00:19:21
You're probably more familiar with bytes.
00:19:23
B-Y-T-E-S is a byte, is a byte, is a byte.
00:19:27
A byte is just 8 bits.
00:19:29
And even those, frankly, aren't that useful if we do out the math.
00:19:32
How high can you count if you have eight bits?
00:19:34
Anyone know?
00:19:35
Say it again?
00:19:38
Higher than that.
00:19:39
Unless you want to go negative, that's fine.
00:19:42
256, technically 255.
00:19:45
Long story short, if we actually got into the weeds of all of these zeros
00:19:48
and ones, and we figured out what 11111111 mathematically adds up
00:19:54
to in decimal, it would indeed be 255, or less
00:19:57
if you want to represent negative numbers as well.
00:20:00
So this is useful because now we can speak, not just in terms of bytes
00:20:04
but, if the files are bigger, kilobytes is thousands of bytes,
00:20:06
megabytes is millions of bytes, gigabytes is billions of bytes,
00:20:10
terabytes are trillions of bytes, and so forth.
00:20:14
We have a vocabulary for these increasingly large quantities of data.
00:20:20
The problem is that, if you're using ASCII and, therefore, eight bits or one
00:20:24
byte per character, and originally, only seven, you
00:20:27
can only represent 255 characters.
00:20:30
And that's actually 256 total characters, including zero.
00:20:33
And that's fine if you're using literally English, in this case,
00:20:37
plus a bunch of punctuation.
00:20:39
But there's many human languages in the world
00:20:42
that need many more symbols and, therefore, many more bits.
00:20:45
So, thankfully, the world decided that we'll indeed
00:20:48
support not just the US English keyboard, but all
00:20:51
of the accented characters that you might want for some languages.
00:20:54
And heck, if we use enough bits, zeros and ones,
00:20:57
not only can we represent all human languages in written form,
00:21:01
as well as some emotions along the way, we
00:21:03
can capture the latter with these things called emojis.
00:21:06
And indeed, these are very much in vogue these days.
00:21:09
You probably send and/or receive many of these things any given day.
00:21:12
These are just characters, like letters of an alphabet, patterns
00:21:16
of zeros and ones that you're receiving, that the world has also standardized.
00:21:20
For instance, there are certain emojis that
00:21:22
are represented with certain patterns of bits.
00:21:24
And when you receive them, your phone, your laptop, your desktop,
00:21:28
displays them as such.
00:21:30
And this newer standard is called Unicode.
00:21:32
So it's a superset of what we called ASCII.
00:21:35
And Unicode is just a mapping of many more numbers to many more letters
00:21:39
or characters, more generally, that might
00:21:42
use eight bits for backwards compatibility
00:21:45
with the old way of doing things with ASCII, but they might also use 16 bits.
00:21:49
And if you have 16 bits, you can actually
00:21:51
represent more than 65,000 possible letters.
00:21:54
And that's getting up there.
00:21:55
And heck, Unicode might even use 32 bits to represent letters and numbers
00:22:01
and punctuation symbols and emojis.
00:22:03
And that would give you up to 4 billion possibilities.
00:22:06
And, I daresay, one of the reasons we see so many emojis these days is we
00:22:09
have so much room.
00:22:11
I mean, we've got room for billions more, literally.
00:22:14
So, in fact, just as a little bit of trivia,
00:22:16
has anyone ever received this decimal number, or if you prefer binary now,
00:22:21
has anyone ever received this pattern of zeros and ones on your phone,
00:22:25
in a text or an email, perhaps this past year?
00:22:29
Well, if you actually look this up, this esoteric sequence of zeros and ones
00:22:33
happens to represent face with medical mask.
00:22:37
And notice that if you've got an iPhone or an Android device,
00:22:40
you might be seeing different things.
00:22:43
In fact, this is the Android version of this, most recently.
00:22:46
This is the iOS version of it, most recently.
00:22:49
And there's bunches of other interpretations by other companies
00:22:51
as well.
00:22:53
So Unicode, as a consortium, if you will,
00:22:55
has standardized the descriptions of what these things are.
00:22:58
But the companies themselves, manufacturers out there,
00:23:02
have generally interpreted it as you see fit.
00:23:05
And this can lead to some human miscommunications.
00:23:08
In fact, for like, literally, embarrassingly, like a year or two,
00:23:11
I started being in the habit of using the emoji that kind of looks
00:23:14
like this because I thought it was like woo, happy face, or whatever.
00:23:17
I didn't realize this is the emoji for hug
00:23:19
because whatever device I was using sort of looks like this, not like this.
00:23:24
And that's because of their interpretation of the data.
00:23:27
This has happened too when what was a gun became a water
00:23:31
pistol in some manufacturers' eyes.
00:23:33
And so it's an interesting dichotomy between what information we all
00:23:37
want to represent and how we choose, ultimately, to represent it.
00:23:42
Questions, then, on these representations of formats,
00:23:45
be it numbers or letters, or soon more.
00:23:49
Yeah? AUDIENCE: Why is decimal popular for a computer
00:23:52
if binary is the basis for everything?
00:23:54
DAVID MALAN: Sorry, why is what so popular?
00:23:56
AUDIENCE: Why is the decimal popular if binary is the fundamental--
00:23:59
DAVID MALAN: Yeah, so we'll come back to this in a few weeks, in fact.
00:24:01
There are other ways to represent numbers.
00:24:03
Binary is one.
00:24:04
Decimal is another.
00:24:06
Unary is another.
00:24:07
And hexadecimal is yet a fourth that uses 16 total digits, literally 0
00:24:12
through 9 plus A, B, C, D, E, F. And somehow,
00:24:15
you can similarly count even higher with those.
00:24:18
We'll see in a few weeks why this is compelling.
00:24:21
But hexadecimal, long story short, uses four bits per digit.
00:24:24
And so, four bits, if you have two digits in hex, that gives you eight.
00:24:28
And it's just a very convenient unit of measure.
00:24:30
And it's also human convention in the world of files and other things.
00:24:34
But we'll come back to that soon.
00:24:35
Other questions?
00:24:36
AUDIENCE: Do the lights on the stage supposedly say that--
00:24:39
DAVID MALAN: Do the lights on the stage supposedly say anything?
00:24:42
Well, if we had thought in advance to use maybe 64 light bulbs,
00:24:46
that would seem to give us 8 total bytes on stage, 8 times 8,
00:24:51
giving us just that.
00:24:52
Maybe.
00:24:54
Good question.
00:24:55
Other questions on 0's and 1's?
00:24:58
It's a little bright in here.
00:25:01
No?
00:25:02
Oh, yes?
00:25:04
Where everyone's pointing somewhere specific.
00:25:08
There we go.
00:25:09
Sorry.
00:25:10
Very bright in this corner.
00:25:11
AUDIENCE: I was just going to ask about the 255 bits,
00:25:14
like with the maximum characters.
00:25:16
[INAUDIBLE] DAVID MALAN: Ah, sure, and we'll come back to this, in some form,
00:25:19
in the coming days too, at a slower pace too,
00:25:22
we have, with eight bits, two possible values for the first
00:25:26
and then two for the next, two for the next, and so forth.
00:25:28
So that's 2 times 2 times 2.
00:25:30
That's 2 to the eighth power total, which
00:25:32
means you can have 256 total possible patterns of zeros and ones.
00:25:36
But as we'll see soon computer scientists, programmers,
00:25:40
software often starts counting at 0 by convention and if you use one of those
00:25:45
patterns, 00000000 to represent the decimal number we know is zero,
00:25:50
you only have 255 other patterns left to count as high as therefore 255.
00:25:56
That's all.
00:25:57
Good question.
00:25:59
All right, so what then might we have besides these emojis and letters
00:26:04
and numbers? Well, we of course have things like colors and programs
00:26:07
like Photoshop and pictures and photos.
00:26:09
Well let me ask the question again.
00:26:11
How might a computer, do you think, knowing what you know now, represents
00:26:14
something like a color?
00:26:16
Like what are our options if all we've got are zeros and ones and switches?
00:26:19
Yeah?
00:26:20
AUDIENCE: RGB
00:26:21
DAVID MALAN: RGB.
00:26:23
RGB indeed is this acronym that represents some amount of red
00:26:27
and some amount of green and blue and indeed computers
00:26:29
can represent colors by just doing that.
00:26:32
Remembering, for instance, this dot.
00:26:33
This yellow dot on the screen that might be part of any of those emojis
00:26:36
these days, well that's some amount of red, some amount of green,
00:26:39
some amount of blue.
00:26:40
And if you sort of mix those colors together,
00:26:42
you can indeed get a very specific one.
00:26:44
And we'll see you in just a moment just that.
00:26:46
So indeed earlier on, humans only used seven bits total.
00:26:51
And it was only once they decided, well, let's add an eighth bit that they
00:26:54
got extended ASCII and that was initially in part
00:26:57
a solution to the same problem of not having enough room, if you will,
00:27:00
in those patterns of zeros and ones to represent all of the characters
00:27:05
that you might want.
00:27:06
But even that wasn't enough and that's why we've now gone up to 16 and 32
00:27:10
and long past 7.
00:27:11
So if we come back now to this one particular color.
00:27:16
RGB was proposed as a scheme, but how might this work?
00:27:19
Well, consider for instance this.
00:27:21
If we do indeed decide as a group to represent any color of the rainbow
00:27:25
with some mixture of some red, some green, and some blue,
00:27:28
we have to decide how to represent the amount of red and green and blue.
00:27:33
Well, it turns out if all we have are zeros and ones, ergo numbers,
00:27:37
let's do just that.
00:27:38
For instance, suppose a computer we're using, these three numbers 72, 73, 33,
00:27:44
no longer in the context of an email or a text message,
00:27:47
but now in the context of something like Photoshop, a program for editing
00:27:51
and creating graphical files, maybe this first number
00:27:55
could be interpreted as representing some amount of red, green, and blue,
00:28:00
respectively. And that's exactly what happens.
00:28:02
You can think of the first digit as red, second as green, third as blue.
00:28:05
And so ultimately when you combine that amount of red, that amount of green,
00:28:10
that amount of blue, it turns out it's going to resemble the shade of yellow.
00:28:14
And indeed, you can come up with a numbers between 0 and 255
00:28:18
for each of those colors to mix any other color that you might want.
00:28:21
And you can actually see this in practice.
00:28:23
Even though our screens, admittedly, are getting really good
00:28:26
on our phones and laptops such that you barely see the dots, they are there.
00:28:30
You might have heard the term pixel before.
00:28:32
Pixel's just a dot on the screen and you've
00:28:34
got thousands, millions of them these days horizontally and vertically.
00:28:38
If I take even this emoji, which again happens
00:28:41
to be one company's interpretation of a face with medical mask
00:28:46
and zoom in a bit, maybe zoom in a bit more,
00:28:48
you can actually start to see these pixels.
00:28:50
Things get pixelated because what you're seeing
00:28:53
is each of the individual dots that compose this particular image.
00:28:57
And apparently each of these individual dots
00:28:59
are probably using 24 bits, eight bits for red, eight bits for green, eight
00:29:04
bits for blue, in some pattern.
00:29:07
This program or some other like Photoshop is interpreting one pattern
00:29:11
and it's white or yellow or black or some brown in between.
00:29:16
So if you look sort of awkwardly, but up close to your phone or your laptop
00:29:19
or maybe your TV, you can see exactly this, too.
00:29:23
All right, well, what about things that we also
00:29:25
watch every day on YouTube or the like?
00:29:27
Things like videos.
00:29:28
How would a computer, knowing what we know now,
00:29:30
represent something like a video?
00:29:35
How might you represent a video using only zeros and ones?
00:29:37
Yeah?
00:29:38
AUDIENCE: As we can see here, they represent images, right?
00:29:43
[INAUDIBLE] sounds of the 0 and 1s as well.
00:29:47
[INAUDIBLE]
00:29:50
DAVID MALAN: Yeah, exactly.
00:29:52
To summarize, what video really adds is just some notion of time.
00:29:55
It's not just one image, it's not just one letter or a number,
00:29:58
it's presumably some kind of sequence because time is passing.
00:30:01
So with a whole bunch of images, maybe 24 maybe 30 per second,
00:30:05
if you fly them by the human's eyes, we can
00:30:08
interpret them using our eyes and brain that there is now
00:30:11
movement and therefore video.
00:30:13
Similarly with audio or music.
00:30:16
If we just came up with some convention for representing those same notes
00:30:20
on a musical instrument, could we have the computer synthesize them, too?
00:30:23
And this might be actually pretty familiar.
00:30:25
Let me pull up a quick video here, which happens to be an old school
00:30:29
version of the same idea.
00:30:31
You might remember from childhood.
00:30:32
[MUSIC PLAYING]
00:30:40
[CLICKING]
00:30:54
So granted that particular video is an actual video
00:30:58
of a paper-based animation, but indeed, that's really all you need,
00:31:02
is some sequence of these images, which themselves of course
00:31:06
are just zeros and ones because they're just this grid of these pixels or dots.
00:31:10
Now something like musical notes like these, those of you who are musicians
00:31:14
might just naturally play these on physical devices,
00:31:16
but computers can certainly represent those sounds, too.
00:31:19
For instance, a popular format for audio is
00:31:22
called MIDI and MIDI might just represent
00:31:24
each note that you saw a moment ago essentially as a sequence of numbers.
00:31:29
But more generally, you might think about music as having notes,
00:31:32
for instance, A through G, maybe some flats and some sharps,
00:31:35
you might have the duration like how long is the note being heard or played
00:31:39
on a piano or some other device, and then
00:31:41
just the volume like how hard does a human in the real world
00:31:43
press down on that key and therefore how loud is that sound?
00:31:46
It would seem that just remembering little details like that quantitatively
00:31:51
we can then represent really all of these otherwise analog human realities.
00:31:57
So that then is really a laundry list of ways
00:32:00
that we can just represent information.
00:32:02
Again, computers or digital have all of these different formats,
00:32:05
but at the end of the day and as fancy as those devices in years
00:32:07
are, it's just zeros and ones, tiny little switches or light bulbs,
00:32:11
if you will, represented in some way and it's up to the software
00:32:14
that you and I and others write to use those zeros
00:32:17
and ones in ways we want to get the computers to do something
00:32:21
more powerfully.
00:32:22
Questions, then, on this representation of information, which I daresay
00:32:27
is ultimately what problem solving is all about, taking in information
00:32:30
and producing new via some process in between.
00:32:35
Any questions there?
00:32:38
Yeah, in back.
00:32:40
AUDIENCE: Yeah, so we talked about how different file formats kind of allow
00:32:43
you to interpret information.
00:32:45
How does a file format like .mp4 discriminate between audio and video
00:32:50
within itself as a value?
00:32:52
DAVID MALAN: So a really good question.
00:32:53
There are many other file formats out there.
00:32:55
You allude to MP4 for video and more generally the use
00:32:58
are these things called codecs and containers.
00:33:01
It's not quite as simple when using larger files, for instance,
00:33:04
in more modern formats that a video is just a sequence of images,
00:33:08
for instance. Why?
00:33:09
If you stored that many images for like a Hollywood movie,
00:33:13
like 24 or 30 of them per second, that's a huge number of images.
00:33:17
And if you've ever taken photos on your phone,
00:33:19
you might know how many megabytes or larger even individual photographs
00:33:23
might be.
00:33:24
So humans have developed over the years a fancier software
00:33:27
that uses much more math to represent the same information more minimally
00:33:32
just using somehow shorter patterns of zeros and ones
00:33:35
than are most simplistic representation here.
00:33:37
And they use what might be called compression.
00:33:40
If you've ever used a zip file or something else,
00:33:42
somehow your computer is using fewer zeros and ones
00:33:45
to represent the same amount of information,
00:33:47
ideally without losing any information.
00:33:49
In the world of multimedia, which we'll touch on a little bit in a few weeks,
00:33:52
there are both lossy and lossless formats out there.
00:33:56
Lossless means you lose no information whatsoever.
00:33:59
But more commonly as you're alluding to one is lossy compression, L-O-S-S-Y,
00:34:05
where you're actually throwing away some amount of quality.
00:34:08
You're getting some amount of pixelation that might not
00:34:10
look perfect to the human, but heck it's a lot cheaper and a lot easier
00:34:13
to distribute.
00:34:14
And in the world of multimedia, you have containers like QuickTime
00:34:17
and other MPEG containers that can combine
00:34:20
different formats of video, different formats of audio in one file,
00:34:24
but there, too, do designers have discretion.
00:34:26
So more in a few weeks, too.
00:34:28
Other questions, then, on information here as well?
00:34:32
Yeah? AUDIENCE: So I know computers used to be very big
00:34:35
and taking up like a whole room and stuff.
00:34:37
Is the reason they've gotten smaller because we can store
00:34:41
this information piecemeal or what?
00:34:43
DAVID MALAN: Exactly.
00:34:44
I mean, back in the day you might have heard of the expression a vacuum
00:34:47
tube, which is like some physically large device that
00:34:50
might have only stored some 0 or 1.
00:34:53
Yes, it is the miniaturization of hardware these days
00:34:55
that has allowed us to store as many and many more zeros and ones much more
00:35:00
closely together.
00:35:01
And as we've built more fancy machines that
00:35:03
can sort of design this hardware at an even smaller scale,
00:35:06
we're just packing more and more into these devices.
00:35:08
But there, too, is a trade off.
00:35:09
For instance, you might know by using your phone or your laptop
00:35:13
for quite a while, maybe on your lap, starts to get warm.
00:35:15
So there are these literal physical side effects
00:35:17
of this where now some of our devices run hot.
00:35:20
This is why like a data center in the real world
00:35:23
might need more air conditioning than a typical place,
00:35:25
because there are these physical artifacts as well.
00:35:28
In fact, if you'd like to see one of the earliest computers from decades ago,
00:35:31
across the river here in now Allston in the new engineering building
00:35:35
is the Harvard Mark 1 computer that will give you a much better mental model
00:35:40
of just that.
00:35:41
Well if we come back now to this first picture
00:35:44
being computer science or really problem solving,
00:35:47
I daresay we have more than enough ways now
00:35:49
to represent information, input and output, so long as we all just agree
00:35:53
on something and thankfully all of those before us have given us
00:35:56
things like ASCII and Unicode.
00:35:57
Not to mention MP4s, word documents, and the like.
00:36:01
But what's inside of this proverbial black box into which these inputs are
00:36:05
going in the outputs are coming?
00:36:06
Well that's where we get this term you might have heard, too.
00:36:09
An algorithm, which is just step-by-step instructions for solving some problem
00:36:14
incarnated in the world of computers by software.
00:36:17
When you write software aka programs, you
00:36:20
are implementing one or more algorithms, one or more sets of instructions
00:36:26
for solving some problem, and maybe you're using this language or that,
00:36:29
but at the end of the day, no matter the language
00:36:31
you use the computer is going to represent what you type
00:36:34
using just zeros and ones.
00:36:37
So what might be a representative algorithm?
00:36:40
Nowadays you might use your phone quite a bit
00:36:42
to make calls or send texts or emails and therefore you
00:36:45
have a whole bunch of contacts in your address book.
00:36:48
Nowadays, of course, this is very digital,
00:36:50
but whether on iOS or Android or the like,
00:36:53
you might have a whole bunch of names, first name
00:36:56
and/or last, as well as numbers and emails and the like.
00:36:58
You might be in the habit of like scrolling through on your phone
00:37:01
all of those names to find the person you want to call.
00:37:05
It's probably sorted alphabetically by first name or last name, A through Z,
00:37:09
or some other symbol.
00:37:11
This is frankly quite the same as we used to do back in my day, CS50,
00:37:16
when we just used a physical book.
00:37:18
In this physical book might be a whole bunch
00:37:20
of names alphabetically sorted from left to right
00:37:22
corresponding to a whole bunch of numbers.
00:37:24
So suppose that in this old Harvard phone book
00:37:27
we want to search for John Harvard.
00:37:29
We might of course start quite simply at the beginning
00:37:31
here, looking at one page at a time, and this is an algorithm.
00:37:36
This is like literally step-by-step looking for the solution
00:37:40
to this problem.
00:37:41
In that sense, if John Harvard's in the phone book,
00:37:43
is this algorithm page-by-page correct, would you say?
00:37:47
AUDIENCE: Yes.
00:37:48
DAVID MALAN: Yes.
00:37:49
Like if John Harvard's in the phone book,
00:37:51
obviously I'm eventually going to get to him, so that's what we mean by correct.
00:37:54
Is it efficient?
00:37:56
Is it well designed, would you say?
00:37:58
No. I mean this is going to take forever even just to get to the Js or the Hs,
00:38:01
depending how this thing's sorted.
00:38:03
All right, well let me go a little faster.
00:38:04
I'll start like two pages at a time.
00:38:06
2, 4, 6, 8, 10, 12, and so forth.
00:38:11
Sounds faster, is faster, is it correct?
00:38:13
AUDIENCE: No.
00:38:14
DAVID MALAN: OK, why is it not correct?
00:38:16
Yeah?
00:38:17
AUDIENCE: So if you're starting on page 1,
00:38:19
you're only going odd number of pages, so if it's on an even number page,
00:38:22
you'll miss it. DAVID MALAN: Exactly.
00:38:23
If I start on an odd number of pages and I'm going two at a time
00:38:26
I might miss pages in between.
00:38:28
And if I therefore conclude when I get to the back
00:38:30
of the book there was no John Harvard, I might have just errored.
00:38:33
This would be again one of these bugs.
00:38:35
But if I try a little harder, I feel like there's a solution.
00:38:39
We don't have to completely throw out this algorithm.
00:38:41
I think we can probably go roughly twice as fast still.
00:38:44
But what should we do instead to fix this?
00:38:47
Yeah, in back.
00:38:48
AUDIENCE: [INAUDIBLE]
00:38:58
DAVID MALAN: Nice.
00:38:59
So I think what many of us, most of us, if we even use this technology any more
00:39:02
these days, we might go roughly to the middle of the phone book
00:39:05
just to kind of get us started.
00:39:06
And now I'm looking down, I'm looking for J, assuming first name, J Harvard,
00:39:10
and it looks like I'm in the M section.
00:39:13
So just to be clear, what should I do next?
00:39:15
AUDIENCE: [INAUDIBLE]
00:39:21
DAVID MALAN: OK, and presumably it is John Harvard
00:39:23
would be to the left of this.
00:39:24
So here's an opportunity to figuratively and literally tear
00:39:27
this particular problem in half, throw half of the problem away.
00:39:32
It's actually pretty easy if you just do it that way.
00:39:34
The hard way is this way.
00:39:36
But I've now just decreased the size of this problem really in half.
00:39:41
So if I started with 1,000 pages of phone numbers and names, now
00:39:45
I'm down to 500.
00:39:46
And already we haven't found John Harvard,
00:39:48
but that's a big bite out of this problem.
00:39:50
I do think it's correct because if J is to the left of M, of course,
00:39:54
he's definitely not going to be over there.
00:39:56
I think if I repeat this again dividing and conquering, if you will,
00:40:00
here I might have gone a little too far.
00:40:02
Now I'm in like the E section.
00:40:03
So let me tear the problem in half again, throw another 250 pages away,
00:40:08
and again repeat, dividing and dividing and conquering
00:40:11
until finally, presumably, I end up with just one page of a phone
00:40:14
book on which John Harvard's name either is or is not,
00:40:18
but because of the algorithm you proposed, step by step,
00:40:21
I know that he's not in anything I discarded.
00:40:24
So traumatic is that might have been made out
00:40:28
to be, it's actually just harnessing pretty good human intuition.
00:40:31
Indeed, this is what programming is all about, too.
00:40:33
It's not about learning a completely new world,
00:40:36
but really just how to harness intuition and ideas that you might already
00:40:40
have and take naturally but learning how to express
00:40:43
them now more succinctly, more precisely,
00:40:45
using things called programming languages.
00:40:49
Why is an algorithm like that if I found John Harvard better than, ultimately,
00:40:54
just doing the first one or even the second
00:40:56
and maybe doubling back to check those even pages?
00:40:59
Well let's just look at little charts here.
00:41:01
Again, we don't have to get into the nuances of numbers,
00:41:04
but if we've got like a chart here, xy plot, on the x-axis
00:41:07
here I claim as the size of the problem.
00:41:09
So measured in the numbers of pages in the phone book.
00:41:12
So the farther you go out here, the more pages are in the phone book.
00:41:16
And here we have time to solve on the y-axis.
00:41:18
So the higher you go up, the more time it's
00:41:21
going to be taking to solve that particular problem.
00:41:24
So let's just arbitrarily say that the first algorithm, involving
00:41:27
like n pages, might be represented graphically like this.
00:41:31
No matter the slope, it's a straight line
00:41:33
because there's presumably a one to one relationship
00:41:36
between numbers of pages and number of seconds or number of page turns.
00:41:40
Why? If the phone company adds another page next year
00:41:43
because some new people move to town, that's
00:41:45
going to require one additional page for me.
00:41:47
One to one.
00:41:49
If, though, we use the second algorithm, flawed though it was,
00:41:52
unless we double back a little bit to fix someone being in between,
00:41:56
that's too going to be a straight line, but it's
00:41:59
going to be a different slope because now there's a 2 to 1 or a 1 to 2
00:42:02
relationship because I'm going to pages at a time.
00:42:06
So if the phone company adds another page or another two pages,
00:42:10
that still only just one more step.
00:42:13
You can see the difference if I kind of draw this.
00:42:15
If this is the phone book in question, this number of pages,
00:42:18
it might take this many seconds on the yellow line
00:42:20
to represent or to find someone like John Harvard.
00:42:24
But of course on the first algorithm, the red line,
00:42:27
it's literally going to take twice as many steps.
00:42:29
And what do the n here mean? n is the go-to variable
00:42:32
for computer scientist or programmer just generically representing a number.
00:42:36
So if the number of pages in the phone book is n,
00:42:38
the number of steps the second algorithm would have taken
00:42:41
would be in the worst case n over 2.
00:42:44
Half as many because you're going twice as fast.
00:42:46
But the third algorithm, actually if you recall your logarithms,
00:42:50
looks a little something like this.
00:42:52
There's a fundamentally different relationship
00:42:54
between the size of the problem and the amount of time required to solve it
00:42:58
that technically is log-based, too, again,
00:43:00
but it's really the shape that's different.
00:43:02
The implication there is that if, for instance, Cambridge and Allston,
00:43:06
two different towns here in Massachusetts, merge next year
00:43:08
and there's just one phone book that's twice as big,
00:43:11
no big deal for that third and final algorithm.
00:43:14
Why?
00:43:15
You just tear the problem one more time in half,
00:43:17
taking one more byte, that's it, not another 1,000
00:43:21
bytes just to get to the solution.
00:43:23
Put another way, you can walk it way, way,
00:43:26
way out here to a much bigger phone book and ultimately
00:43:29
that green line is barely going to have budged.
00:43:32
So this then is just a way of now formalizing and thinking
00:43:35
about what the performance or quality of these algorithms might be.
00:43:42
Before we now make one more formalization of the algorithm itself,
00:43:46
any questions then on this notion of efficiency or now performance of ideas?
00:43:52
Yeah. AUDIENCE: How many phone books have you got?
00:43:56
DAVID MALAN: (LAUGHING) A lot of phone books over the years
00:43:58
and if you or your parents have any more still somewhere we could definitely
00:44:02
use them because they're hard to find.
00:44:04
Other questions?
00:44:05
But thanks.
00:44:07
Other questions here, too?
00:44:09
No.
00:44:10
Oh, was that a murmur?
00:44:11
Yes, over here.
00:44:13
AUDIENCE: You could get Harry Potter as a guest speaker.
00:44:15
DAVID MALAN: Sorry, say again.
00:44:16
AUDIENCE: You could get Harry Potter as a guest speaker.
00:44:18
DAVID MALAN: (LAUGHING) Oh, yeah.
00:44:19
Hopefully.
00:44:20
Then we'd have a little something more to use here.
00:44:23
So now if we want to formalize further what it is we just did,
00:44:28
we can go ahead and introduce this.
00:44:30
A form of code aka pseudocode.
00:44:33
Pseudocode is not a specific language, it's not like something
00:44:35
we're about to start coding in, it's just a way of expressing yourself
00:44:38
in English or any human language succinctly
00:44:41
correctly toward an end of getting your idea for an algorithm across.
00:44:45
So for instance, here might be how we could
00:44:47
formalize the code, the pseudocode for that same algorithm.
00:44:51
Step one was pick up the phone book, as I did.
00:44:54
Step two might be open to the middle of the phone book,
00:44:56
as you proposed that we do first.
00:44:58
Step three was probably to look down at the pages, I did.
00:45:01
And step four gets a little more interesting
00:45:04
because I had to quickly make a decision and ask myself a question.
00:45:07
If person is on page, then I should probably just
00:45:11
go ahead and call that person.
00:45:13
But that probably wasn't the case at least for John Harvard,
00:45:15
and I opened the M section.
00:45:17
So there's this other question I should now
00:45:19
ask else if the person is earlier in the book,
00:45:22
then I should tear the problem in half as I did but go left, so
00:45:26
to speak, and then not just open to the middle of the left half of the book,
00:45:30
but really just go back to step three, repeat myself.
00:45:33
Why? Because I can just repeat what I just did, but with a smaller problem
00:45:37
having taken this big bite.
00:45:39
But, if the person was later in the book,
00:45:41
as might have happened with a different person than John Harvard,
00:45:44
then I should open to the middle of the right half of the book,
00:45:47
again go back to line three, but again, I'm
00:45:49
not going to get sucked doing something forever like this
00:45:51
because I keep shrinking the size of the problem.
00:45:54
Lastly, the only possible scenario that's
00:45:56
left, if John Harvard is not on the page and he's not to the left
00:45:59
and he's not to the right, what should our conclusion be?
00:46:02
AUDIENCE: He's not there.
00:46:03
DAVID MALAN: He's not there.
00:46:04
He's not listed.
00:46:05
So we need to quit in some other form.
00:46:08
Now as an aside, it's kind of deliberate that I buried that last question
00:46:11
at the end because this is what happens all too often in programming,
00:46:16
whether you're new at it or professional,
00:46:17
just not considering all possible cases, corner cases if you will,
00:46:22
that might not happen that often, but if you don't anticipate them
00:46:25
in your own code, pseudocode or otherwise,
00:46:28
this is when and why programs might crash
00:46:31
or you might say stupid little spinning beach balls or hourglasses
00:46:34
or your computer might reboot.
00:46:35
Why? It's doing something sort of unpredictable
00:46:38
if a human, maybe myself, didn't anticipate this.
00:46:42
Like what does this program do if John Harvard is not in the phone book
00:46:45
if I had omitted lines 12 and 13?
00:46:48
I don't know. Maybe it would behave differently on a Mac or PC
00:46:50
because it's sort of undefined behavior.
00:46:53
These are the kinds of omissions that frankly you're invariably
00:46:56
going to make, bugs you're going to introduce,
00:46:58
mistakes you're going to make early on, and me, too, 25 years later.
00:47:02
But you'll get better at thinking about those corner cases
00:47:06
and handling anything that can possibly go wrong and as a result,
00:47:09
your code will be all the better for it.
00:47:11
Now the problem ultimately with learning how to program,
00:47:15
especially if you've never had experience
00:47:16
or even if you do but you learned one language only,
00:47:21
is that they all look a little cryptic at first glance.
00:47:25
But they do share certain commonalities.
00:47:27
In fact, we'll use this pseudocode to define those first.
00:47:30
Highlighted in yellow here are what henceforth
00:47:32
we're going to start calling functions.
00:47:34
Lots of different programming languages exist, but most of them
00:47:37
have what we might call functions, which are actions
00:47:40
or verbs that solve some smaller problem.
00:47:43
That is to say, you might use a whole bunch of functions
00:47:46
to solve a bigger problem because each function tends to do
00:47:50
something very specific or precise.
00:47:52
These then in English might be translated in code, actual computer
00:47:57
code, to these things called functions.
00:47:59
Highlighted in yellow now are what we might call conditionals.
00:48:02
Conditionals are things that you do conditionally
00:48:05
based on the answer to some question.
00:48:07
You can think of them kind of like forks in the road.
00:48:09
Do you go left or go right or some other direction
00:48:12
based on the answer to some question?
00:48:14
Well, what are those questions?
00:48:16
Highlighted now in yellow or what we would call Boolean expressions, named
00:48:20
after a mathematician last name Bool, that simply have yes no answers.
00:48:25
Or, if you prefer, true or false answers or, heck, if you prefer 1 or 0 answers.
00:48:31
We just need to distinguish one scenario from another.
00:48:34
The last thing manifests in this pseudocode
00:48:37
is what I might highlight now and call loops.
00:48:39
Some kind of cycle, some kind of directive that tells us to do something
00:48:43
again and again so that I don't need a 1,000-line program to search
00:48:48
a 1,000-page phone book, I can get away with a 13-line program but sort
00:48:53
of repeat myself inherently in order to solve some problem until I get to that
00:48:58
last step.
00:48:59
So this then is what we might call pseudocode
00:49:01
and indeed there are other characteristics
00:49:05
of programs that we'll touch on before long, things like arguments and return
00:49:08
values, variables, and more, but unfortunately in most languages,
00:49:13
including some we will very deliberately use in this class
00:49:16
and that everyone in the real world these days still uses,
00:49:19
its programs tend to look like this.
00:49:22
This for instance, is a distillation of that very first program
00:49:24
I wrote in 1996 in CS50 itself just to print something on the screen.
00:49:29
In fact, this version here just tries to print quote unquote, "Hello, world."
00:49:34
Which is, dare say, the most canonical first thing that most
00:49:37
any programmer ever gets a computer to say just because,
00:49:41
but look at this mess.
00:49:42
I mean, there's a hash symbol, these angled brackets, parentheses,
00:49:45
words like int, curly braces, quotes, parentheses, semicolons, and back
00:49:49
slashes.
00:49:50
I mean there's more overhead and more syntax and clutter
00:49:54
than there is an actual idea.
00:49:55
Now that's not to say that you won't be able to understand this before long,
00:50:00
because honestly there's not that many patterns, indeed programming languages
00:50:03
have typically a much smaller vocabulary than any actual human language,
00:50:07
but at first it might indeed look quite cryptic.
00:50:10
But you can perhaps infer I have no idea what these other lines do yet,
00:50:14
but "Hello, world." is presumably quote unquote what
00:50:18
will be printed on the screen.
00:50:19
But what we'll do today, after a short break,
00:50:22
and set the stage for next week is introduce
00:50:24
these exact same ideas in just a bit using Scratch,
00:50:27
something that you yourselves might have used
00:50:29
when you're quite younger but without the same vocabulary applied
00:50:32
to those ideas.
00:50:34
The upside of what we'll soon do using Scratch, this graphical programming
00:50:38
language from our friends down the road at MIT, it'll let us today
00:50:41
start to drag and drop things that look like puzzle pieces that interlock
00:50:46
together if it makes logical sense to do so,
00:50:48
but without the distraction of hashes, parentheses,
00:50:51
curly braces, angle brackets, semicolons, and things
00:50:54
that are quite beside the point.
00:50:56
But for now, let's go ahead and take a 10 minute break here
00:50:58
and when we resume, we will start programming.
00:51:01
So this on the screen is a language called
00:51:04
C something that will dive into next week and thankfully
00:51:07
this now on the screen is another language called Python
00:51:10
that we'll also take a look at in a few weeks before long along
00:51:13
with other languages along the way.
00:51:15
Today though, and for this first week, week zero, so to speak,
00:51:19
we use Scratch because again it will allow
00:51:21
us to explore some of those programming fundamentals
00:51:23
that will be in C and in Python and in JavaScript and other languages, too,
00:51:28
but in a way where we don't have to worry about the distractions of syntax.
00:51:32
So the world of Scratch looks like this.
00:51:35
It's a web-based or downloadable programming environment
00:51:38
that has this layout here by default. On the left here
00:51:40
we'll soon see is a palette of puzzle pieces, programming blocks that
00:51:45
represent all of those ideas we just discussed.
00:51:47
And by dragging and dropping these puzzle pieces
00:51:50
or blocks over this big area and connecting them together,
00:51:54
if it makes logical sense to do so, we'll
00:51:56
start programming in this environment.
00:51:58
The environment allows you to have multiple sprites, so to speak.
00:52:01
Multiple characters, things like a cat or anything
00:52:04
else, and those sprites exist in this rectangular world
00:52:08
up here that you can full screen to make bigger and this here by default
00:52:11
is Scratch, who can move up, down, left, right and do many more things, too.
00:52:16
Within its Scratch's world you can think of it
00:52:19
as perhaps a familiar coordinate system with Xs and Ys
00:52:23
which is helpful only when it comes time to position things on the screen.
00:52:27
Right now Scratch is at the default, 0,0, where x equals 0 and y equals 0.
00:52:32
If you were to move the cat way up to the top, x would stay zero,
00:52:36
y would be positive 180.
00:52:37
If you move the cat all the way to the bottom, x would stay zero,
00:52:40
but y would now be negative 180.
00:52:42
And if you went left, x would become negative 240 but y would stay 0,
00:52:47
or to the right x would be 240 and y would stay zero.
00:52:51
So those numbers generally don't so much matter because you can just
00:52:55
move relatively in this world up, down, left, right,
00:52:58
but when it comes time to precisely position
00:53:01
some of these sprites or other imagery, it'll
00:53:03
be helpful just to have that mental model off up, down, left, and right.
00:53:07
Well let's go ahead and make perhaps the simplest of programs here.
00:53:10
I'm going to switch over to the same programming environment
00:53:13
now for a tour of the left hand side.
00:53:15
So by default selected here are the category in blue motion,
00:53:20
which has a whole bunch of puzzle pieces or blocks that relate to motion.
00:53:24
And whereas Scratch as a graphical language
00:53:27
categorizes things by the type of things that these pieces do,
00:53:30
we'll see that throughout this whole palette
00:53:32
we'll have functions and variables and conditionals
00:53:35
and Boolean expressions and more each in a different color and shape.
00:53:38
So for instance, moving 10 steps or turning one way or the other
00:53:42
would be functions categorized here as things like motion.
00:53:45
Under looks in purple, you might have speech bubbles
00:53:49
that you can create by dragging and dropping
00:53:51
these that might say "hello" or whatever for some number of seconds.
00:53:54
Or you could switch costumes, change the cat to look like a dog or a bird
00:53:58
or anything else in between.
00:54:00
Sounds, too.
00:54:01
You can play sounds like "meow" or anything you might import or record,
00:54:05
yourself.
00:54:06
Then there's these things Scratch calls events and the most important of these
00:54:09
is the first, when green flag clicked.
00:54:11
Because if we look over to the right of Scratch's world here,
00:54:14
this rectangular region has this green flag and red stop
00:54:17
sign up above, one of which is for Play one of which is for Stop
00:54:20
and so that's going to allow us to start and stop our actual programs
00:54:24
when that green flag is initially clicked.
00:54:27
But you can listen for other types of events when the spacebar is pressed
00:54:31
or something else, when this sprite is clicked or something else.
00:54:35
Here you already see like a programmer's incarnation of things
00:54:39
you and I take for granted like every day now on our phones.
00:54:42
Any time you tap an icon or drag your finger or hit a button on the side.
00:54:46
These are what a programmer would call events,
00:54:48
things that happen and are often triggered by us
00:54:51
humans and things that a program be it in Scratch or Python
00:54:55
or C or anything else can listen for and respond to.
00:54:59
Indeed, that's why when you tap the phone icon on your phone,
00:55:01
the phone application starts up because someone
00:55:04
wrote software that's listening for a finger press on that particular icon.
00:55:08
So Scratch has these same things, too.
00:55:10
Under Control in orange, you can see that we
00:55:13
can wait for one second or repeat something
00:55:15
some number of times, 10 by default, but we
00:55:17
can change anything in these white circles to anything else.
00:55:20
There's another puzzle piece here forever,
00:55:22
which implies some kind of loop where we can do something again and again.
00:55:25
Even though it seems a little tight, there's
00:55:27
not much room to fit something there, Scratch
00:55:29
is going to have these things grow and shrink
00:55:31
however we want to fill similarly shaped pieces.
00:55:33
Here are those conditionals.
00:55:35
If something is true or false, then do this next thing.
00:55:40
And that's how we can put in this little trapezoid-like shape.
00:55:42
Some form of Boolean expression, a question with a yes/no, true/false,
00:55:46
or one/zero answer and decide whether to do something or not.
00:55:50
You can combine these things, too.
00:55:52
If something is true, do this, else do this other thing.
00:55:55
And you can even tuck one inside of the other
00:55:57
if you want to ask three or four or more questions.
00:56:01
Sensing, too, is going to be a thing.
00:56:03
You can ask questions aka Boolean expressions like is the sprite touching
00:56:07
the mouse pointer, the arrow on the screen?
00:56:10
So that you can start to interact with these programs.
00:56:12
What is the distance between a sprite and a mouse pointer?
00:56:15
You can do simple calculations just to figure out maybe
00:56:17
if the enemy is getting close to the cat.
00:56:20
Under Operator some lower level stuff like math, but also the ability
00:56:23
to pick random numbers, which for a game is great
00:56:25
because then you can kind of vary the difficulty
00:56:27
or what's happening in a game without the same game playing
00:56:30
the same way every time.
00:56:31
And you can combine ideas.
00:56:33
Something and something must be true in order to make that kind of decision
00:56:37
before.
00:56:38
Or we can even join two words together.
00:56:40
Says apple and banana by default, but you can type in or drag and drop
00:56:43
whatever you want there to combine multiple words into full,
00:56:46
larger sentences.
00:56:48
Then lastly down here, there's in orange things called variables.
00:56:52
In math we've obviously got x and y and whatnot.
00:56:54
In programming we'll have the same ability
00:56:56
to store in these named symbols, x or y, values that we care about.
00:57:03
Numbers or letters or words or colors or anything, ultimately.
00:57:06
But in programming you'll see that it's much more conventional not to just
00:57:09
use simple letters like x and y and z, but to actually
00:57:13
give variables full singular or plural words to describe what they are.
00:57:20
Then lastly, if this isn't enough color blocks for you,
00:57:24
you can create your own blocks.
00:57:25
Indeed, this is going to be a programming principle we'll apply today
00:57:29
and with the first problem set whereby once you start to assemble these puzzle
00:57:32
pieces and you realize, oh, would have been nice if those several pieces could
00:57:37
have just been replaced by one had MIT thought to give me that
00:57:40
one puzzle piece, you yourself can make your own blocks
00:57:44
by connecting these all together, giving them a name, and boom,
00:57:47
a new puzzle piece will exist.
00:57:49
So let's do the simplest, most canonical programs
00:57:51
here, starting up with control, and I'm going
00:57:53
to click and drag and drop this thing here when green flag clicked.
00:57:57
Then I'm going to grab one more, for instance under Looks,
00:58:01
and under Looks I'm going to go ahead and just say
00:58:03
something like initially not just Hello but the more canonical
00:58:07
Hello comma world.
00:58:09
Now you might guess that in this programming environment,
00:58:12
I can go over here now and click the green flag and voila,
00:58:15
Hello comma world.
00:58:17
So that's my first program and obviously much
00:58:19
more user friendly than typing out the much more cryptic text that we
00:58:21
saw on the screen that you, too, will type out next week.
00:58:25
But for now, we'll just focus on these ideas, in this case, a function.
00:58:28
So what it is that just happened?
00:58:29
This purple block here is Say, that's the function,
00:58:32
and it seems to take some form of input in the white oval, specifically Hello
00:58:37
comma world. Well this actually fits the paradigm that we looked
00:58:40
at earlier of just inputs and outputs.
00:58:42
So if I may, if you consider what this puzzle piece is doing,
00:58:45
it actually fits this model.
00:58:47
The input in this case is going to be Hello comma world in white.
00:58:51
The algorithm is going to be implemented as a function by MIT called Say
00:58:55
and the output of that is going to be some kind of side effect,
00:58:57
like the cat and the speech bubble are saying Hello, world.
00:59:01
So already even that simple drag and drop
00:59:03
mimics exactly this relatively simple mental model.
00:59:07
So let's take things further.
00:59:08
Let's go ahead now and make the program a little more interactive so
00:59:11
that it says something like Hello, David, or Hello, Carter,
00:59:14
or Hello to you specifically.
00:59:16
And for this, I'm going to go under Sensing.
00:59:18
And you might have to poke around to find these things the first time
00:59:21
around, but I've done this a few times so I kind of know where things are
00:59:24
and what color.
00:59:25
There's this function here.
00:59:26
Ask what's your name, but that's in white,
00:59:28
so we can change the question to anything
00:59:30
we want, and it's going to wait for the human to type in their answer.
00:59:34
This function called Ask is a little different
00:59:37
from the Say block, which just had this side effect of printing a speech
00:59:41
bubble to the screen.
00:59:42
The ask function is even more powerful in that after it asks the human to type
00:59:47
something in. This function is going to hand you back what
00:59:50
they typed in in the form of what's called a return value, which
00:59:55
is stored ultimately and by default this thing called Answer.
00:59:57
This little blue oval here called Answer is again
01:00:00
one of these variables that in math would
01:00:02
be called just x or y but in programming we're saying what it does.
01:00:05
So I'm going to go ahead and do this.
01:00:07
Let me go ahead and drag and drop this block
01:00:09
and I want to ask the question before saying anything,
01:00:11
but you'll notice that Scratch is smart and it's
01:00:13
going to realize I want to insert something in between
01:00:15
and it's just going to move things up and down.
01:00:17
I'm going to let go and ask the default question, what's your name?
01:00:20
And now if I want to go ahead and say hello, David or Carter,
01:00:23
let's just do Hello comma, because I obviously
01:00:26
don't know when I'm writing the program who's going to use it.
01:00:28
So let me now grab another looks block up here, say something again, and now
01:00:35
let me go back to Sensing and now grab the return value, represented
01:00:39
by this other puzzle piece, and let me just drag and drop it here.
01:00:42
Notice it's the same shape, even if it's not quite the same size.
01:00:45
Things will grow or shrink as needed.
01:00:47
All right, so let's now zoom out.
01:00:49
Let me go and stop the old version because I don't want to say Hello,
01:00:52
world anymore.
01:00:53
Let me hit the green flag and what's my name?
01:00:55
All right, David.
01:00:56
Enter.
01:00:58
Huh.
01:00:59
All right, maybe I just wasn't paying close enough attention.
01:01:01
Let me try it again.
01:01:03
Green flag, D-A-V-I-D, Enter.
01:01:06
This seems like a bug.
01:01:09
What's the bug or mistake might you think?
01:01:13
Yeah?
01:01:14
AUDIENCE: Do you need to somehow add them together in the same text box?
01:01:17
DAVID MALAN: Yeah, we kind of want to combine them in the same text box.
01:01:20
And it's technically a bug because this just looks kind of stupid.
01:01:23
It's just saying David after I asked for my name.
01:01:26
I'd like it to say maybe Hello then David,
01:01:29
but it's just blowing past the Hello and printing David.
01:01:32
But let's put our finger on why this is happening.
01:01:34
You're right for the solution, but what's the actual fundamental problem?
01:01:38
In back.
01:01:39
AUDIENCE: So it says hello, but it gets to that last step
01:01:42
so quickly you can't see it.
01:01:43
DAVID MALAN: Perfect.
01:01:44
I mean, computers are really darn fast these days.
01:01:47
It is saying Hello, all of us are just too slow in this room
01:01:50
to even see it because it's then saying David on the screen so fast as well.
01:01:54
So there's a couple of solutions here, and yours is spot on,
01:01:57
but just to poke around, you'll see the first example
01:01:59
of how many ways in programming be it Scratch or C or Python or anything
01:02:03
else, that there are going to be to solve problems?
01:02:05
We'll teach you over the course of these weeks,
01:02:07
sometimes some ways are better relatively than others,
01:02:10
but rarely is there a best way necessarily,
01:02:13
because again reasonable people will disagree.
01:02:15
And what we'll try to teach you over the coming weeks
01:02:17
is how to kind of think through those nuances.
01:02:20
And it's not going to be obvious at first glance,
01:02:22
but the more programs you write, the more feedback
01:02:24
you get, the more bugs that you introduce,
01:02:26
the more you'll get your footing with exactly this kind of problem solving.
01:02:30
So let me try this in a couple of ways.
01:02:33
Up here would be one solution to the problem.
01:02:35
MIT anticipated this kind of issue, especially with first-time programmers,
01:02:40
and I could just use a puzzle piece that says
01:02:42
say the following for two seconds or one second
01:02:44
or whatever, then do the same with the next word
01:02:47
and it might be kind of a bit of a pause,
01:02:50
Hello, one second, two seconds, David, one second, two seconds, but at least
01:02:54
it would look a little more grammatically correct.
01:02:56
But I can do it a little more elegantly, as you've proposed.
01:02:59
Let me go ahead and throw away one of these blocks,
01:03:01
and you can just drag and let go and it'll delete itself.
01:03:04
Let me go down to Operators because this Join block here is the right shape.
01:03:10
So even if you're not sure what goes where, just focus on the shapes first.
01:03:13
Let me drag this over here.
01:03:15
It grew to fill that.
01:03:17
Let me go ahead and say hello comma space.
01:03:20
Now it could just say by default Hello, banana,
01:03:22
but let me go back to Sensing, Drag answer,
01:03:27
and that's going to drag and drop there.
01:03:29
So now notice we're sort of stacking or nesting one block on another
01:03:34
so that the output of one becomes the input to another, but that's OK here.
01:03:38
Let me go ahead and zoom out, hit Stop, and hit Play.
01:03:42
All right, what's your name?
01:03:43
D-A-V-I-D, Enter, and voila.
01:03:45
Now it's presumably as we first intended.
01:03:48
[APPLAUSE]
01:03:49
(LAUGHING) Oh, thank you.
01:03:55
Thank you.
01:03:56
No minus 2 this time.
01:03:57
So consider that even with this additional example,
01:04:02
it still fits the same mental model, but in a little more interesting way.
01:04:05
Here's that new function Ask something and wait.
01:04:08
And notice that in this case too there's an input, otherwise known
01:04:12
henceforth as an argument or a parameter, programming
01:04:15
speak for just an input in the context of a function.
01:04:18
If we use our drawing as before to represent this thing here,
01:04:21
we'll see that the input now is going to be quote unquote "What's your name?"
01:04:26
The algorithm is going to be implemented by way of this new puzzle piece,
01:04:30
the function called Ask, and the output of that thing this time
01:04:33
is not going to be the cat saying anything yet,
01:04:36
but rather it's going to be the actual answer.
01:04:39
So instead of the visual side effect of the speech bubble appearing,
01:04:43
now nothing visible is happening yet.
01:04:45
Thanks to this function it's sort of handing me back like a scrap of paper
01:04:49
with whatever I typed in written on it so I can reuse D-A-V-I-D one or more
01:04:55
times even like I did.
01:04:57
Now what did I then do with that value?
01:05:00
Well consider that with the subsequent function
01:05:04
we had this Say block, too, combined with a join.
01:05:08
So we have this variable called Answer, we're joining it
01:05:11
with that first argument, Hello.
01:05:14
So already we see that some functions like Join
01:05:16
can take not one but two arguments, or inputs, and that's fine.
01:05:20
The output of Join is presumably going to be Hello, David or Hello, Carter
01:05:24
or whatever the human typed in.
01:05:27
That output notice is essentially becoming the input to another function,
01:05:31
Say, just because we've kind of stacked things
01:05:33
or nested them on top of one another.
01:05:35
But methodically, it's really the same idea.
01:05:40
The input now are two things, Hello comma and the return value
01:05:44
from the previous Ask function.
01:05:47
The function now is going to be Join, the output is going to be Hello, David.
01:05:51
But that Hello, David output is now going
01:05:53
to become the input to another function, namely that first block called Say,
01:05:57
and that's then going to have the side effect of printing out Hello, David
01:06:01
on the screen.
01:06:02
So again as sort of sophisticated as ours as yours as others programs
01:06:06
are going to get, they really do fit this very simple mental model
01:06:09
of inputs and outputs and you just have to learn to recognize the vocabulary
01:06:12
and to know what kinds of puzzle pieces or concepts ultimately to apply.
01:06:17
But you can ultimately really kind of spice these things up.
01:06:19
Let me go back to my program here that just is
01:06:21
using the speech bubble at the moment.
01:06:22
Scratch's inside has some pretty fancy interactive features, too.
01:06:26
I click the Extensions button in the bottom left corner.
01:06:28
And let me go ahead and choose the Text to Speech extension.
01:06:33
This is using a Cloud service, so if you have an internet connection
01:06:36
it can actually talk to the Cloud or a third party service,
01:06:39
and this one is going to give me a few new green puzzle pieces, namely
01:06:42
the ability to speak something from my speakers
01:06:45
instead of just saying it textually.
01:06:47
So let me go ahead and drag this.
01:06:48
Now notice I don't have to interlock them if I'm just kind of playing around
01:06:51
and I want to move some things around.
01:06:52
I just want to use this as like a canvas temporarily.
01:06:55
Let me go ahead and steal the Join from here,
01:06:58
put it there, let me throw away the Say block by just moving it
01:07:01
left and letting go, and now let me join this in
01:07:04
so I've now changed my program to be a little more interesting.
01:07:08
So now let me stop the old version.
01:07:10
Let me start the new.
01:07:11
What's your name?
01:07:12
Type in David.
01:07:13
And voila:
01:07:14
PROGRAM: Hello, banana.
01:07:18
DAVID MALAN: (LAUGHING) OK, minus 2 for real.
01:07:20
All right, so what I accidentally threw away there, intentionally
01:07:27
for instructional purposes, was the actual answer
01:07:30
that came back from the ask block.
01:07:32
That's embarrassing.
01:07:33
So now if I play this again, let's click the green icon.
01:07:37
What's your name?
01:07:38
David. And now:
01:07:40
PROGRAM: Hello, David.
01:07:41
DAVID MALAN: There we go.
01:07:42
Hello, David.
01:07:43
All right, thank you.
01:07:45
[APPLAUSE]
01:07:50
OK, so we have these functions then in place, but what more can we do?
01:07:54
Well what about those conditionals and loops and other constructs?
01:07:57
How can we bring these programs to life so it's not just
01:07:59
clicking a button and voila, something's happening?
01:08:01
Let's go ahead and make this now even more interactive.
01:08:04
Let me go ahead and throw away most of these pieces
01:08:06
and let me just spice things up with some more audio under Sound.
01:08:09
I'm going to go to Play Sound Meow until done.
01:08:12
Here we go, green flag.
01:08:13
[MEOW]
01:08:15
OK, it's a little loud, but it did exactly do what it said.
01:08:18
Let's hear it again.
01:08:19
[QUIETER MEOW]
01:08:21
OK. It's kind of an underwhelming program eventually
01:08:23
since you'd like to think that the cat would just meow on its own, but.
01:08:26
[MEOW] I have to keep hitting the button.
01:08:28
Well this seems like an opportunity for doing something again and again.
01:08:31
So all right, well if I wanted to meow, meow, meow,
01:08:33
let me just grab a few of these, or you can even right click or Control click
01:08:37
and you can Copy Paste even in code here.
01:08:39
Let me play this now.
01:08:41
[THREE MEOWS]
01:08:43
All right, so now like it's not really emoting
01:08:45
happiness in quite the same way.
01:08:46
It might be hungry or upset.
01:08:47
So let's slow it down.
01:08:49
Let me go to Control, wait one second in between,
01:08:52
which might be a little less worrisome.
01:08:55
Here we go, Play.
01:08:56
[THREE SLOWER MEOWS]
01:09:01
OK, so if my goal was to make the cat meow three times,
01:09:06
I dare say this code or algorithm is correct.
01:09:10
But let's now critique its design.
01:09:12
Is this well-designed?
01:09:14
And if not, why not?
01:09:17
What are your thoughts here?
01:09:19
Yeah?
01:09:21
AUDIENCE: You could use the forever or a repeat to make it more--
01:09:27
DAVID MALAN: Yeah, so yeah, agreed.
01:09:28
I could use forever or repeat, but let me push a little harder.
01:09:31
But why? Like this works, I'm kind of done with the assignments, what's bad about it?
01:09:36
AUDIENCE: There's too much repetition.
01:09:37
DAVID MALAN: Yeah, there's too much repetition, right?
01:09:40
If I wanted to change the sound that the cat is making
01:09:42
to a different variant of meow or have it bark instead like a dog,
01:09:46
I could change it from the dropdown here apparently,
01:09:48
but then I'd have to change it here and then I'd have to change it here,
01:09:51
and God, if this were even longer that just gets tedious quickly
01:09:54
and you're probably increasing the probability
01:09:56
that you're going to screw up and you're going
01:09:57
to miss one of the dropdowns or something stupid and introduce a bug.
01:10:00
Or, if you wanted to change the number of seconds you're waiting,
01:10:02
you've got to change it in two, maybe even more places.
01:10:05
Again, you're just creating risk for yourself
01:10:07
and potential bugs in the program.
01:10:09
So I do like the repeat or the forever idea so that I don't repeat myself.
01:10:13
And indeed, what I alluded to being possible,
01:10:15
copy pasting earlier, doesn't mean it's a good thing.
01:10:18
And in code, generally speaking, when you
01:10:20
start to copy and paste puzzle pieces or text next week,
01:10:23
you're probably not doing something quite well.
01:10:26
So let me go ahead and throw away most of these to get rid of the duplication,
01:10:30
keeping just two of the blocks that I care about.
01:10:33
Let me grab the Repeat block for now, let me move this inside of the Repeat
01:10:37
block, it's going to grow to fit it, let me reconnect all this
01:10:40
and change the 10 just to a 3, and now, Play.
01:10:44
[THREE SLOW MEOWS]
01:10:49
So, better.
01:10:50
It's the same thing.
01:10:51
It's still correct, but now I've set the stage
01:10:53
to let the cat meow, for instance, four times by changing one thing,
01:10:57
40 times by changing one thing, or it could just use the Forever block
01:11:00
and just walk away and it will meow forever instead.
01:11:03
If that's your goal, that would be better.
01:11:05
A better design but still correct.
01:11:07
But you know what?
01:11:08
Now that I have a program that's designed
01:11:10
to have a cat meow, wow like why?
01:11:13
I mean, MIT invented Scratch, Scratch as a cat,
01:11:16
why is there no puzzle piece called Meow?
01:11:18
This feels like a missed opportunity.
01:11:20
Now to be fair, they gave us all the building blocks
01:11:22
with which we could implement that idea, but a principle of programming
01:11:26
and really computer science is to leverage what we're
01:11:28
going to now start calling Abstraction.
01:11:30
We have step-by-step instructions here, the Repeat, the Play,
01:11:34
and the Wait that collectively implements this idea
01:11:37
that we humans would call meowing.
01:11:38
Wouldn't it be nice to abstract away those several puzzle
01:11:41
pieces into just one that literally just says what it does, meow?
01:11:45
Well here's where we can make our own blocks.
01:11:48
Let me go over here to Scratch under the pink block category
01:11:52
here and let me click Make a Block.
01:11:55
Here I see a slightly different interface
01:11:57
where I can choose a name for it and I'm going to call it Meow.
01:12:00
I'm going to keep it simple.
01:12:01
That's it. No inputs to meow yet.
01:12:03
I'm just going to click OK.
01:12:05
Now I'm just going to clean this up a bit here.
01:12:08
Let me drag and drop Play Sound and Wait over here.
01:12:12
And you know what?
01:12:13
I'm just going to drag this way down here, way down
01:12:15
here because now that I'm done implementing Meow,
01:12:18
I'm going to literally abstract it away, sort of out of sight,
01:12:21
out of mind, because now notice at top left there is a new pink puzzle
01:12:25
piece called Meow.
01:12:26
So at this point, I'd argue it doesn't really matter how Meow is implemented.
01:12:31
Frankly, I don't know how Ask or Say was implemented by MIT.
01:12:35
They abstracted those things away for us.
01:12:37
Now I have a brand new puzzle piece that just says what it is.
01:12:40
And this is now still correct, but arguably better design.
01:12:44
Why?
01:12:45
Because it's just more readable to me, to you,
01:12:47
it's more maintainable when you look at your code
01:12:49
a year from now for the first time because you're sort of finally looking
01:12:52
back at the very first program you wrote.
01:12:53
It says what it does.
01:12:55
The function itself has semantics, which conveys what's going on.
01:12:59
If you really care about how Meow is implemented,
01:13:01
you could scroll down and start to tinker with the underlying
01:13:04
implementation details, but otherwise you don't need to care anymore.
01:13:09
Now I feel like there's an even additional opportunity
01:13:13
here for abstraction and to factor out some of this functionality.
01:13:17
It's kind of lame that I have this Repeat block that
01:13:21
lets me call the Meow function, so to speak, use the Meow function
01:13:24
three times.
01:13:25
Wouldn't it be nice if I could just call them Meow function,
01:13:28
aka use the Meow function, and pass it in input that tells the puzzle
01:13:32
piece how many times I want it to meow?
01:13:35
Well let me go ahead and zoom out and scroll down.
01:13:37
Let me right click or Control click on the pink piece here and choose Edit,
01:13:41
or I could just start from scratch, no pun intended, with a new one.
01:13:44
Now here, rather than just give this thing a name Meow, let me go ahead
01:13:48
and add an input here.
01:13:50
I'm going to go ahead and type in, for instance, n,
01:13:53
for number of times to meow, and just to make
01:13:56
this even more user friendly and self descriptive,
01:13:58
I'm going to add a label, which has no functional impact,
01:14:00
it's just an aesthetic, and I'm just going
01:14:03
to say Times, just to make it read more like English
01:14:05
in this case that tells me what the puzzle piece does.
01:14:08
Now I'm going to click OK.
01:14:09
And now I need to refine this a little bit.
01:14:12
Let me go ahead and grab under Control a repeat block,
01:14:18
let me move the Play, Sound, and Wait, into the repeat block.
01:14:22
I don't want 10 and I also don't want 3 here.
01:14:24
What I want now is this n that is my actual variable that Scratch
01:14:29
is creating for me that represents whatever input the human programmer
01:14:33
provides.
01:14:34
Notice that snaps right in place.
01:14:35
Let me connect this and now voila, I have an even fancier version of Meow
01:14:39
that is parameterized.
01:14:40
It takes input that affects its behavior accordingly.
01:14:44
Now I'm going to scroll back up, because out of sight, out of mind,
01:14:47
I just care that Meow exists.
01:14:49
Now I can tighten up my code, so to speak, use even fewer lines
01:14:52
to do the same thing by throwing away the Repeat block,
01:14:55
reconnecting this new puzzle piece here that takes an input like 3 and voila,
01:15:00
now we're really programming, right?
01:15:02
We've not made any forward progress functionally.
01:15:04
The thing just mouse three times.
01:15:06
But it's a better design.
01:15:09
As you program more and more, these are the kinds
01:15:11
of instincts still start to acquire so that one,
01:15:13
you can start to take a big assignment, a big problem set, something
01:15:16
for homework even, that feels kind of overwhelming at first, like, oh my God
01:15:20
where do I even begin?
01:15:21
But if you start to identify what are the subproblems of a bigger problem?
01:15:25
Then you can start making progress.
01:15:27
I do this to this day where if I have to tackle some programming-related project
01:15:32
it's so easy to drag my feet and ugh, it's going to take forever to start,
01:15:36
until I just start writing down like a to do list
01:15:38
and I start to modularize the program and say, all right, well
01:15:41
what do I want this thing to do?
01:15:42
Meowing.
01:15:43
What's that mean? I've got to have it say something on the screen.
01:15:45
All right, I need to have it say something on the screen
01:15:48
some number of times.
01:15:49
Like literally a mental or written checklist, or pseudocode code,
01:15:52
if you will, in English on a piece of paper or text file,
01:15:55
and then you can decide, OK, the first thing I
01:15:57
need to do for homework to solve this real world problem,
01:16:00
I just need a Meow function.
01:16:02
I need to use a bunch of other code, too,
01:16:04
but I need to create a Meow function and boom,
01:16:06
now you have a piece of the problem solved not unlike we did with the phone
01:16:10
book there, but in this case, we'll have presumably other problems to solve.
01:16:14
All right, so what more can we do?
01:16:16
Let's add a few more pieces to the puzzle here.
01:16:18
Let's actually interact with the cat now.
01:16:20
Let me go ahead and now when the green flag is clicked, let me go ahead
01:16:24
and ask a question using an event here.
01:16:26
Let me go ahead and say, let's see, I want
01:16:31
to do something like implement the notion of petting the cat.
01:16:34
So if the cursor is touching the cat like here, something like this,
01:16:39
it'd be cute if the cat meows like you're petting a cat.
01:16:42
So I'm going to ask the question, when the green flag is clicked,
01:16:45
if let's see I think I need Sensing.
01:16:48
So if touching mouse pointer, this is way too big
01:16:50
but again the shape is fine, so there goes.
01:16:52
Grew to fill.
01:16:53
And then if it's touching the mouse pointer,
01:16:56
that is if the cat to whom this script or this program,
01:16:59
any time I attach puzzle pieces MIT calls them a script
01:17:03
or like a program, if you will, let me go ahead then and choose a sound
01:17:07
and say play sound meow until done.
01:17:10
All right, so here it is to be clear.
01:17:11
When the green flag is clicked, ask the question,
01:17:13
if the cat is touching the mouse pointer then place sound meow.
01:17:18
Here we go. Play.
01:17:20
[SILENCE]
01:17:23
All right, let's try again.
01:17:25
Play.
01:17:26
[SILENCE]
01:17:28
Huh.
01:17:29
I'm worried it's not Scratch's fault. Feels like mine.
01:17:34
What's the bug here?
01:17:37
Why doesn't this work?
01:17:39
Yeah, in back, who just turned.
01:17:42
AUDIENCE: [INAUDIBLE]
01:17:47
DAVID MALAN: Yeah, the problem is the moment I click that green flag,
01:17:50
Scratch asks the question, is the cat touching the mouse pointer?
01:17:54
And obviously it's not because the cursor was like up there a moment ago
01:17:57
and it's not down there.
01:17:58
It's fine if I move the cursor down there, but too late.
01:18:01
The program already asked the question.
01:18:03
The answer was no or false or zero, however you want to think about it,
01:18:06
so no sound was played.
01:18:08
So what might be the solution here be?
01:18:10
I could move my cursor quickly, but that feels
01:18:12
like never going to work out right.
01:18:14
Other solutions here?
01:18:16
Yeah, in way back?
01:18:18
Could you use the forever loop?
01:18:20
The Forever loop.
01:18:21
So I could indeed use this Forever loop because if I want my program
01:18:24
to just constantly listen to me, well let's literally do something forever,
01:18:28
or at least forever as long as the program is
01:18:30
running until I explicitly hit Stop.
01:18:32
So let me grab that.
01:18:33
Let me go to Control, let me grab the Forever block,
01:18:36
let me move the If inside of this Forever block, reconnect this,
01:18:40
go back up here, click the green flag, and now nothing's happened yet,
01:18:43
but let me try moving my cursor now.
01:18:45
[MEOW]
01:18:47
Oh. So now.
01:18:48
[MEOW]
01:18:49
That's kind of cute.
01:18:50
So now the cat is actually responding and it's
01:18:52
going to keep doing this again and again.
01:18:54
So now we have this idea of taking these different ideas, these different puzzle
01:18:58
pieces, assembling them into something more complicated.
01:19:01
I could definitely put a name to this.
01:19:03
I could create a custom block, but for now
01:19:05
let's just consider what kind of more interactivity we can do.
01:19:08
Let me go ahead and do this.
01:19:09
By again grabbing a, when green flag clicked,
01:19:12
let me go ahead and click the video sensing,
01:19:16
and I'm going to rotate the laptop because otherwise we're
01:19:18
going to get a little inception thing here where the camera is picking up
01:19:21
the camera is up there.
01:19:22
So I'm going to go reveal to you what's inside the lectern
01:19:25
here while we rotate this.
01:19:29
Now that we have a non video backdrop, I'm going to say this.
01:19:34
Instead of the green flag clicked, actually, I'm
01:19:37
going to say when the video motion is greater than some arbitrary
01:19:40
measurement of motion, I'm going to go ahead and play sound meow until done.
01:19:47
And then I'm going to get out of the way.
01:19:48
So here's the cat.
01:19:50
We'll put them on top of there.
01:19:53
[MEOW]
01:19:54
OK.
01:19:55
All right, and here we go.
01:19:57
[MEOW]
01:20:00
So my hand is moving faster than 50 something or other,
01:20:03
whatever the unit of measure is.
01:20:05
[MEOW]
01:20:06
AUDIENCE: Aw. DAVID MALAN: (LAUGHING) Thank you.
01:20:08
So now we have an even more interactive version.
01:20:10
[MEOW]
01:20:12
But I think if I sort of slowly.
01:20:15
[LAUGHING]
01:20:17
(LAUGHING) Right?
01:20:18
It's completely creepy, but I'm not like exceeding the threshold--
01:20:23
[MEOW]
01:20:24
Until finally my hand moves as fast as that.
01:20:27
And so here actually is an opportunity to show you
01:20:29
something a former student did.
01:20:31
Let me go ahead here and--
01:20:32
[MEOW TWICE]
01:20:33
OK, got to stop this.
01:20:35
Let me go ahead and zoom out of this in just a moment.
01:20:38
[MEOW] If someone would be--
01:20:39
[LAUGHING]
01:20:40
(LAUGHING) If someone would be comfortable
01:20:42
coming up not only masked but also on camera on the internet
01:20:44
I thought we'd play one of your former classmate's projects here up on stage.
01:20:48
Would anyone like to volunteer here and be up on stage?
01:20:51
Who's that? Yeah.
01:20:52
Come on down. What's your name?
01:20:53
AUDIENCE: Sahar.
01:20:54
DAVID MALAN: Sahar.
01:20:55
All right, come on down.
01:20:57
Let me get it set up for you here.
01:20:58
[MEOW]
01:20:59
[APPLAUSE]
01:21:05
[MEOW]
01:21:10
All right, let me go ahead and full screen this here.
01:21:13
So this is whack-a-mole by one of your firmer predecessors.
01:21:17
It's going to use the camera focusing on your head, which will have
01:21:20
to position inside of this rectangle.
01:21:22
Have you ever played the whack-a-mole game at an arcade?
01:21:24
AUDIENCE: Yeah.
01:21:25
DAVID MALAN: OK. So for those who haven't, these little moles pop up
01:21:27
and with a very fuzzy hammer you sort of hit down.
01:21:29
You though, if you don't mind, you're going
01:21:31
to use your head to do this virtually.
01:21:34
So let's line up your head with this red rectangle, if you could,
01:21:39
we'll do beginner.
01:21:40
[MUSIC PLAYING]
01:21:41
All right, here we go.
01:21:42
Sahar.
01:21:44
Give it a moment.
01:21:45
OK, come a little closer.
01:21:47
[DINGING]
01:21:48
And now hit the moles with your head.
01:21:50
[DING]
01:21:51
There we go, one point.
01:21:53
[DING] One point.
01:21:56
[DINGING]
01:21:58
Nice.
01:22:00
15 seconds to go.
01:22:01
There we go. Oh yeah.
01:22:02
One point.
01:22:04
[LAUGHING]
01:22:06
[DINGING]
01:22:07
Six seconds.
01:22:09
AUDIENCE: Oh no.
01:22:10
DAVID MALAN: There we go.
01:22:11
Quick!
01:22:12
[DINGING]
01:22:14
All right, a round of applause for Sahar.
01:22:16
Thank you.
01:22:18
[APPLAUSE]
01:22:24
So beyond having a little bit of fun here,
01:22:26
the goal was to demonstrate that by using
01:22:28
some fairly simple, primitive, some basic building blocks
01:22:31
but assembling them in a fun way with some music, maybe
01:22:34
some new costumes or artwork, you can really bring programs to life.
01:22:37
But at the end of the day, the only puzzle pieces really involved
01:22:40
were ones like the ones I just dragged and dropped and a few more,
01:22:43
because there were clearly lots of moles.
01:22:45
So the student probably created a few different sprites, not a single cap,
01:22:49
but at least four different moles.
01:22:50
They had like some kind of graphic on the screen that showed Sahar
01:22:53
where to position her head.
01:22:54
There were some kind of timer, maybe a variable
01:22:57
that every second was counting down.
01:22:59
So you can imagine taking what looks like a pretty impressive project
01:23:02
at first glance, and perhaps overwhelming
01:23:04
to solve yourself, but just think about what are the basic building blocks?
01:23:07
And pluck off one piece of the puzzle, so to speak, at a time.
01:23:12
So indeed if we rewind a little bit.
01:23:15
Let me go ahead here and introduce a program
01:23:17
that I myself made back in graduate school
01:23:20
when Scratch was first being developed by MIT.
01:23:23
Let me go ahead and open here, give me just one second,
01:23:26
something that I called back in the day Oscar Time that
01:23:30
looks a little something like this.
01:23:32
If I fullscreen it and hit Play.
01:23:34
[MUSIC - SESAME STREET, "I LOVE TRASH"]
01:23:38
OSCAR THE GROUCH: (SINGING) Oh, I love trash.
01:23:40
DAVID MALAN: So you'll notice a piece of trash is falling.
01:23:42
I can click on it and drag and as I get close and close to the trash can notice
01:23:45
OSCAR THE GROUCH: (SINGING) Anything ragged or--
01:23:47
DAVID MALAN: It wants to go in, it seems.
01:23:49
And if I let go--
01:23:50
OSCAR THE GROUCH: (SINGING) Yes, I--
01:23:52
DAVID MALAN: One point.
01:23:54
Here comes another. OSCAR THE GROUCH: (SINGING) If you really want to see something trashy--
01:23:56
DAVID MALAN: I'll do the same, two points.
01:23:57
OSCAR THE GROUCH: (SINGING) I have here a sneaker that's tattered and worn--
01:24:00
DAVID MALAN: There's a sneaker falling from the sky,
01:24:01
so another sprite of some sort.
01:24:03
OSCAR THE GROUCH: (SINGING) The laces are torn.
01:24:05
A gift from my mother--
01:24:07
DAVID MALAN: I can also get just a little lazy
01:24:09
and just let them fall into the trash themself if I want to.
01:24:13
So you can see it doesn't have to do with my mouse cursor,
01:24:15
it has to do apparently with the distance here.
01:24:18
Let's listen a little further.
01:24:19
I think some additional trash is about to make its appearance.
01:24:23
Presumably there's some kind of variable that's keeping track of this score.
01:24:26
OSCAR THE GROUCH: (SINGING) I love--
01:24:28
DAVID MALAN: OK, let's see what the last chorus here is.
01:24:30
OSCAR THE GROUCH: (SINGING) Rotten stuff.
01:24:32
I have here some newspaper, crusty and
01:24:35
DAVID MALAN: OK, and thus he continues.
01:24:37
And the song actually goes on and on and on
01:24:40
and I do not have fond memories of implementing this and hearing
01:24:43
this song for like 10 straight hours, but it's
01:24:46
a good example to just consider how was this program composed?
01:24:50
How did I go about implementing it the first time around?
01:24:52
And let me go ahead and open up some programs now
01:24:54
that I wrote in advance just so that we could
01:24:56
see how these things are assembled.
01:24:58
Honestly, the first thing I probably did was probably
01:25:02
to do something a little like this.
01:25:04
Here is just a version of the program where
01:25:07
I set out to solve just one problem first
01:25:09
of planting a lamp post in the program.
01:25:12
Right? I kind of had a vision of what I wanted.
01:25:14
You know, it evolved over time, certainly,
01:25:15
but I knew I wanted trash to fall, I wanted
01:25:17
a cute little Oscar the Grouch to pop out
01:25:19
of the trashcan, and some other stuff, but wow that's a lot
01:25:22
to just tackle all at once.
01:25:23
I'm going to start easy, download a picture of a lamp post,
01:25:26
and then drag and drop it into the stage as a costume and boom, that's
01:25:30
version one.
01:25:31
It doesn't functionally do anything.
01:25:33
I mean, literally that's the code that I wrote to do this.
01:25:36
All I did was use like the Backdrops feature
01:25:38
and drag and drop and move things around,
01:25:41
but it got me to version one of my program.
01:25:44
Then what might version two be?
01:25:46
Well I considered what piece of functionality
01:25:48
frankly might be the easiest to pluck off next and the trash can.
01:25:52
That seems like a pretty core piece of functionality.
01:25:54
It just needs to sit there most of the time.
01:25:56
So the next thing I probably did was to open up,
01:25:59
for instance, the trash can version here that looks a little something now
01:26:05
like this.
01:26:06
So this time I'll show you what's inside here.
01:26:08
There is some code, but not much.
01:26:10
Notice at bottom right I change the default cat to a picture of a trashcan,
01:26:14
instead, but it's the same principle that I can control.
01:26:17
And then over here I added this code.
01:26:20
When the green flag is clicked, switch the costume
01:26:23
to something I arbitrarily called Oscar 1.
01:26:25
So I found a couple of different pictures
01:26:26
of a trash can, one that looks closed, one that looks partly open,
01:26:29
and eventually one that has Oscar coming out,
01:26:32
and I just gave them different names.
01:26:33
So I said Switch to Oscar 1, which is the closed one by default,
01:26:36
then forever do the following: if touching the mouse pointer,
01:26:40
then switch the costume to Oscar 2, else switch to Oscar 1.
01:26:45
That is to say, I just wanted to implement this idea of the can opening
01:26:49
and closing, even if it's not exactly what I wanted ultimately,
01:26:52
I just wanted to make some forward progress.
01:26:54
So here, when I run this program by clicking Play, notice what happens.
01:26:59
Nothing yet, but if I get closer to the trash can,
01:27:03
it indeed pops open because it's forever listening
01:27:07
for whether the sprite, the trash can in this case,
01:27:10
is touching the mouse pointer.
01:27:11
And that's it.
01:27:12
That was version 2, if you will.
01:27:15
If I went in now and added the lamp post and compose the program together,
01:27:18
now we're starting to make progress.
01:27:20
Right? Now it would look a little something more like the program
01:27:22
I intended ultimately to create.
01:27:25
What piece did I probably bite off after that?
01:27:27
Well, I think what I did is I probably decided
01:27:30
let me implement one of the pieces of trash, not the shoe in the newspaper
01:27:33
all at once.
01:27:34
Let's just get one piece of trash working correctly first.
01:27:37
So let me go ahead and open this one.
01:27:40
And again, all of these examples will be available on the course's website
01:27:43
so you can see all of these examples, too.
01:27:46
It's not terribly long, I just implement it in advance
01:27:48
so we could flip through kind of quickly.
01:27:50
Here's what I did here.
01:27:52
On the right hand side, I turned my sprite into a piece of trash
01:27:56
this time instead of a cat, instead of a trash can,
01:27:58
and I also created, with Carter's help, a second sprite, this one a floor.
01:28:04
It's literally just a black line because I just wanted initially
01:28:07
to have some notion of a floor so I could detect
01:28:09
if the trash is touching the floor.
01:28:12
Now without seeing the code yet, just hearing that description,
01:28:15
why might I have wanted the second sprite and this black line for a floor
01:28:20
with the trash intending to fall from the sky?
01:28:22
What might I have been thinking?
01:28:24
Like what problem might I be trying to solve?
01:28:26
Yeah? AUDIENCE: You don't want the first sprite to go through it.
01:28:28
DAVID MALAN: Yeah, you don't want the first sprite to start at the top,
01:28:31
go through, and then boom, you completely lose it.
01:28:34
That would not be a very useful thing.
01:28:36
Or it would seem to maybe eat up more and more of the computer's memory
01:28:39
if the trash is just endlessly falling and I can't grab it.
01:28:42
It might be a little traumatic if you tried to get it
01:28:44
and you can't pull it back out and you can't fix the program.
01:28:46
So I just wanted the thing to stop.
01:28:48
So how might I have implemented this?
01:28:50
Let's look at the code at left.
01:28:51
Here I have a bit of randomness, like I proposed earlier exists.
01:28:56
There's this blue function called Go To x,
01:28:59
y that lets me move a sprite to any position,
01:29:03
up, down, left, right, I picked a random x location, either here or over here,
01:29:07
negative 240 to positive 240, and then a y value of 180, which is the top.
01:29:12
This just makes the game more interesting.
01:29:14
It's kind of lame pretty quickly if the trash always falls from the same spot.
01:29:18
Here's this a little bit of randomness, like most any game would have,
01:29:21
that spices things up.
01:29:23
So now if I click the green flag, you'll see that it just falls,
01:29:26
nothing interesting is going to happen, but it
01:29:28
does stop when it touches the black line because notice what we did here.
01:29:33
I'm forever asking the question if the distance of the sprite, the trash,
01:29:37
is to the floor is greater than zero, that's fine.
01:29:41
Change the y location by negative 3.
01:29:44
So move it down 3 pixels, down 3 pixels, until the distance to the floor
01:29:48
is not greater than zero, it is zero or even negative, at which point
01:29:52
it should just stop moving altogether.
01:29:54
There's other ways we could have implemented this,
01:29:56
but this felt like a nice, clean way that logically, just
01:29:58
made it make sense.
01:29:59
OK, now I got some trash falling, I got a trash can that opens and closes,
01:30:03
I have a lamp post, now I'm a good three steps into the program.
01:30:08
We're making progress.
01:30:09
If we consider one or two final pieces, something
01:30:12
like the dragging of the trash, let me go ahead and open up this version 2.
01:30:17
Dragging the trash requires a different type of question.
01:30:21
Let me zoom in here.
01:30:22
Here's the piece of trash.
01:30:24
I only need one sprite, no floor here because I just
01:30:26
want the human to move it up, down, left, right and the human's
01:30:29
not going to physically be able to move it outside of the world.
01:30:32
If we zoom in on this code, the way we've solved this is as follows.
01:30:36
We're using that And conjunction that we glimpsed earlier because when
01:30:40
the green flag is clicked, we're forever asking this question or really
01:30:44
these questions, plural, if the mouse is down
01:30:47
and the trash is touching the mouse pointer, that's equivalent
01:30:53
logically to clicking on the trash.
01:30:55
Go ahead and move the trash to the mouse pointer.
01:30:58
So again it takes this very familiar idea
01:31:00
that you and I take for granted every day on Macs and PCs of clicking
01:31:04
and dragging and dropping.
01:31:05
How is that implemented?
01:31:06
Well Mac OS or Windows are probably asking a question.
01:31:10
For every icon, is the mouse down and is the icon touching the mouse?
01:31:15
If so, go to the location of the mouse forever
01:31:19
while the mouse button is clicked down.
01:31:21
So how does this work in reality now?
01:31:23
Let me go ahead and click on the Play.
01:31:25
Nothing happens at first, but if I click on it, I can move it up,
01:31:30
down, left, right.
01:31:31
It doesn't move thereafter.
01:31:33
So I now need to kind of combine this idea of dragging with falling,
01:31:36
but I bet I could just start to use just one single program.
01:31:39
Right now I'm using separate ones to show different ideas,
01:31:42
but now that's another bite out of the problem.
01:31:45
If we do one last one, something like the scorekeeping
01:31:48
is interesting, because recall that every time we dragged a piece of trash
01:31:51
into the can, Oscar popped out and told us the current score.
01:31:54
So let me go ahead and find this one, Oscar variables,
01:31:59
and let me zoom in on this one.
01:32:01
This one is longer because we combined all of these elements.
01:32:04
So this is the kind of thing that if you looked at first glance, like,
01:32:07
I have no idea how I would have implemented this
01:32:09
from nothing, from scratch literally.
01:32:12
But again, if you take your vision and componenitize it
01:32:16
into these smaller, bite-sized problems, you
01:32:18
could take these baby steps, so to speak, and then
01:32:21
solve everything collectively.
01:32:22
So what's new here is this bottom one.
01:32:25
Forever do the following: if the trash is touching
01:32:30
Oscar, the other sprite that we've now added to the program,
01:32:33
change the score by 1.
01:32:35
This is an orange and indeed if we poke around
01:32:37
we'll see that orange is a variable, like an x or y but with a better name,
01:32:42
changing it means to add 1 or if it's negative subtract 1.
01:32:46
Then go ahead and have the trash go to pick random.
01:32:51
What is this all about?
01:32:53
Well, let me show you what it's doing and then we can infer backwards.
01:32:56
Let me go ahead and hit Play.
01:32:58
All right, it's falling, I'm clicking and dragging it, I'm moving it over,
01:33:01
and I'm letting go.
01:33:02
All right, let me do it once more.
01:33:04
Letting go, let me stop.
01:33:06
Why do I have this function at the end called Go To x and y randomly?
01:33:13
Like what problem is this solving here?
01:33:16
Yeah, in way back.
01:33:18
AUDIENCE: Just the same track teleported to the top
01:33:21
after you put it in the trash can.
01:33:23
DAVID MALAN: Yeah, exactly.
01:33:24
Even though the human perceives this as like a lot of trash falling
01:33:27
from the sky, it's actually the same piece
01:33:29
of trash, just kind of being magically moved back to the top
01:33:32
as though it's a new one.
01:33:33
There, too, you have this idea of reusable code.
01:33:36
If you were constantly copying and pasting your pieces of trash
01:33:40
and creating 20 pieces of trash, 30 pieces of trash, just because you
01:33:43
want the game to have that many levels, probably doing something wrong.
01:33:46
Reuse the code that you wrote, reuse the sprites that you wrote,
01:33:49
and that would give you not just correctness, but also a better design.
01:33:54
Well let's take a look at one final set of building blocks
01:33:57
that we can compose ultimately into something
01:33:59
particularly interactive as follows.
01:34:02
Let me go ahead and zoom out here and let
01:34:03
me propose that we implement something like some kind of maze-based game.
01:34:08
Let me go ahead here.
01:34:09
So I want to implement some maze-based game that
01:34:13
looks at first glance like this.
01:34:15
Let me hit Play. It's not a very fun game yet, but here's a little Harvard
01:34:18
shield, a couple of black lines, this time vertical instead of horizontal,
01:34:22
but notice you can't quite see my hand here,
01:34:24
but I'm using my arrow keys to go down, to go up, to go left, to go right,
01:34:28
but if I keep going right, right, right, right, right, right,
01:34:31
right it's not going anywhere.
01:34:32
And left, left, left, left, left, left, left, left, left, left, left, left,
01:34:35
left it eventually stops.
01:34:36
So before we look at the code, how might this be working?
01:34:41
What kinds of scripts, collections of puzzle pieces,
01:34:44
might collectively help us implement this?
01:34:47
What do you think?
01:34:48
AUDIENCE: [INAUDIBLE]
01:34:56
DAVID MALAN: Perfect, yeah.
01:34:57
There's probably some question being asked, if touching the black line,
01:35:00
and it happens to be a couple of sprites, each of which
01:35:03
is just literally a vertical black line we're probably asking a question like,
01:35:06
are you touching it?
01:35:07
Is the distance to it zero or close to zero?
01:35:10
And if so, we just ignore the left or the right arrow at that point.
01:35:16
So that works. But otherwise, if we're not touching a wall,
01:35:18
what are we probably doing instead forever here?
01:35:22
How is the movement working presumably?
01:35:24
Yeah and back.
01:35:26
Oh are you scratching?
01:35:28
OK, sure.
01:35:29
Let's go on.
01:35:30
AUDIENCE: [INAUDIBLE]
01:35:33
DAVID MALAN: Sorry, say a little louder.
01:35:35
AUDIENCE: Presumably it's continually looking for you to hit the arrow keys
01:35:38
and then moving when you do.
01:35:40
DAVID MALAN: Exactly.
01:35:41
It's continually, forever listening for the arrow keys up, down, left, right,
01:35:44
and if the up arrow is pressed, we're probably
01:35:47
changing the y by a positive value.
01:35:49
If the down arrow is pressed, we're going down by y,
01:35:52
and left and right accordingly.
01:35:54
So let's actually take a quick look.
01:35:55
If I zoom out here and take a look at the code that implements this,
01:35:59
there's a lot going on at first glance, but let's see.
01:36:01
First of all, let me drag some stuff out of the way
01:36:03
because it's kind of overwhelming at first glance,
01:36:05
especially if you, for instance, were poking around online as for problem set
01:36:09
0 just to get inspiration, most projects out there
01:36:11
are going to look overwhelming at first glance
01:36:13
until you start to wrap your mind around what's going on.
01:36:16
But in this case, we've implemented some abstractions
01:36:19
from the get go to explain to ourselves and to anyone else looking
01:36:22
at the code what's going on.
01:36:24
This is that program with the two black lines and the Harvard shield going up,
01:36:29
down, left, and right.
01:36:30
It initially puts the shield in the middle, 0,0, then
01:36:33
forever listens for keyboard, as I think you were describing,
01:36:37
and it feels for the walls, as I think you were describing.
01:36:40
Now how is that implemented?
01:36:42
Don't know yet. These are custom blocks we created as abstractions to kind of hide
01:36:46
those implementation details because honestly that's
01:36:48
all I need to know right now.
01:36:50
But, as aspiring programmers, if we're curious now,
01:36:52
let's scroll down to the actual implementation
01:36:55
of listening for keyboard.
01:36:57
This is the one on the left and it is a little long,
01:36:59
but it's a lot of similar structure.
01:37:02
We're doing the following, if the up arrow is pressed, then change y by 1.
01:37:07
Go up.
01:37:08
If the down arrow is pressed, then change y by negative 1.
01:37:11
Go down.
01:37:12
Right arrow, left arrow, and that's it.
01:37:15
So it just assembles all of those ideas, combines it
01:37:17
into one new block just because it's kind of overwhelming,
01:37:20
let's just implement it once and tuck it away.
01:37:22
And if we scroll now over to the Feel for Walls function,
01:37:26
this now is asking the question as hypothesized,
01:37:30
if I'm touching the left wall, change my x value by 1, sort of move away from it
01:37:35
a little bit. If I'm touching the right wall, then move x by negative 1
01:37:38
to move a little bit away from it.
01:37:40
So it kind of bounces off the wall.
01:37:42
Just in case it slightly went over, we keep the crest within those two walls.
01:37:47
All right, then a couple of more pieces here to introduce.
01:37:51
What if we want to actually add some kind of adversary or opponent
01:37:54
to this game?
01:37:55
Well, let me go ahead to maybe this one here where the adversary in this game
01:38:02
might, for instance, be designed to be bouncing to stand in your way.
01:38:05
This is like a maze and you're trying to get the Harvard shield from the bottom
01:38:08
to the top or vice versa.
01:38:09
Uh oh, Yale is in the way and it seems to be automatically bouncing
01:38:14
back and forth here.
01:38:15
Well, let me ask someone else.
01:38:16
Hypothesize.
01:38:17
How is this working?
01:38:19
This is an idea you have, this as an idea you see.
01:38:21
Let's reverse engineer in your head how it works.
01:38:26
How might this be working?
01:38:27
Yeah, in back.
01:38:28
AUDIENCE: If the Yale symbol is touching a right wall or left wall,
01:38:32
then have it bounce.
01:38:34
DAVID MALAN: Yeah, so if the Yale symbol is
01:38:36
touching the left wall or the right wall, we somehow have it bounce.
01:38:39
And indeed we'll see there's a puzzle piece that can do exactly
01:38:41
that technically off the edge, as we'll see,
01:38:44
but there's another way we can do this.
01:38:45
Let's look at the code.
01:38:46
The way we ourselves can implement exactly
01:38:49
that idea bounce is just with a little bit of logic.
01:38:52
So here's what this version of the program is doing.
01:38:54
It's moving Yale by default to 0,0 just to arbitrarily put it somewhere,
01:38:58
pointing it direction 90 degrees, which means just horizontally, essentially,
01:39:03
and then it's forever doing this: if touching the left wall
01:39:06
or touching the right wall, here's our translation of bounce.
01:39:10
We're just turning 180 degrees.
01:39:11
And the nice thing about that is we don't
01:39:13
have to worry if we're going from right to left or left to right.
01:39:16
180 degrees is going to work on both of the walls.
01:39:19
And that's it.
01:39:21
After we do that, we just move one step, one pixel, at a time
01:39:24
but we're doing it forever so something is happening continually
01:39:28
and the Yale icon is bouncing back and forth.
01:39:30
Well one final piece here, what if now we
01:39:33
want another adversary, a more advanced adversary down the road for instance,
01:39:39
to go and follow us wherever we are such that this time
01:39:44
we want the other sprite to not just bounce back and forth,
01:39:51
but literally follow us no matter where we go.
01:39:55
How might this be implemented on the screen?
01:39:59
I bet it's another forever block, but what's inside?
01:40:01
AUDIENCE: So forever get the location of the of the Harvard shield
01:40:05
and move one step towards it.
01:40:06
DAVID MALAN: Yeah, forever point at the location of the Harvard shield
01:40:09
and go one step toward it.
01:40:11
This is just going to go on forever if I just give up, at least in this version.
01:40:15
Notice it's sort of twitching back and forth because it goes one
01:40:18
pixel then one pixel then one pixel.
01:40:20
It's sort of in a frantic state here.
01:40:21
We haven't finished the game yet, but if we see inside, we'll see exactly that.
01:40:25
It didn't take much to implement this simple idea.
01:40:28
Go to a random position just to make it kind of fair,
01:40:30
initially, then forever point towards Harvard,
01:40:33
which is what we called the Harvard crest sprite, move one step.
01:40:37
Suppose we now wanted to make a more advanced level.
01:40:39
What's a minor change I could logically make to this code just
01:40:42
to make MIT even better at this?
01:40:45
AUDIENCE: Change the number of steps to two.
01:40:46
DAVID MALAN: All right, change the number of steps to two.
01:40:48
So let's try that.
01:40:49
So now they got twice as fast.
01:40:51
Let me go ahead and just get this out of the way.
01:40:53
Oops, let me make it a fair fight.
01:40:56
Green flag.
01:40:58
All right, I unfortunately am still moving one pixel at a time,
01:41:01
so this isn't going to end well.
01:41:03
It caught up to me.
01:41:04
And if we're really aggressive and do something like 20 steps at a time,
01:41:10
click the green flag.
01:41:11
Jesus, OK, so that's how you might then make your levels progressively
01:41:16
harder and harder.
01:41:17
So it's not an accident that we chose these particular examples
01:41:20
here involving these particular schools because we have one more
01:41:23
demonstration we thought we'd introduce today
01:41:25
if we could get one other volunteer to come up and play
01:41:30
what was called by one of your predecessors Ivy's Hardest Game.
01:41:34
Let's see, you in the middle.
01:41:35
Do you want to come on up?
01:41:36
What's your name?
01:41:37
AUDIENCE: Celeste.
01:41:38
DAVID MALAN: Say again?
01:41:39
AUDIENCE: Celeste.
01:41:40
DAVID MALAN: Come a little closer, actually.
01:41:41
Sorry, hard to hear here.
01:41:44
All right, round of applause here if we could, too.
01:41:47
[APPLAUSE]
01:41:53
OK, sorry, what was your name?
01:41:54
AUDIENCE: Celeste.
01:41:55
DAVID MALAN: Ceweste AUDIENCE: Celeste.
01:41:56
DAVID MALAN: Celeste. AUDIENCE: Yes.
01:41:57
DAVID MALAN: Come on over. Nice to meet you, too.
01:41:59
So here we have on this other screen Ivy's Hardest Game
01:42:02
written by a former CS50 student.
01:42:04
I think you'll see that it combines these same principles.
01:42:07
The maze is clearly a little more advanced.
01:42:09
The goal at hand is to initially move the Harvard crest to the sprite all
01:42:14
the way on the right so that you catch up to him in this case,
01:42:17
but you'll see that there's different levels
01:42:18
and different levels of sophistication.
01:42:21
So if you're up for it, you can use just these arrow keys up, down, left, right.
01:42:24
You'll be controlling the Harvard sprite and if we
01:42:27
could raise the volume just a little bit, we'll make this our final example.
01:42:32
Here we go, clicking the green flag.
01:42:34
[MUSIC PLAYING]
01:42:38
Feeling ready?
01:42:39
AUDIENCE: Yep.
01:42:40
DAVID MALAN: Spacebar.
01:42:41
[MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"]
01:42:43
MC HAMMER: (SINGING) Can't touch this.
01:42:45
You can't touch this.
01:42:48
You can't touch this.
01:42:52
Can't touch this.
01:42:55
My, my, my, my music--
01:42:56
DAVID MALAN: Excellent.
01:42:57
MC HAMMER: (SINGING) so hard.
01:42:58
Makes me want to say, oh my Lord.
01:43:00
Thank you for blessing me--
01:43:02
DAVID MALAN: Two Yales now.
01:43:03
MC HAMMER: (SINGING) Feels good when you know you're down.
01:43:06
A super dope homeboy--
01:43:07
AUDIENCE: Oh! DAVID MALAN: Oh!
01:43:09
Keep going.
01:43:10
MC HAMMER: (SINGING) You can't touch this.
01:43:12
I told you, homeboy.
01:43:13
Can't touch this.
01:43:15
Yeah, that's how living--
01:43:16
DAVID MALAN: All right.
01:43:17
MC HAMMER: (SINGING) Can't touch this.
01:43:19
Look at my eyes, man.
01:43:20
You can't touch this.
01:43:23
You let me bust the funky lyrics.
01:43:25
You can't touch this.
01:43:26
Fresh new kicks and pants.
01:43:27
You got it like that and you know you want to dance.
01:43:29
So move out of your seat and get a fly girl and catch this beat.
01:43:33
[LAUGHING] Hold on.
01:43:34
Pump a little bit and let them know what's going on like that, like that.
01:43:38
Cold on a mission, so fall on back.
01:43:39
Let them know that you're too--
01:43:41
DAVID MALAN: There you go.
01:43:42
There you go.
01:43:43
[APPLAUSE]
01:43:46
MC HAMMER: (SINGING) Can't touch this.
01:43:48
Why you standing there, man?
01:43:50
You can't touch this.
01:43:51
Yo, sound the bell.
01:43:52
School's in, sucker.
01:43:54
Can't touch this. Give me a song or rhythm, making them sweat that's what give them.
01:43:58
[CHEERING]
01:43:59
They know.
01:44:00
You talking the Hammer when you're talking about a show.
01:44:02
That's hyped and tight.
01:44:03
Singers are sweating so them a wipe or a tame to learn.
01:44:06
DAVID MALAN: Second to last level.
01:44:08
Oh! MC HAMMER: (SINGING) That chart's legit.
01:44:10
Either work hard or you might as well quit.
01:44:13
That word because you know--
01:44:14
DAVID MALAN: Oh!
01:44:15
Keep going, keep going!
01:44:17
Yes!
01:44:18
MC HAMMER: (SINGING) You can't touch this.
01:44:20
DAVID MALAN: You're almost there.
01:44:22
MC HAMMER: (SINGING) Break it down.
01:44:23
DAVID MALAN: There you go.
01:44:24
Go, go, go!
01:44:26
Oh.
01:44:27
One more.
01:44:30
Yes!
01:44:31
[CHEERING]
01:44:32
There you go.
01:44:36
MC HAMMER: (SINGING) Stop, Hammer time.
01:44:38
"Go with the flow," it is said.
01:44:39
If you can't groove to this, then you're probably dead.
01:44:42
So wave your hands in the air, bust a few moves,
01:44:44
run your fingers through your hair.
01:44:45
This is it. For a winner.
01:44:46
Dance to this and you're going to get thinner.
01:44:49
Now move, slide your rump.
01:44:50
Just for a minute let's all do the bump.
01:44:53
[CHEERING]
01:44:54
DAVID MALAN: Yes!
01:44:55
[APPLAUSE]
01:44:56
Congratulations.
01:45:03
All right, that's it for CS50.
01:45:05
Welcome to the class.
01:45:06
We'll see you next time.
01:45:08
[MUSIC PLAYING]
01:46:26
DAVID J. MALAN: All right.
01:46:27
So this is CS50.
01:46:29
And this is week 1, the one in which you learn a new language, which
01:46:33
is something we technically said last week, at least if you had never
01:46:36
played with this graphical language known as Scratch before, which itself
01:46:39
was a programming language.
01:46:41
But today, as promised, we transition to something
01:46:43
a little more traditional, a little more text-based,
01:46:45
not puzzle piece- or block-based, known as C.
01:46:48
This is an older language.
01:46:49
It's been around for decades.
01:46:50
But it's a language that underlies so many of today's more modern languages,
01:46:54
among them something called Python that we'll also
01:46:57
come to in a few weeks' time.
01:46:58
Indeed, at the end of the semester, the goal
01:47:00
is for you to feel that you've not learned Scratch,
01:47:02
you've not learned C, or even Python, for that matter,
01:47:04
but fundamentally that you've learned how to program.
01:47:07
Unfortunately, when you learn how to program
01:47:09
with a more traditional language like this, there's just so much distraction.
01:47:13
Last week I described all of the syntax, all of the weird punctuation
01:47:17
that you see in this, like the hash symbol, these angled brackets,
01:47:19
parentheses, curly braces, backslash n, and more.
01:47:22
Well, today we're not going to reveal what all of those little particulars
01:47:26
mean.
01:47:27
But by next week, will this no longer look like the proverbial Greek
01:47:31
to you, a language that, presumably, you've never actually seen or typed
01:47:35
before.
01:47:36
But to do that, we'll explore some of the very same topics as last week.
01:47:40
So recall that, via Scratch-- and presumably via problem set 1--
01:47:43
we took a look at things called functions that are actions or verbs.
01:47:46
And related to functions were arguments like inputs.
01:47:49
And related to some functions were returned values like outputs.
01:47:52
Then we talked a bit about conditionals, forks in the road, so to speak,
01:47:56
Boolean expressions, which are just yes/no questions or true/false
01:47:59
questions, loops, which let you do things again and again, variables,
01:48:03
like in math, that let you store values temporarily,
01:48:05
and then even other topics still.
01:48:07
So if you were comfortable on the heels of problem set 0 and last week,
01:48:11
realize that all of these topics are going to remain with us.
01:48:14
So really, today is just about acquiring all the more of a mental model for how
01:48:18
you translate those ideas into, presumably, a very cryptic new syntax--
01:48:22
a new syntax, frankly, that's actually more
01:48:25
simple in some ways than your own human language,
01:48:27
be it English or something else, because there's far fewer vocabulary words.
01:48:31
There's actually far less syntax that you might
01:48:33
have in, say, a typical human language.
01:48:35
But you need to be with these computer languages all the more precise
01:48:39
so that you're most, ultimately, correct,
01:48:41
and ultimately will see to your code is successful along a few other lines
01:48:46
as well. So if you think about the last time you kind of wandered around not really
01:48:49
knowing what you were doing or encountered something new--
01:48:51
might not have been that long ago, entering Harvard Yard for the very
01:48:54
first time, or Old Campus or the like, be it in Cambridge or New Haven--
01:48:58
you didn't really need to know how to do everything as a first year.
01:49:01
You didn't need to know who everyone was,
01:49:03
where everything was, how Harvard or Yale, or anything else for that matter,
01:49:07
worked. You sort of got by day to day by just focusing on those things that matter.
01:49:10
And anything you didn't really understand,
01:49:12
you sort of turned a blind eye to until it's important.
01:49:14
And that's, indeed, what we're going to do today.
01:49:16
And really, for the next several weeks, we'll
01:49:18
focus on details that are initially important
01:49:21
and try to wave our hands, so to speak, at details that, yeah, eventually
01:49:24
we'll get to, might be interesting.
01:49:25
But for now, they might be distractions.
01:49:27
And by distractions, I really mean some of that syntax
01:49:29
to which I alluded earlier.
01:49:31
So by the end of today-- and really, by the end of problem set 1,
01:49:34
your first foray, presumably, into this language called C--
01:49:37
you'll have written some code.
01:49:39
And you'll be asking yourself-- we'll be asking yourselves--
01:49:41
just how good is that code?
01:49:43
Well, first and foremost, per last week, be it in Scratch
01:49:46
or phone book form, code ultimately needs to be correct, to be well done.
01:49:51
You want the problem to be solved correctly.
01:49:53
So that one sort of goes without saying.
01:49:55
And along the way this term, we'll provide you with tools and techniques
01:49:59
so you don't have to just sit there sort of endlessly trying an input,
01:50:02
checking the output, trying another input, checking the output.
01:50:05
There's a lot of automation tools in the real world--
01:50:07
and in this class and others like it-- that
01:50:09
will help facilitate you answering that question for yourself, is my code
01:50:13
correct, according to our specifications or the like.
01:50:15
But then something that's going to take more time
01:50:18
and you're probably not going to feel 100% comfortable with the first week,
01:50:21
the first weeks, is just how well designed your code is.
01:50:24
It's one thing to speak English or write English,
01:50:27
but it's another thing-- or any language, for that matter.
01:50:30
But it's another thing to speak it or write it well.
01:50:32
And we spend all these years in middle school, high school, presumably,
01:50:35
writing papers and other documents, getting grades and feedback on them
01:50:38
as to how well formulated your arguments were, how well structured
01:50:41
your paper was, and the like.
01:50:43
And there's that same idea in programming.
01:50:44
It doesn't matter necessarily that you've just solved a problem correctly.
01:50:49
If your code is a complete visual mess, or if it's crazy long,
01:50:53
it's going to be really hard for someone else
01:50:55
to wrap their mind around what your code is doing and, indeed, to be confident
01:50:59
if it is correct.
01:51:00
And honestly, you-- the next morning, the next year,
01:51:04
the next time you look at that code-- might have no idea
01:51:07
what you yourself were even thinking.
01:51:09
But you will if you focus, too, on designing good code,
01:51:13
getting your algorithms efficient, getting your code nice and clean,
01:51:16
and even making sure your code looks pretty, which
01:51:19
we'd describe as a matter of style.
01:51:20
So in the written human world, having punctuation in the right place,
01:51:24
capitalization and the like-- the sort of way you write an essay
01:51:27
but not necessarily send a text message--
01:51:29
relates to style, for instance.
01:51:31
And so good style in code is going to have
01:51:33
a few of these characteristics that are pretty easily taught and remembered.
01:51:36
But you just have to start to get in the habit of writing code in a certain way.
01:51:41
So these three axes, so to speak, correctness, design, and style,
01:51:44
are really the overarching goals when writing code that
01:51:48
ultimately is going to look like this.
01:51:50
So this program we conjectured last week does
01:51:52
what if you run it on a Mac or PC or somewhere else, presumably?
01:51:56
What does it do?
01:51:57
Yeah?
01:51:59
AUDIENCE: [INAUDIBLE].
01:52:00
DAVID J. MALAN: It just prints, Hello, world.
01:52:02
And honestly, that's kind of atrocious that you
01:52:04
need to hit your keyboard keys this many times with this cryptic syntax just
01:52:08
to get a program to say, Hello, world.
01:52:09
So a spoiler-- in a few weeks' time when we
01:52:11
introduce other, more modern languages, like Python,
01:52:14
you can distill this same logic into literally one line of code.
01:52:18
And so we're getting there, ultimately.
01:52:20
But it's helpful to understand what it is that's going on here,
01:52:23
because even though this is a pretty cryptic syntax,
01:52:25
there's nothing after this week and, really, next week that you shouldn't
01:52:28
be able to understand even about something that right now looks
01:52:30
a little something like this.
01:52:32
So how do you write code?
01:52:33
Well, I've given us sort of the answer to a problem.
01:52:35
How do you print, Hello, world, on the screen?
01:52:37
So what do I do with this code?
01:52:39
Well, we're in the habit of typically writing things with, like, Microsoft
01:52:42
Word or Google documents.
01:52:43
And yeah, I could open up Word or Google Docs or Pages or the like
01:52:48
and just literally transcribe that character for character,
01:52:50
save it, and boom, I've got a program.
01:52:53
But the problem, per last week, is that computers only understand or speak
01:52:56
what other language, so to speak?
01:52:59
AUDIENCE: [INAUDIBLE].
01:53:00
DAVID J. MALAN: Yeah, so binary, zeros and ones.
01:53:02
And so this, obviously, is not zeros and ones.
01:53:04
So it doesn't matter if I put it in a Word doc, Google Doc, Pages file,
01:53:07
or the like. The computer is not going to understand it until I somehow
01:53:10
translate it to zeros and ones.
01:53:11
And honestly, none of those tools that I rattled off
01:53:14
are really appropriate for programming.
01:53:15
Why?
01:53:16
Well, they come with features like bold facing and italics
01:53:19
and sort of fluffy, aesthetic stuff that has no functional impact on what
01:53:22
you're trying to do with your code.
01:53:24
And they don't have the ability, it would
01:53:26
seem, to convert that code ultimately to zeros and ones.
01:53:29
But tools that do have this capability might
01:53:32
be called Integrated Development Environments, or IDEs,
01:53:36
or, more simply, text editors.
01:53:38
A text editor is a tool that a programmer uses perhaps every day
01:53:42
to write their code.
01:53:44
And it's a simple program-- here, for instance, a very popular one
01:53:46
called Visual Studio Code, or VS Code.
01:53:49
And at the top here, you see that I've actually
01:53:51
created in advance before class a very simple empty file called "hello.c."
01:53:56
Why?
01:53:57
Well, .c indicates by convention that this is going to be a file in which
01:54:00
there is C code.
01:54:01
It's not .docx, which would mean in this file is a Microsoft Word document,
01:54:06
or .pages is a Pages file.
01:54:08
This is .c, which means in this file is going to be text in the language called
01:54:12
C. This number 1 here is just an automatic line number that's going help
01:54:16
me keep track of how long or short this program is.
01:54:18
And the cursor is just blinking there, waiting
01:54:21
for me to start typing some code.
01:54:23
Well, let me go ahead and type out exactly the same code.
01:54:26
For me, it comes pretty comfortably from memory.
01:54:28
So I'm going to go ahead and include something called standardio.h--
01:54:31
more on that later.
01:54:32
I'm going to magically type int main(void), whatever that means--
01:54:37
we'll come back to that later--
01:54:38
one of these curly braces and then a sibling there that closes the same.
01:54:43
Then I'm going to hit Tab to indent a few spaces.
01:54:46
And then I'm going to type not print, but printf, then "Hello, world," /n,
01:54:52
close quote, close parenthesis, semicolon.
01:54:55
And I dare say this was essentially the very first program I wrote some
01:55:00
25 years ago.
01:55:01
I wrote it to say, "Hi, CS50."
01:55:03
Now it just says the more canonical, conventional, "Hello, world."
01:55:06
But that's it.
01:55:07
That's my very first program.
01:55:08
And all I need to now do is maybe hit Command-S or Control-S
01:55:11
to save the file.
01:55:12
And voila, I am a programmer.
01:55:15
The catch though, is, OK, how do I run this?
01:55:17
Like, on your Mac or PC, how do you run a program?
01:55:19
Well, usually double-click an icon.
01:55:21
On your phone, you tap an icon.
01:55:23
In this environment that we're using and that many programmers-- dare
01:55:26
say most programmers-- use, you don't have immediately a nice, pretty icon
01:55:31
to double-click on.
01:55:32
That's very user friendly, but it's not very necessary.
01:55:35
Especially when you get more comfortable with programming,
01:55:38
you're going to want to type commands because it's just faster than pointing
01:55:41
and clicking a mouse.
01:55:42
And you're going to want to automate things,
01:55:43
which is a lot easier if it's all command or text-based, as opposed
01:55:46
to mouse and muscular movements.
01:55:49
And so here I have my program.
01:55:52
It lives in this file called "hello.c."
01:55:54
I need to now convert it, though, to zeros and ones.
01:55:58
Well, how do I go about doing this, and how am I going to get from this
01:56:02
so-called code--
01:56:03
or source code, as it's conventionally called--
01:56:06
to this, these zeros and ones that we'll now start calling machine code.
01:56:11
The zeros and ones from last week can be used not only
01:56:13
to represent numbers and letters, colors, audio, video, and more.
01:56:18
It can also represent instructions to a computer, like print, or play a sound,
01:56:24
or delete a file, or save a file.
01:56:26
All the sort of basics of a computer somehow
01:56:28
can be represented by other patterns of zeros and ones.
01:56:32
And just like last week, it depends on the context
01:56:34
in which these numbers are stored.
01:56:36
Sometimes they're interpreted as numbers, like in a spreadsheet.
01:56:39
Sometimes they're interpreted as colors.
01:56:41
Sometimes they're interpreted as instructions, commands to your computer
01:56:46
to do very low-level operations, like print something on the screen.
01:56:50
So fortunately, last week's definition of computer science of problem solving
01:56:55
is a nice mental model for exactly the goal at hand.
01:56:58
I have some input, AKA source code.
01:57:00
I want to output ultimately machine code, those zeros and ones.
01:57:05
I certainly don't want to do this kind of process by hand.
01:57:07
So hopefully there's an algorithm implemented by some special program
01:57:11
that does exactly that.
01:57:12
And those of you who do have some prior experience,
01:57:14
this program might be called a?
01:57:16
A compiler.
01:57:18
So a few of you have, indeed, programmed before.
01:57:20
Not all languages use compilers.
01:57:21
C, in fact, is a language that does use a compiler.
01:57:24
And so I just need to find myself--
01:57:27
on my computer somewhere, presumably-- a so-called compiler,
01:57:31
a program whose purpose in life is to convert one language to another.
01:57:35
And source code written textually in C, like we saw a moment ago,
01:57:40
is source code.
01:57:41
The machine code is the corresponding zeros and ones.
01:57:44
So let me go back to the same programming environment called
01:57:48
Visual Studio Code or VS Code.
01:57:50
This is typically a program you or any programmer on the internet
01:57:53
can download onto their own Mac or PC and be on their way with whatever
01:57:57
computer you own writing some code.
01:57:59
A downside, though, of that approach is that all of us
01:58:02
have slightly different versions of Macs or PCs.
01:58:04
We have slightly different versions of operating systems.
01:58:07
They may or may not be up to date.
01:58:08
It's just a technical support nightmare to create a uniform environment,
01:58:13
especially for an introductory class, where everyone should ideally
01:58:15
be on the same page so we can get you up and running quickly.
01:58:19
And so I'm actually using a cloud-based version of VS Code, something
01:58:23
that you only need a browser to access.
01:58:25
And then you can be on any computer, today or tomorrow.
01:58:28
By the end of the semester, we're going to get you out of the cloud,
01:58:32
so to speak, as best we can and get you onto your own Mac or PC,
01:58:36
so that after this class, especially if it's the only CS class you ever take,
01:58:39
you feel like you can continue programming in any number of languages,
01:58:43
even with CS50 behind you.
01:58:45
But for now, wonderfully, the browser version of VS Code
01:58:47
should pretty much be identical to what the eventual downloadable
01:58:51
version of the same would be.
01:58:53
And you'll see in problem set 1 how to access this
01:58:55
and how to get going yourself with your first programs.
01:58:58
But I haven't mentioned this bottom part of the screen,
01:59:01
this bottom part of the screen.
01:59:03
And this is an area where we have what's called a terminal window.
01:59:06
So this is sort of old-school technology that allows you, with a keyboard,
01:59:10
to interact with a computer, wherever it may be-- on your lap, in your pocket,
01:59:15
or even, in this case, in the cloud.
01:59:17
So on the top-hand portion of this screen
01:59:20
is my text editor, like tabbed windows, like in many programs, where
01:59:24
I can just create files and write code.
01:59:26
The bottom of the screen here, my so-called terminal window,
01:59:30
gives me the ability to run commands on a server
01:59:33
that currently I have exclusive access to.
01:59:35
So because I logged into VS Code with my account online,
01:59:39
I have my own sort of virtual server, if you will, in the cloud--
01:59:44
otherwise known as, in this context, a container.
01:59:46
This has its own operating system for me, its own hard drive,
01:59:49
if you will, where I can save and create files of my own,
01:59:52
separate from yours and vice versa.
01:59:54
And it's at this very simple prompt, which
01:59:57
is conventionally-- but not always-- abbreviated by a dollar sign,
02:00:00
has nothing to do with currency.
02:00:01
It just means, type your commands here.
02:00:03
This is where I'm going to be able to type commands,
02:00:05
like compile my source code into machine code.
02:00:09
So it's a Command Line Interface, or CLI, on top of an operating system
02:00:15
that you might not have ever used or seen, but it's very popular,
02:00:18
called Linux.
02:00:19
Odds are almost all of us in this room are using Mac OS or Windows right now,
02:00:23
but we're all going to start using an operating system called Linux, which
02:00:26
is in a family of operating systems that offer not only this command
02:00:30
line interface, but are used not just for programming, but for serving
02:00:33
websites and developing applications and the like.
02:00:35
And it's, indeed, a familiar and very powerful interface, as we'll see.
02:00:40
So how do I go about making this file, hello.c, into a program?
02:00:45
There's no icon to double-click, but there is a command.
02:00:48
I can type, make hello, at this dollar sign prompt, go ahead and hit Enter,
02:00:54
and nothing appears to happen.
02:00:56
But that's a good thing.
02:00:57
And as we'll see in programming, almost always,
02:00:59
if you don't see anything go wrong, that means everything went right.
02:01:02
So this is going to be a rarity at first,
02:01:04
but this is a good thing that it just seems to do nothing.
02:01:07
But now there is in the folder in my accounts
02:01:11
in this on the cloud a file called "hello."
02:01:15
And it's a bit of a weird command, but you'll get familiar with it
02:01:18
before long.
02:01:19
. just means go into my current folder.
02:01:22
/hello means run the program called "hello" in this current folder.
02:01:28
So ./hello, and then Enter, and voila, now I'm actually not just programming,
02:01:32
but running my actual code.
02:01:35
So what have I just done?
02:01:38
Let me go ahead and do this.
02:01:40
I'm going to go ahead and open up the sidebar of this program,
02:01:42
and you'll see in problem set 1 how to do this.
02:01:44
And this might look a little different based on your own configuration.
02:01:48
Even the color scheme I'm using might ultimately look different from yours,
02:01:51
because it supports a nice colorful theme.
02:01:52
So you can have different colors and brightnesses depending
02:01:56
on your mood or the time of day.
02:01:58
What I've opened here, though, is what is called in VS Code Explorer,
02:02:01
and this is just all of the files in my cloud account.
02:02:04
And there's not many right now.
02:02:05
There's only two.
02:02:06
One is the file called hello.c, and it's highlighted
02:02:09
because I've got it open right there.
02:02:10
And the other is a file called "hello," which is brand new
02:02:14
and was created when I ran that command.
02:02:17
And what's now worth noting is that now things are getting a little more
02:02:21
like Mac OS and Windows.
02:02:22
Like on the left-hand side, you have a GUI, a Graphical User Interface.
02:02:26
But on the bottom here, again, you have a CLI, Command Line Interface.
02:02:30
These are just different ways to interact with computers,
02:02:32
and you'll get comfortable with both.
02:02:34
And honestly, you're certainly familiar and comfortable with GUIs already,
02:02:37
so it's the command line one with which we'll spend some time.
02:02:40
Now suppose that I just wanted to do something
02:02:43
more than compile this program.
02:02:45
Suppose I wanted to go ahead and remove it.
02:02:47
Like, uh-uh, no, I made a mistake.
02:02:48
I want to say, "Hello, CS50," not "Hello, world."
02:02:51
I could just hover up here, like in any software, and I could right-click,
02:02:55
and I could poke around, and there, delete permanently.
02:02:57
So most of us might have that instinct on a Mac or PC.
02:03:00
You right-click or Control-click, and you poke around.
02:03:02
But in a command line interface, let me do this instead.
02:03:05
The command for removing or deleting a file
02:03:08
in the world of Linux, this other operating system,
02:03:12
is just a type rm for remove, and then "hello," Enter.
02:03:16
It's a somewhat cryptic confirmation message, but this just means,
02:03:19
are you sure? I'm going to go ahead and type Y for Yes.
02:03:21
And now when I hit Enter, watch what happens
02:03:24
at top left in the Explorer, the GUI, the graphical interface.
02:03:28
Voila, it disappears.
02:03:30
Not terribly exciting, but this just means
02:03:32
this is a graphical version of what we're seeing here.
02:03:35
And in fact, if you want to never use the GUI again--
02:03:38
I'll go ahead and close it with a keyboard shortcut here--
02:03:41
you can forever just type ls for list and hit Enter.
02:03:45
And you will see in the command line interface
02:03:48
all of the files in your current folder.
02:03:50
So anything you can do with a mouse, you can
02:03:52
do with this command line interface.
02:03:54
And indeed, we'll see many more things that you can do as well.
02:03:57
But the inventors of this, this operating system and its predecessors,
02:04:01
were very succinct.
02:04:02
Like, the command is rm for remove.
02:04:04
The command is ls for list.
02:04:06
It's very terse. Why?
02:04:07
Because it's just faster to type.
02:04:10
So before we forge ahead with making something more interesting than
02:04:14
just "Hello, world," let me pause here to see
02:04:16
if there's questions on source code or machine
02:04:19
code or compiler or this command line interface.
02:04:24
Yeah?
02:04:25
AUDIENCE: [INAUDIBLE].
02:04:26
DAVID J. MALAN: Really good question, and let me recap.
02:04:28
If I were to make changes to the program,
02:04:30
run it, and then maybe make other changes and try to rerun it,
02:04:33
would those changes be reflected, even though I've reworded slightly.
02:04:37
Well, let's do this. I already removed the old version.
02:04:39
So let me go ahead and point out that if I do ./hello now,
02:04:43
I'm going to see some kind of error because I just deleted the file.
02:04:47
No such file or directory, so it's not terribly user friendly,
02:04:50
but it's saying what the problem is.
02:04:51
Let me go ahead and remake it by typing make hello.
02:04:55
Now if I type ls, I'll see not one but two files again, and one of them
02:04:59
is even green with a little asterisk to indicate that it's executable.
02:05:03
It's sort of the textual version of something
02:05:05
you could double-click in our human world.
02:05:07
So now, of course, if I run hello, we're back where I started, "Hello, world."
02:05:10
But now suppose I change it to "Hello, CS50," like I did years ago.
02:05:15
Let me go ahead and save the file with Command-S or Control-S. Down here now,
02:05:21
let me run ./hello again, and voila.
02:05:23
Huh.
02:05:25
So let me ask someone else to answer that question.
02:05:27
What's the missing step?
02:05:29
Why did it not say, "Hello, CS50."
02:05:31
Yeah?
02:05:32
AUDIENCE: [INAUDIBLE].
02:05:33
DAVID J. MALAN: Yeah, so I didn't compile it again.
02:05:35
So sort of newbie mistake, you're going to make this mistake and many others
02:05:38
before long. But now let me go ahead and remake hello, enter.
02:05:42
It's going to seemingly make the same program.
02:05:45
But this time when I run it, it's, "Hello, CS50."
02:05:49
Any other questions on some of these building blocks?
02:05:53
And we'll come back to all the crazy syntax I typed before long.
02:05:56
But for now, we're focusing on just the output.
02:05:58
Yeah?
02:05:59
AUDIENCE: [INAUDIBLE].
02:06:00
DAVID J. MALAN: When I keep running make,
02:06:02
it creates a new version of the machine code.
02:06:05
So it keeps changing the hello program and the hello file, and that's it.
02:06:09
There's no make file, per se.
02:06:12
AUDIENCE: [INAUDIBLE].
02:06:13
DAVID J. MALAN: Good question, no.
02:06:15
If I open up that directory, you'll see that there's just the one.
02:06:18
And it doesn't matter how many times I run make hello--
02:06:21
three, four, five-- it just keeps overwriting the original.
02:06:25
So it's kind of like just saving in the world of Google Docs or Microsoft
02:06:28
Word or the like.
02:06:29
But there's an additional step today.
02:06:31
We have to then convert my words to the computer's, the zeros and ones.
02:06:36
Yeah, in front.
02:06:37
AUDIENCE: [INAUDIBLE].
02:06:38
DAVID J. MALAN: Oh, what happens if I run hello.c?
02:06:40
So let me go ahead and do ./hello.c, which is a mistake you'll invariably
02:06:45
make early on.
02:06:46
Permission denied.
02:06:47
So what does that mean?
02:06:48
This is where the error messages mean something
02:06:50
to the people who designed the operating system, but it's a little cryptic.
02:06:53
It's not that you don't have access to the file.
02:06:55
It means that it's not executable.
02:06:57
This is not something you have permission to run,
02:07:00
but you do have permission to read or write it-- that is, change it.
02:07:04
AUDIENCE: [INAUDIBLE].
02:07:06
DAVID J. MALAN: Oh, really good question.
02:07:08
So if I have named my file, hello dot C, or more generally something
02:07:11
dot C, of the things that Make does is it automatically
02:07:15
picks the file name for me.
02:07:17
And we'll discuss a bit--
02:07:18
we'll discuss this a bit more next week.
02:07:20
Make itself-- is kind of the first of white lies today--
02:07:23
itself is not a compiler.
02:07:25
It's a program that knows how to find and use the compiler on the system
02:07:30
and automatically create the program.
02:07:33
If I use, as we'll discuss next week, the actual compiler myself,
02:07:37
I have to type a much longer sequence of commands to specify explicitly
02:07:41
what do I want the name of my program to be.
02:07:43
Make is a nice program, especially in week 1,
02:07:45
because it just automates all of that for us.
02:07:47
And so here, we have now a program that very simply prints
02:07:51
something on the screen.
02:07:52
So let's not put this into the context of where
02:07:54
we left off last time in the context of Scratch and inputs and outputs.
02:07:58
So we discuss the last time, of course, functions and arguments.
02:08:01
Functions, again, are those actions and verbs like say, or ask, or the like.
02:08:07
And the arguments were the inputs to those functions,
02:08:09
generally in those little white ovals that, in Scratch, you
02:08:11
could type words or numbers into.
02:08:13
We'll see, in all of the languages we're going to see this term,
02:08:16
have that same capability.
02:08:18
And let's just start to translate one of these things to another.
02:08:20
So for instance, let's put this same program in C,
02:08:24
in the context of Scratch.
02:08:26
This is what Hello, World looked like last week in the form of one function.
02:08:29
This week, of course, it looks like print.
02:08:32
And then the parentheses, notice, are kind of
02:08:34
deliberately designed in the world of Scratch to resemble that same shape.
02:08:38
Even though this is a white oval, you kind of get
02:08:40
that it's kind of evoking that same idea with the parentheses.
02:08:45
Technically the function in C, it's not called say.
02:08:49
It's not even called print.
02:08:50
It's called printf.
02:08:51
The F stands for formatted, but we'll see what that means in a moment.
02:08:55
But printf is the closest analogous function
02:08:57
for say in the world of C. Notice if, though, you
02:09:00
want to print something like Hello, World or Hello CS50 in C,
02:09:05
you don't just write the words as we did last week.
02:09:08
You also had an add what, if you notice already
02:09:11
what's missing from this version.
02:09:13
Yeah, so the double quotes on the left and the right.
02:09:15
So, that's necessary in C whenever you have a string of words.
02:09:20
And I'm using that word deliberately.
02:09:22
Whenever you have multiple words like this, this is known as a string
02:09:26
as we'll see.
02:09:27
And you have to put it in double quotes, not single quotes.
02:09:30
You have to put it in double quotes.
02:09:32
There's one other stupid thing that we need to have in my C code
02:09:36
in order to get this function to do something ultimately, which is what?
02:09:40
Semicolon.
02:09:41
So just like in our human world, you eventually
02:09:44
got into the habit of using, at least in formal writing, periods.
02:09:47
Semicolon is generally what you use to finish your thought
02:09:50
in the world of programming with C.
02:09:53
All right, so we have that function in place.
02:09:55
Now, what does this really fit into in terms of the mental model?
02:09:59
Well, functions take arguments.
02:10:01
And it turns out functions can have different types of outputs.
02:10:04
And we've actually seen both already last week.
02:10:07
One type of output from a function can be something called a side effect.
02:10:11
And it generally refers to something visual,
02:10:13
like something appearing on the screen or a sound playing from your computer.
02:10:17
It's sort of a side effect of the function doing its thing.
02:10:20
And indeed, last week we saw this in the context of passing in something
02:10:24
like Hello, World as input to the say function.
02:10:27
And we saw on the screen Hello, World, but it was kind of a one off.
02:10:31
It's one and done.
02:10:32
You can't actually do anything with that visual output
02:10:35
other than consume it, visually, with your human eyes.
02:10:38
But sometimes, recall last week, we had functions like the ask block that
02:10:43
actually returned me some value.
02:10:44
Remember the ask, what's your name.
02:10:47
It handed me back whatever answer the human typed in.
02:10:50
It didn't just arbitrarily display it on the screen.
02:10:52
The cat didn't necessarily say it on the screen.
02:10:55
It was stored, instead, in that special variable that was called answer.
02:11:01
Because some functions have not side effects but return values.
02:11:05
They hand you back an output that you can use and reuse,
02:11:09
unlike the side effect, which, again displays and that's it.
02:11:11
You can't sort of catch it and hold on to it.
02:11:14
So, in the context of last week, we had the ask block.
02:11:17
And that had this special answer return value.
02:11:20
In C, we're going to see in just a moment,
02:11:22
we could translate this as follows.
02:11:25
The closest match I can propose for the ask block
02:11:29
is a function that we're going to start calling get string.
02:11:31
String is, again, a word, a set of words, like a phrase
02:11:34
or a sentence in programming.
02:11:36
It, too, is a function insofar as it takes input and pretty much--
02:11:41
this isn't always true-- but very often when
02:11:43
you have a word in C followed by an open parenthesis and a closed parenthesis,
02:11:48
it's most likely the name of a function.
02:11:50
And we're going to see that there's some exceptions to that.
02:11:53
But for now this indeed looks like a function
02:11:55
because it matches that pattern.
02:11:57
If I want to ask the question, what's your name, question mark--
02:12:00
and I'm even going to deliberately put a space there just to kind of move
02:12:03
the cursor a little bit over so that the human isn't typing literally
02:12:07
after the question mark.
02:12:08
So that's just the nitpicky aesthetic.
02:12:10
This is perhaps the closest analog to just asking that question.
02:12:14
But because the ask block returns a value,
02:12:18
the analog here forget string is that it, too, returns a value.
02:12:22
It doesn't just print the human's input.
02:12:23
It hands it back to you in the form of a variable, a.k.a. return value,
02:12:28
that I can then use and reuse.
02:12:30
Now ideally it would be as simple as this literally
02:12:33
saying answer on the left equals.
02:12:36
And this is where things start to diverge
02:12:38
from math and sort of our human world.
02:12:40
This equal sign, henceforth, is not the equal sign.
02:12:44
It is the assignment operator.
02:12:46
To assign a value means to store a value in some variable.
02:12:50
And you read these things, weirdly, right to left.
02:12:53
So here is a function called get string.
02:12:55
I claim that it's going to return to you whatever
02:12:58
the human types in as their name.
02:13:00
It's going to get stored over here on the left because
02:13:03
of this so-called assignment operator, that yes is an equal sign.
02:13:06
But it doesn't mean equality in this context.
02:13:08
It makes things equal.
02:13:10
But it does so by copying the value on the right into the thing on the left.
02:13:14
Unfortunately, we're not quite done yet with C.
02:13:16
And this is where, again, it gets a little annoying at first
02:13:19
where Scratch just let us express our ideas without so much syntax.
02:13:23
In C when you have a variable you don't just
02:13:27
give it a name like you did in Scratch.
02:13:29
You also have to tell the computer in advance what type of value
02:13:33
it is storing.
02:13:34
String is one such type of value.
02:13:37
Int, for integer, is going to be another.
02:13:40
And there's even more than that we'll see today and beyond.
02:13:42
And this is partly an answer to the question that came up one or more times
02:13:46
last week, which was how does a computer distinguish this pattern of zeros
02:13:49
and ones from this.
02:13:51
Like is this a letter, a number, a color, a piece of video.
02:13:55
And I just claimed last week that it totally depends on the program.
02:13:58
It depends on the context.
02:13:59
And that's true.
02:14:00
But within those programs, it often depends
02:14:04
on what the human programmer said the type of the value is.
02:14:08
If this specifies that the string, which means
02:14:10
interpret the following zeros and ones that
02:14:12
are stored in my program as words or letters, more generally.
02:14:16
If it's an int for integer, it would be implying, by the programmer,
02:14:20
treat the following zeros and ones in my program as a number,
02:14:24
an integer, not a string.
02:14:26
So here's where this week, unlike with Scratch,
02:14:29
which is kind of figures out what you mean, with C in a lot of languages
02:14:33
you have to be this pedantic and tell it what you mean.
02:14:36
There's still one stupid thing missing from my code here.
02:14:39
What's still missing here?
02:14:40
Yeah.
02:14:41
AUDIENCE: [INAUDIBLE]
02:14:42
DAVID J. MALAN: And we still need the stupid semicolon.
02:14:44
And I'm sort of impugning it here.
02:14:45
Because honestly, these are the kinds of stupid mistakes
02:14:48
you're going to make today, tomorrow, this weekend, next week,
02:14:50
a few weeks from now, until you start to notice this and recognize it
02:14:54
as well as you do English or whatever your spoken language is.
02:14:58
Yeah, question.
02:14:59
Good question.
02:15:00
Suppose I mix apples and oranges, so to speak,
02:15:03
and I try to put a string in an int or an int in a string,
02:15:06
the compiler is going to complain.
02:15:08
So when I run that make command as I did earlier,
02:15:10
it's not going to be nice and blissfully quiet and just give me another prompt.
02:15:14
It's going to yell at me with honestly a very cryptic
02:15:17
looking error message until we get the muscle memory for reading it.
02:15:20
Other questions.
02:15:23
Ah, what happened to the backslash n.
02:15:24
So, we'll come back to that in just a moment, if we may.
02:15:26
Because I have deliberately omitted it here but we did have it earlier.
02:15:29
And we'll see the different behavior in a sec.
02:15:32
Other questions.
02:15:36
Yeah, not at all nitpicky.
02:15:37
These are the kinds of things that just matter.
02:15:39
And it's going to take time to recognize and develop this muscle memory.
02:15:43
Everything I've typed here except, for the W at the moment, is lowercase.
02:15:48
And the W is capitalized just because it's English.
02:15:50
Everything else is lowercase.
02:15:51
And this kind of varies by language and also context.
02:15:54
So, in many languages the convention is to use all lowercase letters
02:15:58
for your variable names.
02:16:00
Other languages might use some capitals, as well.
02:16:02
But we'll talk about that before long.
02:16:04
But this is the kind of thing that matters
02:16:05
and is hard to see at first, especially when a little S doesn't look that
02:16:09
different when it's on your tiny laptop screen from a capital S.
02:16:12
But you'll start to develop these instincts.
02:16:15
All right, so besides this particular block,
02:16:18
let's go ahead and consider how we can go about implementing this now in code.
02:16:22
So let me switch back to VS Code here.
02:16:24
This was the program I had earlier.
02:16:26
And let me go ahead and undo my CS50 change.
02:16:28
And this time just rerun it.
02:16:30
Rerun Make on Hello with the original version with the backslash n.
02:16:35
Enter, nothing bad seems to have happened.
02:16:37
So dot slash Hello, enter Hello, World.
02:16:40
Now, if you're curious, this is a good instinct
02:16:43
to start to acquire what happens if I get rid of this.
02:16:45
Well, I'm probably not going to break things too badly.
02:16:47
So let's try.
02:16:48
Let me go ahead now and do Make Hello.
02:16:51
Still compile.
02:16:52
So it's not a really bad mistake.
02:16:53
So let me go ahead and run dot slash Hello.
02:16:56
What's the difference here?
02:16:58
Yeah, what do you see that's different?
02:17:01
Yeah, the dollar sign, my so-called prompt, stayed on the same line.
02:17:05
Why? Well, we can presumably infer now that the backslash
02:17:08
n is some fancy notation for saying create a new line,
02:17:12
move the cursor, so to speak, to the next line.
02:17:14
Notice that the cursor will move to the next line in my terminal window.
02:17:18
If I keep hitting it, it just automatically,
02:17:20
by nature of hitting enter, does it.
02:17:22
But it'd be kind of stupid if when you run a program in this world,
02:17:25
simple as it is, if the next command is now
02:17:28
weirdly spaced in the middle of the terminal with the dollar sign,
02:17:31
it just looks sloppy.
02:17:33
It's really just an aesthetic argument.
02:17:35
And notice that it's not acceptable or correct to do this, to hit enter there.
02:17:40
Let me go ahead and save that, though, and see what happens.
02:17:43
Let me go ahead now and run Make Hello enter.
02:17:47
Oh my god, like four errors.
02:17:49
This is like, what, 10 lines of errors for a one line program.
02:17:53
And this is where, again, you'll start to develop the instincts for just
02:17:56
reading this stuff.
02:17:57
These kinds of tools, like the compiler tool we're using,
02:18:00
were not designed necessarily with user friendliness in mind.
02:18:03
That's changed over the decades, but certainly early
02:18:06
on it's really just meant to be correct and precise with its errors.
02:18:09
So what did I do here?
02:18:11
Missing terminating close quote character,
02:18:13
long story short, when you have a string in C,
02:18:17
your double quotes just have to be on the same line just because.
02:18:20
Now, there's the slight white lie.
02:18:22
There's ways around this.
02:18:23
But the best way around it is to use this so-called escape sequence.
02:18:29
To escape something means generally to put a backslash, and then
02:18:32
a special symbol like n for new line.
02:18:34
And this is just the agreed upon way that humans, decades ago, decided,
02:18:39
OK you don't just hit your enter key.
02:18:42
You instead put backslash n and that tells the computer
02:18:46
to move the cursor to the new line.
02:18:49
So again, kind of cryptic.
02:18:50
But once you know it, that's it.
02:18:52
It's just another word in our vocabulary.
02:18:55
So now let me transition to making my program a little more interactive.
02:18:58
Instead of just saying Hello, world, let me
02:19:00
change it like last week to say Hello, David,
02:19:02
or whoever is interacting with the program.
02:19:04
So I'm going to do string answer gets, get string,
02:19:07
quote unquote, what's your name.
02:19:11
I'm not going to bother with a new line here.
02:19:13
I could. This is now just a judgment call.
02:19:15
I deliberately want the human to type their name on the same line
02:19:17
just because.
02:19:18
And how do I now print this?
02:19:20
Well last week recall we used say.
02:19:22
And then we use the other block called join.
02:19:25
So the idea here is the same.
02:19:27
But the syntax this week is going to be a little different.
02:19:30
It's going to be printf, which prints something on the screen.
02:19:33
I'm going to go ahead and say Hello comma.
02:19:38
And let me just go with this initially with the backslash n, semicolon.
02:19:43
Let me go ahead and recompile my code.
02:19:46
Whoops, damn doesn't work still.
02:19:50
And look at all these errors.
02:19:51
There's more errors than code I wrote.
02:19:54
But what's going on here?
02:19:56
Well, this is actually something, a mistake you'll see,
02:19:58
somewhat often, at least initially.
02:20:01
And let's start to glean what's going on here.
02:20:03
So here, if I look at the very first line of output after the dollar sign--
02:20:06
so even though it jumped down the screen pretty fast,
02:20:09
I wrote Make Hello at the dollar sign, prompt.
02:20:12
And then here's the first error.
02:20:14
On Hello dot C, line 5--
02:20:17
technically character 5, but generally line is enough to get you going--
02:20:21
there's an error, use of undeclared identifier string.
02:20:24
Did you mean standard in?
02:20:27
So, I didn't.
02:20:28
And this is not an obvious solution at first.
02:20:30
But you'll start to recognize these patterns in error messages.
02:20:34
It turns out that if I want to use string, I actually have to do this.
02:20:39
I have to include another library up here, another line of code,
02:20:43
rather, called CS50 dot H. We'll come back
02:20:46
to what this means in just a moment.
02:20:48
But if I now retroactively say, all right, what does standard I/O
02:20:54
do for us up here.
02:20:55
Before I added that new line, what is standard I/O doing?
02:20:59
Well, if you think back to Scratch, there
02:21:01
were a few examples with the camera and with the speech to-- the text to voice.
02:21:07
Remember I had to poke around in the extensions button.
02:21:10
And then I had to load it into Scratch.
02:21:12
It didn't come natively with Scratch.
02:21:14
C is quite like that.
02:21:15
Some functions come with the language.
02:21:18
But for the most part, if you want to use a function, an action or a verb
02:21:22
like printf, you have to load that extension, so to speak,
02:21:26
that more traditionally is called a library.
02:21:29
So there is a standard I/O library, STD I/O, standard I/O,
02:21:35
where I/O just means input and output.
02:21:37
Which means, just like in MIT's World, there
02:21:39
was an extension for doing text to voice or for using your camera.
02:21:43
In C, there's an extension, a.k.a.
02:21:45
a library, for doing standard input and output.
02:21:49
And so if you want to use any functions related to standard input and output,
02:21:53
like text from a keyboard, you have to include standard I/O dot
02:21:58
H. And then can you use printf.
02:22:02
Same goes here.
02:22:03
Get string, it turns out, is a function that CS50 wrote some time ago.
02:22:08
And as we'll see over the coming weeks, it just
02:22:10
makes it way easier to get input from a user.
02:22:14
C is very good with printf at printing output on the screen.
02:22:18
C makes it really annoying and hard, as we'll see in a few weeks,
02:22:21
to just get input from the user.
02:22:23
So we wrote a function called get_string,
02:22:25
but the only way you can use that is to load the extension,
02:22:29
a.k.a. load the library called CS50.
02:22:32
And we'll come back in time, like, why is it .h, why is it a hash symbol.
02:22:35
But for now, standard I/O is a library that
02:22:39
gives you access to printf and input- and output-related stuff.
02:22:42
CS50 is a second library that provides you
02:22:45
with access to functions that don't come with C
02:22:48
that include something like get_string.
02:22:51
So with that said, we've now kind of teased apart
02:22:55
at a high level what lines 2 and now 1 are doing.
02:22:58
Let me go ahead and rerun make hello.
02:23:00
Now it worked.
02:23:01
So all those crazy error messages were resolved by just one fix,
02:23:05
so key takeaway is not to get overwhelmed by the sheer number
02:23:07
of errors.
02:23:08
Let me now do ./hello and if I type in my name, what am I going to say?
02:23:16
What do you think?
02:23:19
Yeah, hello answer, because the computer is going to take me literally.
02:23:24
And it turns out that if you just write "hello,
02:23:26
answer" all in the double quotes, you're really just passing
02:23:29
English as the input to the printf function,
02:23:32
you're not actually passing in the variable.
02:23:34
And unfortunately in C, it's not quite as
02:23:37
easy to plug things in to other things that you've typed.
02:23:40
Remember in Scratch, there was not just the Save block
02:23:43
but the Join block, which was kind of pretty,
02:23:45
you can combine apples and oranges--
02:23:47
or was it apple and banana?
02:23:48
Then we changed it to hello and then the answer that the human typed in.
02:23:52
In C, the syntax is going to be a little different.
02:23:54
You tell the computer inside of your double quotes that you want to have
02:23:59
a placeholder there, a so-called format code. %s means, hey, computer,
02:24:05
put a string here eventually.
02:24:08
Then outside of your quotes, you just add a comma and then you type
02:24:12
in whatever variable you want the computer to plug in at that %s location
02:24:18
for you.
02:24:19
So %s is a format code which serves as a placeholder.
02:24:23
And now the printf function was designed by humans years
02:24:26
ago to figure out how to do the apple and banana
02:24:28
thing of joining two words together.
02:24:30
It's not nearly as user-friendly as it is in Scratch,
02:24:33
but it's a very common paradigm.
02:24:35
So let me try and rerun this now. make hello.
02:24:38
No errors, that's good.
02:24:40
./hello.
02:24:41
What's my name, David?
02:24:42
If I type Enter now, now it's hello.
02:24:45
David.
02:24:46
And the printf, here's the F in printf.
02:24:48
It formats its input for you by using these placeholders for things like
02:24:53
strings, represented again by %s.
02:24:58
So a quick question then, if I focus here on line 7 for just a moment
02:25:02
and even zoom in here, how many inputs is printf taking as a function?
02:25:09
A moment ago, I'll admit that it was taking one input, "hello, world,"
02:25:13
quote unquote.
02:25:14
How many inputs might you infer printf is taking now?
02:25:19
2.
02:25:20
And it's implied by this comma here, which is separating the first one,
02:25:25
quote, unquote, "hello, %s" from the second one, answer.
02:25:29
And then just as a quick safety check here, why is it not 3?
02:25:32
Because there's obviously two commas here.
02:25:35
Why is it not actually 3 arguments or inputs?
02:25:38
AUDIENCE: [INAUDIBLE]
02:25:42
DAVID J. MALAN: Exactly.
02:25:43
The comma to the left is actually part of my English grammar,
02:25:46
that's all, so same syntax.
02:25:47
And, again, here's where programming can just be confusing
02:25:50
early on because we're using the same special punctuation to mean
02:25:52
different things, it just depends on the context.
02:25:55
And so now is actually a good time to point out
02:25:57
all of the somewhat pretty colors that have been popping up on the screen
02:26:01
here--
02:26:02
even though I wasn't going to a format menu, I wasn't boldfacing things,
02:26:06
I certainly wasn't changing things to red or blue or whatnot--
02:26:08
that's because a text editor like VS Code syntax highlights for you.
02:26:13
This is a feature of so many different programming environments nowadays,
02:26:17
VS Code does it as well.
02:26:18
If your text editor understands the language that you're programming in--
02:26:23
C, in this case--
02:26:24
it highlights in different colors the different types of ideas in your code.
02:26:28
So, for instance, string and answer here are in black,
02:26:31
but get_string a function is in this sort of nasty brown-yellow
02:26:35
here right now, but that's just how it displays on the screen.
02:26:38
The string, though, here in red is kind of jumping out at me,
02:26:41
and that's marginally useful.
02:26:42
The %s is in blue.
02:26:44
That's kind of nice, because it's jumping out at me.
02:26:46
And so it's just using different colors to make different things on the screen
02:26:49
pop so you can focus on how these ideas interrelate
02:26:53
and, honestly, when you might make a mistake.
02:26:55
For instance, let me accidentally leave off this quote here.
02:26:58
And now all of a sudden, notice if I delete the quote,
02:27:03
the colors start to get a little awry.
02:27:05
But if I go back there and put it back, now everything's back in place.
02:27:09
What's another feature of this text editor?
02:27:11
Notice when my cursor is next to this parenthesis, which
02:27:15
demarcates the end of the inputs to the function,
02:27:18
notice that highlighted in green here is the opening parenthesis.
02:27:21
Why?
02:27:22
It's just a visually useful thing, especially when
02:27:24
you start writing more and more code, just to make
02:27:26
sure your parentheses are lining up.
02:27:28
And that's true for these curly braces over here on the left and the right.
02:27:31
We'll come back to those in a moment.
02:27:33
If I put my cursor there, you can see that these things correspond
02:27:36
to one another.
02:27:37
So it's nothing in your code fundamentally, it's just the editor
02:27:40
trying to help you, the human, program.
02:27:42
And you can even see it, though it's a little subtle--
02:27:45
see these four dots here and these four dots here?
02:27:48
That's my indentation.
02:27:49
I configured VS Code to indent by four spaces, which
02:27:53
is a very common convention.
02:27:54
Any time I hit the Tab key, this too can help you make sure--
02:27:58
once we have more interesting and longer programs--
02:28:00
that everything lines up nice and neatly.
02:28:04
Phew.
02:28:05
All right, any questions then on printf or more?
02:28:07
Yeah.
02:28:08
AUDIENCE: [? Would ?] the printf [INAUDIBLE]??
02:28:11
DAVID J. MALAN: Short answer, yes. printf
02:28:12
can handle more than one type of variable or value.
02:28:16
%s is one.
02:28:17
We're going to see %i is another for plugging in an integer.
02:28:20
You can have multiple i's, multiple s's, and even other symbols too.
02:28:24
We'll come back to that in just a little bit.
02:28:26
printf can take many more arguments than just these two.
02:28:29
This is just meant to be representative.
02:28:32
Yeah, over here.
02:28:34
Can you declare variables within the printf?
02:28:36
No.
02:28:37
The only variable I'm using right now is answer,
02:28:39
and it's got to be done outside the context of printf in this case.
02:28:43
Good question, we'll see more of that before long.
02:28:46
Yeah, in back.
02:28:47
AUDIENCE: [INAUDIBLE]
02:28:49
DAVID J. MALAN: How do we download the CS50 library?
02:28:51
So we will show you in problems set 1 exactly how to do that.
02:28:55
It's automatically done for you in our version of VS Code in the cloud.
02:28:58
If, ultimately, you program on your own Mac or PC, either initially or later
02:29:01
on, it's also installable online.
02:29:03
But if you want to ask that via online or afterward,
02:29:07
we can point you in the right direction.
02:29:08
But PSet 1 will itself.
02:29:10
Yeah.
02:29:11
AUDIENCE: [INAUDIBLE]
02:29:13
DAVID J. MALAN: String is the type of the variable or, more properly,
02:29:16
the data type of the variable.
02:29:19
int is another keyword I alluded to earlier, I haven't used it yet.
02:29:22
int, for integer, is going to be another type, or data type, of variable.
02:29:26
AUDIENCE: OK. [? Thank you. ?]
02:29:27
DAVID J. MALAN: Yeah.
02:29:28
AUDIENCE: [INAUDIBLE]
02:29:29
DAVID J. MALAN: Oh, good question.
02:29:31
Could I go ahead and just plug in this function,
02:29:34
kind of like we did in Scratch, getting rid of the variable altogether
02:29:39
and just do this, which recall, is reminiscent of what
02:29:42
I did in Scratch by plopping block on top of block on block?
02:29:45
Am I answering that right?
02:29:48
Can I put string in front of get_string?
02:29:50
No. You only put the word string in front of a variable
02:29:53
that you want to make string.
02:29:55
And even though I'm apparently answering the wrong question,
02:29:57
let me go ahead and zoom out, save this, do make hello again.
02:30:01
Seems to compile OK.
02:30:03
If I run ./hello, type in David, voila.
02:30:06
That, too, works.
02:30:07
And so, actually, let's go down this rabbit hole for just a moment.
02:30:10
Clearly, it's still correct--
02:30:12
at least, based on my limited testing.
02:30:14
Is this better designed or worse designed?
02:30:18
Let's open that question like we did last week.
02:30:20
Yeah?
02:30:21
Yeah, I kind of agree with that.
02:30:23
Reasonable people could disagree, but I do
02:30:26
agree that this seems harder to read because I start reading here,
02:30:29
but wait a minute. get_string is going to get used first,
02:30:32
and then it's going to give me back a value.
02:30:34
So, yeah, it just feels like it was nicer to read top to bottom,
02:30:37
I would say.
02:30:38
Your thoughts? AUDIENCE: [INAUDIBLE]
02:30:39
DAVID J. MALAN: Yeah.
02:30:40
And so this is useful if I only want to print out the person's name once.
02:30:44
If I want to use it later in a longer program, I'm out of luck,
02:30:47
and so I haven't saved it in a variable.
02:30:49
So I think, long story short, we could debate this all day long.
02:30:53
But in this case, eh, if you can make a reasonable argument one
02:30:56
way or the other, that's a pretty solid ground to stand on.
02:30:59
But, invariably, reasonable people are going
02:31:01
to disagree, whether first-time programmers or many years after that.
02:31:05
So let's frame this one last example in the context of the same process
02:31:09
of taking inputs and outputs.
02:31:11
The functions we've been talking about all
02:31:13
take inputs, otherwise now known as arguments, or parameters, pretty much
02:31:17
synonymous.
02:31:18
That's just the fancy word for an input to a function.
02:31:21
And some functions have either side effects, like we saw--
02:31:25
printing something, saying something on the screen,
02:31:27
sort of visually or audibly--
02:31:28
or they return a value, which is a reusable value, like name or answer,
02:31:33
in this case.
02:31:35
If we look then at what we did last time in the world of Scratch last week,
02:31:39
the input was what's your name, the function was ask,
02:31:41
and the return value was answer.
02:31:44
And now let's take a look at this block, which is honestly a more user-friendly
02:31:49
version of what we just did with the %s.
02:31:51
Last week we said save, then join, then hello and answer.
02:31:54
But the interesting takeaway there was not how to say hello anything.
02:31:58
It was the fact that in Scratch 2, the output of one function,
02:32:03
like the green join, could become the input to another function,
02:32:08
the purple say.
02:32:09
The syntax in C is admittedly pretty different,
02:32:12
but the idea is essentially the same.
02:32:14
Here, though, we have hello, a placeholder,
02:32:18
but we have to, in this world of C, tell printf
02:32:21
what we want to plug in for that placeholder.
02:32:25
It's just different. But that's the way to do it.
02:32:27
When we get to Python and other languages later in the term,
02:32:29
there's actually easier ways to do this.
02:32:31
But this is a very common paradigm, particularly when
02:32:34
you want to format your data in some way.
02:32:37
All right, let's then take a step back to where we began,
02:32:40
which was with that whole program, which had the include
02:32:43
and it had int main(void) and all of this other cryptic syntax.
02:32:47
This Scratch piece last week was kind of like the go-to
02:32:51
whenever you want to have a main part of your program.
02:32:53
It's not the only way to start a Scratch program.
02:32:55
You could listen for clicks or other things, not just the green flag.
02:32:59
But this was probably the most popular place to start a program in Scratch.
02:33:03
In C, the closest analog is to literally write this out.
02:33:07
So just like last week, if you were in the habit of dragging and dropping
02:33:10
when green flag clicked, as a C programmer,
02:33:13
the first thing you would do is after creating an empty file,
02:33:15
like I did with hello.c, you'd probably type int
02:33:18
main(void) open curly brace, closed curly brace,
02:33:22
and then you can put all of your code inside of those curly braces.
02:33:26
So just like Scratch had this sort of magnetic nature
02:33:29
to it where the puzzle pieces would snap together, C, as a text-based language,
02:33:33
tends to use these curly braces, one of them opened, the other one closed.
02:33:38
And anything inside of those braces, so to speak,
02:33:41
is part of this puzzle piece, a.k.a.
02:33:44
main.
02:33:45
So what was atop them?
02:33:47
We went down this rabbit hole moment ago with these things called header files,
02:33:50
even though I didn't call them by this name.
02:33:52
But, indeed, when we have a whole program in Scratch, super easy.
02:33:56
Just have the one green flag clicked and then say hello, world.
02:33:59
There's no special syntax.
02:34:00
After all, it's meant to be very user-friendly and graphical.
02:34:03
In C, though, you technically can't just put int main(void) printf hello, world.
02:34:09
You also need this.
02:34:10
Because, again, you need to tell the compiler to load the library--
02:34:16
code that someone else wrote-- so that the compiler knows what printf even is.
02:34:22
You have to load the CS50 library whenever
02:34:24
you want to use get_string or other functions, like get_int,
02:34:28
as we'll soon see.
02:34:29
Otherwise, the compiler won't know what get_string is.
02:34:32
You just have to do it this way.
02:34:35
The specific file name I'm mentioning here,
02:34:37
stdio.h, cs50.h, is what C programmers called a call a header file.
02:34:44
We'll see eventually what's inside of those files.
02:34:46
But long story short, it's like a menu of all of the available functions.
02:34:51
So in cs50.h, there's a menu mentioning get_string, get_int,
02:34:54
and a bunch of other stuff.
02:34:56
And in stdio.h, there's a menu of functions, among which are printf.
02:35:01
And that menu is what prepares the compiler
02:35:04
to know how to implement those same functions.
02:35:08
All right, let me pause here.
02:35:10
Question.
02:35:11
AUDIENCE: [INAUDIBLE]
02:35:15
DAVID J. MALAN: Not quite.
02:35:16
A library provides all of the functionality we're talking about.
02:35:20
A header file is the very specific mechanism via which you include it.
02:35:25
And we'll discuss this more next week.
02:35:27
For now, they're essentially the same, but we'll discuss
02:35:30
nuances between the two next week.
02:35:32
Yeah, the library would be standard I/O. The library would CS50.
02:35:36
The corresponding header file is stdio.h, cs50.h.
02:35:41
Indeed.
02:35:42
Other questions. Yeah.
02:35:43
AUDIENCE: [INAUDIBLE]
02:35:45
DAVID J. MALAN: Indeed.
02:35:46
That, too, is on the menu.
02:35:48
We'll come back to that.
02:35:49
But the word string--
02:35:50
incredibly common in the world of programming, it's not a CS50 idea--
02:35:54
but in C, there's technically no such data type as string by default.
02:35:59
We have sort of conjured it up to simplify the first few weeks.
02:36:02
That's a training wheel that we'll very deliberately, in a few weeks,
02:36:05
take away, and we'll see why we've been using get_string and string.
02:36:09
Because C otherwise makes things quite more challenging early on,
02:36:14
which then gets besides the point for us.
02:36:16
Yeah.
02:36:17
AUDIENCE: [INAUDIBLE]
02:36:19
DAVID J. MALAN: Yes.
02:36:20
Early on, you will have to use whatever is prescribed by the specification.
02:36:23
That will include CS50's functions.
02:36:24
Long story short, you referred, I think, a moment ago to another function
02:36:28
called scanf, we won't talk about for a few weeks.
02:36:30
Long story short, in C, it's pretty easy and possible to get input from a user.
02:36:36
The catch is that it's really easy to do it dangerously.
02:36:40
And C, because it's an older, lower-level language, so to speak,
02:36:45
that gives you pretty much ultimate control over your computer's hardware.
02:36:49
It's very easy to make mistakes.
02:36:51
And, indeed, that's too why we use the library,
02:36:55
so your code won't crash unintendedly.
02:36:58
All right, so with this in mind, we have this now mapping
02:37:01
between the Scratch version and the other.
02:37:03
Let me just give you a quick tour of some of the other placeholders and data
02:37:06
types that students will start seeing as we assemble more interesting programs.
02:37:10
In the world of Linux, here is a non-exhaustive list
02:37:13
of commands with which you'll get familiar over the next few weeks
02:37:16
by playing with problem sets.
02:37:18
We've only seen two of these so far, ls for list, rm for others.
02:37:22
But I mention them now just so that it doesn't
02:37:24
feel too foreign when you see them on screen or online in a problem set.
02:37:30
cp is going to stand for copy.
02:37:32
mkdir is going to stand for make directory. mv is
02:37:35
going to stand for move or rename.
02:37:38
rmdir is going to be remove directory, and cd is going to be for change /
02:37:44
and let me show you this last one here first,
02:37:46
only because it's something you'll use so commonly.
02:37:49
If I go back to my code here on the screen, I'm going to go ahead
02:37:53
and re-open the little GUI on the left-hand side, the so-called Explorer,
02:37:58
revealing that I've got two files, hello and hello.c
02:38:01
so nothing has changed since there.
02:38:02
Suppose now that it's a few weeks into class
02:38:05
and I want to start organizing the code I'm
02:38:07
writing so that I have a folder for this week or next week,
02:38:10
or maybe a folder for problem set 1, problem set 2.
02:38:13
I can do this in a few ways.
02:38:15
In the GUI, I can go up here and do what most of you
02:38:18
would do instinctively on a Mac or PC.
02:38:19
You look for like a folder icon, you click it,
02:38:22
and then you name a folder like PSet1, Enter.
02:38:25
Voila, you've got a folder called PSet1.
02:38:28
I can confirm as much with my command line interface by typing what command?
02:38:34
How can I list what's in my folder?
02:38:36
Yeah, so ls for list.
02:38:38
And now I see hello--
02:38:39
and it's green with an asterisk because that's
02:38:41
my executable, my runnable program--
02:38:43
hello.c, which is my source code, and now PSet1 with a slash
02:38:46
at the end, which just implies that it's indeed a folder.
02:38:50
All right, I didn't really want to do it that way.
02:38:52
I'd like to do it more advanced.
02:38:53
So let me go ahead and right-click on PSet1, delete permanently.
02:38:57
I get a scary irreversible error message.
02:38:59
But there's nothing in it, so that's fine.
02:39:01
Now I've deleted it using the GUI.
02:39:03
But now let me go ahead and start doing the same thing from the command line.
02:39:08
And if you're wondering how things keep disappearing,
02:39:11
if you hit Control-L in your terminal window or explicitly type clear,
02:39:15
it will delete everything you previously typed just to clean things up.
02:39:18
In practice, you don't need to be doing this often.
02:39:20
I'm doing it just to keep our focus on my latest commands.
02:39:23
If I do-- what was the command to make a new directory?
02:39:26
AUDIENCE: [INAUDIBLE]
02:39:28
DAVID J. MALAN: Yeah, so mkdir, make directory.
02:39:30
Let me create PSet1, Enter.
02:39:32
And notice at left, there's my PSet1.
02:39:34
If I want to get a little overzealous, plan for next week,
02:39:37
here's my PSet2 directory.
02:39:39
Suppose now I want to open those folders on a Mac or PC or in this GUI,
02:39:44
I could double-click on it like this, and you'd
02:39:46
see this little arrow is moving.
02:39:48
It's not doing anything because there's nothing in there, but that's fine.
02:39:51
But suppose again I want to get more comfortable with my command line.
02:39:55
Notice if I type ls now, I see all four same things.
02:39:59
Let me change directories with cd space PSet1 Enter.
02:40:05
And now notice two things will have happened.
02:40:08
One, my prompt has changed slightly to remind me where I am,
02:40:14
just to keep me sane so that I don't forget what folder I'm actually in.
02:40:17
So here is just a visual reminder of what folder I'm currently in.
02:40:21
If I type ls now, what should I see after hitting Enter?
02:40:26
Nothing, because I've only created empty folders so far.
02:40:29
And, indeed, I see nothing.
02:40:31
If I wanted to create a folder called Mario for a program that might be
02:40:35
called Mario this week, I can do that.
02:40:37
Now if I type ls, there is Mario.
02:40:40
Now if I do cd Mario, notice my prompt's going
02:40:42
to change to be a little more precise.
02:40:44
Now I'm in PSet1/Mario.
02:40:47
And notice what's happening at top left.
02:40:49
Nothing now, because these folders are collapsed.
02:40:51
But if I click the little triangle, there I see Mario.
02:40:54
Nothing's going on in there because there's no files yet.
02:40:56
But suppose now I want to create a file called mario.c.
02:41:00
I could go up here, I could click the little plus icon, and use the GUI.
02:41:04
Or I can just type code mario.c.
02:41:07
Voila.
02:41:08
That creates a new tab for me.
02:41:10
I'm not going to write any code in here yet, but I am going to save the file.
02:41:13
And now at top left, you'll see that mario.c appears.
02:41:16
So at some point, you can eventually just close the Explorer.
02:41:19
Because, again, it's not providing you with any new information.
02:41:22
It's maybe more user-friendly, but there's
02:41:23
nothing you can't do at the command line that you could do with the GUI.
02:41:27
All right, but now I'm kind of stuck.
02:41:29
How do I get out of this folder?
02:41:32
In my Mac or PC world, I'd probably click
02:41:34
the Back button or something like that or just close it and start all over.
02:41:37
In the terminal window, I can do cd dot dot.
02:41:42
Dot dot is a nickname, if you will, for the parent directory.
02:41:46
That is, the previous directory.
02:41:48
So if I hit Enter now, notice I'm going to close the Mario folder,
02:41:52
a.k.a. directory, and now I'm back in PSet1.
02:41:56
Or, if I want to be fancy, let me go back into Mario temporarily.
02:42:00
If I type ls, there's mario.c, just to orient us.
02:42:03
If I want to do multiple things at a time, I could do cd../..
02:42:09
which goes to my parent to my grandparent all in one breath.
02:42:13
And voila, now I'm back in my default folder, if you will.
02:42:16
And one last little trick of the trade, if I'm in PSet1/Mario like I
02:42:20
was a moment ago, and you're just tired of all the navigation,
02:42:24
if you just type cd and hit Enter, it'll whisk you
02:42:26
away back to your default folder, and you don't have
02:42:29
to worry about getting there manually.
02:42:31
Recall a bit ago, though, that I was running hello as this, ./hello.
02:42:38
If dot refers to my parent, perhaps infer here syntactically,
02:42:42
what does a single dot mean instead?
02:42:46
It means this directory, your current directory.
02:42:49
Why is that necessary?
02:42:50
It just makes super explicit to the computer
02:42:52
that I want the program called hello that's installed here,
02:42:55
not in some random other folder on my hard drive, so to speak.
02:42:59
I want the one that's right here instead.
02:43:02
All right, so besides these commands, there's
02:43:04
going to be others that we encounter over time.
02:43:06
Those are kind of the basics.
02:43:07
That allows you to wean yourself off of a GUI, Graphical User Interface,
02:43:11
and start using more comfortably, with practice and time,
02:43:14
a command line interface instead.
02:43:16
Well, what about those other types, now back in the world of C?
02:43:19
Those commands were not C. Those are just command-specific to a command line
02:43:23
interface, like in Linux, which, again, we're using in the cloud.
02:43:28
It's an alternative to Mac OS and Windows.
02:43:30
Back in the world of C now, we've seen strings, which are words.
02:43:34
I mentioned int or integer, but there's others as well.
02:43:38
In the world of C, we've seen string, we will see int.
02:43:42
If you want a bigger integer, there's something literally called a long.
02:43:46
If you want a single character, there's something called a char.
02:43:49
If you want a Boolean value, true or false, there is a bool.
02:43:53
And if you want a floating-point value--
02:43:56
a fancy way of saying a real number, something with a decimal point in it--
02:43:59
that is what C and other languages call a float.
02:44:03
And if you want even more numbers after the decimal point that
02:44:06
is more precision, you can use something called a double.
02:44:10
That is to say, here is, again, an example in programming
02:44:14
where it's up to you now to provide the computer with hints, essentially,
02:44:17
that it will rely on to know what is this pattern of zeros and ones.
02:44:21
Is it a number, a letter?
02:44:23
Is it a sound, an image, a color, or the like?
02:44:26
These are the types of data types that provide exactly those hints.
02:44:30
What are the functions that come in the menu that is the CS50 library?
02:44:35
We talked about standard I/O, and that's just one function so far, printf.
02:44:39
In the CS50 library, you can see that it follows a pattern.
02:44:42
The C50 library exists largely for the first few weeks
02:44:45
of the class to make our lives easier when you just want to get user input.
02:44:50
So if you want to get a string, like a word or words from the human,
02:44:53
you use get_string.
02:44:54
If you want to get an integer from the user, you're going to use get_int.
02:44:57
When you want to get any of those other data types, for the most part,
02:45:01
you use get_ something else.
02:45:03
And they're indeed all lowercase by convention.
02:45:06
What about printf?
02:45:07
If we have the ability now to store different types of data
02:45:10
and we have functions with which to get different types of data,
02:45:13
how might you go about printing different types of data?
02:45:16
Well, we've seen %s for string, %i for integer, %c for char,
02:45:23
%f for a float or a double, those real numbers I described earlier,
02:45:30
and then %li for a long integer.
02:45:33
So here's the first example of inconsistencies.
02:45:36
In an ideal world, that would just be %l and we'd move on.
02:45:38
It's %li instead in this case.
02:45:42
That's printf and some of its format codes.
02:45:45
What more might we do?
02:45:47
Well, in C, as we'll see--
02:45:49
no pun intended-- there is a whole bunch of operators.
02:45:51
And, indeed, computers, one of the first things they did
02:45:54
was a lot of math and calculations, so there's a lot of operators like these.
02:45:57
Computers, and in turn, C, really good at addition, subtraction,
02:46:01
multiplication, division, and even the percent sign,
02:46:04
which is the remainder operator.
02:46:05
There's a special symbol in C and other languages
02:46:08
just for getting the remainder, when you divide one number by another.
02:46:13
There are other features in the world of C, like variables, as we've seen.
02:46:18
And there's also what is of playfully called syntactic sugar that
02:46:22
makes it easier over time to write fewer characters
02:46:27
but express your thoughts the same.
02:46:29
So just as a single example of this, as a single example,
02:46:33
consider this use of a variable last week.
02:46:37
Here in Scratch is how you might set a variable called counter to 0.
02:46:41
In C, it's going to be similar.
02:46:43
If you want the variable to be called counter,
02:46:46
you literally write the word counter, or whatever you want it to be called.
02:46:49
You then use the assignment operator, a.k.a. the equals sign,
02:46:53
and you assign it whatever its initial value should be here on the right.
02:46:56
So, again, the 0 is going to get copied from right to left into the variable
02:47:01
because of that single equal sign.
02:47:02
But this isn't sufficient in C. What else
02:47:05
is missing on the right-hand side, instinctively now?
02:47:08
Even if you've never programmed in this before.
02:47:11
Yeah, in front.
02:47:12
AUDIENCE: Semicolon.
02:47:13
DAVID J. MALAN: A semicolon at the end.
02:47:14
And one other thing, I think, is probably missing.
02:47:16
Again.
02:47:17
AUDIENCE: A data type.
02:47:18
DAVID J. MALAN: A data type.
02:47:19
So if we can keep going back and forth here,
02:47:22
what data type seems appropriate intuitively for counter?
02:47:26
int for integer.
02:47:27
So, indeed, we need to tell the computer when
02:47:29
creating a variable what type of data we want,
02:47:32
and we need to finish our thought with the semicolon.
02:47:35
So there might be a counterpart there.
02:47:38
What about in Scratch if we wanted to increment that counter variable?
02:47:43
We had this very user-friendly puzzle piece last time
02:47:45
that was change counter by 1, or add 1 to counter.
02:47:49
In C, here's where things get a little more interesting.
02:47:54
And pretty commonly done, you might do this. counter = counter + 1;
02:47:58
with a semicolon.
02:47:59
And this is where, again, it's important to note, the equal sign,
02:48:02
it's not equality.
02:48:03
Otherwise, this makes no sense.
02:48:05
counter cannot equal counter plus 1, right?
02:48:08
That just doesn't work if we're talking about integers here.
02:48:11
That's because the equal sign is assignment.
02:48:13
So it can certainly be the case that you calculate
02:48:15
counter plus 1, whatever that is, then you update the value of counter
02:48:19
from right to left to be that new value.
02:48:22
This, as we'll see, is a very common thing
02:48:25
to do in programming just to kind of count upward, for whatever reason.
02:48:29
You can write this more succinctly.
02:48:30
This code here is what we'll call syntactic sugar, sort
02:48:34
of a fancy way of saying the same thing with fewer words or fewer characters
02:48:39
on the screen.
02:48:40
This also adds 1, or whatever number you type over here,
02:48:44
to the variable on the left.
02:48:46
And there's one other form of syntactic sugar we're going to start seeing too,
02:48:49
and it's even more terse than this.
02:48:51
That too will increment counter by 1 by literally changing its value by 1.
02:48:56
Or if you change it to minus minus, subtracting 1 from it.
02:48:59
You can't do that with 2 and 3 and 4, but you
02:49:02
can do it by default with just plus plus or minus minus adding or subtracting 1.
02:49:07
Yeah.
02:49:08
AUDIENCE: [INAUDIBLE]
02:49:12
DAVID J. MALAN: Ah, so when you are changing a variable that already
02:49:15
has been created, as we did with the code that looked like this,
02:49:20
you no longer need to remind the computer what the data type is.
02:49:23
Thankfully, the computer is at least as smart as that.
02:49:27
It will remember the type of the data that you intended.
02:49:31
Other questions or comments on this?
02:49:34
All right, that's quite a lot.
02:49:36
Why don't we go ahead and here take a 10-minute break?
02:49:38
And we'll be back, we'll start writing some code.
02:49:41
All right, so we are back.
02:49:44
We've just looked at some of the basics of compiling,
02:49:48
even if it doesn't quite feel that basic.
02:49:50
But now, let's actually start focusing really
02:49:52
on writing more and more code, more and more interesting
02:49:55
code, kind of like we dove into Scratch last week.
02:49:58
So here I have these code open.
02:50:00
I've closed the GUI.
02:50:01
I'm going to focus more on my terminal window and my code editors.
02:50:04
Many different ways I can create new files, but I want to create something
02:50:07
called a calculator.
02:50:08
So, again, within this environment of VS Code,
02:50:11
I can literally write the code command which is VS Code specific,
02:50:15
and it just creates a new file for me automatically.
02:50:18
Or I could do that in the GUI.
02:50:20
I'm going to go ahead and create this file called calculator.c
02:50:23
and I'm going to go ahead and include some familiar things.
02:50:25
So I'm just going to go ahead and proactively include cs50.h, stdio.h.
02:50:30
I'm going to go ahead from memory and do the int void main--
02:50:33
more on that next week, why it's int, why it's void, and so forth.
02:50:38
And now let me just implement a very simple calculator.
02:50:40
We saw some mathematical operators, like plus and the like.
02:50:44
So let's actually use this.
02:50:45
So let me go ahead and first give myself a variable
02:50:48
called x, sort of like grade school math or algebra.
02:50:52
Let me go ahead then and get an int, which is new,
02:50:55
but I mentioned this exists.
02:50:57
And then let me just ask the user for whatever their x value is.
02:51:00
The thing in the quotes is just the English, or the string
02:51:03
that I'm printing on the screen. so I could say anything I want.
02:51:06
I'm just going to say x colon to prompt the user accordingly.
02:51:09
Now I'm going to go ahead and get another variable called y.
02:51:12
I'm going to get int again.
02:51:13
And now, I'm going to prompt the user for y.
02:51:16
And I'm just very nitpickly using a space just
02:51:18
to move the cursor so it doesn't look too messy on the screen.
02:51:22
And then lastly, let me go ahead and just print out the sum of x and y.
02:51:27
In an ideal world, I would just say something like printf x + y.
02:51:31
But that is not valid in C. The first argument, recall, in printf
02:51:36
has to be a string in double quotes.
02:51:39
So if I want to print out the value of an integer,
02:51:43
I need to put something in quotes here, maybe followed by a newline,
02:51:47
if I want to move the cursor as well.
02:51:49
So, again, we only glimpsed it briefly, but what
02:51:51
do I replace these question marks with if I want a placeholder for an integer?
02:51:55
AUDIENCE: [INAUDIBLE]
02:51:56
DAVID J. MALAN: Yeah, so %i.
02:51:57
Just like %s was string, %i is integer.
02:52:00
So I change this %i.
02:52:02
And now if I want to add x and y, for instance, super-- simple calculator,
02:52:06
doesn't do much of anything other than addition of two integers--
02:52:09
I think this works.
02:52:11
And, again, it looks definitely cryptic at first glance.
02:52:14
It would be if programming weren't this cryptic.
02:52:16
Other languages will clean this up for us.
02:52:18
But, again, if you focus on the basics, printf takes one input first--
02:52:22
which is a format string with English or whatever language,
02:52:26
some placeholders, maybe--
02:52:27
then it takes potentially more arguments after the comma,
02:52:31
like the value of x plus y.
02:52:33
All right, let me go ahead now and make calculator,
02:52:36
which, again, compiles my source code in C,
02:52:41
pictured above, and converts it into corresponding machine
02:52:44
code, or zeros and ones.
02:52:46
No error messages.
02:52:47
so that's already good.
02:52:48
Now I do ./calculator.
02:52:50
Let's do 1 plus 1 and Enter.
02:52:53
Voila.
02:52:54
Now I have the makings of a calculator.
02:52:57
Now let's start to tinker with this a little bit.
02:53:00
What if I instead had done this?
02:53:02
int z = x + y and then plug-in z here.
02:53:08
If I rerun make calculator, Enter, rerun ./calculator, type in 1 plus 1,
02:53:14
still equals 2, and let me claim that it will work for other values as well--
02:53:20
which of these versions is better designed?
02:53:22
If both seem to be correct at very cursory glance, is this version better
02:53:27
or is the previous one without the z?
02:53:30
OK, so this one is arguably better because I've now got a reusable
02:53:34
variable called z that I cannot only print but, heck,
02:53:36
if my program is longer, I can use it elsewhere.
02:53:39
Counterthoughts?
02:53:40
AUDIENCE: [INAUDIBLE]
02:53:41
DAVID J. MALAN: Yeah.
02:53:42
Debatable, like before, because it depends on my intent.
02:53:44
And, honestly, I think a pretty good argument
02:53:46
can be made for the first version.
02:53:48
Because if I have no intention of-- as you note--
02:53:50
using that variable again, you know what?
02:53:53
Maybe I might as well do this, just because it's
02:53:55
one less thing to think about.
02:53:57
It's one less distraction.
02:53:58
It's one less line of code to have to understand.
02:54:00
It's just a little tighter.
02:54:02
So here, again, it does depend on your intention.
02:54:04
But this field is pretty reasonable.
02:54:07
And I think, as someone noted earlier, when
02:54:09
I did the same thing with get_string, that, yeah, maybe kind of crossed
02:54:13
s line because get_string and the what's your name inside of it,
02:54:16
it was just so much longer.
02:54:17
But x + y, eh, it's not that hard to wrap our mind around what's
02:54:20
going on inside of the printf argument.
02:54:23
So, again, these are the kinds of thoughts that hopefully you'll
02:54:25
acquire the instinct for on not necessarily reaching
02:54:28
the same answer as someone else, but, again, the thought
02:54:30
process is what matters here.
02:54:33
All right, so how might I enhance this program a little bit?
02:54:36
Let's just talk about style for just a moment.
02:54:38
So x and y, at least in this case, are pretty reasonable variable names.
02:54:42
Why?
02:54:43
Because that's the go-to variable names in math
02:54:45
when you're adding two things together.
02:54:47
So x and y seem pretty reasonable.
02:54:48
I could have done something like, well, maybe my first variable
02:54:53
should be called first number and my next variable
02:54:56
should be called second number.
02:54:58
And then down here, I would have to change this
02:55:00
to first number plus second number.
02:55:04
Like, eh, this isn't really adding anything semantically
02:55:07
to help my comprehension.
02:55:08
But that would be one other direction we could have taken things.
02:55:11
So if you have very simple ideas that are conventionally
02:55:14
expressed with common variable names like x and y, totally fine here.
02:55:18
What if I want to annotate this program and remind myself what it is it does?
02:55:22
Well, I can add in C what are called comments.
02:55:25
With a slash slash, two forward slashes, you can write a note to yourself,
02:55:30
like prompt user for x.
02:55:32
And then down here, I could do something like prompt user
02:55:35
for y, just to remind myself what I'm doing there.
02:55:37
And down here, perform addition.
02:55:40
Now, in this case, I'm not sure these commands are really
02:55:42
adding all that much.
02:55:44
Because in the time it took me to write and eventually read these comments,
02:55:47
I could have just read the three lines of code.
02:55:49
But as our programs get more sophisticated
02:55:52
and you start to learn more syntax--
02:55:55
that, honestly, you might forget the next day, the next week,
02:55:58
the next month-- might be useful to have these notes to self that
02:56:01
reminds you of what your code is doing or maybe even
02:56:04
how it is doing that thing.
02:56:06
With these early programs, not really necessary,
02:56:09
doesn't really add all that much to our comprehension,
02:56:11
but it is a mechanism you have in place that
02:56:14
can help you actually remind yourself or remind someone
02:56:18
else what it is that's going on.
02:56:20
Well, let me go ahead and rerun this again in this current version,
02:56:23
make calculator.
02:56:24
And here, too, you might think I'm typing crazy fast--
02:56:27
not really. I'm hitting Tab a lot.
02:56:28
So it turns out that Linux, the operating system
02:56:32
we're using here in the cloud--
02:56:33
but, actually, Windows and Mac OS nowadays support this too--
02:56:36
supports autocomplete.
02:56:38
So if you only have one program that starts with C-A-L,
02:56:42
you don't have to finish writing calculator, you can just hit Tab,
02:56:45
and the computer will finish your thought for you.
02:56:47
The other thing you can do is if you hit Up and keep going up,
02:56:51
you'll scroll through your entire history of commands.
02:56:54
So there too, I've been saving some keystrokes
02:56:56
by hitting Up quickly rather than retyping the same darn thing again
02:56:59
and again. So, again, just another little convenience
02:57:02
to make programming and interacting with the command line interface even faster.
02:57:05
All right, let me go ahead and just make sure it's compiled in the current form.
02:57:09
The comments have no functional impact.
02:57:11
These green things are just notes to self.
02:57:13
Let me run calculator with maybe-- how about this?
02:57:15
Instead of 1 plus 1, how about 1 billion--
02:57:22
whoops, let's do that again.
02:57:23
Wa, da, da.
02:57:24
1 million, 1 billion, and another 1 billion, and that answer is 2 billion.
02:57:30
All right, so that seems correct.
02:57:31
Let's run this program one more time.
02:57:33
How about 2 billion plus another 2 billion?
02:57:39
Did you know that?
02:57:42
So, apparently, it's not so correct.
02:57:45
And, clearly, running 1 plus 1 was not the most robust testing of my code
02:57:49
here.
02:57:50
What might have gone wrong?
02:57:53
What might have gone wrong?
02:57:54
Yeah.
02:57:55
AUDIENCE: [INAUDIBLE]
02:57:57
DAVID J. MALAN: Yeah.
02:57:58
The computer probably ran out of space with bits.
02:58:00
So it turns out with these data types-- we've been talking about string and int
02:58:05
and also float and char and those other things-- they all use a specific,
02:58:09
and, most importantly, finite number of bits to represent them.
02:58:13
It can vary by computer.
02:58:14
Newer computers use more bits, older computers tended to use fewer bits.
02:58:18
It's not necessarily standardized for all of these data types.
02:58:21
But in this case, in this environment, it is using 32 bits for an integer.
02:58:28
That's a lot. So with 32 bits, you can count pretty high.
02:58:30
This is 64 light bulbs on the stage and could count even higher.
02:58:34
An int is only using half of these, or we have two integers here on the stage.
02:58:38
Now, if you think back to last week, we talked about 8 bits at one point.
02:58:42
And if you have 8 bits, 8 zeros and ones, you can count as high as 256--
02:58:47
just a good number to generally remember as trivia.
02:58:49
8 bits gives you 256 permutations of zeros and ones.
02:58:53
32 gives you roughly how many, if anyone knows?
02:58:57
It's 2 to the 32 power.
02:58:59
So it's roughly 4 billion, 2 to the 32.
02:59:02
If you don't know that, it's fine.
02:59:04
Most programmers, though, eventually remember these kinds of heuristics.
02:59:07
So it's roughly 4 billion.
02:59:08
So that feels like enough.
02:59:10
2 billion plus 2 billion is exactly 4 billion.
02:59:13
And that actually should fit in a 32-bit integer.
02:59:17
The catch is that my Mac, your PC, and the like
02:59:20
also like to support negative numbers.
02:59:22
And if you want to support both positive and negative numbers, that technically
02:59:26
means with 32-bit integers, you can count as high
02:59:29
as roughly 2 billion positive or 2 billion negative
02:59:33
in the other direction.
02:59:34
That's still 4 billion, give or take, but it's only half as many
02:59:38
in one direction or the other.
02:59:39
So how could I go about implementing a correct calculator here?
02:59:44
What might the solution be?
02:59:47
Yeah, so not just li, which was for long integer.
02:59:50
I have to make one more change, which is to the data type itself.
02:59:54
So let me go back up here and change x from an int to a long, a.k.a.
02:59:59
long integer.
03:00:00
And then let me change y as well.
03:00:02
And then let me change the format code per the little cheat sheet we had up
03:00:05
a few minutes ago to li.
03:00:07
Let me recompile the calculator--
03:00:10
seems to work OK.
03:00:11
Let's rerun it.
03:00:12
Now let's do 1 plus 1.
03:00:14
That's should obviously be the same.
03:00:15
Now let's do 2 billion and another 2 billion
03:00:20
and cross our fingers this time.
03:00:22
Now we're counting as high as 4 billion.
03:00:24
And we can go way higher than 4 billion, but we're only
03:00:27
kicking the can down the street a bit.
03:00:29
Even though we're now using--
03:00:31
with a long--
03:00:32
64 bits, which is as long as this stage now, that's still a finite value.
03:00:37
It might be a really big value, but it's still finite.
03:00:40
And we'll come back at the end of today to these kinds
03:00:42
of fundamental limitations.
03:00:44
Because arguably now, my calculator is correct for like millions,
03:00:48
billions of possible inputs but not all.
03:00:51
And that's problematic if you actually want
03:00:53
to use my calculator for any possible inputs, not just
03:00:58
ones that are roughly less than, say, 2 billion, as in this case.
03:01:03
All right, any questions then on that?
03:01:05
But it's really just a precursor for all the problems
03:01:07
that we're going to have to eventually deal with later on.
03:01:10
AUDIENCE: [INAUDIBLE]
03:01:15
DAVID J. MALAN: A good question.
03:01:17
Yes. If we were still using z, we would also have to change it to a long.
03:01:20
Otherwise, we'd be ignoring 32 of the bits that
03:01:23
had been added together via the longs.
03:01:25
Good question.
03:01:27
All right, so how about we spice things up with maybe not just addition here,
03:01:32
how about something with some conditions?
03:01:35
Let's start to ask some actual questions.
03:01:37
So a moment ago, recall that we had just the declaration of variables.
03:01:44
Now let's look back at something in Scratch that
03:01:46
looked a little something like this, a bunch of puzzle pieces
03:01:48
asking questions by way of these conditionals
03:01:50
and then these Boolean expressions here in green, maybe saying something
03:01:53
like x is less than y.
03:01:55
In C, this actually maps pretty cleanly.
03:01:58
It's much cleaner from left to right than it was with printf and join.
03:02:02
Here, we have just code that looks like this.
03:02:04
If, a space, two parentheses and then x less than y,
03:02:09
and then we have something like printf there in the middle.
03:02:12
So here, it's actually kind of a nice mapping.
03:02:14
Notice that, just as the yellow puzzle piece in Scratch
03:02:16
is kind of hugging the purple puzzle piece,
03:02:18
that's effectively the role that these curly braces are playing.
03:02:21
They're sort of encapsulating all of the code on the inside.
03:02:24
The parentheses represent the Boolean expression
03:02:27
that needs to be asked and answered to decide whether or not to do this thing.
03:02:32
And here's an exception to what I alluded to earlier.
03:02:34
Usually, when you see a word and then a parenthesis, something,
03:02:38
and then closed parenthesis, I claimed that's usually a function.
03:02:42
And I'm still feeling pretty good about that claim.
03:02:44
But there are exceptions.
03:02:45
And the word if is not a function.
03:02:48
It's just a programming construct.
03:02:50
It's a feature of the C language that similarly uses parentheses, just
03:02:55
for different purposes for a Boolean expression.
03:02:58
How about something like this?
03:02:59
Last week, if you wanted to have a two-way fork in the road,
03:03:02
go this way or that way, you can have if and else.
03:03:05
In C, that would look a little something like this.
03:03:08
And if we add in the printf's, it now looks quite like the same,
03:03:11
but it adds, of course, the word else and then a couple of more curly braces.
03:03:15
As an aside, in C, It's not strictly necessary to have curly braces
03:03:21
if you have only one line of code indented underneath.
03:03:25
For best practice, though, do so anyway, because it makes super clear to you
03:03:30
and ultimately anyone else reading your code
03:03:31
that you intend for just that one or more line of code to execute.
03:03:35
How about this from last week?
03:03:37
Here was a three-way fork in the road.
03:03:38
If x is less than y, else if x is greater than y, else if x equals y.
03:03:45
Now, here's where you have some disparities between Scratch
03:03:47
and C. Scratch uses an equals sign for equality, to compare two values.
03:03:52
C uses a single equals sign for assignment from right
03:03:55
to left, minor difference between the two worlds.
03:03:58
In C, we could implement the same code like this, the addition being
03:04:02
just this additional else if.
03:04:04
And if we add in the printf's, it looks a little something now like this.
03:04:08
This is correct both in the Scratch world and in the C world.
03:04:12
But could someone make a claim that this is not, again, well-designed?
03:04:16
Exactly.
03:04:17
We don't need the last if.
03:04:18
We need the else, at least, but we don't need the last if.
03:04:21
Because, at least in the world of comparing integers,
03:04:24
it's either going to be less than, greater than, or equal to.
03:04:27
There is no other case.
03:04:28
So you can save a few seconds, if you will,
03:04:31
of your program running-- a blink of the eye-- by only asking two questions
03:04:35
and then inferring what the answer to the third
03:04:37
must be just by nature of your own human logic here.
03:04:41
Now, why is that a good thing?
03:04:42
If, for instance, x and y happen to equal each other--
03:04:46
I type in 1 and 1 for both values, either in Scratch or in the C world--
03:04:51
in the case of this version, you're sort of stupidly
03:04:55
asking three questions, all of which are going to get asked
03:04:59
even though the answer is no, no, yes.
03:05:02
That is false, false, true.
03:05:05
That seems to be unnecessary because if we instead optimize this code,
03:05:09
get rid of the unnecessary if and just do as you proposed logically--
03:05:13
else print that x is equal to y--
03:05:16
now if x indeed equals y because they're both 1 or some other value,
03:05:20
now you're only going to ask two questions, so 2/3 as many questions,
03:05:26
and then you're going to get your same correct result.
03:05:29
So, again, a minor detail, but, again, the kinds of things
03:05:32
you should be thinking about, not only as
03:05:33
you write your code to be correct but also write
03:05:36
it to be well-designed as well.
03:05:38
All right, so why don't we go ahead and translate this
03:05:41
into the context of an actual program here?
03:05:44
I'll create a blank window here.
03:05:46
And let's do something with points, like points on my own very first CS50
03:05:50
problem set.
03:05:51
Let me go ahead and run code of points.c.
03:05:54
That's just going to give me a new text file.
03:05:56
And then up here, I'm going to do my usual, include cs50.h.
03:06:00
include stdio.h.
03:06:03
int main void.
03:06:04
So a lot of boilerplate, so to speak, in these early programs.
03:06:08
And now, let's see.
03:06:09
Let's ask the user, how many points did they
03:06:12
lose on their most recent CS50 PSet?
03:06:15
So sort of evoke my photograph of my own very first PSet last week
03:06:19
where I lost a couple of points myself.
03:06:20
So int points = get_int.
03:06:23
Then I'll ask a question in English like, how many points did you lose,
03:06:27
question mark, space?
03:06:29
And then once I have this answer, let's now ask some questions of it.
03:06:33
So if points is less than 2--
03:06:36
borrowing the syntax that we saw on the screen a moment ago--
03:06:40
let's go ahead and print out something explanatory
03:06:42
like you lost fewer points than me, backslash n.
03:06:49
else if points greater than 2--
03:06:52
which is, again how many I lost--
03:06:53
I'm going to go ahead and print out you lost more points than me, backslash n.
03:06:59
else if-- wait a minute, else seems to be sufficient logically here.
03:07:03
I'm just going to go ahead and print out something
03:07:05
like you lost the same number of points as me, backslash n.
03:07:12
So, really, just a straightforward application of that simple idea
03:07:16
but to a concrete scenario here.
03:07:18
So let me go ahead and save this.
03:07:21
Let me go ahead and run make points, Enter.
03:07:24
No errors, that's good.
03:07:26
Run points.
03:07:27
And then, how many points did you lose?
03:07:29
How about, it's 1 point?
03:07:31
All right, you lost fewer points than me.
03:07:32
How about 0 points?
03:07:34
Even better. How about 3 points?
03:07:36
And so forth.
03:07:37
So, again, we have the ability to express in C now pretty basic idea
03:07:40
from last week in reality, which is this notion of conditionals and asking
03:07:44
questions.
03:07:46
There's something subtle here, though, that's maybe not super well-designed
03:07:50
that someone might call a magic number.
03:07:53
This is programming speak for something I've done here.
03:07:56
There's a bit of redundancy unrelated to the if and the else and the else.
03:08:01
But is there something I typed twice just to ask, perhaps, for the obvious?
03:08:07
Exactly, I've hard-coded, so to speak, manually typed out the number 2--
03:08:11
in two locations, in this case--
03:08:13
that did not come from the user.
03:08:15
So, apparently, once I compile this, this is it.
03:08:18
You're always comparing yourself to me in like, 1996,
03:08:21
which for better or for worse, is all the program can do.
03:08:24
But this is an example too of a magic number in the sense
03:08:27
like, wait, where did that 2 come from, and why is it in two places?
03:08:31
It feels like we are setting the stage for just a higher probability
03:08:35
of screwing up down the road.
03:08:36
Because the longer this code gets, suppose I'm comparing against 2 points
03:08:39
elsewhere--
03:08:40
2, 3, 4, 5 places--
03:08:42
am I going to keep typing the number 2?
03:08:45
Like, yeah, that's fine.
03:08:46
It's correct. It's going to work.
03:08:47
But, honestly, eventually, you're going to screw up,
03:08:49
and you're going to miss one of the 2's, you're going to change it to a 3,
03:08:52
because maybe I did worse the next year, or 1, I did better.
03:08:54
And you don't want these numbers to get out of sync.
03:08:57
So what would be a logical improvement to this design,
03:09:01
rather than hard-coding the same number sort of magically
03:09:04
in two or more places?
03:09:07
Yeah, why don't I make a variable that I can use in there?
03:09:09
So, for instance, I could create a variable
03:09:11
like this, another integer called mine.
03:09:14
And I'm just going to initialize it to 2.
03:09:16
And then I'm going to change mentions of 2 to this.
03:09:19
And mine is a pretty reasonable name for a variable insofar
03:09:23
as it refers to exactly whose points are in question.
03:09:27
There's a risk here, though, minor though it is.
03:09:29
I could accidentally change mine at some point.
03:09:32
Maybe I forget what mine represents, and I do some addition or subtraction.
03:09:36
So there's a way to tell the computer "don't trust me,
03:09:39
because I'm going to screw up eventually"
03:09:40
by making a variable constant too.
03:09:43
So a constant in a programming language--
03:09:45
this did not exist in Scratch--
03:09:47
is just an additional hint to the computer that essentially enables
03:09:50
you to program more defensively.

Description:

Learn the basics of computer science from Harvard University. This is CS50, an introduction to the intellectual enterprises of computer science and the art of programming. 💻 Slides, source code, and more at https://cs50.harvard.edu/x. ❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp ⭐️ Course Contents ⭐️ ⌨️ (00:00:00) Lecture 0 - Scratch ⌨️ (01:45:08) Lecture 1 - C ⌨️ (04:13:23) Lecture 2 - Arrays ⌨️ (06:20:43) Lecture 3 - Algorithms ⌨️ (08:37:55) Lecture 4 - Memory ⌨️ (11:03:17) Lecture 5 - Data Structures ⌨️ (13:15:36) Lecture 6 - Python ⌨️ (15:39:25) Lecture 7 - SQL ⌨️ (18:00:55) Lecture 8 - HTML, CSS, JavaScript ⌨️ (20:23:38) Lecture 9 - Flask ⌨️ (22:39:01) Lecture 10 - Emoji ⌨️ (24:02:50) Cybersecurity Recorded in 2021. --- HOW TO JOIN CS50 COMMUNITIES Discord: https://discord.com/invite/cs50 Ed: https://edstem.org/us/join/gEcnkZ Facebook Group: https://www.facebook.com/unsupportedbrowser Faceboook Page: https://www.facebook.com/unsupportedbrowser GitHub: https://github.com/cs50 Gitter: https://app.gitter.im/#/room/#cs50_x:gitter.im Instagram: https://www.facebook.com/unsupportedbrowser LinkedIn Group: https://www.linkedin.com/uas/login?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fgroups%2F7437240%2F LinkedIn Page: https://www.linkedin.com/school/cs50/ Medium: https://cs50.medium.com/ Quora: https://www.quora.com/topic/CS50 Reddit: https://www.reddit.com/r/cs50/ Slack: https://cs50x.slack.com/join/shared_invite/zt-30lzxiehd-p8chC8fLQyf4u4feEXMpYg Snapchat: https://www.snapchat.com/add/cs50 SoundCloud: https://soundcloud.com/cs50 Stack Exchange: https://cs50.stackexchange.com/ TikTok: https://www.tiktok.com/@cs50 Twitter: https://twitter.com/cs50 YouTube: https://www.youtube.com/cs50 HOW TO FOLLOW DAVID J. MALAN Facebook: https://www.facebook.com/unsupportedbrowser GitHub: https://github.com/dmalan Instagram: https://www.facebook.com/unsupportedbrowser LinkedIn: https://www.linkedin.com/in/malan/ TikTok: https://www.tiktok.com/@davidjmalan Twitter: https://twitter.com/davidjmalan LICENSE CC BY-NC-SA 4.0 Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License https://creativecommons.org/licenses/by-nc-sa/4.0/ 🎉 Thanks to our Champion and Sponsor supporters: 👾 Raymond Odero 👾 Agustín Kussrow 👾 aldo ferretti 👾 Otis Morgan 👾 DeezMaster -- Learn to code for free and get a developer job: https://www.freecodecamp.org/ Read hundreds of articles on programming: https://www.freecodecamp.org/news

Mediafile available in formats

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

question iconHow can I download "Harvard CS50 – Full Computer Science University Course" video?arrow icon

    http://uniloader.pro/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

    The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

    UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

    UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

question iconWhich format of "Harvard CS50 – Full Computer Science University Course" video should I choose?arrow icon

    The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

question iconWhy does my computer freeze when loading a "Harvard CS50 – Full Computer Science University Course" video?arrow icon

    The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

question iconHow can I download "Harvard CS50 – Full Computer Science University Course" video to my phone?arrow icon

    You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

question iconHow can I download an audio track (music) to MP3 "Harvard CS50 – Full Computer Science University Course"?arrow icon

    The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

question iconHow can I save a frame from a video "Harvard CS50 – Full Computer Science University Course"?arrow icon

    This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

question iconHow do I play and download streaming video?arrow icon

    For this purpose you need VLC-player, which can be downloaded for free from the official website https://www.videolan.org/vlc/.

    How to play streaming video through VLC player:

    • in video formats, hover your mouse over "Streaming Video**";
    • right-click on "Copy link";
    • open VLC-player;
    • select Media - Open Network Stream - Network in the menu;
    • paste the copied link into the input field;
    • click "Play".

    To download streaming video via VLC player, you need to convert it:

    • copy the video address (URL);
    • select "Open Network Stream" in the "Media" item of VLC player and paste the link to the video into the input field;
    • click on the arrow on the "Play" button and select "Convert" in the list;
    • select "Video - H.264 + MP3 (MP4)" in the "Profile" line;
    • click the "Browse" button to select a folder to save the converted video and click the "Start" button;
    • conversion speed depends on the resolution and duration of the video.

    Warning: this download method no longer works with most YouTube videos.

question iconWhat's the price of all this stuff?arrow icon

    It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.

Follow our social media for updates