# Harvard CS50 – Full Computer Science University Course - YouTube
https://www.youtube.com/watch?v=8mAITcNt710

## Transcript:
- [00:00](https://www.youtube.com/watch?v=undefined&t=0s) If you want to learn about computer
science and the art of programming, this course is where to start. CS50 is considered by many to be one
of the best computer science courses in the world. This is a Harvard University course taught by Dr. David
Malan and we are proud to bring it to the freeCodeCamp YouTube channel.
- [00:17](https://www.youtube.com/watch?v=undefined&t=17s) Throughout a series of lectures, Dr.
Malan will teach you how to think algorithmically and solve problems efficiently. And make sure to check
the description for a lot of extra resources that go along with the course. [MUSIC PLAYING]
- [01:46](https://www.youtube.com/watch?v=undefined&t=106s) DAVID MALAN: All right, this is CS50,
Harvard University's introduction to the intellectual enterprises of computer science and the art of
programming, back here on campus in beautiful Sanders Theatre for the first time in quite a while. So
welcome to the class. My name is David-- OK. [CHEERING AND APPLAUSE] So my name is David Malan.
- [02:13](https://www.youtube.com/watch?v=undefined&t=133s) And I took this class myself some time
ago, but almost didn't. It was sophomore fall and I was sitting in on the class. And I was a little curious
but, eh, it didn't really feel like the field for me. I was definitely a computer person, but computer science
felt like something altogether. And I only got up the nerve to take the class, ultimately, because the
professor at the time, Brian Kernighan, allowed me to take the class pass/fail, initially.
- [02:36](https://www.youtube.com/watch?v=undefined&t=156s) And that is what made all the
difference. I quickly found that computer science is not just about programming and working in isolation
on your computer. It's really about problem solving more generally. And there was something about
homework, frankly, that was, like, actually fun for perhaps the first time in, what, 19 years.
- [02:51](https://www.youtube.com/watch?v=undefined&t=171s) And there was something about this
ability that I discovered, along with all of my classmates, to actually create something and bring a
computer to life to solve a problem, and sort of bring to bear something that I'd been using every day
but didn't really know how to harness, that's been gratifying ever since, and definitely challenging and
frustrating.
- [03:08](https://www.youtube.com/watch?v=undefined&t=188s) Like, to this day, all these years later,
you're going to run up against mistakes, otherwise known as bugs, in programming, that just drive you
nuts. And you feel like you've hit a wall. But the trick really is to give it enough time, to take a step back,
take a break when you need to. And there's nothing better, I daresay, than that sense of gratification and
pride, really, when you get something to work, and in a class like this, present, ultimately, at term's end,
something like your very own final project.
- [03:32](https://www.youtube.com/watch?v=undefined&t=212s) Now, this isn't to say that I took to it
100% perfectly. In fact, just this past week, I looked in my old CS50 binder, which I still have from some
25 years ago, and took a photo of what was apparently the very first program that I wrote and
submitted, and quickly received minus 2 points on. But this is a program that we'll soon see in the
coming days that does something quite simply like print "Hello, CS50," in this case, to the screen.
- [03:58](https://www.youtube.com/watch?v=undefined&t=238s) And to be fair, I technically hadn't
really followed the directions, which is why I lost those couple of points. But if you just look at this,
especially if you've never programmed before, you might have heard about programming language but
you've never typed something like this out, undoubtedly it's going to look cryptic.
- [04:11](https://www.youtube.com/watch?v=undefined&t=251s) But unlike human languages, frankly,
which were a lot more sophisticated, a lot more vocabulary, a lot more grammatical rules, programming,
once you start to wrap your mind around what it is and how it works and what these various languages
are, it's so easy, you'll see, after a few months of a class like this, to start teaching yourself, subsequently,
other languages, as they may come, in the coming years as well.
- [04:33](https://www.youtube.com/watch?v=undefined&t=273s) So what ultimately matters in this
particular course is not so much where you end up relative to your classmates but where you end up
relative to yourself when you began. And indeed, you'll begin today. And the only experience that
matters ultimately in this class is your own. And so, consider where you are today.
- [04:49](https://www.youtube.com/watch?v=undefined&t=289s) Consider, perhaps, just how cryptic
something like that looked a few seconds ago. And take comfort in knowing just some months from now
all of that will be within your own grasp. And if you're thinking that, OK, surely the person in front of me,
to the left, to the right, behind me, knows more than me, that's statistically not the case.
- [05:05](https://www.youtube.com/watch?v=undefined&t=305s) 2/3 of CS50 students have never taken
a CS course before, which is to say, you're in very good company throughout this whole term. So then,
what is computer science? I claim that it's problem solving. And the upside of that is that problem
solving is something we sort of do all the time. But a computer science class, learning to program, I think
kind of cleans up your thoughts.
- [05:28](https://www.youtube.com/watch?v=undefined&t=328s) It helps you learn how to think more
methodically, more carefully, more correctly, more precisely. Because, honestly, the computer is not
going to do what you want unless you are correct and precise and methodical. And so, as such, there's
these fringe benefits of just learning to think like a computer scientist and a programmer.
- [05:43](https://www.youtube.com/watch?v=undefined&t=343s) And it doesn't take all that much to
start doing so. This, for instance, is perhaps the simplest picture of computer science, sure, but really
problem solving in general. Problems are all about taking input, like the problem you want to solve. You
want to get the solution, a.k.a. output. And so, something interesting has got to be happening in here, in
here, when you're trying to get from those inputs to outputs.
- [06:03](https://www.youtube.com/watch?v=undefined&t=363s) Now, in the world of computers
specifically, we need to decide in advance how we represent these inputs and outputs. We all just need
to decide, whether it's Macs or PCs or phones or something else, that we're all going to speak some
common language, irrespective of our human languages as well. And you may very well know that
computers tend to speak only what language, so to speak? Assembly, one, but binary, two, might be your
go-to.
- [06:29](https://www.youtube.com/watch?v=undefined&t=389s) And binary, by implying two, means
that the world of computers has just two digits at its disposal, 0 and 1. And indeed, we humans have
many more than that, certainly not just zeros and ones alone. But a computer indeed only has zeros and
ones. And yet, somehow they can do so much. They can crunch numbers in Excel, send text messages,
create images and artwork and movies and more.
- [06:51](https://www.youtube.com/watch?v=undefined&t=411s) And so, how do you get from
something as simple as a few zeros, a few ones, to all of the stuff that we're doing today in our pockets
and laptops and desktops? Well, it turns out that we can start quite simply. If a computer were to want
to do something as simple as count, well, what could it do? Well, in our human world, we might count
doing this, like 1, 2, 3, 4, 5, using so-called unitary notation, literally the digits on your fingers where one
finger represents one person in the room, if I'm, for instance, taking attendance.
- [07:19](https://www.youtube.com/watch?v=undefined&t=439s) Now, we humans would typically
actually count 1, 2, 3, 4, 5, 6. And we'd go past just those five digits and count much higher, using zeros
through nines. But computers, somehow, only have these zeros and ones. So if a computer only
somehow speaks binary, zeros and ones, how does it even count past the number 1? Well, here are 3
zeros, of course.
- [07:39](https://www.youtube.com/watch?v=undefined&t=459s) And if you translate this number in
binary, 000, to a more familiar number in decimal, we would just call this zero. Enough said. If we were
to represent, with a computer, the number 1, it would actually be 001, which, not surprisingly, is exactly
the same as we might do in our human world, but we might not bother writing out the two zeros at the
beginning.
- [08:00](https://www.youtube.com/watch?v=undefined&t=480s) But a computer, now, if it wants to
count as high as two, it doesn't have the digit 2. And so it has to use a different pattern of zeros and
ones. And that happens to be 010. So this is not 10 with a zero in front of it. It's indeed zero one zero in
the context of binary. And if we want to count higher now than two, we're going to have to tweak these
zeros and ones further to get 3.
- [08:20](https://www.youtube.com/watch?v=undefined&t=500s) And then if we want 4 or 5 or 6 or 7,
we're just kind of toggling these zeros and ones, a.k.a. bits, for binary digits that represent, via these
different patterns, different numbers that you and I, as humans, know, of course, as the so-called
decimal system, 0 through 9, dec implying 10, 10 digits, those zeros through nine.
- [08:40](https://www.youtube.com/watch?v=undefined&t=520s) So why that particular pattern? And
why these particular zeros and ones? Well, it turns out that representing one thing or the other is just
really simple for a computer. Why? At the end of the day, they're powered by electricity. And it's a really
simple thing to just either store some electricity or don't store some electricity.
- [08:58](https://www.youtube.com/watch?v=undefined&t=538s) Like, that's as simple as the world can
get, on or off. 1 or 0, so to speak. So, in fact, inside of a computer, a phone, anything these days that's
electronic, pretty much, is some number of switches, otherwise known as transistors. And they're tiny.
You've got thousands, millions of them in your Mac or PC or phone these days.
- [09:15](https://www.youtube.com/watch?v=undefined&t=555s) And these are just tiny little switches
that can get turned on and off. And by turning those things on and off in patterns, a computer can count
from 0 on up to 7, and even higher than that. And so these switches, really, you can think of being as like
switches like this. Let me just borrow one of our little stage lights here.
- [09:30](https://www.youtube.com/watch?v=undefined&t=570s) Here's a light bulb. It's currently off.
And so, I could just think of this as representing, in my laptop, a transistor, a switch, representing 0. But if
I allow some electricity to flow, now I, in fact, have a 1. Well, how do I count higher than 1? I, of course,
need another light bulb. So let me grab another one here.
- [09:49](https://www.youtube.com/watch?v=undefined&t=589s) And if I put it in that same kind of
pattern, I don't want to just do this. That's sort of the old finger counting way of unary, just 1, 2. I want to
actually take into account the pattern of these things being on and off. So if this was one a moment ago,
what I think I did earlier was I turned it off and let the next one over be on, a.k.a.
- [10:11](https://www.youtube.com/watch?v=undefined&t=611s) 010. And let me get us a third bit, if
you will. And that feels like enough. Here is that same pattern now, starting at the beginning with 3. So
here is 000. Here is 001. Here is 010, a.k.a., in our human world of decimal, 2. And then we could, of
course, keep counting further. This now would be 3 and dot dot dot.
- [10:38](https://www.youtube.com/watch?v=undefined&t=638s) If this other bulb now goes on, and
that switch is turned and all three stay on-- this, again, was what number? AUDIENCE: Seven. DAVID
MALAN: OK, so, seven. So it's just as simple, relatively, as that, if you will. But how is it that these
patterns came to be? Well, these patterns actually follow something very familiar.
- [10:57](https://www.youtube.com/watch?v=undefined&t=657s) You and I don't really think about it at
this level anymore because we've probably been doing math and numbers since grade school or
whatnot. But if we consider something in decimal, like the number 123, I immediately jump to that. This
looks like 123 in decimal. But why? It's really just three symbols, a 1, a 2 with a bit of curve, a 3 with a
couple of curves, that you and I now instinctively just assign meaning to.
- [11:22](https://www.youtube.com/watch?v=undefined&t=682s) But if we do rewind a few years, that
is one hundred twenty-three because you're assigning meaning to each of these columns. The 3 is in the
so-called ones place. The 2 is in the so-called tens place. And the 1 is in the so-called hundreds place.
And then the math ensues quickly in your head. This is technically 100 times 1, plus 10 times 2, plus 1
times 3, a.k.a.
- [11:47](https://www.youtube.com/watch?v=undefined&t=707s) 100 plus 20 plus 3. And there we get
the sort of mathematical notion we know as 123. Well, nicely enough, in binary, it's actually the same
thing. It's just these columns mean a little something different. If you use three digits in decimal, and you
have the ones place, the tens place, and the hundreds place, well, why was that 1, 10, and 100? They're
technically just powers of 10.
- [12:11](https://www.youtube.com/watch?v=undefined&t=731s) So 10 to the 0, 10 to the 1, 10 to the 2.
Why 10? Decimal system, "dec" meaning 10. You have 8 and 10 digits, 0 through 9. In the binary system,
if you're going to use three digits, just change the bases if you're using only zeros and ones. So now it's
powers of 2, 2 to the 0, 2 to the 1, 2 to the 2, a.k.a.
- [12:29](https://www.youtube.com/watch?v=undefined&t=749s) 1 and 2 and 4, respectively. And if you
keep going, it's going to be 8s column, 16s column, 32, 64, and so forth. So, why did we get these
patterns that we did? Here's your 000 because it's 4 times 0, 2 times 0, 1 times 0, obviously 0. This is why
we got the decimal number 1 in binary. This is why we got the number 2 in binary, because it's 4 times 0,
plus 2 times 1, plus 1 times 0, and now 3, and now 4, and now 5, and now 6, and now 7.
- [13:02](https://www.youtube.com/watch?v=undefined&t=782s) And, of course, if you wanted to count
as high as 8, to be clear, what do you have to do? What does a computer need to do to count even
higher than 7? AUDIENCE: Add a bit. DAVID MALAN: Add a bit. Add another light bulb, another switch.
And, indeed, computers have standardized just how many zeros and ones, or bits or switches, they throw
at these kinds of problems.
- [13:19](https://www.youtube.com/watch?v=undefined&t=799s) And, in fact, most computers would
typically use at least eight at a time. And even if you're only counting as high as three or seven, you
would still use eight and have a whole bunch of zeros. But that's OK, because the computers these days
certainly have so many more, thousands, millions of transistors and switches that that's quite OK.
- [13:38](https://www.youtube.com/watch?v=undefined&t=818s) All right, so, with that said, if we can
now count as high as seven or, frankly, as high as we want, that only seems to make computers useful for
things like Excel, like number crunching. But computers, of course, let you send text messages, write
documents, and so much more. So how would a computer represent something like a letter, like the
letter A of the English alphabet, if, at the end of the day, all they have is switches? Any thoughts? Yeah.
- [14:04](https://www.youtube.com/watch?v=undefined&t=844s) AUDIENCE: You can represent letters
in numbers. DAVID MALAN: OK, so we could represent letters using numbers. OK, so what's a proposal?
What number should represent what? AUDIENCE: Say if you were starting at the beginning of the
alphabet, you could say 1 is A, 2 is B, 3 is C. DAVID MALAN: Perfect. Yeah, we just all have to agree
somehow that one number is going to represent one letter.
- [14:24](https://www.youtube.com/watch?v=undefined&t=864s) So 1 is A, 2 is B, 3 is C, Z is 26, and so
forth. Maybe we can even take into account uppercase and lowercase. We just have to agree and sort of
write it down in some global standard. And humans, indeed, did just that. They didn't use 1, 2, 3. It turns
out they started a little higher up. Capital A has been standardized as the number 65.
- [14:44](https://www.youtube.com/watch?v=undefined&t=884s) And capital B has been standardized
as the number 66. And you can kind of imagine how it goes up from there. And that's because whatever
you're representing, ultimately, can only be stored, at the end of the day, as zeros and ones. And so,
some humans in a room before, decided that capital A shall be 65, or, really, this pattern of zeros and
ones inside of every computer in the world, 01000001.
- [15:09](https://www.youtube.com/watch?v=undefined&t=909s) So if that pattern of zeros and ones
ever appears in a computer, it might be interpreted then as indeed a capital letter A, eight of those bits
at a time. But I worry, just to be clear, we might have now created a problem. It might seem, if I play this
naively, that, OK, how do I now actually do math with the number 65? If now Excel displays 65 is an A, let
alone Bs and Cs.
- [15:34](https://www.youtube.com/watch?v=undefined&t=934s) So how might a computer do as
you've proposed, have this mapping from numbers to letters, but still support numbers? It feels like
we've given something up. Yeah? AUDIENCE: By having a prefix for letters? DAVID MALAN: By having a
prefix? AUDIENCE: You could have prefixes and suffixes. DAVID MALAN: OK, so we could perhaps have
some kind of prefix, like some pattern of zeros and ones-- I like this-- that indicates to the computer here
comes another pattern that represents a letter.
- [15:59](https://www.youtube.com/watch?v=undefined&t=959s) Here comes another pattern that
represents a number or a letter. So, not bad. I like that. Other thoughts? How might a computer
distinguish these two? Yeah. AUDIENCE: Have a different file format, so, like, odd text or just check the
graphic or-- DAVID MALAN: Indeed, and that's spot-on. Nothing wrong with what you suggested, but the
world generally does just that.
- [16:21](https://www.youtube.com/watch?v=undefined&t=981s) The reason we have all of these
different file formats in the world, like JPEG and GIF and PNGs and Word documents, .docx, and Excel
files and so forth, is because a bunch of humans got in a room and decided, well, in the context of this
type of file, or really, more specifically, in the context of this type of program, Excel versus Photoshop
versus Google Docs or the like, we shall interpret any patterns of zeros and ones as being maybe
numbers for Excel, maybe letters in, like, a text messaging program or Google Docs,
- [16:51](https://www.youtube.com/watch?v=undefined&t=1011s) or maybe even colors of the rainbow
in something like Photoshop and more. So it's context dependent. And we'll see, when we ourselves
start programming, you the programmer will ultimately provide some hints to the computer that tells
the computer, interpret it as follows. So, similar in spirit to that, but not quite a standardized with these
prefixes.
- [17:09](https://www.youtube.com/watch?v=undefined&t=1029s) So this system here actually has a
name ASCII, the American Standard Code for Information Interchange. And indeed, it began here in the
US, and that's why it's actually a little biased toward A's through Z's and a bit of punctuation as well. And
that quickly became a problem. But if we start simply now, in English, the mapping itself is fairly
straightforward.
- [17:29](https://www.youtube.com/watch?v=undefined&t=1049s) So if A is 65, B it 66, and dot dot dot,
suppose that you received a text message, an email, from a friend, and underneath the hood, so to
speak, if you kind of looked inside the computer, what you technically received in this text or this email
happened to be the numbers 72, 73, 33, or, really, the underlying pattern of zeros and ones.
- [17:51](https://www.youtube.com/watch?v=undefined&t=1071s) What might your friend have sent
you as a message, if it's 72, 73, 33? AUDIENCE: Hey. DAVID MALAN: Hey? Close. AUDIENCE: Hi. DAVID
MALAN: Hi. It's, indeed, hi. Why? Well, apparently, according to this little cheat sheet, H is 72, I is 73. It's
not obvious from this chart what the 33 is, but indeed, this pattern represents "hi.
- [18:12](https://www.youtube.com/watch?v=undefined&t=1092s) " And anyone want to guess, or if you
know, what 33 is? AUDIENCE: Exclamation point. DAVID MALAN: Exclamation point. And this is, frankly,
not the kind of thing most people know. But it's easily accessible by a nice user-friendly chart like this. So
this is an ASCII chart. When I said that we just need to write down this mapping earlier, this is what
people did.
- [18:28](https://www.youtube.com/watch?v=undefined&t=1108s) They wrote it down in a book or in a
chart. And, for instance, here is our 72 for H, here is our 73 for I, and here is our 33 for exclamation
point. And computers, Macs, PCs, iPhones, Android devices, just know this mapping by heart, if you will.
They've been designed to understand those letters. So here, I might have received "hi.
- [18:48](https://www.youtube.com/watch?v=undefined&t=1128s) " Technically, what I've received is
these patterns of zeros and ones. But it's important to note that when you get these patterns of zeros
and ones in any format, be it email or text or a file, they do tend to come in standard lengths, with a
certain number of zeros and ones altogether. And this happens to be 8 plus 8, plus 8.
- [19:07](https://www.youtube.com/watch?v=undefined&t=1147s) So just to get the message "hi,
exclamation point," you would have received at least, it would seem, some 24 bits. But frankly, bits are
so tiny, literally and mathematically, that we don't tend to think or talk, generally, in terms of bits. You're
probably more familiar with bytes. B-Y-T-E-S is a byte, is a byte, is a byte.
- [19:27](https://www.youtube.com/watch?v=undefined&t=1167s) A byte is just 8 bits. And even those,
frankly, aren't that useful if we do out the math. How high can you count if you have eight bits? Anyone
know? Say it again? Higher than that. Unless you want to go negative, that's fine. 256, technically 255.
Long story short, if we actually got into the weeds of all of these zeros and ones, and we figured out
what 11111111 mathematically adds up to in decimal, it would indeed be 255, or less if you want to
represent negative numbers as well.
- [20:00](https://www.youtube.com/watch?v=undefined&t=1200s) So this is useful because now we can
speak, not just in terms of bytes but, if the files are bigger, kilobytes is thousands of bytes, megabytes is
millions of bytes, gigabytes is billions of bytes, terabytes are trillions of bytes, and so forth. We have a
vocabulary for these increasingly large quantities of data.
- [20:21](https://www.youtube.com/watch?v=undefined&t=1221s) The problem is that, if you're using
ASCII and, therefore, eight bits or one byte per character, and originally, only seven, you can only
represent 255 characters. And that's actually 256 total characters, including zero. And that's fine if you're
using literally English, in this case, plus a bunch of punctuation.
- [20:39](https://www.youtube.com/watch?v=undefined&t=1239s) But there's many human languages
in the world that need many more symbols and, therefore, many more bits. So, thankfully, the world
decided that we'll indeed support not just the US English keyboard, but all of the accented characters
that you might want for some languages. And heck, if we use enough bits, zeros and ones, not only can
we represent all human languages in written form, as well as some emotions along the way, we can
capture the latter with these things called emojis.
- [21:07](https://www.youtube.com/watch?v=undefined&t=1267s) And indeed, these are very much in
vogue these days. You probably send and/or receive many of these things any given day. These are just
characters, like letters of an alphabet, patterns of zeros and ones that you're receiving, that the world
has also standardized. For instance, there are certain emojis that are represented with certain patterns
of bits.
- [21:25](https://www.youtube.com/watch?v=undefined&t=1285s) And when you receive them, your
phone, your laptop, your desktop, displays them as such. And this newer standard is called Unicode. So
it's a superset of what we called ASCII. And Unicode is just a mapping of many more numbers to many
more letters or characters, more generally, that might use eight bits for backwards compatibility with the
old way of doing things with ASCII, but they might also use 16 bits.
- [21:50](https://www.youtube.com/watch?v=undefined&t=1310s) And if you have 16 bits, you can
actually represent more than 65,000 possible letters. And that's getting up there. And heck, Unicode
might even use 32 bits to represent letters and numbers and punctuation symbols and emojis. And that
would give you up to 4 billion possibilities. And, I daresay, one of the reasons we see so many emojis
these days is we have so much room.
- [22:11](https://www.youtube.com/watch?v=undefined&t=1331s) I mean, we've got room for billions
more, literally. So, in fact, just as a little bit of trivia, has anyone ever received this decimal number, or if
you prefer binary now, has anyone ever received this pattern of zeros and ones on your phone, in a text
or an email, perhaps this past year? Well, if you actually look this up, this esoteric sequence of zeros and
ones happens to represent face with medical mask.
- [22:37](https://www.youtube.com/watch?v=undefined&t=1357s) And notice that if you've got an
iPhone or an Android device, you might be seeing different things. In fact, this is the Android version of
this, most recently. This is the iOS version of it, most recently. And there's bunches of other
interpretations by other companies as well. So Unicode, as a consortium, if you will, has standardized the
descriptions of what these things are.
- [22:59](https://www.youtube.com/watch?v=undefined&t=1379s) But the companies themselves,
manufacturers out there, have generally interpreted it as you see fit. And this can lead to some human
miscommunications. In fact, for like, literally, embarrassingly, like a year or two, I started being in the
habit of using the emoji that kind of looks like this because I thought it was like woo, happy face, or
whatever.
- [23:18](https://www.youtube.com/watch?v=undefined&t=1398s) I didn't realize this is the emoji for
hug because whatever device I was using sort of looks like this, not like this. And that's because of their
interpretation of the data. This has happened too when what was a gun became a water pistol in some
manufacturers' eyes. And so it's an interesting dichotomy between what information we all want to
represent and how we choose, ultimately, to represent it.
- [23:43](https://www.youtube.com/watch?v=undefined&t=1423s) Questions, then, on these
representations of formats, be it numbers or letters, or soon more. Yeah? AUDIENCE: Why is decimal
popular for a computer if binary is the basis for everything? DAVID MALAN: Sorry, why is what so
popular? AUDIENCE: Why is the decimal popular if binary is the fundamental-- DAVID MALAN: Yeah, so
we'll come back to this in a few weeks, in fact.
- [24:02](https://www.youtube.com/watch?v=undefined&t=1442s) There are other ways to represent
numbers. Binary is one. Decimal is another. Unary is another. And hexadecimal is yet a fourth that uses
16 total digits, literally 0 through 9 plus A, B, C, D, E, F. And somehow, you can similarly count even higher
with those. We'll see in a few weeks why this is compelling.
- [24:21](https://www.youtube.com/watch?v=undefined&t=1461s) But hexadecimal, long story short,
uses four bits per digit. And so, four bits, if you have two digits in hex, that gives you eight. And it's just a
very convenient unit of measure. And it's also human convention in the world of files and other things.
But we'll come back to that soon. Other questions? AUDIENCE: Do the lights on the stage supposedly say
that-- DAVID MALAN: Do the lights on the stage supposedly say anything? Well, if we had thought in
advance to use maybe 64 light bulbs, that would seem to give us 8 total bytes on stage, 8 times 8,
- [24:52](https://www.youtube.com/watch?v=undefined&t=1492s) giving us just that. Maybe. Good
question. Other questions on 0's and 1's? It's a little bright in here. No? Oh, yes? Where everyone's
pointing somewhere specific. There we go. Sorry. Very bright in this corner. AUDIENCE: I was just going to
ask about the 255 bits, like with the maximum characters.
- [25:16](https://www.youtube.com/watch?v=undefined&t=1516s) [INAUDIBLE] DAVID MALAN: Ah,
sure, and we'll come back to this, in some form, in the coming days too, at a slower pace too, we have,
with eight bits, two possible values for the first and then two for the next, two for the next, and so forth.
So that's 2 times 2 times 2. That's 2 to the eighth power total, which means you can have 256 total
possible patterns of zeros and ones.
- [25:37](https://www.youtube.com/watch?v=undefined&t=1537s) But as we'll see soon computer
scientists, programmers, software often starts counting at 0 by convention and if you use one of those
patterns, 00000000 to represent the decimal number we know is zero, you only have 255 other patterns
left to count as high as therefore 255. That's all. Good question.
- [25:59](https://www.youtube.com/watch?v=undefined&t=1559s) All right, so what then might we have
besides these emojis and letters and numbers? Well, we of course have things like colors and programs
like Photoshop and pictures and photos. Well let me ask the question again. How might a computer, do
you think, knowing what you know now, represents something like a color? Like what are our options if
all we've got are zeros and ones and switches? Yeah? AUDIENCE: RGB DAVID MALAN: RGB.
- [26:23](https://www.youtube.com/watch?v=undefined&t=1583s) RGB indeed is this acronym that
represents some amount of red and some amount of green and blue and indeed computers can
represent colors by just doing that. Remembering, for instance, this dot. This yellow dot on the screen
that might be part of any of those emojis these days, well that's some amount of red, some amount of
green, some amount of blue.
- [26:40](https://www.youtube.com/watch?v=undefined&t=1600s) And if you sort of mix those colors
together, you can indeed get a very specific one. And we'll see you in just a moment just that. So indeed
earlier on, humans only used seven bits total. And it was only once they decided, well, let's add an
eighth bit that they got extended ASCII and that was initially in part a solution to the same problem of
not having enough room, if you will, in those patterns of zeros and ones to represent all of the characters
that you might want.
- [27:06](https://www.youtube.com/watch?v=undefined&t=1626s) But even that wasn't enough and
that's why we've now gone up to 16 and 32 and long past 7. So if we come back now to this one
particular color. RGB was proposed as a scheme, but how might this work? Well, consider for instance
this. If we do indeed decide as a group to represent any color of the rainbow with some mixture of some
red, some green, and some blue, we have to decide how to represent the amount of red and green and
blue.
- [27:33](https://www.youtube.com/watch?v=undefined&t=1653s) Well, it turns out if all we have are
zeros and ones, ergo numbers, let's do just that. For instance, suppose a computer we're using, these
three numbers 72, 73, 33, no longer in the context of an email or a text message, but now in the context
of something like Photoshop, a program for editing and creating graphical files, maybe this first number
could be interpreted as representing some amount of red, green, and blue, respectively.
- [28:01](https://www.youtube.com/watch?v=undefined&t=1681s) And that's exactly what happens. You
can think of the first digit as red, second as green, third as blue. And so ultimately when you combine
that amount of red, that amount of green, that amount of blue, it turns out it's going to resemble the
shade of yellow. And indeed, you can come up with a numbers between 0 and 255 for each of those
colors to mix any other color that you might want.
- [28:21](https://www.youtube.com/watch?v=undefined&t=1701s) And you can actually see this in
practice. Even though our screens, admittedly, are getting really good on our phones and laptops such
that you barely see the dots, they are there. You might have heard the term pixel before. Pixel's just a
dot on the screen and you've got thousands, millions of them these days horizontally and vertically.
- [28:38](https://www.youtube.com/watch?v=undefined&t=1718s) If I take even this emoji, which again
happens to be one company's interpretation of a face with medical mask and zoom in a bit, maybe zoom
in a bit more, you can actually start to see these pixels. Things get pixelated because what you're seeing
is each of the individual dots that compose this particular image.
- [28:57](https://www.youtube.com/watch?v=undefined&t=1737s) And apparently each of these
individual dots are probably using 24 bits, eight bits for red, eight bits for green, eight bits for blue, in
some pattern. This program or some other like Photoshop is interpreting one pattern and it's white or
yellow or black or some brown in between. So if you look sort of awkwardly, but up close to your phone
or your laptop or maybe your TV, you can see exactly this, too.
- [29:24](https://www.youtube.com/watch?v=undefined&t=1764s) All right, well, what about things that
we also watch every day on YouTube or the like? Things like videos. How would a computer, knowing
what we know now, represent something like a video? How might you represent a video using only zeros
and ones? Yeah? AUDIENCE: As we can see here, they represent images, right? [INAUDIBLE] sounds of
the 0 and 1s as well.
- [29:48](https://www.youtube.com/watch?v=undefined&t=1788s) [INAUDIBLE] DAVID MALAN: Yeah,
exactly. To summarize, what video really adds is just some notion of time. It's not just one image, it's not
just one letter or a number, it's presumably some kind of sequence because time is passing. So with a
whole bunch of images, maybe 24 maybe 30 per second, if you fly them by the human's eyes, we can
interpret them using our eyes and brain that there is now movement and therefore video.
- [30:13](https://www.youtube.com/watch?v=undefined&t=1813s) Similarly with audio or music. If we
just came up with some convention for representing those same notes on a musical instrument, could
we have the computer synthesize them, too? And this might be actually pretty familiar. Let me pull up a
quick video here, which happens to be an old school version of the same idea.
- [30:31](https://www.youtube.com/watch?v=undefined&t=1831s) You might remember from
childhood. [MUSIC PLAYING] [CLICKING] So granted that particular video is an actual video of a paper-
based animation, but indeed, that's really all you need, is some sequence of these images, which
themselves of course are just zeros and ones because they're just this grid of these pixels or dots.
- [31:10](https://www.youtube.com/watch?v=undefined&t=1870s) Now something like musical notes
like these, those of you who are musicians might just naturally play these on physical devices, but
computers can certainly represent those sounds, too. For instance, a popular format for audio is called
MIDI and MIDI might just represent each note that you saw a moment ago essentially as a sequence of
numbers.
- [31:29](https://www.youtube.com/watch?v=undefined&t=1889s) But more generally, you might think
about music as having notes, for instance, A through G, maybe some flats and some sharps, you might
have the duration like how long is the note being heard or played on a piano or some other device, and
then just the volume like how hard does a human in the real world press down on that key and therefore
how loud is that sound? It would seem that just remembering little details like that quantitatively we can
then represent really all of these otherwise analog human realities.
- [31:57](https://www.youtube.com/watch?v=undefined&t=1917s) So that then is really a laundry list of
ways that we can just represent information. Again, computers or digital have all of these different
formats, but at the end of the day and as fancy as those devices in years are, it's just zeros and ones, tiny
little switches or light bulbs, if you will, represented in some way and it's up to the software that you and
I and others write to use those zeros and ones in ways we want to get the computers to do something
more powerfully.
- [32:23](https://www.youtube.com/watch?v=undefined&t=1943s) Questions, then, on this
representation of information, which I daresay is ultimately what problem solving is all about, taking in
information and producing new via some process in between. Any questions there? Yeah, in back.
AUDIENCE: Yeah, so we talked about how different file formats kind of allow you to interpret
information.
- [32:46](https://www.youtube.com/watch?v=undefined&t=1966s) How does a file format like .mp4
discriminate between audio and video within itself as a value? DAVID MALAN: So a really good question.
There are many other file formats out there. You allude to MP4 for video and more generally the use are
these things called codecs and containers. It's not quite as simple when using larger files, for instance, in
more modern formats that a video is just a sequence of images, for instance.
- [33:09](https://www.youtube.com/watch?v=undefined&t=1989s) Why? If you stored that many images
for like a Hollywood movie, like 24 or 30 of them per second, that's a huge number of images. And if
you've ever taken photos on your phone, you might know how many megabytes or larger even individual
photographs might be. So humans have developed over the years a fancier software that uses much
more math to represent the same information more minimally just using somehow shorter patterns of
zeros and ones than are most simplistic representation here.
- [33:38](https://www.youtube.com/watch?v=undefined&t=2018s) And they use what might be called
compression. If you've ever used a zip file or something else, somehow your computer is using fewer
zeros and ones to represent the same amount of information, ideally without losing any information. In
the world of multimedia, which we'll touch on a little bit in a few weeks, there are both lossy and lossless
formats out there.
- [33:56](https://www.youtube.com/watch?v=undefined&t=2036s) Lossless means you lose no
information whatsoever. But more commonly as you're alluding to one is lossy compression, L-O-S-S-Y,
where you're actually throwing away some amount of quality. You're getting some amount of pixelation
that might not look perfect to the human, but heck it's a lot cheaper and a lot easier to distribute.
- [34:15](https://www.youtube.com/watch?v=undefined&t=2055s) And in the world of multimedia, you
have containers like QuickTime and other MPEG containers that can combine different formats of video,
different formats of audio in one file, but there, too, do designers have discretion. So more in a few
weeks, too. Other questions, then, on information here as well? Yeah? AUDIENCE: So I know computers
used to be very big and taking up like a whole room and stuff.
- [34:38](https://www.youtube.com/watch?v=undefined&t=2078s) Is the reason they've gotten smaller
because we can store this information piecemeal or what? DAVID MALAN: Exactly. I mean, back in the
day you might have heard of the expression a vacuum tube, which is like some physically large device
that might have only stored some 0 or 1. Yes, it is the miniaturization of hardware these days that has
allowed us to store as many and many more zeros and ones much more closely together.
- [35:02](https://www.youtube.com/watch?v=undefined&t=2102s) And as we've built more fancy
machines that can sort of design this hardware at an even smaller scale, we're just packing more and
more into these devices. But there, too, is a trade off. For instance, you might know by using your phone
or your laptop for quite a while, maybe on your lap, starts to get warm.
- [35:16](https://www.youtube.com/watch?v=undefined&t=2116s) So there are these literal physical
side effects of this where now some of our devices run hot. This is why like a data center in the real
world might need more air conditioning than a typical place, because there are these physical artifacts as
well. In fact, if you'd like to see one of the earliest computers from decades ago, across the river here in
now Allston in the new engineering building is the Harvard Mark 1 computer that will give you a much
better mental model of just that.
- [35:42](https://www.youtube.com/watch?v=undefined&t=2142s) Well if we come back now to this first
picture being computer science or really problem solving, I daresay we have more than enough ways
now to represent information, input and output, so long as we all just agree on something and thankfully
all of those before us have given us things like ASCII and Unicode.
- [35:58](https://www.youtube.com/watch?v=undefined&t=2158s) Not to mention MP4s, word
documents, and the like. But what's inside of this proverbial black box into which these inputs are going
in the outputs are coming? Well that's where we get this term you might have heard, too. An algorithm,
which is just step-by-step instructions for solving some problem incarnated in the world of computers by
software.
- [36:18](https://www.youtube.com/watch?v=undefined&t=2178s) When you write software aka
programs, you are implementing one or more algorithms, one or more sets of instructions for solving
some problem, and maybe you're using this language or that, but at the end of the day, no matter the
language you use the computer is going to represent what you type using just zeros and ones.
- [36:38](https://www.youtube.com/watch?v=undefined&t=2198s) So what might be a representative
algorithm? Nowadays you might use your phone quite a bit to make calls or send texts or emails and
therefore you have a whole bunch of contacts in your address book. Nowadays, of course, this is very
digital, but whether on iOS or Android or the like, you might have a whole bunch of names, first name
and/or last, as well as numbers and emails and the like.
- [36:59](https://www.youtube.com/watch?v=undefined&t=2219s) You might be in the habit of like
scrolling through on your phone all of those names to find the person you want to call. It's probably
sorted alphabetically by first name or last name, A through Z, or some other symbol. This is frankly quite
the same as we used to do back in my day, CS50, when we just used a physical book.
- [37:18](https://www.youtube.com/watch?v=undefined&t=2238s) In this physical book might be a
whole bunch of names alphabetically sorted from left to right corresponding to a whole bunch of
numbers. So suppose that in this old Harvard phone book we want to search for John Harvard. We might
of course start quite simply at the beginning here, looking at one page at a time, and this is an algorithm.
- [37:36](https://www.youtube.com/watch?v=undefined&t=2256s) This is like literally step-by-step
looking for the solution to this problem. In that sense, if John Harvard's in the phone book, is this
algorithm page-by-page correct, would you say? AUDIENCE: Yes. DAVID MALAN: Yes. Like if John
Harvard's in the phone book, obviously I'm eventually going to get to him, so that's what we mean by
correct.
- [37:55](https://www.youtube.com/watch?v=undefined&t=2275s) Is it efficient? Is it well designed,
would you say? No. I mean this is going to take forever even just to get to the Js or the Hs, depending
how this thing's sorted. All right, well let me go a little faster. I'll start like two pages at a time. 2, 4, 6, 8,
10, 12, and so forth. Sounds faster, is faster, is it correct? AUDIENCE: No.
- [38:14](https://www.youtube.com/watch?v=undefined&t=2294s) DAVID MALAN: OK, why is it not
correct? Yeah? AUDIENCE: So if you're starting on page 1, you're only going odd number of pages, so if
it's on an even number page, you'll miss it. DAVID MALAN: Exactly. If I start on an odd number of pages
and I'm going two at a time I might miss pages in between.
- [38:28](https://www.youtube.com/watch?v=undefined&t=2308s) And if I therefore conclude when I
get to the back of the book there was no John Harvard, I might have just errored. This would be again
one of these bugs. But if I try a little harder, I feel like there's a solution. We don't have to completely
throw out this algorithm. I think we can probably go roughly twice as fast still.
- [38:44](https://www.youtube.com/watch?v=undefined&t=2324s) But what should we do instead to fix
this? Yeah, in back. AUDIENCE: [INAUDIBLE] DAVID MALAN: Nice. So I think what many of us, most of us,
if we even use this technology any more these days, we might go roughly to the middle of the phone
book just to kind of get us started. And now I'm looking down, I'm looking for J, assuming first name, J
Harvard, and it looks like I'm in the M section.
- [39:13](https://www.youtube.com/watch?v=undefined&t=2353s) So just to be clear, what should I do
next? AUDIENCE: [INAUDIBLE] DAVID MALAN: OK, and presumably it is John Harvard would be to the left
of this. So here's an opportunity to figuratively and literally tear this particular problem in half, throw half
of the problem away. It's actually pretty easy if you just do it that way.
- [39:35](https://www.youtube.com/watch?v=undefined&t=2375s) The hard way is this way. But I've
now just decreased the size of this problem really in half. So if I started with 1,000 pages of phone
numbers and names, now I'm down to 500. And already we haven't found John Harvard, but that's a big
bite out of this problem. I do think it's correct because if J is to the left of M, of course, he's definitely not
going to be over there.
- [39:56](https://www.youtube.com/watch?v=undefined&t=2396s) I think if I repeat this again dividing
and conquering, if you will, here I might have gone a little too far. Now I'm in like the E section. So let me
tear the problem in half again, throw another 250 pages away, and again repeat, dividing and dividing
and conquering until finally, presumably, I end up with just one page of a phone book on which John
Harvard's name either is or is not, but because of the algorithm you proposed, step by step, I know that
he's not in anything I discarded.
- [40:25](https://www.youtube.com/watch?v=undefined&t=2425s) So traumatic is that might have been
made out to be, it's actually just harnessing pretty good human intuition. Indeed, this is what
programming is all about, too. It's not about learning a completely new world, but really just how to
harness intuition and ideas that you might already have and take naturally but learning how to express
them now more succinctly, more precisely, using things called programming languages.
- [40:49](https://www.youtube.com/watch?v=undefined&t=2449s) Why is an algorithm like that if I
found John Harvard better than, ultimately, just doing the first one or even the second and maybe
doubling back to check those even pages? Well let's just look at little charts here. Again, we don't have to
get into the nuances of numbers, but if we've got like a chart here, xy plot, on the x-axis here I claim as
the size of the problem.
- [41:09](https://www.youtube.com/watch?v=undefined&t=2469s) So measured in the numbers of
pages in the phone book. So the farther you go out here, the more pages are in the phone book. And
here we have time to solve on the y-axis. So the higher you go up, the more time it's going to be taking
to solve that particular problem. So let's just arbitrarily say that the first algorithm, involving like n pages,
might be represented graphically like this.
- [41:32](https://www.youtube.com/watch?v=undefined&t=2492s) No matter the slope, it's a straight
line because there's presumably a one to one relationship between numbers of pages and number of
seconds or number of page turns. Why? If the phone company adds another page next year because
some new people move to town, that's going to require one additional page for me.
- [41:47](https://www.youtube.com/watch?v=undefined&t=2507s) One to one. If, though, we use the
second algorithm, flawed though it was, unless we double back a little bit to fix someone being in
between, that's too going to be a straight line, but it's going to be a different slope because now there's
a 2 to 1 or a 1 to 2 relationship because I'm going to pages at a time.
- [42:06](https://www.youtube.com/watch?v=undefined&t=2526s) So if the phone company adds
another page or another two pages, that still only just one more step. You can see the difference if I kind
of draw this. If this is the phone book in question, this number of pages, it might take this many seconds
on the yellow line to represent or to find someone like John Harvard.
- [42:25](https://www.youtube.com/watch?v=undefined&t=2545s) But of course on the first algorithm,
the red line, it's literally going to take twice as many steps. And what do the n here mean? n is the go-to
variable for computer scientist or programmer just generically representing a number. So if the number
of pages in the phone book is n, the number of steps the second algorithm would have taken would be in
the worst case n over 2.
- [42:44](https://www.youtube.com/watch?v=undefined&t=2564s) Half as many because you're going
twice as fast. But the third algorithm, actually if you recall your logarithms, looks a little something like
this. There's a fundamentally different relationship between the size of the problem and the amount of
time required to solve it that technically is log-based, too, again, but it's really the shape that's different.
- [43:03](https://www.youtube.com/watch?v=undefined&t=2583s) The implication there is that if, for
instance, Cambridge and Allston, two different towns here in Massachusetts, merge next year and
there's just one phone book that's twice as big, no big deal for that third and final algorithm. Why? You
just tear the problem one more time in half, taking one more byte, that's it, not another 1,000 bytes just
to get to the solution.
- [43:24](https://www.youtube.com/watch?v=undefined&t=2604s) Put another way, you can walk it way,
way, way out here to a much bigger phone book and ultimately that green line is barely going to have
budged. So this then is just a way of now formalizing and thinking about what the performance or
quality of these algorithms might be. Before we now make one more formalization of the algorithm
itself, any questions then on this notion of efficiency or now performance of ideas? Yeah.
- [43:53](https://www.youtube.com/watch?v=undefined&t=2633s) AUDIENCE: How many phone books
have you got? DAVID MALAN: (LAUGHING) A lot of phone books over the years and if you or your
parents have any more still somewhere we could definitely use them because they're hard to find. Other
questions? But thanks. Other questions here, too? No. Oh, was that a murmur? Yes, over here.
- [44:13](https://www.youtube.com/watch?v=undefined&t=2653s) AUDIENCE: You could get Harry
Potter as a guest speaker. DAVID MALAN: Sorry, say again. AUDIENCE: You could get Harry Potter as a
guest speaker. DAVID MALAN: (LAUGHING) Oh, yeah. Hopefully. Then we'd have a little something more
to use here. So now if we want to formalize further what it is we just did, we can go ahead and introduce
this.
- [44:30](https://www.youtube.com/watch?v=undefined&t=2670s) A form of code aka pseudocode.
Pseudocode is not a specific language, it's not like something we're about to start coding in, it's just a
way of expressing yourself in English or any human language succinctly correctly toward an end of
getting your idea for an algorithm across. So for instance, here might be how we could formalize the
code, the pseudocode for that same algorithm.
- [44:51](https://www.youtube.com/watch?v=undefined&t=2691s) Step one was pick up the phone
book, as I did. Step two might be open to the middle of the phone book, as you proposed that we do
first. Step three was probably to look down at the pages, I did. And step four gets a little more interesting
because I had to quickly make a decision and ask myself a question.
- [45:08](https://www.youtube.com/watch?v=undefined&t=2708s) If person is on page, then I should
probably just go ahead and call that person. But that probably wasn't the case at least for John Harvard,
and I opened the M section. So there's this other question I should now ask else if the person is earlier in
the book, then I should tear the problem in half as I did but go left, so to speak, and then not just open
to the middle of the left half of the book, but really just go back to step three, repeat myself.
- [45:33](https://www.youtube.com/watch?v=undefined&t=2733s) Why? Because I can just repeat what
I just did, but with a smaller problem having taken this big bite. But, if the person was later in the book,
as might have happened with a different person than John Harvard, then I should open to the middle of
the right half of the book, again go back to line three, but again, I'm not going to get sucked doing
something forever like this because I keep shrinking the size of the problem.
- [45:55](https://www.youtube.com/watch?v=undefined&t=2755s) Lastly, the only possible scenario
that's left, if John Harvard is not on the page and he's not to the left and he's not to the right, what
should our conclusion be? AUDIENCE: He's not there. DAVID MALAN: He's not there. He's not listed. So
we need to quit in some other form. Now as an aside, it's kind of deliberate that I buried that last
question at the end because this is what happens all too often in programming, whether you're new at it
or professional, just not considering all possible cases, corner cases if you will,
- [46:22](https://www.youtube.com/watch?v=undefined&t=2782s) that might not happen that often,
but if you don't anticipate them in your own code, pseudocode or otherwise, this is when and why
programs might crash or you might say stupid little spinning beach balls or hourglasses or your computer
might reboot. Why? It's doing something sort of unpredictable if a human, maybe myself, didn't
anticipate this.
- [46:42](https://www.youtube.com/watch?v=undefined&t=2802s) Like what does this program do if
John Harvard is not in the phone book if I had omitted lines 12 and 13? I don't know. Maybe it would
behave differently on a Mac or PC because it's sort of undefined behavior. These are the kinds of
omissions that frankly you're invariably going to make, bugs you're going to introduce, mistakes you're
going to make early on, and me, too, 25 years later.
- [47:03](https://www.youtube.com/watch?v=undefined&t=2823s) But you'll get better at thinking
about those corner cases and handling anything that can possibly go wrong and as a result, your code
will be all the better for it. Now the problem ultimately with learning how to program, especially if
you've never had experience or even if you do but you learned one language only, is that they all look a
little cryptic at first glance.
- [47:25](https://www.youtube.com/watch?v=undefined&t=2845s) But they do share certain
commonalities. In fact, we'll use this pseudocode to define those first. Highlighted in yellow here are
what henceforth we're going to start calling functions. Lots of different programming languages exist, but
most of them have what we might call functions, which are actions or verbs that solve some smaller
problem.
- [47:44](https://www.youtube.com/watch?v=undefined&t=2864s) That is to say, you might use a whole
bunch of functions to solve a bigger problem because each function tends to do something very specific
or precise. These then in English might be translated in code, actual computer code, to these things
called functions. Highlighted in yellow now are what we might call conditionals.
- [48:03](https://www.youtube.com/watch?v=undefined&t=2883s) Conditionals are things that you do
conditionally based on the answer to some question. You can think of them kind of like forks in the road.
Do you go left or go right or some other direction based on the answer to some question? Well, what are
those questions? Highlighted now in yellow or what we would call Boolean expressions, named after a
mathematician last name Bool, that simply have yes no answers.
- [48:25](https://www.youtube.com/watch?v=undefined&t=2905s) Or, if you prefer, true or false
answers or, heck, if you prefer 1 or 0 answers. We just need to distinguish one scenario from another.
The last thing manifests in this pseudocode is what I might highlight now and call loops. Some kind of
cycle, some kind of directive that tells us to do something again and again so that I don't need a 1,000-
line program to search a 1,000-page phone book, I can get away with a 13-line program but sort of
repeat myself inherently in order to solve some problem until I get to that
- [48:58](https://www.youtube.com/watch?v=undefined&t=2938s) last step. So this then is what we
might call pseudocode and indeed there are other characteristics of programs that we'll touch on before
long, things like arguments and return values, variables, and more, but unfortunately in most languages,
including some we will very deliberately use in this class and that everyone in the real world these days
still uses, its programs tend to look like this.
- [49:22](https://www.youtube.com/watch?v=undefined&t=2962s) This for instance, is a distillation of
that very first program I wrote in 1996 in CS50 itself just to print something on the screen. In fact, this
version here just tries to print quote unquote, "Hello, world." Which is, dare say, the most canonical first
thing that most any programmer ever gets a computer to say just because, but look at this mess.
- [49:43](https://www.youtube.com/watch?v=undefined&t=2983s) I mean, there's a hash symbol, these
angled brackets, parentheses, words like int, curly braces, quotes, parentheses, semicolons, and back
slashes. I mean there's more overhead and more syntax and clutter than there is an actual idea. Now
that's not to say that you won't be able to understand this before long, because honestly there's not that
many patterns, indeed programming languages have typically a much smaller vocabulary than any actual
human language, but at first it might indeed look quite cryptic.
- [50:11](https://www.youtube.com/watch?v=undefined&t=3011s) But you can perhaps infer I have no
idea what these other lines do yet, but "Hello, world." is presumably quote unquote what will be printed
on the screen. But what we'll do today, after a short break, and set the stage for next week is introduce
these exact same ideas in just a bit using Scratch, something that you yourselves might have used when
you're quite younger but without the same vocabulary applied to those ideas.
- [50:34](https://www.youtube.com/watch?v=undefined&t=3034s) The upside of what we'll soon do
using Scratch, this graphical programming language from our friends down the road at MIT, it'll let us
today start to drag and drop things that look like puzzle pieces that interlock together if it makes logical
sense to do so, but without the distraction of hashes, parentheses, curly braces, angle brackets,
semicolons, and things that are quite beside the point.
- [50:56](https://www.youtube.com/watch?v=undefined&t=3056s) But for now, let's go ahead and take
a 10 minute break here and when we resume, we will start programming. So this on the screen is a
language called C something that will dive into next week and thankfully this now on the screen is
another language called Python that we'll also take a look at in a few weeks before long along with other
languages along the way.
- [51:16](https://www.youtube.com/watch?v=undefined&t=3076s) Today though, and for this first week,
week zero, so to speak, we use Scratch because again it will allow us to explore some of those
programming fundamentals that will be in C and in Python and in JavaScript and other languages, too,
but in a way where we don't have to worry about the distractions of syntax.
- [51:33](https://www.youtube.com/watch?v=undefined&t=3093s) So the world of Scratch looks like
this. It's a web-based or downloadable programming environment that has this layout here by default.
On the left here we'll soon see is a palette of puzzle pieces, programming blocks that represent all of
those ideas we just discussed. And by dragging and dropping these puzzle pieces or blocks over this big
area and connecting them together, if it makes logical sense to do so, we'll start programming in this
environment.
- [51:59](https://www.youtube.com/watch?v=undefined&t=3119s) The environment allows you to have
multiple sprites, so to speak. Multiple characters, things like a cat or anything else, and those sprites
exist in this rectangular world up here that you can full screen to make bigger and this here by default is
Scratch, who can move up, down, left, right and do many more things, too.
- [52:16](https://www.youtube.com/watch?v=undefined&t=3136s) Within its Scratch's world you can
think of it as perhaps a familiar coordinate system with Xs and Ys which is helpful only when it comes
time to position things on the screen. Right now Scratch is at the default, 0,0, where x equals 0 and y
equals 0. If you were to move the cat way up to the top, x would stay zero, y would be positive 180.
- [52:38](https://www.youtube.com/watch?v=undefined&t=3158s) If you move the cat all the way to the
bottom, x would stay zero, but y would now be negative 180. And if you went left, x would become
negative 240 but y would stay 0, or to the right x would be 240 and y would stay zero. So those numbers
generally don't so much matter because you can just move relatively in this world up, down, left, right,
but when it comes time to precisely position some of these sprites or other imagery, it'll be helpful just
to have that mental model off up, down, left, and right.
- [53:07](https://www.youtube.com/watch?v=undefined&t=3187s) Well let's go ahead and make
perhaps the simplest of programs here. I'm going to switch over to the same programming environment
now for a tour of the left hand side. So by default selected here are the category in blue motion, which
has a whole bunch of puzzle pieces or blocks that relate to motion.
- [53:24](https://www.youtube.com/watch?v=undefined&t=3204s) And whereas Scratch as a graphical
language categorizes things by the type of things that these pieces do, we'll see that throughout this
whole palette we'll have functions and variables and conditionals and Boolean expressions and more
each in a different color and shape. So for instance, moving 10 steps or turning one way or the other
would be functions categorized here as things like motion.
- [53:46](https://www.youtube.com/watch?v=undefined&t=3226s) Under looks in purple, you might
have speech bubbles that you can create by dragging and dropping these that might say "hello" or
whatever for some number of seconds. Or you could switch costumes, change the cat to look like a dog
or a bird or anything else in between. Sounds, too. You can play sounds like "meow" or anything you
might import or record, yourself.
- [54:06](https://www.youtube.com/watch?v=undefined&t=3246s) Then there's these things Scratch
calls events and the most important of these is the first, when green flag clicked. Because if we look over
to the right of Scratch's world here, this rectangular region has this green flag and red stop sign up
above, one of which is for Play one of which is for Stop and so that's going to allow us to start and stop
our actual programs when that green flag is initially clicked.
- [54:28](https://www.youtube.com/watch?v=undefined&t=3268s) But you can listen for other types of
events when the spacebar is pressed or something else, when this sprite is clicked or something else.
Here you already see like a programmer's incarnation of things you and I take for granted like every day
now on our phones. Any time you tap an icon or drag your finger or hit a button on the side.
- [54:46](https://www.youtube.com/watch?v=undefined&t=3286s) These are what a programmer would
call events, things that happen and are often triggered by us humans and things that a program be it in
Scratch or Python or C or anything else can listen for and respond to. Indeed, that's why when you tap
the phone icon on your phone, the phone application starts up because someone wrote software that's
listening for a finger press on that particular icon.
- [55:09](https://www.youtube.com/watch?v=undefined&t=3309s) So Scratch has these same things,
too. Under Control in orange, you can see that we can wait for one second or repeat something some
number of times, 10 by default, but we can change anything in these white circles to anything else.
There's another puzzle piece here forever, which implies some kind of loop where we can do something
again and again.
- [55:26](https://www.youtube.com/watch?v=undefined&t=3326s) Even though it seems a little tight,
there's not much room to fit something there, Scratch is going to have these things grow and shrink
however we want to fill similarly shaped pieces. Here are those conditionals. If something is true or false,
then do this next thing. And that's how we can put in this little trapezoid-like shape.
- [55:43](https://www.youtube.com/watch?v=undefined&t=3343s) Some form of Boolean expression, a
question with a yes/no, true/false, or one/zero answer and decide whether to do something or not. You
can combine these things, too. If something is true, do this, else do this other thing. And you can even
tuck one inside of the other if you want to ask three or four or more questions.
- [56:01](https://www.youtube.com/watch?v=undefined&t=3361s) Sensing, too, is going to be a thing.
You can ask questions aka Boolean expressions like is the sprite touching the mouse pointer, the arrow
on the screen? So that you can start to interact with these programs. What is the distance between a
sprite and a mouse pointer? You can do simple calculations just to figure out maybe if the enemy is
getting close to the cat.
- [56:20](https://www.youtube.com/watch?v=undefined&t=3380s) Under Operator some lower level
stuff like math, but also the ability to pick random numbers, which for a game is great because then you
can kind of vary the difficulty or what's happening in a game without the same game playing the same
way every time. And you can combine ideas. Something and something must be true in order to make
that kind of decision before.
- [56:38](https://www.youtube.com/watch?v=undefined&t=3398s) Or we can even join two words
together. Says apple and banana by default, but you can type in or drag and drop whatever you want
there to combine multiple words into full, larger sentences. Then lastly down here, there's in orange
things called variables. In math we've obviously got x and y and whatnot.
- [56:55](https://www.youtube.com/watch?v=undefined&t=3415s) In programming we'll have the same
ability to store in these named symbols, x or y, values that we care about. Numbers or letters or words or
colors or anything, ultimately. But in programming you'll see that it's much more conventional not to just
use simple letters like x and y and z, but to actually give variables full singular or plural words to describe
what they are.
- [57:20](https://www.youtube.com/watch?v=undefined&t=3440s) Then lastly, if this isn't enough color
blocks for you, you can create your own blocks. Indeed, this is going to be a programming principle we'll
apply today and with the first problem set whereby once you start to assemble these puzzle pieces and
you realize, oh, would have been nice if those several pieces could have just been replaced by one had
MIT thought to give me that one puzzle piece, you yourself can make your own blocks by connecting
these all together, giving them a name, and boom, a new puzzle piece will exist.
- [57:49](https://www.youtube.com/watch?v=undefined&t=3469s) So let's do the simplest, most
canonical programs here, starting up with control, and I'm going to click and drag and drop this thing
here when green flag clicked. Then I'm going to grab one more, for instance under Looks, and under
Looks I'm going to go ahead and just say something like initially not just Hello but the more canonical
Hello comma world.
- [58:10](https://www.youtube.com/watch?v=undefined&t=3490s) Now you might guess that in this
programming environment, I can go over here now and click the green flag and voila, Hello comma
world. So that's my first program and obviously much more user friendly than typing out the much more
cryptic text that we saw on the screen that you, too, will type out next week.
- [58:25](https://www.youtube.com/watch?v=undefined&t=3505s) But for now, we'll just focus on these
ideas, in this case, a function. So what it is that just happened? This purple block here is Say, that's the
function, and it seems to take some form of input in the white oval, specifically Hello comma world. Well
this actually fits the paradigm that we looked at earlier of just inputs and outputs.
- [58:42](https://www.youtube.com/watch?v=undefined&t=3522s) So if I may, if you consider what this
puzzle piece is doing, it actually fits this model. The input in this case is going to be Hello comma world in
white. The algorithm is going to be implemented as a function by MIT called Say and the output of that is
going to be some kind of side effect, like the cat and the speech bubble are saying Hello, world.
- [59:01](https://www.youtube.com/watch?v=undefined&t=3541s) So already even that simple drag and
drop mimics exactly this relatively simple mental model. So let's take things further. Let's go ahead now
and make the program a little more interactive so that it says something like Hello, David, or Hello,
Carter, or Hello to you specifically. And for this, I'm going to go under Sensing.
- [59:19](https://www.youtube.com/watch?v=undefined&t=3559s) And you might have to poke around
to find these things the first time around, but I've done this a few times so I kind of know where things
are and what color. There's this function here. Ask what's your name, but that's in white, so we can
change the question to anything we want, and it's going to wait for the human to type in their answer.
- [59:35](https://www.youtube.com/watch?v=undefined&t=3575s) This function called Ask is a little
different from the Say block, which just had this side effect of printing a speech bubble to the screen.
The ask function is even more powerful in that after it asks the human to type something in. This
function is going to hand you back what they typed in in the form of what's called a return value, which
is stored ultimately and by default this thing called Answer.
- [59:58](https://www.youtube.com/watch?v=undefined&t=3598s) This little blue oval here called
Answer is again one of these variables that in math would be called just x or y but in programming we're
saying what it does. So I'm going to go ahead and do this. Let me go ahead and drag and drop this block
and I want to ask the question before saying anything, but you'll notice that Scratch is smart and it's
going to realize I want to insert something in between and it's just going to move things up and down.
- [1:00:18](https://www.youtube.com/watch?v=undefined&t=3618s) I'm going to let go and ask the
default question, what's your name? And now if I want to go ahead and say hello, David or Carter, let's
just do Hello comma, because I obviously don't know when I'm writing the program who's going to use
it. So let me now grab another looks block up here, say something again, and now let me go back to
Sensing and now grab the return value, represented by this other puzzle piece, and let me just drag and
drop it here.
- [1:00:43](https://www.youtube.com/watch?v=undefined&t=3643s) Notice it's the same shape, even if
it's not quite the same size. Things will grow or shrink as needed. All right, so let's now zoom out. Let me
go and stop the old version because I don't want to say Hello, world anymore. Let me hit the green flag
and what's my name? All right, David. Enter.
- [1:00:58](https://www.youtube.com/watch?v=undefined&t=3658s) Huh. All right, maybe I just wasn't
paying close enough attention. Let me try it again. Green flag, D-A-V-I-D, Enter. This seems like a bug.
What's the bug or mistake might you think? Yeah? AUDIENCE: Do you need to somehow add them
together in the same text box? DAVID MALAN: Yeah, we kind of want to combine them in the same text
box.
- [1:01:21](https://www.youtube.com/watch?v=undefined&t=3681s) And it's technically a bug because
this just looks kind of stupid. It's just saying David after I asked for my name. I'd like it to say maybe Hello
then David, but it's just blowing past the Hello and printing David. But let's put our finger on why this is
happening. You're right for the solution, but what's the actual fundamental problem? In back.
- [1:01:39](https://www.youtube.com/watch?v=undefined&t=3699s) AUDIENCE: So it says hello, but it
gets to that last step so quickly you can't see it. DAVID MALAN: Perfect. I mean, computers are really
darn fast these days. It is saying Hello, all of us are just too slow in this room to even see it because it's
then saying David on the screen so fast as well. So there's a couple of solutions here, and yours is spot
on, but just to poke around, you'll see the first example of how many ways in programming be it Scratch
or C or Python or anything else, that there are going to be to solve problems?
- [1:02:05](https://www.youtube.com/watch?v=undefined&t=3725s) We'll teach you over the course of
these weeks, sometimes some ways are better relatively than others, but rarely is there a best way
necessarily, because again reasonable people will disagree. And what we'll try to teach you over the
coming weeks is how to kind of think through those nuances. And it's not going to be obvious at first
glance, but the more programs you write, the more feedback you get, the more bugs that you introduce,
the more you'll get your footing with exactly this kind of problem solving.
- [1:02:31](https://www.youtube.com/watch?v=undefined&t=3751s) So let me try this in a couple of
ways. Up here would be one solution to the problem. MIT anticipated this kind of issue, especially with
first-time programmers, and I could just use a puzzle piece that says say the following for two seconds or
one second or whatever, then do the same with the next word and it might be kind of a bit of a pause,
Hello, one second, two seconds, David, one second, two seconds, but at least it would look a little more
grammatically correct.
- [1:02:57](https://www.youtube.com/watch?v=undefined&t=3777s) But I can do it a little more
elegantly, as you've proposed. Let me go ahead and throw away one of these blocks, and you can just
drag and let go and it'll delete itself. Let me go down to Operators because this Join block here is the
right shape. So even if you're not sure what goes where, just focus on the shapes first.
- [1:03:14](https://www.youtube.com/watch?v=undefined&t=3794s) Let me drag this over here. It grew
to fill that. Let me go ahead and say hello comma space. Now it could just say by default Hello, banana,
but let me go back to Sensing, Drag answer, and that's going to drag and drop there. So now notice we're
sort of stacking or nesting one block on another so that the output of one becomes the input to another,
but that's OK here.
- [1:03:38](https://www.youtube.com/watch?v=undefined&t=3818s) Let me go ahead and zoom out, hit
Stop, and hit Play. All right, what's your name? D-A-V-I-D, Enter, and voila. Now it's presumably as we
first intended. [APPLAUSE] (LAUGHING) Oh, thank you. Thank you. No minus 2 this time. So consider that
even with this additional example, it still fits the same mental model, but in a little more interesting way.
- [1:04:06](https://www.youtube.com/watch?v=undefined&t=3846s) Here's that new function Ask
something and wait. And notice that in this case too there's an input, otherwise known henceforth as an
argument or a parameter, programming speak for just an input in the context of a function. If we use our
drawing as before to represent this thing here, we'll see that the input now is going to be quote unquote
"What's your name?" The algorithm is going to be implemented by way of this new puzzle piece, the
function called Ask, and the output of that thing this time
- [1:04:33](https://www.youtube.com/watch?v=undefined&t=3873s) is not going to be the cat saying
anything yet, but rather it's going to be the actual answer. So instead of the visual side effect of the
speech bubble appearing, now nothing visible is happening yet. Thanks to this function it's sort of
handing me back like a scrap of paper with whatever I typed in written on it so I can reuse D-A-V-I-D one
or more times even like I did.
- [1:04:58](https://www.youtube.com/watch?v=undefined&t=3898s) Now what did I then do with that
value? Well consider that with the subsequent function we had this Say block, too, combined with a join.
So we have this variable called Answer, we're joining it with that first argument, Hello. So already we see
that some functions like Join can take not one but two arguments, or inputs, and that's fine.
- [1:05:20](https://www.youtube.com/watch?v=undefined&t=3920s) The output of Join is presumably
going to be Hello, David or Hello, Carter or whatever the human typed in. That output notice is
essentially becoming the input to another function, Say, just because we've kind of stacked things or
nested them on top of one another. But methodically, it's really the same idea.
- [1:05:40](https://www.youtube.com/watch?v=undefined&t=3940s) The input now are two things,
Hello comma and the return value from the previous Ask function. The function now is going to be Join,
the output is going to be Hello, David. But that Hello, David output is now going to become the input to
another function, namely that first block called Say, and that's then going to have the side effect of
printing out Hello, David on the screen.
- [1:06:03](https://www.youtube.com/watch?v=undefined&t=3963s) So again as sort of sophisticated as
ours as yours as others programs are going to get, they really do fit this very simple mental model of
inputs and outputs and you just have to learn to recognize the vocabulary and to know what kinds of
puzzle pieces or concepts ultimately to apply. But you can ultimately really kind of spice these things up.
- [1:06:20](https://www.youtube.com/watch?v=undefined&t=3980s) Let me go back to my program here
that just is using the speech bubble at the moment. Scratch's inside has some pretty fancy interactive
features, too. I click the Extensions button in the bottom left corner. And let me go ahead and choose the
Text to Speech extension. This is using a Cloud service, so if you have an internet connection it can
actually talk to the Cloud or a third party service, and this one is going to give me a few new green puzzle
pieces, namely the ability to speak something from my speakers
- [1:06:45](https://www.youtube.com/watch?v=undefined&t=4005s) instead of just saying it textually. So
let me go ahead and drag this. Now notice I don't have to interlock them if I'm just kind of playing
around and I want to move some things around. I just want to use this as like a canvas temporarily. Let
me go ahead and steal the Join from here, put it there, let me throw away the Say block by just moving it
left and letting go, and now let me join this in so I've now changed my program to be a little more
interesting.
- [1:07:09](https://www.youtube.com/watch?v=undefined&t=4029s) So now let me stop the old version.
Let me start the new. What's your name? Type in David. And voila: PROGRAM: Hello, banana. DAVID
MALAN: (LAUGHING) OK, minus 2 for real. All right, so what I accidentally threw away there,
intentionally for instructional purposes, was the actual answer that came back from the ask block.
- [1:07:33](https://www.youtube.com/watch?v=undefined&t=4053s) That's embarrassing. So now if I
play this again, let's click the green icon. What's your name? David. And now: PROGRAM: Hello, David.
DAVID MALAN: There we go. Hello, David. All right, thank you. [APPLAUSE] OK, so we have these
functions then in place, but what more can we do? Well what about those conditionals and loops and
other constructs? How can we bring these programs to life so it's not just clicking a button and voila,
something's happening? Let's go ahead and make this now even more interactive.
- [1:08:04](https://www.youtube.com/watch?v=undefined&t=4084s) Let me go ahead and throw away
most of these pieces and let me just spice things up with some more audio under Sound. I'm going to go
to Play Sound Meow until done. Here we go, green flag. [MEOW] OK, it's a little loud, but it did exactly
do what it said. Let's hear it again. [QUIETER MEOW] OK. It's kind of an underwhelming program
eventually since you'd like to think that the cat would just meow on its own, but.
- [1:08:27](https://www.youtube.com/watch?v=undefined&t=4107s) [MEOW] I have to keep hitting the
button. Well this seems like an opportunity for doing something again and again. So all right, well if I
wanted to meow, meow, meow, let me just grab a few of these, or you can even right click or Control
click and you can Copy Paste even in code here. Let me play this now.
- [1:08:41](https://www.youtube.com/watch?v=undefined&t=4121s) [THREE MEOWS] All right, so now
like it's not really emoting happiness in quite the same way. It might be hungry or upset. So let's slow it
down. Let me go to Control, wait one second in between, which might be a little less worrisome. Here
we go, Play. [THREE SLOWER MEOWS] OK, so if my goal was to make the cat meow three times, I dare
say this code or algorithm is correct.
- [1:09:10](https://www.youtube.com/watch?v=undefined&t=4150s) But let's now critique its design. Is
this well-designed? And if not, why not? What are your thoughts here? Yeah? AUDIENCE: You could use
the forever or a repeat to make it more-- DAVID MALAN: Yeah, so yeah, agreed. I could use forever or
repeat, but let me push a little harder. But why? Like this works, I'm kind of done with the assignments,
what's bad about it? AUDIENCE: There's too much repetition.
- [1:09:38](https://www.youtube.com/watch?v=undefined&t=4178s) DAVID MALAN: Yeah, there's too
much repetition, right? If I wanted to change the sound that the cat is making to a different variant of
meow or have it bark instead like a dog, I could change it from the dropdown here apparently, but then
I'd have to change it here and then I'd have to change it here, and God, if this were even longer that just
gets tedious quickly and you're probably increasing the probability that you're going to screw up and
you're going to miss one of the dropdowns or something stupid and introduce a bug.
- [1:10:00](https://www.youtube.com/watch?v=undefined&t=4200s) Or, if you wanted to change the
number of seconds you're waiting, you've got to change it in two, maybe even more places. Again, you're
just creating risk for yourself and potential bugs in the program. So I do like the repeat or the forever
idea so that I don't repeat myself. And indeed, what I alluded to being possible, copy pasting earlier,
doesn't mean it's a good thing.
- [1:10:19](https://www.youtube.com/watch?v=undefined&t=4219s) And in code, generally speaking,
when you start to copy and paste puzzle pieces or text next week, you're probably not doing something
quite well. So let me go ahead and throw away most of these to get rid of the duplication, keeping just
two of the blocks that I care about. Let me grab the Repeat block for now, let me move this inside of the
Repeat block, it's going to grow to fit it, let me reconnect all this and change the 10 just to a 3, and now,
Play.
- [1:10:44](https://www.youtube.com/watch?v=undefined&t=4244s) [THREE SLOW MEOWS] So, better.
It's the same thing. It's still correct, but now I've set the stage to let the cat meow, for instance, four
times by changing one thing, 40 times by changing one thing, or it could just use the Forever block and
just walk away and it will meow forever instead. If that's your goal, that would be better.
- [1:11:05](https://www.youtube.com/watch?v=undefined&t=4265s) A better design but still correct. But
you know what? Now that I have a program that's designed to have a cat meow, wow like why? I mean,
MIT invented Scratch, Scratch as a cat, why is there no puzzle piece called Meow? This feels like a missed
opportunity. Now to be fair, they gave us all the building blocks with which we could implement that
idea, but a principle of programming and really computer science is to leverage what we're going to now
start calling Abstraction.
- [1:11:30](https://www.youtube.com/watch?v=undefined&t=4290s) We have step-by-step instructions
here, the Repeat, the Play, and the Wait that collectively implements this idea that we humans would
call meowing. Wouldn't it be nice to abstract away those several puzzle pieces into just one that literally
just says what it does, meow? Well here's where we can make our own blocks.
- [1:11:48](https://www.youtube.com/watch?v=undefined&t=4308s) Let me go over here to Scratch
under the pink block category here and let me click Make a Block. Here I see a slightly different interface
where I can choose a name for it and I'm going to call it Meow. I'm going to keep it simple. That's it. No
inputs to meow yet. I'm just going to click OK.
- [1:12:05](https://www.youtube.com/watch?v=undefined&t=4325s) Now I'm just going to clean this up
a bit here. Let me drag and drop Play Sound and Wait over here. And you know what? I'm just going to
drag this way down here, way down here because now that I'm done implementing Meow, I'm going to
literally abstract it away, sort of out of sight, out of mind, because now notice at top left there is a new
pink puzzle piece called Meow.
- [1:12:27](https://www.youtube.com/watch?v=undefined&t=4347s) So at this point, I'd argue it doesn't
really matter how Meow is implemented. Frankly, I don't know how Ask or Say was implemented by MIT.
They abstracted those things away for us. Now I have a brand new puzzle piece that just says what it is.
And this is now still correct, but arguably better design.
- [1:12:44](https://www.youtube.com/watch?v=undefined&t=4364s) Why? Because it's just more
readable to me, to you, it's more maintainable when you look at your code a year from now for the first
time because you're sort of finally looking back at the very first program you wrote. It says what it does.
The function itself has semantics, which conveys what's going on.
- [1:12:59](https://www.youtube.com/watch?v=undefined&t=4379s) If you really care about how Meow
is implemented, you could scroll down and start to tinker with the underlying implementation details,
but otherwise you don't need to care anymore. Now I feel like there's an even additional opportunity
here for abstraction and to factor out some of this functionality.
- [1:13:18](https://www.youtube.com/watch?v=undefined&t=4398s) It's kind of lame that I have this
Repeat block that lets me call the Meow function, so to speak, use the Meow function three times.
Wouldn't it be nice if I could just call them Meow function, aka use the Meow function, and pass it in
input that tells the puzzle piece how many times I want it to meow? Well let me go ahead and zoom out
and scroll down.
- [1:13:38](https://www.youtube.com/watch?v=undefined&t=4418s) Let me right click or Control click
on the pink piece here and choose Edit, or I could just start from scratch, no pun intended, with a new
one. Now here, rather than just give this thing a name Meow, let me go ahead and add an input here.
I'm going to go ahead and type in, for instance, n, for number of times to meow, and just to make this
even more user friendly and self descriptive, I'm going to add a label, which has no functional impact, it's
just an aesthetic, and I'm just going to say Times, just to make it read more like English
- [1:14:06](https://www.youtube.com/watch?v=undefined&t=4446s) in this case that tells me what the
puzzle piece does. Now I'm going to click OK. And now I need to refine this a little bit. Let me go ahead
and grab under Control a repeat block, let me move the Play, Sound, and Wait, into the repeat block. I
don't want 10 and I also don't want 3 here. What I want now is this n that is my actual variable that
Scratch is creating for me that represents whatever input the human programmer provides.
- [1:14:34](https://www.youtube.com/watch?v=undefined&t=4474s) Notice that snaps right in place. Let
me connect this and now voila, I have an even fancier version of Meow that is parameterized. It takes
input that affects its behavior accordingly. Now I'm going to scroll back up, because out of sight, out of
mind, I just care that Meow exists. Now I can tighten up my code, so to speak, use even fewer lines to do
the same thing by throwing away the Repeat block, reconnecting this new puzzle piece here that takes
an input like 3 and voila, now we're really programming, right?
- [1:15:02](https://www.youtube.com/watch?v=undefined&t=4502s) We've not made any forward
progress functionally. The thing just mouse three times. But it's a better design. As you program more
and more, these are the kinds of instincts still start to acquire so that one, you can start to take a big
assignment, a big problem set, something for homework even, that feels kind of overwhelming at first,
like, oh my God where do I even begin? But if you start to identify what are the subproblems of a bigger
problem? Then you can start making progress.
- [1:15:27](https://www.youtube.com/watch?v=undefined&t=4527s) I do this to this day where if I have
to tackle some programming-related project it's so easy to drag my feet and ugh, it's going to take
forever to start, until I just start writing down like a to do list and I start to modularize the program and
say, all right, well what do I want this thing to do? Meowing.
- [1:15:43](https://www.youtube.com/watch?v=undefined&t=4543s) What's that mean? I've got to have
it say something on the screen. All right, I need to have it say something on the screen some number of
times. Like literally a mental or written checklist, or pseudocode code, if you will, in English on a piece of
paper or text file, and then you can decide, OK, the first thing I need to do for homework to solve this
real world problem, I just need a Meow function.
- [1:16:02](https://www.youtube.com/watch?v=undefined&t=4562s) I need to use a bunch of other
code, too, but I need to create a Meow function and boom, now you have a piece of the problem solved
not unlike we did with the phone book there, but in this case, we'll have presumably other problems to
solve. All right, so what more can we do? Let's add a few more pieces to the puzzle here.
- [1:16:19](https://www.youtube.com/watch?v=undefined&t=4579s) Let's actually interact with the cat
now. Let me go ahead and now when the green flag is clicked, let me go ahead and ask a question using
an event here. Let me go ahead and say, let's see, I want to do something like implement the notion of
petting the cat. So if the cursor is touching the cat like here, something like this, it'd be cute if the cat
meows like you're petting a cat.
- [1:16:42](https://www.youtube.com/watch?v=undefined&t=4602s) So I'm going to ask the question,
when the green flag is clicked, if let's see I think I need Sensing. So if touching mouse pointer, this is way
too big but again the shape is fine, so there goes. Grew to fill. And then if it's touching the mouse
pointer, that is if the cat to whom this script or this program, any time I attach puzzle pieces MIT calls
them a script or like a program, if you will, let me go ahead then and choose a sound and say play sound
meow until done.
- [1:17:10](https://www.youtube.com/watch?v=undefined&t=4630s) All right, so here it is to be clear.
When the green flag is clicked, ask the question, if the cat is touching the mouse pointer then place
sound meow. Here we go. Play. [SILENCE] All right, let's try again. Play. [SILENCE] Huh. I'm worried it's not
Scratch's fault. Feels like mine. What's the bug here? Why doesn't this work? Yeah, in back, who just
turned.
- [1:17:42](https://www.youtube.com/watch?v=undefined&t=4662s) AUDIENCE: [INAUDIBLE] DAVID
MALAN: Yeah, the problem is the moment I click that green flag, Scratch asks the question, is the cat
touching the mouse pointer? And obviously it's not because the cursor was like up there a moment ago
and it's not down there. It's fine if I move the cursor down there, but too late.
- [1:18:01](https://www.youtube.com/watch?v=undefined&t=4681s) The program already asked the
question. The answer was no or false or zero, however you want to think about it, so no sound was
played. So what might be the solution here be? I could move my cursor quickly, but that feels like never
going to work out right. Other solutions here? Yeah, in way back? Could you use the forever loop? The
Forever loop.
- [1:18:21](https://www.youtube.com/watch?v=undefined&t=4701s) So I could indeed use this Forever
loop because if I want my program to just constantly listen to me, well let's literally do something
forever, or at least forever as long as the program is running until I explicitly hit Stop. So let me grab that.
Let me go to Control, let me grab the Forever block, let me move the If inside of this Forever block,
reconnect this, go back up here, click the green flag, and now nothing's happened yet, but let me try
moving my cursor now.
- [1:18:46](https://www.youtube.com/watch?v=undefined&t=4726s) [MEOW] Oh. So now. [MEOW]
That's kind of cute. So now the cat is actually responding and it's going to keep doing this again and
again. So now we have this idea of taking these different ideas, these different puzzle pieces, assembling
them into something more complicated. I could definitely put a name to this.
- [1:19:04](https://www.youtube.com/watch?v=undefined&t=4744s) I could create a custom block, but
for now let's just consider what kind of more interactivity we can do. Let me go ahead and do this. By
again grabbing a, when green flag clicked, let me go ahead and click the video sensing, and I'm going to
rotate the laptop because otherwise we're going to get a little inception thing here where the camera is
picking up the camera is up there.
- [1:19:22](https://www.youtube.com/watch?v=undefined&t=4762s) So I'm going to go reveal to you
what's inside the lectern here while we rotate this. Now that we have a non video backdrop, I'm going to
say this. Instead of the green flag clicked, actually, I'm going to say when the video motion is greater than
some arbitrary measurement of motion, I'm going to go ahead and play sound meow until done.
- [1:19:47](https://www.youtube.com/watch?v=undefined&t=4787s) And then I'm going to get out of
the way. So here's the cat. We'll put them on top of there. [MEOW] OK. All right, and here we go.
[MEOW] So my hand is moving faster than 50 something or other, whatever the unit of measure is.
[MEOW] AUDIENCE: Aw. DAVID MALAN: (LAUGHING) Thank you. So now we have an even more
interactive version.
- [1:20:11](https://www.youtube.com/watch?v=undefined&t=4811s) [MEOW] But I think if I sort of
slowly. [LAUGHING] (LAUGHING) Right? It's completely creepy, but I'm not like exceeding the threshold--
[MEOW] Until finally my hand moves as fast as that. And so here actually is an opportunity to show you
something a former student did. Let me go ahead here and-- [MEOW TWICE] OK, got to stop this.
- [1:20:36](https://www.youtube.com/watch?v=undefined&t=4836s) Let me go ahead and zoom out of
this in just a moment. [MEOW] If someone would be-- [LAUGHING] (LAUGHING) If someone would be
comfortable coming up not only masked but also on camera on the internet I thought we'd play one of
your former classmate's projects here up on stage. Would anyone like to volunteer here and be up on
stage? Who's that? Yeah.
- [1:20:52](https://www.youtube.com/watch?v=undefined&t=4852s) Come on down. What's your
name? AUDIENCE: Sahar. DAVID MALAN: Sahar. All right, come on down. Let me get it set up for you
here. [MEOW] [APPLAUSE] [MEOW] All right, let me go ahead and full screen this here. So this is whack-
a-mole by one of your firmer predecessors. It's going to use the camera focusing on your head, which
will have to position inside of this rectangle.
- [1:21:22](https://www.youtube.com/watch?v=undefined&t=4882s) Have you ever played the whack-a-
mole game at an arcade? AUDIENCE: Yeah. DAVID MALAN: OK. So for those who haven't, these little
moles pop up and with a very fuzzy hammer you sort of hit down. You though, if you don't mind, you're
going to use your head to do this virtually. So let's line up your head with this red rectangle, if you could,
we'll do beginner.
- [1:21:40](https://www.youtube.com/watch?v=undefined&t=4900s) [MUSIC PLAYING] All right, here we
go. Sahar. Give it a moment. OK, come a little closer. [DINGING] And now hit the moles with your head.
[DING] There we go, one point. [DING] One point. [DINGING] Nice. 15 seconds to go. There we go. Oh
yeah. One point. [LAUGHING] [DINGING] Six seconds. AUDIENCE: Oh no. DAVID MALAN: There we go.
- [1:22:12](https://www.youtube.com/watch?v=undefined&t=4932s) Quick! [DINGING] All right, a round
of applause for Sahar. Thank you. [APPLAUSE] So beyond having a little bit of fun here, the goal was to
demonstrate that by using some fairly simple, primitive, some basic building blocks but assembling them
in a fun way with some music, maybe some new costumes or artwork, you can really bring programs to
life.
- [1:22:38](https://www.youtube.com/watch?v=undefined&t=4958s) But at the end of the day, the only
puzzle pieces really involved were ones like the ones I just dragged and dropped and a few more,
because there were clearly lots of moles. So the student probably created a few different sprites, not a
single cap, but at least four different moles. They had like some kind of graphic on the screen that
showed Sahar where to position her head.
- [1:22:55](https://www.youtube.com/watch?v=undefined&t=4975s) There were some kind of timer,
maybe a variable that every second was counting down. So you can imagine taking what looks like a
pretty impressive project at first glance, and perhaps overwhelming to solve yourself, but just think
about what are the basic building blocks? And pluck off one piece of the puzzle, so to speak, at a time.
- [1:23:12](https://www.youtube.com/watch?v=undefined&t=4992s) So indeed if we rewind a little bit.
Let me go ahead here and introduce a program that I myself made back in graduate school when Scratch
was first being developed by MIT. Let me go ahead and open here, give me just one second, something
that I called back in the day Oscar Time that looks a little something like this.
- [1:23:33](https://www.youtube.com/watch?v=undefined&t=5013s) If I fullscreen it and hit Play.
[MUSIC - SESAME STREET, "I LOVE TRASH"] OSCAR THE GROUCH: (SINGING) Oh, I love trash. DAVID
MALAN: So you'll notice a piece of trash is falling. I can click on it and drag and as I get close and close to
the trash can notice OSCAR THE GROUCH: (SINGING) Anything ragged or-- DAVID MALAN: It wants to go
in, it seems.
- [1:23:50](https://www.youtube.com/watch?v=undefined&t=5030s) And if I let go-- OSCAR THE
GROUCH: (SINGING) Yes, I-- DAVID MALAN: One point. Here comes another. OSCAR THE GROUCH:
(SINGING) If you really want to see something trashy-- DAVID MALAN: I'll do the same, two points.
OSCAR THE GROUCH: (SINGING) I have here a sneaker that's tattered and worn-- DAVID MALAN: There's
a sneaker falling from the sky, so another sprite of some sort.
- [1:24:03](https://www.youtube.com/watch?v=undefined&t=5043s) OSCAR THE GROUCH: (SINGING)
The laces are torn. A gift from my mother-- DAVID MALAN: I can also get just a little lazy and just let
them fall into the trash themself if I want to. So you can see it doesn't have to do with my mouse cursor,
it has to do apparently with the distance here. Let's listen a little further.
- [1:24:20](https://www.youtube.com/watch?v=undefined&t=5060s) I think some additional trash is
about to make its appearance. Presumably there's some kind of variable that's keeping track of this
score. OSCAR THE GROUCH: (SINGING) I love-- DAVID MALAN: OK, let's see what the last chorus here is.
OSCAR THE GROUCH: (SINGING) Rotten stuff. I have here some newspaper, crusty and DAVID MALAN:
OK, and thus he continues.
- [1:24:38](https://www.youtube.com/watch?v=undefined&t=5078s) And the song actually goes on and
on and on and I do not have fond memories of implementing this and hearing this song for like 10
straight hours, but it's a good example to just consider how was this program composed? How did I go
about implementing it the first time around? And let me go ahead and open up some programs now that
I wrote in advance just so that we could see how these things are assembled.
- [1:24:59](https://www.youtube.com/watch?v=undefined&t=5099s) Honestly, the first thing I probably
did was probably to do something a little like this. Here is just a version of the program where I set out to
solve just one problem first of planting a lamp post in the program. Right? I kind of had a vision of what I
wanted. You know, it evolved over time, certainly, but I knew I wanted trash to fall, I wanted a cute little
Oscar the Grouch to pop out of the trashcan, and some other stuff, but wow that's a lot to just tackle all
at once.
- [1:25:24](https://www.youtube.com/watch?v=undefined&t=5124s) I'm going to start easy, download a
picture of a lamp post, and then drag and drop it into the stage as a costume and boom, that's version
one. It doesn't functionally do anything. I mean, literally that's the code that I wrote to do this. All I did
was use like the Backdrops feature and drag and drop and move things around, but it got me to version
one of my program.
- [1:25:44](https://www.youtube.com/watch?v=undefined&t=5144s) Then what might version two be?
Well I considered what piece of functionality frankly might be the easiest to pluck off next and the trash
can. That seems like a pretty core piece of functionality. It just needs to sit there most of the time. So the
next thing I probably did was to open up, for instance, the trash can version here that looks a little
something now like this.
- [1:26:06](https://www.youtube.com/watch?v=undefined&t=5166s) So this time I'll show you what's
inside here. There is some code, but not much. Notice at bottom right I change the default cat to a
picture of a trashcan, instead, but it's the same principle that I can control. And then over here I added
this code. When the green flag is clicked, switch the costume to something I arbitrarily called Oscar 1.
- [1:26:25](https://www.youtube.com/watch?v=undefined&t=5185s) So I found a couple of different
pictures of a trash can, one that looks closed, one that looks partly open, and eventually one that has
Oscar coming out, and I just gave them different names. So I said Switch to Oscar 1, which is the closed
one by default, then forever do the following: if touching the mouse pointer, then switch the costume to
Oscar 2, else switch to Oscar 1.
- [1:26:46](https://www.youtube.com/watch?v=undefined&t=5206s) That is to say, I just wanted to
implement this idea of the can opening and closing, even if it's not exactly what I wanted ultimately, I
just wanted to make some forward progress. So here, when I run this program by clicking Play, notice
what happens. Nothing yet, but if I get closer to the trash can, it indeed pops open because it's forever
listening for whether the sprite, the trash can in this case, is touching the mouse pointer.
- [1:27:12](https://www.youtube.com/watch?v=undefined&t=5232s) And that's it. That was version 2, if
you will. If I went in now and added the lamp post and compose the program together, now we're
starting to make progress. Right? Now it would look a little something more like the program I intended
ultimately to create. What piece did I probably bite off after that? Well, I think what I did is I probably
decided let me implement one of the pieces of trash, not the shoe in the newspaper all at once.
- [1:27:34](https://www.youtube.com/watch?v=undefined&t=5254s) Let's just get one piece of trash
working correctly first. So let me go ahead and open this one. And again, all of these examples will be
available on the course's website so you can see all of these examples, too. It's not terribly long, I just
implement it in advance so we could flip through kind of quickly.
- [1:27:51](https://www.youtube.com/watch?v=undefined&t=5271s) Here's what I did here. On the right
hand side, I turned my sprite into a piece of trash this time instead of a cat, instead of a trash can, and I
also created, with Carter's help, a second sprite, this one a floor. It's literally just a black line because I
just wanted initially to have some notion of a floor so I could detect if the trash is touching the floor.
- [1:28:12](https://www.youtube.com/watch?v=undefined&t=5292s) Now without seeing the code yet,
just hearing that description, why might I have wanted the second sprite and this black line for a floor
with the trash intending to fall from the sky? What might I have been thinking? Like what problem might
I be trying to solve? Yeah? AUDIENCE: You don't want the first sprite to go through it.
- [1:28:29](https://www.youtube.com/watch?v=undefined&t=5309s) DAVID MALAN: Yeah, you don't
want the first sprite to start at the top, go through, and then boom, you completely lose it. That would
not be a very useful thing. Or it would seem to maybe eat up more and more of the computer's memory
if the trash is just endlessly falling and I can't grab it.
- [1:28:43](https://www.youtube.com/watch?v=undefined&t=5323s) It might be a little traumatic if you
tried to get it and you can't pull it back out and you can't fix the program. So I just wanted the thing to
stop. So how might I have implemented this? Let's look at the code at left. Here I have a bit of
randomness, like I proposed earlier exists. There's this blue function called Go To x, y that lets me move a
sprite to any position, up, down, left, right, I picked a random x location, either here or over here,
negative 240 to positive 240, and then a y value of 180, which is the top.
- [1:29:12](https://www.youtube.com/watch?v=undefined&t=5352s) This just makes the game more
interesting. It's kind of lame pretty quickly if the trash always falls from the same spot. Here's this a little
bit of randomness, like most any game would have, that spices things up. So now if I click the green flag,
you'll see that it just falls, nothing interesting is going to happen, but it does stop when it touches the
black line because notice what we did here.
- [1:29:33](https://www.youtube.com/watch?v=undefined&t=5373s) I'm forever asking the question if
the distance of the sprite, the trash, is to the floor is greater than zero, that's fine. Change the y location
by negative 3. So move it down 3 pixels, down 3 pixels, until the distance to the floor is not greater than
zero, it is zero or even negative, at which point it should just stop moving altogether.
- [1:29:54](https://www.youtube.com/watch?v=undefined&t=5394s) There's other ways we could have
implemented this, but this felt like a nice, clean way that logically, just made it make sense. OK, now I got
some trash falling, I got a trash can that opens and closes, I have a lamp post, now I'm a good three steps
into the program. We're making progress. If we consider one or two final pieces, something like the
dragging of the trash, let me go ahead and open up this version 2.
- [1:30:18](https://www.youtube.com/watch?v=undefined&t=5418s) Dragging the trash requires a
different type of question. Let me zoom in here. Here's the piece of trash. I only need one sprite, no floor
here because I just want the human to move it up, down, left, right and the human's not going to
physically be able to move it outside of the world. If we zoom in on this code, the way we've solved this
is as follows.
- [1:30:36](https://www.youtube.com/watch?v=undefined&t=5436s) We're using that And conjunction
that we glimpsed earlier because when the green flag is clicked, we're forever asking this question or
really these questions, plural, if the mouse is down and the trash is touching the mouse pointer, that's
equivalent logically to clicking on the trash. Go ahead and move the trash to the mouse pointer.
- [1:30:59](https://www.youtube.com/watch?v=undefined&t=5459s) So again it takes this very familiar
idea that you and I take for granted every day on Macs and PCs of clicking and dragging and dropping.
How is that implemented? Well Mac OS or Windows are probably asking a question. For every icon, is
the mouse down and is the icon touching the mouse? If so, go to the location of the mouse forever while
the mouse button is clicked down.
- [1:31:22](https://www.youtube.com/watch?v=undefined&t=5482s) So how does this work in reality
now? Let me go ahead and click on the Play. Nothing happens at first, but if I click on it, I can move it up,
down, left, right. It doesn't move thereafter. So I now need to kind of combine this idea of dragging with
falling, but I bet I could just start to use just one single program.
- [1:31:40](https://www.youtube.com/watch?v=undefined&t=5500s) Right now I'm using separate ones
to show different ideas, but now that's another bite out of the problem. If we do one last one,
something like the scorekeeping is interesting, because recall that every time we dragged a piece of trash
into the can, Oscar popped out and told us the current score.
- [1:31:55](https://www.youtube.com/watch?v=undefined&t=5515s) So let me go ahead and find this
one, Oscar variables, and let me zoom in on this one. This one is longer because we combined all of
these elements. So this is the kind of thing that if you looked at first glance, like, I have no idea how I
would have implemented this from nothing, from scratch literally.
- [1:32:12](https://www.youtube.com/watch?v=undefined&t=5532s) But again, if you take your vision
and componenitize it into these smaller, bite-sized problems, you could take these baby steps, so to
speak, and then solve everything collectively. So what's new here is this bottom one. Forever do the
following: if the trash is touching Oscar, the other sprite that we've now added to the program, change
the score by 1.
- [1:32:36](https://www.youtube.com/watch?v=undefined&t=5556s) This is an orange and indeed if we
poke around we'll see that orange is a variable, like an x or y but with a better name, changing it means
to add 1 or if it's negative subtract 1. Then go ahead and have the trash go to pick random. What is this
all about? Well, let me show you what it's doing and then we can infer backwards.
- [1:32:57](https://www.youtube.com/watch?v=undefined&t=5577s) Let me go ahead and hit Play. All
right, it's falling, I'm clicking and dragging it, I'm moving it over, and I'm letting go. All right, let me do it
once more. Letting go, let me stop. Why do I have this function at the end called Go To x and y
randomly? Like what problem is this solving here? Yeah, in way back.
- [1:33:18](https://www.youtube.com/watch?v=undefined&t=5598s) AUDIENCE: Just the same track
teleported to the top after you put it in the trash can. DAVID MALAN: Yeah, exactly. Even though the
human perceives this as like a lot of trash falling from the sky, it's actually the same piece of trash, just
kind of being magically moved back to the top as though it's a new one.
- [1:33:34](https://www.youtube.com/watch?v=undefined&t=5614s) There, too, you have this idea of
reusable code. If you were constantly copying and pasting your pieces of trash and creating 20 pieces of
trash, 30 pieces of trash, just because you want the game to have that many levels, probably doing
something wrong. Reuse the code that you wrote, reuse the sprites that you wrote, and that would give
you not just correctness, but also a better design.
- [1:33:55](https://www.youtube.com/watch?v=undefined&t=5635s) Well let's take a look at one final
set of building blocks that we can compose ultimately into something particularly interactive as follows.
Let me go ahead and zoom out here and let me propose that we implement something like some kind of
maze-based game. Let me go ahead here. So I want to implement some maze-based game that looks at
first glance like this.
- [1:34:15](https://www.youtube.com/watch?v=undefined&t=5655s) Let me hit Play. It's not a very fun
game yet, but here's a little Harvard shield, a couple of black lines, this time vertical instead of horizontal,
but notice you can't quite see my hand here, but I'm using my arrow keys to go down, to go up, to go
left, to go right, but if I keep going right, right, right, right, right, right, right it's not going anywhere.
- [1:34:33](https://www.youtube.com/watch?v=undefined&t=5673s) And left, left, left, left, left, left, left,
left, left, left, left, left, left it eventually stops. So before we look at the code, how might this be working?
What kinds of scripts, collections of puzzle pieces, might collectively help us implement this? What do
you think? AUDIENCE: [INAUDIBLE] DAVID MALAN: Perfect, yeah.
- [1:34:58](https://www.youtube.com/watch?v=undefined&t=5698s) There's probably some question
being asked, if touching the black line, and it happens to be a couple of sprites, each of which is just
literally a vertical black line we're probably asking a question like, are you touching it? Is the distance to
it zero or close to zero? And if so, we just ignore the left or the right arrow at that point.
- [1:35:16](https://www.youtube.com/watch?v=undefined&t=5716s) So that works. But otherwise, if
we're not touching a wall, what are we probably doing instead forever here? How is the movement
working presumably? Yeah and back. Oh are you scratching? OK, sure. Let's go on. AUDIENCE:
[INAUDIBLE] DAVID MALAN: Sorry, say a little louder. AUDIENCE: Presumably it's continually looking for
you to hit the arrow keys and then moving when you do.
- [1:35:40](https://www.youtube.com/watch?v=undefined&t=5740s) DAVID MALAN: Exactly. It's
continually, forever listening for the arrow keys up, down, left, right, and if the up arrow is pressed, we're
probably changing the y by a positive value. If the down arrow is pressed, we're going down by y, and left
and right accordingly. So let's actually take a quick look.
- [1:35:56](https://www.youtube.com/watch?v=undefined&t=5756s) If I zoom out here and take a look
at the code that implements this, there's a lot going on at first glance, but let's see. First of all, let me
drag some stuff out of the way because it's kind of overwhelming at first glance, especially if you, for
instance, were poking around online as for problem set 0 just to get inspiration, most projects out there
are going to look overwhelming at first glance until you start to wrap your mind around what's going on.
- [1:36:16](https://www.youtube.com/watch?v=undefined&t=5776s) But in this case, we've
implemented some abstractions from the get go to explain to ourselves and to anyone else looking at
the code what's going on. This is that program with the two black lines and the Harvard shield going up,
down, left, and right. It initially puts the shield in the middle, 0,0, then forever listens for keyboard, as I
think you were describing, and it feels for the walls, as I think you were describing.
- [1:36:40](https://www.youtube.com/watch?v=undefined&t=5800s) Now how is that implemented?
Don't know yet. These are custom blocks we created as abstractions to kind of hide those
implementation details because honestly that's all I need to know right now. But, as aspiring
programmers, if we're curious now, let's scroll down to the actual implementation of listening for
keyboard.
- [1:36:57](https://www.youtube.com/watch?v=undefined&t=5817s) This is the one on the left and it is a
little long, but it's a lot of similar structure. We're doing the following, if the up arrow is pressed, then
change y by 1. Go up. If the down arrow is pressed, then change y by negative 1. Go down. Right arrow,
left arrow, and that's it. So it just assembles all of those ideas, combines it into one new block just
because it's kind of overwhelming, let's just implement it once and tuck it away.
- [1:37:23](https://www.youtube.com/watch?v=undefined&t=5843s) And if we scroll now over to the
Feel for Walls function, this now is asking the question as hypothesized, if I'm touching the left wall,
change my x value by 1, sort of move away from it a little bit. If I'm touching the right wall, then move x
by negative 1 to move a little bit away from it. So it kind of bounces off the wall.
- [1:37:42](https://www.youtube.com/watch?v=undefined&t=5862s) Just in case it slightly went over, we
keep the crest within those two walls. All right, then a couple of more pieces here to introduce. What if
we want to actually add some kind of adversary or opponent to this game? Well, let me go ahead to
maybe this one here where the adversary in this game might, for instance, be designed to be bouncing
to stand in your way.
- [1:38:05](https://www.youtube.com/watch?v=undefined&t=5885s) This is like a maze and you're trying
to get the Harvard shield from the bottom to the top or vice versa. Uh oh, Yale is in the way and it seems
to be automatically bouncing back and forth here. Well, let me ask someone else. Hypothesize. How is
this working? This is an idea you have, this as an idea you see.
- [1:38:21](https://www.youtube.com/watch?v=undefined&t=5901s) Let's reverse engineer in your head
how it works. How might this be working? Yeah, in back. AUDIENCE: If the Yale symbol is touching a right
wall or left wall, then have it bounce. DAVID MALAN: Yeah, so if the Yale symbol is touching the left wall
or the right wall, we somehow have it bounce. And indeed we'll see there's a puzzle piece that can do
exactly that technically off the edge, as we'll see, but there's another way we can do this.
- [1:38:46](https://www.youtube.com/watch?v=undefined&t=5926s) Let's look at the code. The way we
ourselves can implement exactly that idea bounce is just with a little bit of logic. So here's what this
version of the program is doing. It's moving Yale by default to 0,0 just to arbitrarily put it somewhere,
pointing it direction 90 degrees, which means just horizontally, essentially, and then it's forever doing
this: if touching the left wall or touching the right wall, here's our translation of bounce.
- [1:39:10](https://www.youtube.com/watch?v=undefined&t=5950s) We're just turning 180 degrees.
And the nice thing about that is we don't have to worry if we're going from right to left or left to right.
180 degrees is going to work on both of the walls. And that's it. After we do that, we just move one step,
one pixel, at a time but we're doing it forever so something is happening continually and the Yale icon is
bouncing back and forth.
- [1:39:31](https://www.youtube.com/watch?v=undefined&t=5971s) Well one final piece here, what if
now we want another adversary, a more advanced adversary down the road for instance, to go and
follow us wherever we are such that this time we want the other sprite to not just bounce back and
forth, but literally follow us no matter where we go. How might this be implemented on the screen? I bet
it's another forever block, but what's inside? AUDIENCE: So forever get the location of the of the Harvard
shield and move one step towards it.
- [1:40:07](https://www.youtube.com/watch?v=undefined&t=6007s) DAVID MALAN: Yeah, forever point
at the location of the Harvard shield and go one step toward it. This is just going to go on forever if I just
give up, at least in this version. Notice it's sort of twitching back and forth because it goes one pixel then
one pixel then one pixel. It's sort of in a frantic state here.
- [1:40:22](https://www.youtube.com/watch?v=undefined&t=6022s) We haven't finished the game yet,
but if we see inside, we'll see exactly that. It didn't take much to implement this simple idea. Go to a
random position just to make it kind of fair, initially, then forever point towards Harvard, which is what
we called the Harvard crest sprite, move one step.
- [1:40:37](https://www.youtube.com/watch?v=undefined&t=6037s) Suppose we now wanted to make a
more advanced level. What's a minor change I could logically make to this code just to make MIT even
better at this? AUDIENCE: Change the number of steps to two. DAVID MALAN: All right, change the
number of steps to two. So let's try that. So now they got twice as fast.
- [1:40:51](https://www.youtube.com/watch?v=undefined&t=6051s) Let me go ahead and just get this
out of the way. Oops, let me make it a fair fight. Green flag. All right, I unfortunately am still moving one
pixel at a time, so this isn't going to end well. It caught up to me. And if we're really aggressive and do
something like 20 steps at a time, click the green flag.
- [1:41:12](https://www.youtube.com/watch?v=undefined&t=6072s) Jesus, OK, so that's how you might
then make your levels progressively harder and harder. So it's not an accident that we chose these
particular examples here involving these particular schools because we have one more demonstration
we thought we'd introduce today if we could get one other volunteer to come up and play what was
called by one of your predecessors Ivy's Hardest Game.
- [1:41:34](https://www.youtube.com/watch?v=undefined&t=6094s) Let's see, you in the middle. Do you
want to come on up? What's your name? AUDIENCE: Celeste. DAVID MALAN: Say again? AUDIENCE:
Celeste. DAVID MALAN: Come a little closer, actually. Sorry, hard to hear here. All right, round of
applause here if we could, too. [APPLAUSE] OK, sorry, what was your name? AUDIENCE: Celeste.
- [1:41:55](https://www.youtube.com/watch?v=undefined&t=6115s) DAVID MALAN: Ceweste
AUDIENCE: Celeste. DAVID MALAN: Celeste. AUDIENCE: Yes. DAVID MALAN: Come on over. Nice to meet
you, too. So here we have on this other screen Ivy's Hardest Game written by a former CS50 student. I
think you'll see that it combines these same principles. The maze is clearly a little more advanced.
- [1:42:10](https://www.youtube.com/watch?v=undefined&t=6130s) The goal at hand is to initially move
the Harvard crest to the sprite all the way on the right so that you catch up to him in this case, but you'll
see that there's different levels and different levels of sophistication. So if you're up for it, you can use
just these arrow keys up, down, left, right.
- [1:42:25](https://www.youtube.com/watch?v=undefined&t=6145s) You'll be controlling the Harvard
sprite and if we could raise the volume just a little bit, we'll make this our final example. Here we go,
clicking the green flag. [MUSIC PLAYING] Feeling ready? AUDIENCE: Yep. DAVID MALAN: Spacebar.
[MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] MC HAMMER: (SINGING) Can't touch this.
- [1:42:45](https://www.youtube.com/watch?v=undefined&t=6165s) You can't touch this. You can't
touch this. Can't touch this. My, my, my, my music-- DAVID MALAN: Excellent. MC HAMMER: (SINGING)
so hard. Makes me want to say, oh my Lord. Thank you for blessing me-- DAVID MALAN: Two Yales now.
MC HAMMER: (SINGING) Feels good when you know you're down. A super dope homeboy-- AUDIENCE:
Oh! DAVID MALAN: Oh! Keep going.
- [1:43:10](https://www.youtube.com/watch?v=undefined&t=6190s) MC HAMMER: (SINGING) You can't
touch this. I told you, homeboy. Can't touch this. Yeah, that's how living-- DAVID MALAN: All right. MC
HAMMER: (SINGING) Can't touch this. Look at my eyes, man. You can't touch this. You let me bust the
funky lyrics. You can't touch this. Fresh new kicks and pants.
- [1:43:27](https://www.youtube.com/watch?v=undefined&t=6207s) You got it like that and you know
you want to dance. So move out of your seat and get a fly girl and catch this beat. [LAUGHING] Hold on.
Pump a little bit and let them know what's going on like that, like that. Cold on a mission, so fall on back.
Let them know that you're too-- DAVID MALAN: There you go.
- [1:43:42](https://www.youtube.com/watch?v=undefined&t=6222s) There you go. [APPLAUSE] MC
HAMMER: (SINGING) Can't touch this. Why you standing there, man? You can't touch this. Yo, sound the
bell. School's in, sucker. Can't touch this. Give me a song or rhythm, making them sweat that's what give
them. [CHEERING] They know. You talking the Hammer when you're talking about a show.
- [1:44:03](https://www.youtube.com/watch?v=undefined&t=6243s) That's hyped and tight. Singers are
sweating so them a wipe or a tame to learn. DAVID MALAN: Second to last level. Oh! MC HAMMER:
(SINGING) That chart's legit. Either work hard or you might as well quit. That word because you know--
DAVID MALAN: Oh! Keep going, keep going! Yes! MC HAMMER: (SINGING) You can't touch this.
- [1:44:20](https://www.youtube.com/watch?v=undefined&t=6260s) DAVID MALAN: You're almost
there. MC HAMMER: (SINGING) Break it down. DAVID MALAN: There you go. Go, go, go! Oh. One more.
Yes! [CHEERING] There you go. MC HAMMER: (SINGING) Stop, Hammer time. "Go with the flow," it is
said. If you can't groove to this, then you're probably dead. So wave your hands in the air, bust a few
moves, run your fingers through your hair.
- [1:44:45](https://www.youtube.com/watch?v=undefined&t=6285s) This is it. For a winner. Dance to
this and you're going to get thinner. Now move, slide your rump. Just for a minute let's all do the bump.
[CHEERING] DAVID MALAN: Yes! [APPLAUSE] Congratulations. All right, that's it for CS50. Welcome to the
class. We'll see you next time. [MUSIC PLAYING] DAVID J. MALAN: All right.
- [1:46:28](https://www.youtube.com/watch?v=undefined&t=6388s) So this is CS50. And this is week 1,
the one in which you learn a new language, which is something we technically said last week, at least if
you had never played with this graphical language known as Scratch before, which itself was a
programming language. But today, as promised, we transition to something a little more traditional, a
little more text-based, not puzzle piece- or block-based, known as C.
- [1:46:49](https://www.youtube.com/watch?v=undefined&t=6409s) This is an older language. It's been
around for decades. But it's a language that underlies so many of today's more modern languages,
among them something called Python that we'll also come to in a few weeks' time. Indeed, at the end of
the semester, the goal is for you to feel that you've not learned Scratch, you've not learned C, or even
Python, for that matter, but fundamentally that you've learned how to program.
- [1:47:07](https://www.youtube.com/watch?v=undefined&t=6427s) Unfortunately, when you learn how
to program with a more traditional language like this, there's just so much distraction. Last week I
described all of the syntax, all of the weird punctuation that you see in this, like the hash symbol, these
angled brackets, parentheses, curly braces, backslash n, and more.
- [1:47:23](https://www.youtube.com/watch?v=undefined&t=6443s) Well, today we're not going to
reveal what all of those little particulars mean. But by next week, will this no longer look like the
proverbial Greek to you, a language that, presumably, you've never actually seen or typed before. But to
do that, we'll explore some of the very same topics as last week.
- [1:47:40](https://www.youtube.com/watch?v=undefined&t=6460s) So recall that, via Scratch-- and
presumably via problem set 1-- we took a look at things called functions that are actions or verbs. And
related to functions were arguments like inputs. And related to some functions were returned values like
outputs. Then we talked a bit about conditionals, forks in the road, so to speak, Boolean expressions,
which are just yes/no questions or true/false questions, loops, which let you do things again and again,
variables, like in math, that let you store values temporarily,
- [1:48:06](https://www.youtube.com/watch?v=undefined&t=6486s) and then even other topics still. So
if you were comfortable on the heels of problem set 0 and last week, realize that all of these topics are
going to remain with us. So really, today is just about acquiring all the more of a mental model for how
you translate those ideas into, presumably, a very cryptic new syntax-- a new syntax, frankly, that's
actually more simple in some ways than your own human language, be it English or something else,
because there's far fewer vocabulary words.
- [1:48:31](https://www.youtube.com/watch?v=undefined&t=6511s) There's actually far less syntax that
you might have in, say, a typical human language. But you need to be with these computer languages all
the more precise so that you're most, ultimately, correct, and ultimately will see to your code is
successful along a few other lines as well. So if you think about the last time you kind of wandered
around not really knowing what you were doing or encountered something new-- might not have been
that long ago, entering Harvard Yard for the very first time, or Old Campus or the like, be it in Cambridge
or New Haven--
- [1:48:58](https://www.youtube.com/watch?v=undefined&t=6538s) you didn't really need to know how
to do everything as a first year. You didn't need to know who everyone was, where everything was, how
Harvard or Yale, or anything else for that matter, worked. You sort of got by day to day by just focusing
on those things that matter. And anything you didn't really understand, you sort of turned a blind eye to
until it's important.
- [1:49:15](https://www.youtube.com/watch?v=undefined&t=6555s) And that's, indeed, what we're
going to do today. And really, for the next several weeks, we'll focus on details that are initially important
and try to wave our hands, so to speak, at details that, yeah, eventually we'll get to, might be interesting.
But for now, they might be distractions.
- [1:49:27](https://www.youtube.com/watch?v=undefined&t=6567s) And by distractions, I really mean
some of that syntax to which I alluded earlier. So by the end of today-- and really, by the end of problem
set 1, your first foray, presumably, into this language called C-- you'll have written some code. And you'll
be asking yourself-- we'll be asking yourselves-- just how good is that code? Well, first and foremost, per
last week, be it in Scratch or phone book form, code ultimately needs to be correct, to be well done.
- [1:49:52](https://www.youtube.com/watch?v=undefined&t=6592s) You want the problem to be solved
correctly. So that one sort of goes without saying. And along the way this term, we'll provide you with
tools and techniques so you don't have to just sit there sort of endlessly trying an input, checking the
output, trying another input, checking the output. There's a lot of automation tools in the real world--
and in this class and others like it-- that will help facilitate you answering that question for yourself, is my
code correct, according to our specifications or the like.
- [1:50:16](https://www.youtube.com/watch?v=undefined&t=6616s) But then something that's going to
take more time and you're probably not going to feel 100% comfortable with the first week, the first
weeks, is just how well designed your code is. It's one thing to speak English or write English, but it's
another thing-- or any language, for that matter.
- [1:50:30](https://www.youtube.com/watch?v=undefined&t=6630s) But it's another thing to speak it or
write it well. And we spend all these years in middle school, high school, presumably, writing papers and
other documents, getting grades and feedback on them as to how well formulated your arguments were,
how well structured your paper was, and the like. And there's that same idea in programming.
- [1:50:45](https://www.youtube.com/watch?v=undefined&t=6645s) It doesn't matter necessarily that
you've just solved a problem correctly. If your code is a complete visual mess, or if it's crazy long, it's
going to be really hard for someone else to wrap their mind around what your code is doing and, indeed,
to be confident if it is correct. And honestly, you-- the next morning, the next year, the next time you
look at that code-- might have no idea what you yourself were even thinking.
- [1:51:09](https://www.youtube.com/watch?v=undefined&t=6669s) But you will if you focus, too, on
designing good code, getting your algorithms efficient, getting your code nice and clean, and even
making sure your code looks pretty, which we'd describe as a matter of style. So in the written human
world, having punctuation in the right place, capitalization and the like-- the sort of way you write an
essay but not necessarily send a text message-- relates to style, for instance.
- [1:51:31](https://www.youtube.com/watch?v=undefined&t=6691s) And so good style in code is going
to have a few of these characteristics that are pretty easily taught and remembered. But you just have to
start to get in the habit of writing code in a certain way. So these three axes, so to speak, correctness,
design, and style, are really the overarching goals when writing code that ultimately is going to look like
this.
- [1:51:50](https://www.youtube.com/watch?v=undefined&t=6710s) So this program we conjectured
last week does what if you run it on a Mac or PC or somewhere else, presumably? What does it do?
Yeah? AUDIENCE: [INAUDIBLE]. DAVID J. MALAN: It just prints, Hello, world. And honestly, that's kind of
atrocious that you need to hit your keyboard keys this many times with this cryptic syntax just to get a
program to say, Hello, world.
- [1:52:10](https://www.youtube.com/watch?v=undefined&t=6730s) So a spoiler-- in a few weeks' time
when we introduce other, more modern languages, like Python, you can distill this same logic into
literally one line of code. And so we're getting there, ultimately. But it's helpful to understand what it is
that's going on here, because even though this is a pretty cryptic syntax, there's nothing after this week
and, really, next week that you shouldn't be able to understand even about something that right now
looks a little something like this.
- [1:52:32](https://www.youtube.com/watch?v=undefined&t=6752s) So how do you write code? Well,
I've given us sort of the answer to a problem. How do you print, Hello, world, on the screen? So what do
I do with this code? Well, we're in the habit of typically writing things with, like, Microsoft Word or
Google documents. And yeah, I could open up Word or Google Docs or Pages or the like and just literally
transcribe that character for character, save it, and boom, I've got a program.
- [1:52:53](https://www.youtube.com/watch?v=undefined&t=6773s) But the problem, per last week, is
that computers only understand or speak what other language, so to speak? AUDIENCE: [INAUDIBLE].
DAVID J. MALAN: Yeah, so binary, zeros and ones. And so this, obviously, is not zeros and ones. So it
doesn't matter if I put it in a Word doc, Google Doc, Pages file, or the like.
- [1:53:08](https://www.youtube.com/watch?v=undefined&t=6788s) The computer is not going to
understand it until I somehow translate it to zeros and ones. And honestly, none of those tools that I
rattled off are really appropriate for programming. Why? Well, they come with features like bold facing
and italics and sort of fluffy, aesthetic stuff that has no functional impact on what you're trying to do
with your code.
- [1:53:25](https://www.youtube.com/watch?v=undefined&t=6805s) And they don't have the ability, it
would seem, to convert that code ultimately to zeros and ones. But tools that do have this capability
might be called Integrated Development Environments, or IDEs, or, more simply, text editors. A text
editor is a tool that a programmer uses perhaps every day to write their code.
- [1:53:44](https://www.youtube.com/watch?v=undefined&t=6824s) And it's a simple program-- here,
for instance, a very popular one called Visual Studio Code, or VS Code. And at the top here, you see that
I've actually created in advance before class a very simple empty file called "hello.c." Why? Well, .
- [1:54:01](https://www.youtube.com/watch?v=undefined&t=6841s) c indicates by convention that this
is going to be a file in which there is C code. It's not .docx, which would mean in this file is a Microsoft
Word document, or .pages is a Pages file. This is .c, which means in this file is going to be text in the
language called C. This number 1 here is just an automatic line number that's going help me keep track
of how long or short this program is.
- [1:54:18](https://www.youtube.com/watch?v=undefined&t=6858s) And the cursor is just blinking
there, waiting for me to start typing some code. Well, let me go ahead and type out exactly the same
code. For me, it comes pretty comfortably from memory. So I'm going to go ahead and include
something called standardio.h-- more on that later. I'm going to magically type int main(void), whatever
that means-- we'll come back to that later-- one of these curly braces and then a sibling there that closes
the same.
- [1:54:44](https://www.youtube.com/watch?v=undefined&t=6884s) Then I'm going to hit Tab to indent
a few spaces. And then I'm going to type not print, but printf, then "Hello, world," /n, close quote, close
parenthesis, semicolon. And I dare say this was essentially the very first program I wrote some 25 years
ago. I wrote it to say, "Hi, CS50.
- [1:55:03](https://www.youtube.com/watch?v=undefined&t=6903s) " Now it just says the more
canonical, conventional, "Hello, world." But that's it. That's my very first program. And all I need to now
do is maybe hit Command-S or Control-S to save the file. And voila, I am a programmer. The catch
though, is, OK, how do I run this? Like, on your Mac or PC, how do you run a program? Well, usually
double-click an icon.
- [1:55:21](https://www.youtube.com/watch?v=undefined&t=6921s) On your phone, you tap an icon. In
this environment that we're using and that many programmers-- dare say most programmers-- use, you
don't have immediately a nice, pretty icon to double-click on. That's very user friendly, but it's not very
necessary. Especially when you get more comfortable with programming, you're going to want to type
commands because it's just faster than pointing and clicking a mouse.
- [1:55:42](https://www.youtube.com/watch?v=undefined&t=6942s) And you're going to want to
automate things, which is a lot easier if it's all command or text-based, as opposed to mouse and
muscular movements. And so here I have my program. It lives in this file called "hello.c." I need to now
convert it, though, to zeros and ones. Well, how do I go about doing this, and how am I going to get from
this so-called code-- or source code, as it's conventionally called-- to this, these zeros and ones that we'll
now start calling machine code.
- [1:56:11](https://www.youtube.com/watch?v=undefined&t=6971s) The zeros and ones from last week
can be used not only to represent numbers and letters, colors, audio, video, and more. It can also
represent instructions to a computer, like print, or play a sound, or delete a file, or save a file. All the sort
of basics of a computer somehow can be represented by other patterns of zeros and ones.
- [1:56:32](https://www.youtube.com/watch?v=undefined&t=6992s) And just like last week, it depends
on the context in which these numbers are stored. Sometimes they're interpreted as numbers, like in a
spreadsheet. Sometimes they're interpreted as colors. Sometimes they're interpreted as instructions,
commands to your computer to do very low-level operations, like print something on the screen.
- [1:56:51](https://www.youtube.com/watch?v=undefined&t=7011s) So fortunately, last week's
definition of computer science of problem solving is a nice mental model for exactly the goal at hand. I
have some input, AKA source code. I want to output ultimately machine code, those zeros and ones. I
certainly don't want to do this kind of process by hand. So hopefully there's an algorithm implemented
by some special program that does exactly that.
- [1:57:13](https://www.youtube.com/watch?v=undefined&t=7033s) And those of you who do have
some prior experience, this program might be called a? A compiler. So a few of you have, indeed,
programmed before. Not all languages use compilers. C, in fact, is a language that does use a compiler.
And so I just need to find myself-- on my computer somewhere, presumably-- a so-called compiler, a
program whose purpose in life is to convert one language to another.
- [1:57:36](https://www.youtube.com/watch?v=undefined&t=7056s) And source code written textually
in C, like we saw a moment ago, is source code. The machine code is the corresponding zeros and ones.
So let me go back to the same programming environment called Visual Studio Code or VS Code. This is
typically a program you or any programmer on the internet can download onto their own Mac or PC and
be on their way with whatever computer you own writing some code.
- [1:58:00](https://www.youtube.com/watch?v=undefined&t=7080s) A downside, though, of that
approach is that all of us have slightly different versions of Macs or PCs. We have slightly different
versions of operating systems. They may or may not be up to date. It's just a technical support nightmare
to create a uniform environment, especially for an introductory class, where everyone should ideally be
on the same page so we can get you up and running quickly.
- [1:58:19](https://www.youtube.com/watch?v=undefined&t=7099s) And so I'm actually using a cloud-
based version of VS Code, something that you only need a browser to access. And then you can be on
any computer, today or tomorrow. By the end of the semester, we're going to get you out of the cloud,
so to speak, as best we can and get you onto your own Mac or PC, so that after this class, especially if it's
the only CS class you ever take, you feel like you can continue programming in any number of languages,
even with CS50 behind you.
- [1:58:45](https://www.youtube.com/watch?v=undefined&t=7125s) But for now, wonderfully, the
browser version of VS Code should pretty much be identical to what the eventual downloadable version
of the same would be. And you'll see in problem set 1 how to access this and how to get going yourself
with your first programs. But I haven't mentioned this bottom part of the screen, this bottom part of the
screen.
- [1:59:03](https://www.youtube.com/watch?v=undefined&t=7143s) And this is an area where we have
what's called a terminal window. So this is sort of old-school technology that allows you, with a
keyboard, to interact with a computer, wherever it may be-- on your lap, in your pocket, or even, in this
case, in the cloud. So on the top-hand portion of this screen is my text editor, like tabbed windows, like
in many programs, where I can just create files and write code.
- [1:59:27](https://www.youtube.com/watch?v=undefined&t=7167s) The bottom of the screen here, my
so-called terminal window, gives me the ability to run commands on a server that currently I have
exclusive access to. So because I logged into VS Code with my account online, I have my own sort of
virtual server, if you will, in the cloud-- otherwise known as, in this context, a container.
- [1:59:47](https://www.youtube.com/watch?v=undefined&t=7187s) This has its own operating system
for me, its own hard drive, if you will, where I can save and create files of my own, separate from yours
and vice versa. And it's at this very simple prompt, which is conventionally-- but not always-- abbreviated
by a dollar sign, has nothing to do with currency. It just means, type your commands here.
- [2:00:04](https://www.youtube.com/watch?v=undefined&t=7204s) This is where I'm going to be able
to type commands, like compile my source code into machine code. So it's a Command Line Interface, or
CLI, on top of an operating system that you might not have ever used or seen, but it's very popular, called
Linux. Odds are almost all of us in this room are using Mac OS or Windows right now, but we're all going
to start using an operating system called Linux, which is in a family of operating systems that offer not
only this command line interface, but are used not just for programming, but for serving
- [2:00:33](https://www.youtube.com/watch?v=undefined&t=7233s) websites and developing
applications and the like. And it's, indeed, a familiar and very powerful interface, as we'll see. So how do
I go about making this file, hello.c, into a program? There's no icon to double-click, but there is a
command. I can type, make hello, at this dollar sign prompt, go ahead and hit Enter, and nothing appears
to happen.
- [2:00:56](https://www.youtube.com/watch?v=undefined&t=7256s) But that's a good thing. And as
we'll see in programming, almost always, if you don't see anything go wrong, that means everything
went right. So this is going to be a rarity at first, but this is a good thing that it just seems to do nothing.
But now there is in the folder in my accounts in this on the cloud a file called "hello.
- [2:01:15](https://www.youtube.com/watch?v=undefined&t=7275s) " And it's a bit of a weird
command, but you'll get familiar with it before long. . just means go into my current folder. /hello means
run the program called "hello" in this current folder. So ./hello, and then Enter, and voila, now I'm
actually not just programming, but running my actual code.
- [2:01:36](https://www.youtube.com/watch?v=undefined&t=7296s) So what have I just done? Let me
go ahead and do this. I'm going to go ahead and open up the sidebar of this program, and you'll see in
problem set 1 how to do this. And this might look a little different based on your own configuration. Even
the color scheme I'm using might ultimately look different from yours, because it supports a nice colorful
theme.
- [2:01:53](https://www.youtube.com/watch?v=undefined&t=7313s) So you can have different colors
and brightnesses depending on your mood or the time of day. What I've opened here, though, is what is
called in VS Code Explorer, and this is just all of the files in my cloud account. And there's not many right
now. There's only two. One is the file called hello.
- [2:02:09](https://www.youtube.com/watch?v=undefined&t=7329s) c, and it's highlighted because I've
got it open right there. And the other is a file called "hello," which is brand new and was created when I
ran that command. And what's now worth noting is that now things are getting a little more like Mac OS
and Windows. Like on the left-hand side, you have a GUI, a Graphical User Interface.
- [2:02:26](https://www.youtube.com/watch?v=undefined&t=7346s) But on the bottom here, again, you
have a CLI, Command Line Interface. These are just different ways to interact with computers, and you'll
get comfortable with both. And honestly, you're certainly familiar and comfortable with GUIs already, so
it's the command line one with which we'll spend some time.
- [2:02:41](https://www.youtube.com/watch?v=undefined&t=7361s) Now suppose that I just wanted to
do something more than compile this program. Suppose I wanted to go ahead and remove it. Like, uh-
uh, no, I made a mistake. I want to say, "Hello, CS50," not "Hello, world." I could just hover up here, like
in any software, and I could right-click, and I could poke around, and there, delete permanently.
- [2:02:58](https://www.youtube.com/watch?v=undefined&t=7378s) So most of us might have that
instinct on a Mac or PC. You right-click or Control-click, and you poke around. But in a command line
interface, let me do this instead. The command for removing or deleting a file in the world of Linux, this
other operating system, is just a type rm for remove, and then "hello," Enter.
- [2:03:16](https://www.youtube.com/watch?v=undefined&t=7396s) It's a somewhat cryptic
confirmation message, but this just means, are you sure? I'm going to go ahead and type Y for Yes. And
now when I hit Enter, watch what happens at top left in the Explorer, the GUI, the graphical interface.
Voila, it disappears. Not terribly exciting, but this just means this is a graphical version of what we're
seeing here.
- [2:03:36](https://www.youtube.com/watch?v=undefined&t=7416s) And in fact, if you want to never
use the GUI again-- I'll go ahead and close it with a keyboard shortcut here-- you can forever just type ls
for list and hit Enter. And you will see in the command line interface all of the files in your current folder.
So anything you can do with a mouse, you can do with this command line interface.
- [2:03:54](https://www.youtube.com/watch?v=undefined&t=7434s) And indeed, we'll see many more
things that you can do as well. But the inventors of this, this operating system and its predecessors, were
very succinct. Like, the command is rm for remove. The command is ls for list. It's very terse. Why?
Because it's just faster to type. So before we forge ahead with making something more interesting than
just "Hello, world," let me pause here to see if there's questions on source code or machine code or
compiler or this command line interface.
- [2:04:24](https://www.youtube.com/watch?v=undefined&t=7464s) Yeah? AUDIENCE: [INAUDIBLE].
DAVID J. MALAN: Really good question, and let me recap. If I were to make changes to the program, run
it, and then maybe make other changes and try to rerun it, would those changes be reflected, even
though I've reworded slightly. Well, let's do this. I already removed the old version.
- [2:04:40](https://www.youtube.com/watch?v=undefined&t=7480s) So let me go ahead and point out
that if I do ./hello now, I'm going to see some kind of error because I just deleted the file. No such file or
directory, so it's not terribly user friendly, but it's saying what the problem is. Let me go ahead and
remake it by typing make hello. Now if I type ls, I'll see not one but two files again, and one of them is
even green with a little asterisk to indicate that it's executable.
- [2:05:04](https://www.youtube.com/watch?v=undefined&t=7504s) It's sort of the textual version of
something you could double-click in our human world. So now, of course, if I run hello, we're back where
I started, "Hello, world." But now suppose I change it to "Hello, CS50," like I did years ago. Let me go
ahead and save the file with Command-S or Control-S.
- [2:05:21](https://www.youtube.com/watch?v=undefined&t=7521s) Down here now, let me run ./hello
again, and voila. Huh. So let me ask someone else to answer that question. What's the missing step?
Why did it not say, "Hello, CS50." Yeah? AUDIENCE: [INAUDIBLE]. DAVID J. MALAN: Yeah, so I didn't
compile it again. So sort of newbie mistake, you're going to make this mistake and many others before
long.
- [2:05:39](https://www.youtube.com/watch?v=undefined&t=7539s) But now let me go ahead and
remake hello, enter. It's going to seemingly make the same program. But this time when I run it, it's,
"Hello, CS50." Any other questions on some of these building blocks? And we'll come back to all the
crazy syntax I typed before long. But for now, we're focusing on just the output.
- [2:05:59](https://www.youtube.com/watch?v=undefined&t=7559s) Yeah? AUDIENCE: [INAUDIBLE].
DAVID J. MALAN: When I keep running make, it creates a new version of the machine code. So it keeps
changing the hello program and the hello file, and that's it. There's no make file, per se. AUDIENCE:
[INAUDIBLE]. DAVID J. MALAN: Good question, no. If I open up that directory, you'll see that there's just
the one.
- [2:06:18](https://www.youtube.com/watch?v=undefined&t=7578s) And it doesn't matter how many
times I run make hello-- three, four, five-- it just keeps overwriting the original. So it's kind of like just
saving in the world of Google Docs or Microsoft Word or the like. But there's an additional step today.
We have to then convert my words to the computer's, the zeros and ones.
- [2:06:36](https://www.youtube.com/watch?v=undefined&t=7596s) Yeah, in front. AUDIENCE:
[INAUDIBLE]. DAVID J. MALAN: Oh, what happens if I run hello.c? So let me go ahead and do ./hello.c,
which is a mistake you'll invariably make early on. Permission denied. So what does that mean? This is
where the error messages mean something to the people who designed the operating system, but it's a
little cryptic.
- [2:06:54](https://www.youtube.com/watch?v=undefined&t=7614s) It's not that you don't have access
to the file. It means that it's not executable. This is not something you have permission to run, but you
do have permission to read or write it-- that is, change it. AUDIENCE: [INAUDIBLE]. DAVID J. MALAN: Oh,
really good question. So if I have named my file, hello dot C, or more generally something dot C, of the
things that Make does is it automatically picks the file name for me.
- [2:07:17](https://www.youtube.com/watch?v=undefined&t=7637s) And we'll discuss a bit-- we'll
discuss this a bit more next week. Make itself-- is kind of the first of white lies today-- itself is not a
compiler. It's a program that knows how to find and use the compiler on the system and automatically
create the program. If I use, as we'll discuss next week, the actual compiler myself, I have to type a much
longer sequence of commands to specify explicitly what do I want the name of my program to be.
- [2:07:43](https://www.youtube.com/watch?v=undefined&t=7663s) Make is a nice program, especially
in week 1, because it just automates all of that for us. And so here, we have now a program that very
simply prints something on the screen. So let's not put this into the context of where we left off last time
in the context of Scratch and inputs and outputs. So we discuss the last time, of course, functions and
arguments.
- [2:08:02](https://www.youtube.com/watch?v=undefined&t=7682s) Functions, again, are those actions
and verbs like say, or ask, or the like. And the arguments were the inputs to those functions, generally in
those little white ovals that, in Scratch, you could type words or numbers into. We'll see, in all of the
languages we're going to see this term, have that same capability.
- [2:08:18](https://www.youtube.com/watch?v=undefined&t=7698s) And let's just start to translate one
of these things to another. So for instance, let's put this same program in C, in the context of Scratch.
This is what Hello, World looked like last week in the form of one function. This week, of course, it looks
like print. And then the parentheses, notice, are kind of deliberately designed in the world of Scratch to
resemble that same shape.
- [2:08:39](https://www.youtube.com/watch?v=undefined&t=7719s) Even though this is a white oval,
you kind of get that it's kind of evoking that same idea with the parentheses. Technically the function in
C, it's not called say. It's not even called print. It's called printf. The F stands for formatted, but we'll see
what that means in a moment. But printf is the closest analogous function for say in the world of C.
- [2:09:01](https://www.youtube.com/watch?v=undefined&t=7741s) Notice if, though, you want to print
something like Hello, World or Hello CS50 in C, you don't just write the words as we did last week. You
also had an add what, if you notice already what's missing from this version. Yeah, so the double quotes
on the left and the right. So, that's necessary in C whenever you have a string of words.
- [2:09:21](https://www.youtube.com/watch?v=undefined&t=7761s) And I'm using that word
deliberately. Whenever you have multiple words like this, this is known as a string as we'll see. And you
have to put it in double quotes, not single quotes. You have to put it in double quotes. There's one other
stupid thing that we need to have in my C code in order to get this function to do something ultimately,
which is what? Semicolon.
- [2:09:42](https://www.youtube.com/watch?v=undefined&t=7782s) So just like in our human world,
you eventually got into the habit of using, at least in formal writing, periods. Semicolon is generally what
you use to finish your thought in the world of programming with C. All right, so we have that function in
place. Now, what does this really fit into in terms of the mental model? Well, functions take arguments.
- [2:10:01](https://www.youtube.com/watch?v=undefined&t=7801s) And it turns out functions can have
different types of outputs. And we've actually seen both already last week. One type of output from a
function can be something called a side effect. And it generally refers to something visual, like something
appearing on the screen or a sound playing from your computer.
- [2:10:18](https://www.youtube.com/watch?v=undefined&t=7818s) It's sort of a side effect of the
function doing its thing. And indeed, last week we saw this in the context of passing in something like
Hello, World as input to the say function. And we saw on the screen Hello, World, but it was kind of a
one off. It's one and done. You can't actually do anything with that visual output other than consume it,
visually, with your human eyes.
- [2:10:39](https://www.youtube.com/watch?v=undefined&t=7839s) But sometimes, recall last week,
we had functions like the ask block that actually returned me some value. Remember the ask, what's
your name. It handed me back whatever answer the human typed in. It didn't just arbitrarily display it on
the screen. The cat didn't necessarily say it on the screen.
- [2:10:55](https://www.youtube.com/watch?v=undefined&t=7855s) It was stored, instead, in that
special variable that was called answer. Because some functions have not side effects but return values.
They hand you back an output that you can use and reuse, unlike the side effect, which, again displays
and that's it. You can't sort of catch it and hold on to it.
- [2:11:14](https://www.youtube.com/watch?v=undefined&t=7874s) So, in the context of last week, we
had the ask block. And that had this special answer return value. In C, we're going to see in just a
moment, we could translate this as follows. The closest match I can propose for the ask block is a
function that we're going to start calling get string. String is, again, a word, a set of words, like a phrase
or a sentence in programming.
- [2:11:37](https://www.youtube.com/watch?v=undefined&t=7897s) It, too, is a function insofar as it
takes input and pretty much-- this isn't always true-- but very often when you have a word in C followed
by an open parenthesis and a closed parenthesis, it's most likely the name of a function. And we're going
to see that there's some exceptions to that.
- [2:11:53](https://www.youtube.com/watch?v=undefined&t=7913s) But for now this indeed looks like a
function because it matches that pattern. If I want to ask the question, what's your name, question
mark-- and I'm even going to deliberately put a space there just to kind of move the cursor a little bit
over so that the human isn't typing literally after the question mark.
- [2:12:08](https://www.youtube.com/watch?v=undefined&t=7928s) So that's just the nitpicky aesthetic.
This is perhaps the closest analog to just asking that question. But because the ask block returns a value,
the analog here forget string is that it, too, returns a value. It doesn't just print the human's input. It han
- [2:12:28](https://www.youtube.com/watch?v=undefined&t=7948s) ds it back to you in the form of a
variable, a.k.a. return value, that I can then use and reuse. Now ideally it would be as simple as this
literally saying answer on the left equals. And this is where things start to diverge from math and sort of
our human world. This equal sign, henceforth, is not the equal sign. It is the assignment operator. To
assign a value means to store a value in some variable.
- [2:12:50](https://www.youtube.com/watch?v=undefined&t=7970s) And you read these things, weirdly,
right to left. So here is a function called get string. I claim that it's going to return to you whatever the
human types in as their name. It's going to get stored over here on the left because of this so-called
assignment operator, that yes is an equal sign. But it doesn't mean equality in this context.
- [2:13:09](https://www.youtube.com/watch?v=undefined&t=7989s) It makes things equal. But it does
so by copying the value on the right into the thing on the left. Unfortunately, we're not quite done yet
with C. And this is where, again, it gets a little annoying at first where Scratch just let us express our ideas
without so much syntax. In C when you have a variable you don't just give it a name like you did in
Scratch.
- [2:13:29](https://www.youtube.com/watch?v=undefined&t=8009s) You also have to tell the computer
in advance what type of value it is storing. String is one such type of value. Int, for integer, is going to be
another. And there's even more than that we'll see today and beyond. And this is partly an answer to the
question that came up one or more times last week, which was how does a computer distinguish this
pattern of zeros and ones from this.
- [2:13:51](https://www.youtube.com/watch?v=undefined&t=8031s) Like is this a letter, a number, a
color, a piece of video. And I just claimed last week that it totally depends on the program. It depends on
the context. And that's true. But within those programs, it often depends on what the human
programmer said the type of the value is. If this specifies that the string, which means interpret the
following zeros and ones that are stored in my program as words or letters, more generally.
- [2:14:17](https://www.youtube.com/watch?v=undefined&t=8057s) If it's an int for integer, it would be
implying, by the programmer, treat the following zeros and ones in my program as a number, an integer,
not a string. So here's where this week, unlike with Scratch, which is kind of figures out what you mean,
with C in a lot of languages you have to be this pedantic and tell it what you mean.
- [2:14:36](https://www.youtube.com/watch?v=undefined&t=8076s) There's still one stupid thing
missing from my code here. What's still missing here? Yeah. AUDIENCE: [INAUDIBLE] DAVID J. MALAN:
And we still need the stupid semicolon. And I'm sort of impugning it here. Because honestly, these are
the kinds of stupid mistakes you're going to make today, tomorrow, this weekend, next week, a few
weeks from now, until you start to notice this and recognize it as well as you do English or whatever your
spoken language is.
- [2:14:58](https://www.youtube.com/watch?v=undefined&t=8098s) Yeah, question. Good question.
Suppose I mix apples and oranges, so to speak, and I try to put a string in an int or an int in a string, the
compiler is going to complain. So when I run that make command as I did earlier, it's not going to be nice
and blissfully quiet and just give me another prompt. It's going to yell at me with honestly a very cryptic
looking error message until we get the muscle memory for reading it.
- [2:15:20](https://www.youtube.com/watch?v=undefined&t=8120s) Other questions. Ah, what
happened to the backslash n. So, we'll come back to that in just a moment, if we may. Because I have
deliberately omitted it here but we did have it earlier. And we'll see the different behavior in a sec. Other
questions. Yeah, not at all nitpicky. These are the kinds of things that just matter.
- [2:15:40](https://www.youtube.com/watch?v=undefined&t=8140s) And it's going to take time to
recognize and develop this muscle memory. Everything I've typed here except, for the W at the moment,
is lowercase. And the W is capitalized just because it's English. Everything else is lowercase. And this kind
of varies by language and also context. So, in many languages the convention is to use all lowercase
letters for your variable names.
- [2:16:00](https://www.youtube.com/watch?v=undefined&t=8160s) Other languages might use some
capitals, as well. But we'll talk about that before long. But this is the kind of thing that matters and is
hard to see at first, especially when a little S doesn't look that different when it's on your tiny laptop
screen from a capital S. But you'll start to develop these instincts.
- [2:16:16](https://www.youtube.com/watch?v=undefined&t=8176s) All right, so besides this particular
block, let's go ahead and consider how we can go about implementing this now in code. So let me switch
back to VS Code here. This was the program I had earlier. And let me go ahead and undo my CS50
change. And this time just rerun it. Rerun Make on Hello with the original version with the backslash n.
- [2:16:35](https://www.youtube.com/watch?v=undefined&t=8195s) Enter, nothing bad seems to have
happened. So dot slash Hello, enter Hello, World. Now, if you're curious, this is a good instinct to start to
acquire what happens if I get rid of this. Well, I'm probably not going to break things too badly. So let's
try. Let me go ahead now and do Make Hello. Still compile.
- [2:16:52](https://www.youtube.com/watch?v=undefined&t=8212s) So it's not a really bad mistake. So
let me go ahead and run dot slash Hello. What's the difference here? Yeah, what do you see that's
different? Yeah, the dollar sign, my so-called prompt, stayed on the same line. Why? Well, we can
presumably infer now that the backslash n is some fancy notation for saying create a new line, move the
cursor, so to speak, to the next line.
- [2:17:15](https://www.youtube.com/watch?v=undefined&t=8235s) Notice that the cursor will move to
the next line in my terminal window. If I keep hitting it, it just automatically, by nature of hitting enter,
does it. But it'd be kind of stupid if when you run a program in this world, simple as it is, if the next
command is now weirdly spaced in the middle of the terminal with the dollar sign, it just looks sloppy.
- [2:17:33](https://www.youtube.com/watch?v=undefined&t=8253s) It's really just an aesthetic
argument. And notice that it's not acceptable or correct to do this, to hit enter there. Let me go ahead
and save that, though, and see what happens. Let me go ahead now and run Make Hello enter. Oh my
god, like four errors. This is like, what, 10 lines of errors for a one line program.
- [2:17:53](https://www.youtube.com/watch?v=undefined&t=8273s) And this is where, again, you'll start
to develop the instincts for just reading this stuff. These kinds of tools, like the compiler tool we're using,
were not designed necessarily with user friendliness in mind. That's changed over the decades, but
certainly early on it's really just meant to be correct and precise with its errors.
- [2:18:10](https://www.youtube.com/watch?v=undefined&t=8290s) So what did I do here? Missing
terminating close quote character, long story short, when you have a string in C, your double quotes just
have to be on the same line just because. Now, there's the slight white lie. There's ways around this. But
the best way around it is to use this so-called escape sequence.
- [2:18:30](https://www.youtube.com/watch?v=undefined&t=8310s) To escape something means
generally to put a backslash, and then a special symbol like n for new line. And this is just the agreed
upon way that humans, decades ago, decided, OK you don't just hit your enter key. You instead put
backslash n and that tells the computer to move the cursor to the new line.
- [2:18:49](https://www.youtube.com/watch?v=undefined&t=8329s) So again, kind of cryptic. But once
you know it, that's it. It's just another word in our vocabulary. So now let me transition to making my
program a little more interactive. Instead of just saying Hello, world, let me change it like last week to say
Hello, David, or whoever is interacting with the program.
- [2:19:04](https://www.youtube.com/watch?v=undefined&t=8344s) So I'm going to do string answer
gets, get string, quote unquote, what's your name. I'm not going to bother with a new line here. I could.
This is now just a judgment call. I deliberately want the human to type their name on the same line just
because. And how do I now print this? Well last week recall we used say.
- [2:19:23](https://www.youtube.com/watch?v=undefined&t=8363s) And then we use the other block
called join. So the idea here is the same. But the syntax this week is going to be a little different. It's
going to be printf, which prints something on the screen. I'm going to go ahead and say Hello comma.
And let me just go with this initially with the backslash n, semicolon.
- [2:19:43](https://www.youtube.com/watch?v=undefined&t=8383s) Let me go ahead and recompile my
code. Whoops, damn doesn't work still. And look at all these errors. There's more errors than code I
wrote. But what's going on here? Well, this is actually something, a mistake you'll see, somewhat often,
at least initially. And let's start to glean what's going on here.
- [2:20:03](https://www.youtube.com/watch?v=undefined&t=8403s) So here, if I look at the very first
line of output after the dollar sign-- so even though it jumped down the screen pretty fast, I wrote Make
Hello at the dollar sign, prompt. And then here's the first error. On Hello dot C, line 5-- technically
character 5, but generally line is enough to get you going-- there's an error, use of undeclared identifier
string.
- [2:20:25](https://www.youtube.com/watch?v=undefined&t=8425s) Did you mean standard in? So, I
didn't. And this is not an obvious solution at first. But you'll start to recognize these patterns in error
messages. It turns out that if I want to use string, I actually have to do this. I have to include another
library up here, another line of code, rather, called CS50 dot H.
- [2:20:46](https://www.youtube.com/watch?v=undefined&t=8446s) We'll come back to what this
means in just a moment. But if I now retroactively say, all right, what does standard I/O do for us up
here. Before I added that new line, what is standard I/O doing? Well, if you think back to Scratch, there
were a few examples with the camera and with the speech to-- the text to voice. Remember I had to
poke around in the extensions button.
- [2:21:10](https://www.youtube.com/watch?v=undefined&t=8470s) And then I had to load it into
Scratch. It didn't come natively with Scratch. C is quite like that. Some functions come with the language.
But for the most part, if you want to use a function, an action or a verb like printf, you have to load that
extension, so to speak, that more traditionally is called a library.
- [2:21:29](https://www.youtube.com/watch?v=undefined&t=8489s) So there is a standard I/O library,
STD I/O, standard I/O, where I/O just means input and output. Which means, just like in MIT's World,
there was an extension for doing text to voice or for using your camera. In C, there's an extension, a.k.a.
a library, for doing standard input and output. And so if you want to use any functions related to
standard input and output, like text from a keyboard, you have to include standard I/O dot H. And then
can you use printf.
- [2:22:02](https://www.youtube.com/watch?v=undefined&t=8522s) Same goes here. Get string, it turns
out, is a function that CS50 wrote some time ago. And as we'll see over the coming weeks, it just makes
it way easier to get input from a user. C is very good with printf at printing output on the screen. C makes
it really annoying and hard, as we'll see in a few weeks, to just get input from the user.
- [2:22:23](https://www.youtube.com/watch?v=undefined&t=8543s) So we wrote a function called
get_string, but the only way you can use that is to load the extension, a.k.a. load the library called CS50.
And we'll come back in time, like, why is it .h, why is it a hash symbol. But for now, standard I/O is a
library that gives you access to printf and input- and output-related stuff.
- [2:22:43](https://www.youtube.com/watch?v=undefined&t=8563s) CS50 is a second library that
provides you with access to functions that don't come with C that include something like get_string. So
with that said, we've now kind of teased apart at a high level what lines 2 and now 1 are doing. Let me
go ahead and rerun make hello. Now it worked. So all those crazy error messages were resolved by just
one fix, so key takeaway is not to get overwhelmed by the sheer number of errors.
- [2:23:09](https://www.youtube.com/watch?v=undefined&t=8589s) Let me now do ./hello and if I type
in my name, what am I going to say? What do you think? Yeah, hello answer, because the computer is
going to take me literally. And it turns out that if you just write "hello, answer" all in the double quotes,
you're really just passing English as the input to the printf function, you're not actually passing in the
variable.
- [2:23:34](https://www.youtube.com/watch?v=undefined&t=8614s) And unfortunately in C, it's not
quite as easy to plug things in to other things that you've typed. Remember in Scratch, there was not just
the Save block but the Join block, which was kind of pretty, you can combine apples and oranges-- or was
it apple and banana? Then we changed it to hello and then the answer that the human typed in.
- [2:23:53](https://www.youtube.com/watch?v=undefined&t=8633s) In C, the syntax is going to be a
little different. You tell the computer inside of your double quotes that you want to have a placeholder
there, a so-called format code. %s means, hey, computer, put a string here eventually. Then outside of
your quotes, you just add a comma and then you type in whatever variable you want the computer to
plug in at that %s location for you.
- [2:24:19](https://www.youtube.com/watch?v=undefined&t=8659s) So %s is a format code which
serves as a placeholder. And now the printf function was designed by humans years ago to figure out
how to do the apple and banana thing of joining two words together. It's not nearly as user-friendly as it
is in Scratch, but it's a very common paradigm. So let me try and rerun this now. make hello.
- [2:24:39](https://www.youtube.com/watch?v=undefined&t=8679s) No errors, that's good. ./hello.
What's my name, David? If I type Enter now, now it's hello. David. And the printf, here's the F in printf. It
formats its input for you by using these placeholders for things like strings, represented again by %s. So a
quick question then, if I focus here on line 7 for just a moment and even zoom in here, how many inputs
is printf taking as a function? A moment ago, I'll admit that it was taking one input, "hello, world," quote
unquote.
- [2:25:15](https://www.youtube.com/watch?v=undefined&t=8715s) How many inputs might you infer
printf is taking now? 2. And it's implied by this comma here, which is separating the first one, quote,
unquote, "hello, %s" from the second one, answer. And then just as a quick safety check here, why is it
not 3? Because there's obviously two commas here.
- [2:25:35](https://www.youtube.com/watch?v=undefined&t=8735s) Why is it not actually 3 arguments
or inputs? AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Exactly. The comma to the left is actually part of my
English grammar, that's all, so same syntax. And, again, here's where programming can just be confusing
early on because we're using the same special punctuation to mean different things, it just depends on
the context.
- [2:25:56](https://www.youtube.com/watch?v=undefined&t=8756s) And so now is actually a good time
to point out all of the somewhat pretty colors that have been popping up on the screen here-- even
though I wasn't going to a format menu, I wasn't boldfacing things, I certainly wasn't changing things to
red or blue or whatnot-- that's because a text editor like VS Code syntax highlights for you.
- [2:26:14](https://www.youtube.com/watch?v=undefined&t=8774s) This is a feature of so many
different programming environments nowadays, VS Code does it as well. If your text editor understands
the language that you're programming in-- C, in this case-- it highlights in different colors the different
types of ideas in your code. So, for instance, string and answer here are in black, but get_string a
function is in this sort of nasty brown-yellow here right now, but that's just how it displays on the screen.
- [2:26:38](https://www.youtube.com/watch?v=undefined&t=8798s) The string, though, here in red is
kind of jumping out at me, and that's marginally useful. The %s is in blue. That's kind of nice, because it's
jumping out at me. And so it's just using different colors to make different things on the screen pop so
you can focus on how these ideas interrelate and, honestly, when you might make a mistake.
- [2:26:55](https://www.youtube.com/watch?v=undefined&t=8815s) For instance, let me accidentally
leave off this quote here. And now all of a sudden, notice if I delete the quote, the colors start to get a
little awry. But if I go back there and put it back, now everything's back in place. What's another feature
of this text editor? Notice when my cursor is next to this parenthesis, which demarcates the end of the
inputs to the function, notice that highlighted in green here is the opening parenthesis.
- [2:27:22](https://www.youtube.com/watch?v=undefined&t=8842s) Why? It's just a visually useful
thing, especially when you start writing more and more code, just to make sure your parentheses are
lining up. And that's true for these curly braces over here on the left and the right. We'll come back to
those in a moment. If I put my cursor there, you can see that these things correspond to one another.
- [2:27:38](https://www.youtube.com/watch?v=undefined&t=8858s) So it's nothing in your code
fundamentally, it's just the editor trying to help you, the human, program. And you can even see it,
though it's a little subtle-- see these four dots here and these four dots here? That's my indentation. I
configured VS Code to indent by four spaces, which is a very common convention.
- [2:27:55](https://www.youtube.com/watch?v=undefined&t=8875s) Any time I hit the Tab key, this too
can help you make sure-- once we have more interesting and longer programs-- that everything lines up
nice and neatly. Phew. All right, any questions then on printf or more? Yeah. AUDIENCE: [? Would ?] the
printf [INAUDIBLE]?? DAVID J. MALAN: Short answer, yes. printf can handle more than one type of
variable or value.
- [2:28:16](https://www.youtube.com/watch?v=undefined&t=8896s) %s is one. We're going to see %i is
another for plugging in an integer. You can have multiple i's, multiple s's, and even other symbols too.
We'll come back to that in just a little bit. printf can take many more arguments than just these two. This
is just meant to be representative. Yeah, over here.
- [2:28:34](https://www.youtube.com/watch?v=undefined&t=8914s) Can you declare variables within
the printf? No. The only variable I'm using right now is answer, and it's got to be done outside the
context of printf in this case. Good question, we'll see more of that before long. Yeah, in back.
AUDIENCE: [INAUDIBLE] DAVID J.
- [2:28:52](https://www.youtube.com/watch?v=undefined&t=8932s) MALAN: How do we download the
CS50 library? So we will show you in problems set 1 exactly how to do that. It's automatically done for
you in our version of VS Code in the cloud. If, ultimately, you program on your own Mac or PC, either
initially or later on, it's also installable online. But if you want to ask that via online or afterward, we can
point you in the right direction.
- [2:29:09](https://www.youtube.com/watch?v=undefined&t=8949s) But PSet 1 will itself. Yeah.
AUDIENCE: [INAUDIBLE] DAVID J. MALAN: String is the type of the variable or, more properly, the data
type of the variable. int is another keyword I alluded to earlier, I haven't used it yet. int, for integer, is
going to be another type, or data type, of variable. AUDIENCE: OK. [? Thank you.
- [2:29:27](https://www.youtube.com/watch?v=undefined&t=8967s) ?] DAVID J. MALAN: Yeah.
AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Oh, good question. Could I go ahead and just plug in this
function, kind of like we did in Scratch, getting rid of the variable altogether and just do this, which recall,
is reminiscent of what I did in Scratch by plopping block on top of block on block? Am I answering that
right? Can I put string in front of get_string? No.
- [2:29:51](https://www.youtube.com/watch?v=undefined&t=8991s) You only put the word string in
front of a variable that you want to make string. And even though I'm apparently answering the wrong
question, let me go ahead and zoom out, save this, do make hello again. Seems to compile OK. If I run
./hello, type in David, voila. That, too, works. And so, actually, let's go down this rabbit hole for just a
moment.
- [2:30:10](https://www.youtube.com/watch?v=undefined&t=9010s) Clearly, it's still correct-- at least,
based on my limited testing. Is this better designed or worse designed? Let's open that question like we
did last week. Yeah? Yeah, I kind of agree with that. Reasonable people could disagree, but I do agree
that this seems harder to read because I start reading here, but wait a minute.
- [2:30:32](https://www.youtube.com/watch?v=undefined&t=9032s) get_string is going to get used first,
and then it's going to give me back a value. So, yeah, it just feels like it was nicer to read top to bottom, I
would say. Your thoughts? AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Yeah. And so this is useful if I only
want to print out the person's name once. If I want to use it later in a longer program, I'm out of luck,
and so I haven't saved it in a variable.
- [2:30:49](https://www.youtube.com/watch?v=undefined&t=9049s) So I think, long story short, we
could debate this all day long. But in this case, eh, if you can make a reasonable argument one way or
the other, that's a pretty solid ground to stand on. But, invariably, reasonable people are going to
disagree, whether first-time programmers or many years after that.
- [2:31:06](https://www.youtube.com/watch?v=undefined&t=9066s) So let's frame this one last example
in the context of the same process of taking inputs and outputs. The functions we've been talking about
all take inputs, otherwise now known as arguments, or parameters, pretty much synonymous. That's just
the fancy word for an input to a function. And some functions have either side effects, like we saw--
printing something, saying something on the screen, sort of visually or audibly-- or they return a value,
which is a reusable value, like name or answer,
- [2:31:34](https://www.youtube.com/watch?v=undefined&t=9094s) in this case. If we look then at what
we did last time in the world of Scratch last week, the input was what's your name, the function was ask,
and the return value was answer. And now let's take a look at this block, which is honestly a more user-
friendly version of what we just did with the %s. Last week we said save, then join, then hello and
answer.
- [2:31:55](https://www.youtube.com/watch?v=undefined&t=9115s) But the interesting takeaway there
was not how to say hello anything. It was the fact that in Scratch 2, the output of one function, like the
green join, could become the input to another function, the purple say. The syntax in C is admittedly
pretty different, but the idea is essentially the same. Here, though, we have hello, a placeholder, but we
have to, in this world of C, tell printf what we want to plug in for that placeholder.
- [2:32:25](https://www.youtube.com/watch?v=undefined&t=9145s) It's just different. But that's the
way to do it. When we get to Python and other languages later in the term, there's actually easier ways
to do this. But this is a very common paradigm, particularly when you want to format your data in some
way. All right, let's then take a step back to where we began, which was with that whole program, which
had the include and it had int main(void) and all of this other cryptic syntax.
- [2:32:48](https://www.youtube.com/watch?v=undefined&t=9168s) This Scratch piece last week was
kind of like the go-to whenever you want to have a main part of your program. It's not the only way to
start a Scratch program. You could listen for clicks or other things, not just the green flag. But this was
probably the most popular place to start a program in Scratch.
- [2:33:04](https://www.youtube.com/watch?v=undefined&t=9184s) In C, the closest analog is to
literally write this out. So just like last week, if you were in the habit of dragging and dropping when
green flag clicked, as a C programmer, the first thing you would do is after creating an empty file, like I
did with hello.c, you'd probably type int main(void) open curly brace, closed curly brace, and then you
can put all of your code inside of those curly braces.
- [2:33:26](https://www.youtube.com/watch?v=undefined&t=9206s) So just like Scratch had this sort of
magnetic nature to it where the puzzle pieces would snap together, C, as a text-based language, tends to
use these curly braces, one of them opened, the other one closed. And anything inside of those braces,
so to speak, is part of this puzzle piece, a.k.a. main. So what was atop them? We went down this rabbit
hole moment ago with these things called header files, even though I didn't call them by this name.
- [2:33:53](https://www.youtube.com/watch?v=undefined&t=9233s) But, indeed, when we have a
whole program in Scratch, super easy. Just have the one green flag clicked and then say hello, world.
There's no special syntax. After all, it's meant to be very user-friendly and graphical. In C, though, you
technically can't just put int main(void) printf hello, world.
- [2:34:10](https://www.youtube.com/watch?v=undefined&t=9250s) You also need this. Because, again,
you need to tell the compiler to load the library-- code that someone else wrote-- so that the compiler
knows what printf even is. You have to load the CS50 library whenever you want to use get_string or
other functions, like get_int, as we'll soon see. Otherwise, the compiler won't know what get_string is.
- [2:34:33](https://www.youtube.com/watch?v=undefined&t=9273s) You just have to do it this way. The
specific file name I'm mentioning here, stdio.h, cs50.h, is what C programmers called a call a header file.
We'll see eventually what's inside of those files. But long story short, it's like a menu of all of the
available functions. So in cs50.
- [2:34:55](https://www.youtube.com/watch?v=undefined&t=9295s) h, there's a menu mentioning
get_string, get_int, and a bunch of other stuff. And in stdio.h, there's a menu of functions, among which
are printf. And that menu is what prepares the compiler to know how to implement those same
functions. All right, let me pause here. Question. AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Not quite. A
library provides all of the functionality we're talking about.
- [2:35:21](https://www.youtube.com/watch?v=undefined&t=9321s) A header file is the very specific
mechanism via which you include it. And we'll discuss this more next week. For now, they're essentially
the same, but we'll discuss nuances between the two next week. Yeah, the library would be standard
I/O. The library would CS50. The corresponding header file is stdio.h, cs50.h.
- [2:35:42](https://www.youtube.com/watch?v=undefined&t=9342s) Indeed. Other questions. Yeah.
AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Indeed. That, too, is on the menu. We'll come back to that.
But the word string-- incredibly common in the world of programming, it's not a CS50 idea-- but in C,
there's technically no such data type as string by default. We have sort of conjured it up to simplify the
first few weeks.
- [2:36:02](https://www.youtube.com/watch?v=undefined&t=9362s) That's a training wheel that we'll
very deliberately, in a few weeks, take away, and we'll see why we've been using get_string and string.
Because C otherwise makes things quite more challenging early on, which then gets besides the point for
us. Yeah. AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Yes.
- [2:36:21](https://www.youtube.com/watch?v=undefined&t=9381s) Early on, you will have to use
whatever is prescribed by the specification. That will include CS50's functions. Long story short, you
referred, I think, a moment ago to another function called scanf, we won't talk about for a few weeks.
Long story short, in C, it's pretty easy and possible to get input from a user.
- [2:36:37](https://www.youtube.com/watch?v=undefined&t=9397s) The catch is that it's really easy to
do it dangerously. And C, because it's an older, lower-level language, so to speak, that gives you pretty
much ultimate control over your computer's hardware. It's very easy to make mistakes. And, indeed,
that's too why we use the library, so your code won't crash unintendedly.
- [2:36:59](https://www.youtube.com/watch?v=undefined&t=9419s) All right, so with this in mind, we
have this now mapping between the Scratch version and the other. Let me just give you a quick tour of
some of the other placeholders and data types that students will start seeing as we assemble more
interesting programs. In the world of Linux, here is a non-exhaustive list of commands with which you'll
get familiar over the next few weeks by playing with problem sets.
- [2:37:18](https://www.youtube.com/watch?v=undefined&t=9438s) We've only seen two of these so
far, ls for list, rm for others. But I mention them now just so that it doesn't feel too foreign when you see
them on screen or online in a problem set. cp is going to stand for copy. mkdir is going to stand for make
directory. mv is going to stand for move or rename.
- [2:37:38](https://www.youtube.com/watch?v=undefined&t=9458s) rmdir is going to be remove
directory, and cd is going to be for change / and let me show you this last one here first, only because it's
something you'll use so commonly. If I go back to my code here on the screen, I'm going to go ahead and
re-open the little GUI on the left-hand side, the so-called Explorer, revealing that I've got two files, hello
and hello.
- [2:38:01](https://www.youtube.com/watch?v=undefined&t=9481s) c so nothing has changed since
there. Suppose now that it's a few weeks into class and I want to start organizing the code I'm writing so
that I have a folder for this week or next week, or maybe a folder for problem set 1, problem set 2. I can
do this in a few ways. In the GUI, I can go up here and do what most of you would do instinctively on a
Mac or PC.
- [2:38:20](https://www.youtube.com/watch?v=undefined&t=9500s) You look for like a folder icon, you
click it, and then you name a folder like PSet1, Enter. Voila, you've got a folder called PSet1. I can confirm
as much with my command line interface by typing what command? How can I list what's in my folder?
Yeah, so ls for list. And now I see hello-- and it's green with an asterisk because that's my executable, my
runnable program-- hello.
- [2:38:47](https://www.youtube.com/watch?v=undefined&t=9527s) c, which is my source code, and
now PSet1 with a slash at the end, which just implies that it's indeed a folder. All right, I didn't really
want to do it that way. I'd like to do it more advanced. So let me go ahead and right-click on PSet1,
delete permanently. I get a scary irreversible error message. But there's nothing in it, so that's fine.
- [2:39:01](https://www.youtube.com/watch?v=undefined&t=9541s) Now I've deleted it using the GUI.
But now let me go ahead and start doing the same thing from the command line. And if you're
wondering how things keep disappearing, if you hit Control-L in your terminal window or explicitly type
clear, it will delete everything you previously typed just to clean things up.
- [2:39:18](https://www.youtube.com/watch?v=undefined&t=9558s) In practice, you don't need to be
doing this often. I'm doing it just to keep our focus on my latest commands. If I do-- what was the
command to make a new directory? AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Yeah, so mkdir, make
directory. Let me create PSet1, Enter. And notice at left, there's my PSet1.
- [2:39:35](https://www.youtube.com/watch?v=undefined&t=9575s) If I want to get a little overzealous,
plan for next week, here's my PSet2 directory. Suppose now I want to open those folders on a Mac or PC
or in this GUI, I could double-click on it like this, and you'd see this little arrow is moving. It's not doing
anything because there's nothing in there, but that's fine.
- [2:39:51](https://www.youtube.com/watch?v=undefined&t=9591s) But suppose again I want to get
more comfortable with my command line. Notice if I type ls now, I see all four same things. Let me
change directories with cd space PSet1 Enter. And now notice two things will have happened. One, my
prompt has changed slightly to remind me where I am, just to keep me sane so that I don't forget what
folder I'm actually in.
- [2:40:18](https://www.youtube.com/watch?v=undefined&t=9618s) So here is just a visual reminder of
what folder I'm currently in. If I type ls now, what should I see after hitting Enter? Nothing, because I've
only created empty folders so far. And, indeed, I see nothing. If I wanted to create a folder called Mario
for a program that might be called Mario this week, I can do that.
- [2:40:38](https://www.youtube.com/watch?v=undefined&t=9638s) Now if I type ls, there is Mario.
Now if I do cd Mario, notice my prompt's going to change to be a little more precise. Now I'm in
PSet1/Mario. And notice what's happening at top left. Nothing now, because these folders are collapsed.
But if I click the little triangle, there I see Mario. Nothing's going on in there because there's no files yet.
- [2:40:57](https://www.youtube.com/watch?v=undefined&t=9657s) But suppose now I want to create a
file called mario.c. I could go up here, I could click the little plus icon, and use the GUI. Or I can just type
code mario.c. Voila. That creates a new tab for me. I'm not going to write any code in here yet, but I am
going to save the file. And now at top left, you'll see that mario.c appears.
- [2:41:17](https://www.youtube.com/watch?v=undefined&t=9677s) So at some point, you can
eventually just close the Explorer. Because, again, it's not providing you with any new information. It's
maybe more user-friendly, but there's nothing you can't do at the command line that you could do with
the GUI. All right, but now I'm kind of stuck. How do I get out of this folder? In my Mac or PC world, I'd
probably click the Back button or something like that or just close it and start all over.
- [2:41:37](https://www.youtube.com/watch?v=undefined&t=9697s) In the terminal window, I can do cd
dot dot. Dot dot is a nickname, if you will, for the parent directory. That is, the previous directory. So if I
hit Enter now, notice I'm going to close the Mario folder, a.k.a. directory, and now I'm back in PSet1. Or, if
I want to be fancy, let me go back into Mario temporarily.
- [2:42:00](https://www.youtube.com/watch?v=undefined&t=9720s) If I type ls, there's mario.c, just to
orient us. If I want to do multiple things at a time, I could do cd../.. which goes to my parent to my
grandparent all in one breath. And voila, now I'm back in my default folder, if you will. And one last little
trick of the trade, if I'm in PSet1/Mario like I was a moment ago, and you're just tired of all the
navigation, if you just type cd and hit Enter, it'll whisk you away back to your default folder, and you don't
have to worry about getting there manually.
- [2:42:31](https://www.youtube.com/watch?v=undefined&t=9751s) Recall a bit ago, though, that I was
running hello as this, ./hello. If dot refers to my parent, perhaps infer here syntactically, what does a
single dot mean instead? It means this directory, your current directory. Why is that necessary? It just
makes super explicit to the computer that I want the program called hello that's installed here, not in
some random other folder on my hard drive, so to speak.
- [2:43:00](https://www.youtube.com/watch?v=undefined&t=9780s) I want the one that's right here
instead. All right, so besides these commands, there's going to be others that we encounter over time.
Those are kind of the basics. That allows you to wean yourself off of a GUI, Graphical User Interface, and
start using more comfortably, with practice and time, a command line interface instead.
- [2:43:16](https://www.youtube.com/watch?v=undefined&t=9796s) Well, what about those other
types, now back in the world of C? Those commands were not C. Those are just command-specific to a
command line interface, like in Linux, which, again, we're using in the cloud. It's an alternative to Mac OS
and Windows. Back in the world of C now, we've seen strings, which are words.
- [2:43:35](https://www.youtube.com/watch?v=undefined&t=9815s) I mentioned int or integer, but
there's others as well. In the world of C, we've seen string, we will see int. If you want a bigger integer,
there's something literally called a long. If you want a single character, there's something called a char. If
you want a Boolean value, true or false, there is a bool.
- [2:43:54](https://www.youtube.com/watch?v=undefined&t=9834s) And if you want a floating-point
value-- a fancy way of saying a real number, something with a decimal point in it-- that is what C and
other languages call a float. And if you want even more numbers after the decimal point that is more
precision, you can use something called a double. That is to say, here is, again, an example in
programming where it's up to you now to provide the computer with hints, essentially, that it will rely on
to know what is this pattern of zeros and ones.
- [2:44:22](https://www.youtube.com/watch?v=undefined&t=9862s) Is it a number, a letter? Is it a
sound, an image, a color, or the like? These are the types of data types that provide exactly those hints.
What are the functions that come in the menu that is the CS50 library? We talked about standard I/O,
and that's just one function so far, printf. In the CS50 library, you can see that it follows a pattern.
- [2:44:43](https://www.youtube.com/watch?v=undefined&t=9883s) The C50 library exists largely for
the first few weeks of the class to make our lives easier when you just want to get user input. So if you
want to get a string, like a word or words from the human, you use get_string. If you want to get an
integer from the user, you're going to use get_int. When you want to get any of those other data types,
for the most part, you use get_ something else.
- [2:45:04](https://www.youtube.com/watch?v=undefined&t=9904s) And they're indeed all lowercase by
convention. What about printf? If we have the ability now to store different types of data and we have
functions with which to get different types of data, how might you go about printing different types of
data? Well, we've seen %s for string, %i for integer, %c for char, %f for a float or a double, those real
numbers I described earlier, and then %li for a long integer.
- [2:45:34](https://www.youtube.com/watch?v=undefined&t=9934s) So here's the first example of
inconsistencies. In an ideal world, that would just be %l and we'd move on. It's %li instead in this case.
That's printf and some of its format codes. What more might we do? Well, in C, as we'll see-- no pun
intended-- there is a whole bunch of operators. And, indeed, computers, one of the first things they did
was a lot of math and calculations, so there's a lot of operators like these.
- [2:45:58](https://www.youtube.com/watch?v=undefined&t=9958s) Computers, and in turn, C, really
good at addition, subtraction, multiplication, division, and even the percent sign, which is the remainder
operator. There's a special symbol in C and other languages just for getting the remainder, when you
divide one number by another. There are other features in the world of C, like variables, as we've seen.
- [2:46:19](https://www.youtube.com/watch?v=undefined&t=9979s) And there's also what is of playfully
called syntactic sugar that makes it easier over time to write fewer characters but express your thoughts
the same. So just as a single example of this, as a single example, consider this use of a variable last
week. Here in Scratch is how you might set a variable called counter to 0.
- [2:46:42](https://www.youtube.com/watch?v=undefined&t=10002s) In C, it's going to be similar. If you
want the variable to be called counter, you literally write the word counter, or whatever you want it to be
called. You then use the assignment operator, a.k.a. the equals sign, and you assign it whatever its initial
value should be here on the right. So, again, the 0 is going to get copied from right to left into the
variable because of that single equal sign.
- [2:47:03](https://www.youtube.com/watch?v=undefined&t=10023s) But this isn't sufficient in C. What
else is missing on the right-hand side, instinctively now? Even if you've never programmed in this before.
Yeah, in front. AUDIENCE: Semicolon. DAVID J. MALAN: A semicolon at the end. And one other thing, I
think, is probably missing. Again. AUDIENCE: A data type.
- [2:47:18](https://www.youtube.com/watch?v=undefined&t=10038s) DAVID J. MALAN: A data type. So
if we can keep going back and forth here, what data type seems appropriate intuitively for counter? int
for integer. So, indeed, we need to tell the computer when creating a variable what type of data we
want, and we need to finish our thought with the semicolon. So there might be a counterpart there.
- [2:47:38](https://www.youtube.com/watch?v=undefined&t=10058s) What about in Scratch if we
wanted to increment that counter variable? We had this very user-friendly puzzle piece last time that
was change counter by 1, or add 1 to counter. In C, here's where things get a little more interesting. And
pretty commonly done, you might do this. counter = counter + 1; with a semicolon.
- [2:47:59](https://www.youtube.com/watch?v=undefined&t=10079s) And this is where, again, it's
important to note, the equal sign, it's not equality. Otherwise, this makes no sense. counter cannot equal
counter plus 1, right? That just doesn't work if we're talking about integers here. That's because the
equal sign is assignment. So it can certainly be the case that you calculate counter plus 1, whatever that
is, then you update the value of counter from right to left to be that new value.
- [2:48:23](https://www.youtube.com/watch?v=undefined&t=10103s) This, as we'll see, is a very
common thing to do in programming just to kind of count upward, for whatever reason. You can write
this more succinctly. This code here is what we'll call syntactic sugar, sort of a fancy way of saying the
same thing with fewer words or fewer characters on the screen.
- [2:48:40](https://www.youtube.com/watch?v=undefined&t=10120s) This also adds 1, or whatever
number you type over here, to the variable on the left. And there's one other form of syntactic sugar
we're going to start seeing too, and it's even more terse than this. That too will increment counter by 1
by literally changing its value by 1. Or if you change it to minus minus, subtracting 1 from it.
- [2:49:00](https://www.youtube.com/watch?v=undefined&t=10140s) You can't do that with 2 and 3 and
4, but you can do it by default with just plus plus or minus minus adding or subtracting 1. Yeah.
AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Ah, so when you are changing a variable that already has been
created, as we did with the code that looked like this, you no longer need to remind the computer what
the data type is.
- [2:49:24](https://www.youtube.com/watch?v=undefined&t=10164s) Thankfully, the computer is at
least as smart as that. It will remember the type of the data that you intended. Other questions or
comments on this? All right, that's quite a lot. Why don't we go ahead and here take a 10-minute break?
And we'll be back, we'll start writing some code. All right, so we are back.
- [2:49:45](https://www.youtube.com/watch?v=undefined&t=10185s) We've just looked at some of the
basics of compiling, even if it doesn't quite feel that basic. But now, let's actually start focusing really on
writing more and more code, more and more interesting code, kind of like we dove into Scratch last
week. So here I have these code open. I've closed the GUI.
- [2:50:02](https://www.youtube.com/watch?v=undefined&t=10202s) I'm going to focus more on my
terminal window and my code editors. Many different ways I can create new files, but I want to create
something called a calculator. So, again, within this environment of VS Code, I can literally write the code
command which is VS Code specific, and it just creates a new file for me automatically.
- [2:50:18](https://www.youtube.com/watch?v=undefined&t=10218s) Or I could do that in the GUI. I'm
going to go ahead and create this file called calculator.c and I'm going to go ahead and include some
familiar things. So I'm just going to go ahead and proactively include cs50.h, stdio.h. I'm going to go
ahead from memory and do the int void main-- more on that next week, why it's int, why it's void, and so
forth.
- [2:50:38](https://www.youtube.com/watch?v=undefined&t=10238s) And now let me just implement a
very simple calculator. We saw some mathematical operators, like plus and the like. So let's actually use
this. So let me go ahead and first give myself a variable called x, sort of like grade school math or algebra.
Let me go ahead then and get an int, which is new, but I mentioned this exists.
- [2:50:57](https://www.youtube.com/watch?v=undefined&t=10257s) And then let me just ask the user
for whatever their x value is. The thing in the quotes is just the English, or the string that I'm printing on
the screen. so I could say anything I want. I'm just going to say x colon to prompt the user accordingly.
Now I'm going to go ahead and get another variable called y.
- [2:51:12](https://www.youtube.com/watch?v=undefined&t=10272s) I'm going to get int again. And
now, I'm going to prompt the user for y. And I'm just very nitpickly using a space just to move the cursor
so it doesn't look too messy on the screen. And then lastly, let me go ahead and just print out the sum of
x and y. In an ideal world, I would just say something like printf x + y.
- [2:51:32](https://www.youtube.com/watch?v=undefined&t=10292s) But that is not valid in C. The first
argument, recall, in printf has to be a string in double quotes. So if I want to print out the value of an
integer, I need to put something in quotes here, maybe followed by a newline, if I want to move the
cursor as well. So, again, we only glimpsed it briefly, but what do I replace these question marks with if I
want a placeholder for an integer? AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Yeah, so %i.
- [2:51:58](https://www.youtube.com/watch?v=undefined&t=10318s) Just like %s was string, %i is
integer. So I change this %i. And now if I want to add x and y, for instance, super-- simple calculator,
doesn't do much of anything other than addition of two integers-- I think this works. And, again, it looks
definitely cryptic at first glance. It would be if programming weren't this cryptic.
- [2:52:16](https://www.youtube.com/watch?v=undefined&t=10336s) Other languages will clean this up
for us. But, again, if you focus on the basics, printf takes one input first-- which is a format string with
English or whatever language, some placeholders, maybe-- then it takes potentially more arguments
after the comma, like the value of x plus y. All right, let me go ahead now and make calculator, which,
again, compiles my source code in C, pictured above, and converts it into corresponding machine code,
or zeros and ones.
- [2:52:46](https://www.youtube.com/watch?v=undefined&t=10366s) No error messages. so that's
already good. Now I do ./calculator. Let's do 1 plus 1 and Enter. Voila. Now I have the makings of a
calculator. Now let's start to tinker with this a little bit. What if I instead had done this? int z = x + y and
then plug-in z here. If I rerun make calculator, Enter, rerun .
- [2:53:15](https://www.youtube.com/watch?v=undefined&t=10395s) /calculator, type in 1 plus 1, still
equals 2, and let me claim that it will work for other values as well-- which of these versions is better
designed? If both seem to be correct at very cursory glance, is this version better or is the previous one
without the z? OK, so this one is arguably better because I've now got a reusable variable called z that I
cannot only print but, heck, if my program is longer, I can use it elsewhere.
- [2:53:39](https://www.youtube.com/watch?v=undefined&t=10419s) Counterthoughts? AUDIENCE:
[INAUDIBLE] DAVID J. MALAN: Yeah. Debatable, like before, because it depends on my intent. And,
honestly, I think a pretty good argument can be made for the first version. Because if I have no intention
of-- as you note-- using that variable again, you know what? Maybe I might as well do this, just because
it's one less thing to think about.
- [2:53:57](https://www.youtube.com/watch?v=undefined&t=10437s) It's one less distraction. It's one
less line of code to have to understand. It's just a little tighter. So here, again, it does depend on your
intention. But this field is pretty reasonable. And I think, as someone noted earlier, when I did the same
thing with get_string, that, yeah, maybe kind of crossed s line because get_string and the what's your
name inside of it, it was just so much longer.
- [2:54:17](https://www.youtube.com/watch?v=undefined&t=10457s) But x + y, eh, it's not that hard to
wrap our mind around what's going on inside of the printf argument. So, again, these are the kinds of
thoughts that hopefully you'll acquire the instinct for on not necessarily reaching the same answer as
someone else, but, again, the thought process is what matters here.
- [2:54:33](https://www.youtube.com/watch?v=undefined&t=10473s) All right, so how might I enhance
this program a little bit? Let's just talk about style for just a moment. So x and y, at least in this case, are
pretty reasonable variable names. Why? Because that's the go-to variable names in math when you're
adding two things together. So x and y seem pretty reasonable.
- [2:54:49](https://www.youtube.com/watch?v=undefined&t=10489s) I could have done something like,
well, maybe my first variable should be called first number and my next variable should be called second
number. And then down here, I would have to change this to first number plus second number. Like, eh,
this isn't really adding anything semantically to help my comprehension.
- [2:55:08](https://www.youtube.com/watch?v=undefined&t=10508s) But that would be one other
direction we could have taken things. So if you have very simple ideas that are conventionally expressed
with common variable names like x and y, totally fine here. What if I want to annotate this program and
remind myself what it is it does? Well, I can add in C what are called comments.
- [2:55:25](https://www.youtube.com/watch?v=undefined&t=10525s) With a slash slash, two forward
slashes, you can write a note to yourself, like prompt user for x. And then down here, I could do
something like prompt user for y, just to remind myself what I'm doing there. And down here, perform
addition. Now, in this case, I'm not sure these commands are really adding all that much.
- [2:55:44](https://www.youtube.com/watch?v=undefined&t=10544s) Because in the time it took me to
write and eventually read these comments, I could have just read the three lines of code. But as our
programs get more sophisticated and you start to learn more syntax-- that, honestly, you might forget
the next day, the next week, the next month-- might be useful to have these notes to self that reminds
you of what your code is doing or maybe even how it is doing that thing.
- [2:56:06](https://www.youtube.com/watch?v=undefined&t=10566s) With these early programs, not
really necessary, doesn't really add all that much to our comprehension, but it is a mechanism you have
in place that can help you actually remind yourself or remind someone else what it is that's going on.
Well, let me go ahead and rerun this again in this current version, make calculator.
- [2:56:24](https://www.youtube.com/watch?v=undefined&t=10584s) And here, too, you might think I'm
typing crazy fast-- not really. I'm hitting Tab a lot. So it turns out that Linux, the operating system we're
using here in the cloud-- but, actually, Windows and Mac OS nowadays support this too-- supports
autocomplete. So if you only have one program that starts with C-A-L, you don't have to finish writing
calculator, you can just hit Tab, and the computer will finish your thought for you.
- [2:56:48](https://www.youtube.com/watch?v=undefined&t=10608s) The other thing you can do is if
you hit Up and keep going up, you'll scroll through your entire history of commands. So there too, I've
been saving some keystrokes by hitting Up quickly rather than retyping the same darn thing again and
again. So, again, just another little convenience to make programming and interacting with the command
line interface even faster.
- [2:57:06](https://www.youtube.com/watch?v=undefined&t=10626s) All right, let me go ahead and just
make sure it's compiled in the current form. The comments have no functional impact. These green
things are just notes to self. Let me run calculator with maybe-- how about this? Instead of 1 plus 1, how
about 1 billion-- whoops, let's do that again. Wa, da, da. 1 million, 1 billion, and another 1 billion, and
that answer is 2 billion.
- [2:57:30](https://www.youtube.com/watch?v=undefined&t=10650s) All right, so that seems correct.
Let's run this program one more time. How about 2 billion plus another 2 billion? Did you know that? So,
apparently, it's not so correct. And, clearly, running 1 plus 1 was not the most robust testing of my code
here. What might have gone wrong? What might have gone wrong? Yeah.
- [2:57:55](https://www.youtube.com/watch?v=undefined&t=10675s) AUDIENCE: [INAUDIBLE] DAVID J.
MALAN: Yeah. The computer probably ran out of space with bits. So it turns out with these data types--
we've been talking about string and int and also float and char and those other things-- they all use a
specific, and, most importantly, finite number of bits to represent them.
- [2:58:13](https://www.youtube.com/watch?v=undefined&t=10693s) It can vary by computer. Newer
computers use more bits, older computers tended to use fewer bits. It's not necessarily standardized for
all of these data types. But in this case, in this environment, it is using 32 bits for an integer. That's a lot.
So with 32 bits, you can count pretty high. This is 64 light bulbs on the stage and could count even higher.
- [2:58:34](https://www.youtube.com/watch?v=undefined&t=10714s) An int is only using half of these,
or we have two integers here on the stage. Now, if you think back to last week, we talked about 8 bits at
one point. And if you have 8 bits, 8 zeros and ones, you can count as high as 256-- just a good number to
generally remember as trivia. 8 bits gives you 256 permutations of zeros and ones.
- [2:58:54](https://www.youtube.com/watch?v=undefined&t=10734s) 32 gives you roughly how many, if
anyone knows? It's 2 to the 32 power. So it's roughly 4 billion, 2 to the 32. If you don't know that, it's
fine. Most programmers, though, eventually remember these kinds of heuristics. So it's roughly 4 billion.
So that feels like enough. 2 billion plus 2 billion is exactly 4 billion.
- [2:59:14](https://www.youtube.com/watch?v=undefined&t=10754s) And that actually should fit in a
32-bit integer. The catch is that my Mac, your PC, and the like also like to support negative numbers. And
if you want to support both positive and negative numbers, that technically means with 32-bit integers,
you can count as high as roughly 2 billion positive or 2 billion negative in the other direction.
- [2:59:35](https://www.youtube.com/watch?v=undefined&t=10775s) That's still 4 billion, give or take,
but it's only half as many in one direction or the other. So how could I go about implementing a correct
calculator here? What might the solution be? Yeah, so not just li, which was for long integer. I have to
make one more change, which is to the data type itself.
- [2:59:55](https://www.youtube.com/watch?v=undefined&t=10795s) So let me go back up here and
change x from an int to a long, a.k.a. long integer. And then let me change y as well. And then let me
change the format code per the little cheat sheet we had up a few minutes ago to li. Let me recompile
the calculator-- seems to work OK. Let's rerun it. Now let's do 1 plus 1.
- [3:00:14](https://www.youtube.com/watch?v=undefined&t=10814s) That's should obviously be the
same. Now let's do 2 billion and another 2 billion and cross our fingers this time. Now we're counting as
high as 4 billion. And we can go way higher than 4 billion, but we're only kicking the can down the street
a bit. Even though we're now using-- with a long-- 64 bits, which is as long as this stage now, that's still a
finite value.
- [3:00:38](https://www.youtube.com/watch?v=undefined&t=10838s) It might be a really big value, but
it's still finite. And we'll come back at the end of today to these kinds of fundamental limitations.
Because arguably now, my calculator is correct for like millions, billions of possible inputs but not all. And
that's problematic if you actually want to use my calculator for any possible inputs, not just ones that are
roughly less than, say, 2 billion, as in this case.
- [3:01:03](https://www.youtube.com/watch?v=undefined&t=10863s) All right, any questions then on
that? But it's really just a precursor for all the problems that we're going to have to eventually deal with
later on. AUDIENCE: [INAUDIBLE] DAVID J. MALAN: A good question. Yes. If we were still using z, we
would also have to change it to a long. Otherwise, we'd be ignoring 32 of the bits that had been added
together via the longs.
- [3:01:26](https://www.youtube.com/watch?v=undefined&t=10886s) Good question. All right, so how
about we spice things up with maybe not just addition here, how about something with some
conditions? Let's start to ask some actual questions. So a moment ago, recall that we had just the
declaration of variables. Now let's look back at something in Scratch that looked a little something like
this, a bunch of puzzle pieces asking questions by way of these conditionals and then these Boolean
expressions here in green, maybe saying something like x is less than y.
- [3:01:55](https://www.youtube.com/watch?v=undefined&t=10915s) In C, this actually maps pretty
cleanly. It's much cleaner from left to right than it was with printf and join. Here, we have just code that
looks like this. If, a space, two parentheses and then x less than y, and then we have something like printf
there in the middle. So here, it's actually kind of a nice mapping.
- [3:02:14](https://www.youtube.com/watch?v=undefined&t=10934s) Notice that, just as the yellow
puzzle piece in Scratch is kind of hugging the purple puzzle piece, that's effectively the role that these
curly braces are playing. They're sort of encapsulating all of the code on the inside. The parentheses
represent the Boolean expression that needs to be asked and answered to decide whether or not to do
this thing.
- [3:02:32](https://www.youtube.com/watch?v=undefined&t=10952s) And here's an exception to what I
alluded to earlier. Usually, when you see a word and then a parenthesis, something, and then closed
parenthesis, I claimed that's usually a function. And I'm still feeling pretty good about that claim. But
there are exceptions. And the word if is not a function.
- [3:02:48](https://www.youtube.com/watch?v=undefined&t=10968s) It's just a programming construct.
It's a feature of the C language that similarly uses parentheses, just for different purposes for a Boolean
expression. How about something like this? Last week, if you wanted to have a two-way fork in the road,
go this way or that way, you can have if and else. In C, that would look a little something like this.
- [3:03:08](https://www.youtube.com/watch?v=undefined&t=10988s) And if we add in the printf's, it
now looks quite like the same, but it adds, of course, the word else and then a couple of more curly
braces. As an aside, in C, It's not strictly necessary to have curly braces if you have only one line of code
indented underneath. For best practice, though, do so anyway, because it makes super clear to you and
ultimately anyone else reading your code that you intend for just that one or more line of code to
execute.
- [3:03:35](https://www.youtube.com/watch?v=undefined&t=11015s) How about this from last week?
Here was a three-way fork in the road. If x is less than y, else if x is greater than y, else if x equals y. Now,
here's where you have some disparities between Scratch and C. Scratch uses an equals sign for equality,
to compare two values. C uses a single equals sign for assignment from right to left, minor difference
between the two worlds.
- [3:03:59](https://www.youtube.com/watch?v=undefined&t=11039s) In C, we could implement the
same code like this, the addition being just this additional else if. And if we add in the printf's, it looks a
little something now like this. This is correct both in the Scratch world and in the C world. But could
someone make a claim that this is not, again, well-designed? Exactly.
- [3:04:17](https://www.youtube.com/watch?v=undefined&t=11057s) We don't need the last if. We
need the else, at least, but we don't need the last if. Because, at least in the world of comparing integers,
it's either going to be less than, greater than, or equal to. There is no other case. So you can save a few
seconds, if you will, of your program running-- a blink of the eye-- by only asking two questions and then
inferring what the answer to the third must be just by nature of your own human logic here.
- [3:04:41](https://www.youtube.com/watch?v=undefined&t=11081s) Now, why is that a good thing? If,
for instance, x and y happen to equal each other-- I type in 1 and 1 for both values, either in Scratch or in
the C world-- in the case of this version, you're sort of stupidly asking three questions, all of which are
going to get asked even though the answer is no, no, yes.
- [3:05:02](https://www.youtube.com/watch?v=undefined&t=11102s) That is false, false, true. That
seems to be unnecessary because if we instead optimize this code, get rid of the unnecessary if and just
do as you proposed logically-- else print that x is equal to y-- now if x indeed equals y because they're
both 1 or some other value, now you're only going to ask two questions, so 2/3 as many questions, and
then you're going to get your same correct result.
- [3:05:29](https://www.youtube.com/watch?v=undefined&t=11129s) So, again, a minor detail, but,
again, the kinds of things you should be thinking about, not only as you write your code to be correct but
also write it to be well-designed as well. All right, so why don't we go ahead and translate this into the
context of an actual program here? I'll create a blank window here.
- [3:05:47](https://www.youtube.com/watch?v=undefined&t=11147s) And let's do something with
points, like points on my own very first CS50 problem set. Let me go ahead and run code of points.c.
That's just going to give me a new text file. And then up here, I'm going to do my usual, include cs50.h.
include stdio.h. int main void. So a lot of boilerplate, so to speak, in these early programs.
- [3:06:08](https://www.youtube.com/watch?v=undefined&t=11168s) And now, let's see. Let's ask the
user, how many points did they lose on their most recent CS50 PSet? So sort of evoke my photograph of
my own very first PSet last week where I lost a couple of points myself. So int points = get_int. Then I'll
ask a question in English like, how many points did you lose, question mark, space? And then once I have
this answer, let's now ask some questions of it.
- [3:06:34](https://www.youtube.com/watch?v=undefined&t=11194s) So if points is less than 2--
borrowing the syntax that we saw on the screen a moment ago-- let's go ahead and print out something
explanatory like you lost fewer points than me, backslash n. else if points greater than 2-- which is, again
how many I lost-- I'm going to go ahead and print out you lost more points than me, backslash n.
- [3:07:00](https://www.youtube.com/watch?v=undefined&t=11220s) else if-- wait a minute, else seems
to be sufficient logically here. I'm just going to go ahead and print out something like you lost the same
number of points as me, backslash n. So, really, just a straightforward application of that simple idea but
to a concrete scenario here. So let me go ahead and save this.
- [3:07:21](https://www.youtube.com/watch?v=undefined&t=11241s) Let me go ahead and run make
points, Enter. No errors, that's good. Run points. And then, how many points did you lose? How about,
it's 1 point? All right, you lost fewer points than me. How about 0 points? Even better. How about 3
points? And so forth. So, again, we have the ability to express in C now pretty basic idea from last week
in reality, which is this notion of conditionals and asking questions.
- [3:07:46](https://www.youtube.com/watch?v=undefined&t=11266s) There's something subtle here,
though, that's maybe not super well-designed that someone might call a magic number. This is
programming speak for something I've done here. There's a bit of redundancy unrelated to the if and the
else and the else. But is there something I typed twice just to ask, perhaps, for the obvious? Exactly, I've
hard-coded, so to speak, manually typed out the number 2-- in two locations, in this case-- that did not
come from the user.
- [3:08:16](https://www.youtube.com/watch?v=undefined&t=11296s) So, apparently, once I compile
this, this is it. You're always comparing yourself to me in like, 1996, which for better or for worse, is all
the program can do. But this is an example too of a magic number in the sense like, wait, where did that
2 come from, and why is it in two places? It feels like we are setting the stage for just a higher probability
of screwing up down the road.
- [3:08:36](https://www.youtube.com/watch?v=undefined&t=11316s) Because the longer this code gets,
suppose I'm comparing against 2 points elsewhere-- 2, 3, 4, 5 places-- am I going to keep typing the
number 2? Like, yeah, that's fine. It's correct. It's going to work. But, honestly, eventually, you're going to
screw up, and you're going to miss one of the 2's, you're going to change it to a 3, because maybe I did
worse the next year, or 1, I did better.
- [3:08:55](https://www.youtube.com/watch?v=undefined&t=11335s) And you don't want these
numbers to get out of sync. So what would be a logical improvement to this design, rather than hard-
coding the same number sort of magically in two or more places? Yeah, why don't I make a variable that
I can use in there? So, for instance, I could create a variable like this, another integer called mine.
- [3:09:15](https://www.youtube.com/watch?v=undefined&t=11355s) And I'm just going to initialize it to
2. And then I'm going to change mentions of 2 to this. And mine is a pretty reasonable name for a
variable insofar as it refers to exactly whose points are in question. There's a risk here, though, minor
though it is. I could accidentally change mine at some point.
- [3:09:32](https://www.youtube.com/watch?v=undefined&t=11372s) Maybe I forget what mine
represents, and I do some addition or subtraction. So there's a way to tell the computer "don't trust me,
because I'm going to screw up eventually" by making a variable constant too. So a constant in a
programming language-- this did not exist in Scratch-- is just an additional hint to the computer that
essentially enables you to program more defensively.
- [3:09:52](https://www.youtube.com/watch?v=undefined&t=11392s) If you don't trust yourself
necessarily to not screw up later, or honestly, in practice, if you know that number should never change,
make it constant and never think about it again. This tells the compiler to make sure that even you later
in your code cannot change the number 2. And another convention in C and other languages, when you
have a constant, it's often common to just capitalize the variable.
- [3:10:17](https://www.youtube.com/watch?v=undefined&t=11417s) Kind of like you're yelling, but it
really just visually makes it stand out. So it's kind of like a nice rule of thumb that helps you realize, oh,
that must be a constant. Capitalization alone does not make it constant. The word const does. But the
capitalization is just a visual reminder that this is somewhere, somehow a constant.
- [3:10:35](https://www.youtube.com/watch?v=undefined&t=11435s) So just a minor refinement, but,
again, we're sort of getting better at programming just by instilling these kinds of heuristics. Questions,
then, on conditionals in C or these constants? Yeah. AUDIENCE: Why do you not use semicolons after line
9 and line 13? DAVID J.
- [3:10:59](https://www.youtube.com/watch?v=undefined&t=11459s) MALAN: Yeah, why do you not use
a semicolon in lines 9, 13? Just because. This is the way the language was designed. And it's confusing
early on. Generally speaking, when you're using conditionals-- and eventually, we'll see loops-- there's no
semicolons involved. For now, assume that semicolons usually finish your thought after a function. That's
not 100% reliable of a heuristic, but it'll get you most of the way there.
- [3:11:20](https://www.youtube.com/watch?v=undefined&t=11480s) And just because. Left hand was
not talking to the right hand when some of these languages were designed. All right, so let's do
something else. How about this? If I have the ability to ask something conditionally-- is this thing true or
is this other thing-- could I write a very simple program that does something basic like, tells me if a
number the human types is even or odd? Well, let me just get the framework for that in place.
- [3:11:42](https://www.youtube.com/watch?v=undefined&t=11502s) Let me go ahead and write code
of a parity-- is a fancy way of saying even or odd. And let me go ahead and include cs50.h, include
stdio.h, int main void-- again, more on those down the road. But, for now, I'm going to go ahead and get
a number n from the user by calling get_int and asking them for whatever n is.
- [3:12:03](https://www.youtube.com/watch?v=undefined&t=11523s) And then now I'm going to
introduce some pseudocode. So here's the first example of a program, honestly, that I'm not really sure
how to proceed. So let me just resort to some pseudocode using comments. Eventually, I'll get rid of this
and write actual code. But if n is even, then print-- actually, let me just print that.
- [3:12:24](https://www.youtube.com/watch?v=undefined&t=11544s) Let me just go ahead and say
printf, quote unquote, "even", because I know how to use printf. else-- all right, I know how to printf odd,
so let me just say printf, quote unquote, "odd". So here, I've sort of taken a bite out of the problem, if
you will. And let me go ahead and put in my little placeholders.
- [3:12:43](https://www.youtube.com/watch?v=undefined&t=11563s) I want to do some kind of
conditions. So if, question marks now, let me go ahead and fill in the blanks here. else I'll put this here.
So I think I'm OK now. I'm getting closer to solving this. But I still have this question mark here. How,
using syntax we've seen, might I determine if n is even or odd? What do you think? Nice.
- [3:13:08](https://www.youtube.com/watch?v=undefined&t=11588s) There's this little operator I
mentioned by name earlier, the remainder operator, that will let you do exactly that. If you divide any
number by 2, that mathematical heuristic is going to tell you if it's even or odd based on whether there's
a remainder of 0 or 1. And that's nice because the alternative would seem to be doing something stupid
like if n == 0 or if n equals 2 or n equals 4-- your code would be infinitely long if you had to ask all
possible questions.
- [3:13:37](https://www.youtube.com/watch?v=undefined&t=11617s) But if I do n divided by 2 and look
at the remainder-- it's a little cryptic, but this will indeed do the trick. So the percent sign is the
remainder operator. It does numerator divided by denominator and returns not the result of that but,
rather, the remainder of that. So if you divide anything by 2, it's going to be a 0 or 1 remainder.
- [3:14:03](https://www.youtube.com/watch?v=undefined&t=11643s) And if, indeed, 2 divides into n
evenly, giving you 0, then you're going to print even. Else, it's got to be odd. But there is something odd--
pun intended-- in this highlighted line. What is another new piece of syntax, apparently, besides the
percent sign? What's a little off there? Yeah. Yeah, so that's not a typo.
- [3:14:26](https://www.youtube.com/watch?v=undefined&t=11666s) And I even caught myself verbally
saying it a moment ago, just because it's so ingrained. What must this mean here? Yeah. AUDIENCE:
[INAUDIBLE] DAVID J. MALAN: Yeah, if something's equivalent to the other. So now this is the equality
operator. It's not assignment from right to left. And this one too is an example of, literally, humans not
really planning ahead, perhaps, left hand not talking to right hand in that someone decided, let's use the
equals sign for assignment.
- [3:14:49](https://www.youtube.com/watch?v=undefined&t=11689s) And then some number of
minutes or days later, people are like, damn, how do we now compare for equality? Well, let's just use
two. And if you think this is a little weird, in some languages, like JavaScript, there's a third version where
you use three equal signs. So, again, it's humans that design these languages.
- [3:15:03](https://www.youtube.com/watch?v=undefined&t=11703s) So if you're ever frustrated by
them, confused by them, eh, admittedly, it might just not have been the best design. But we just kind of
have to live with it ever since. So let me go ahead and zoom out here. Let me go ahead and make parity
here. So make parity-- and, again, parity is just the name of my file, parity.c.
- [3:15:21](https://www.youtube.com/watch?v=undefined&t=11721s) ./parity, type in a number like 2.
That's indeed even. 4, that's indeed even. 3, that's indeed odd, and so forth. If we continue testing,
presumably, we'll get the same kinds of answers. How about something else? Let me go ahead now and
let me start copying and pasting some of this code because, admittedly, it's getting a little tedious to
keep typing out all of that boilerplate at the top.
- [3:15:43](https://www.youtube.com/watch?v=undefined&t=11743s) Let me create a program called
agree.c that's reminiscent of any of those forms you have to agree to online with a checkbox or typing in
yes or no or the like. So let me throw away all the guts of this main program and now ask something like
this. Let me go ahead and prompt user to agree to something.
- [3:16:02](https://www.youtube.com/watch?v=undefined&t=11762s) I'm going to go ahead and say,
how about get_string do you agree-- whatever the question might be-- and I want the human to type y
or n for yes or no, respectively. So if it's only a single character, actually, I can actually get by with just
get_char. Not used it before, but it was on our menu of functions from the CS50 library.
- [3:16:22](https://www.youtube.com/watch?v=undefined&t=11782s) And if I want to get the user's
response, the return value should be a char also on the left. So now we've seen strings, ints, and now
chars, if we only care about a single letter. And now let's go ahead, check whether user agreed. So how
about if c == "y", then let me go ahead and, inside of my curly braces, print out agreed or some such
sentence like that.
- [3:16:52](https://www.youtube.com/watch?v=undefined&t=11812s) else if they did not type c-- or you
know what? Let's be explicit here, just so they can't type z or b or some random letter. else if c=="n" n
for no, then let me go ahead and print out not agreed, or something like that. And I'm just going to
ignore the user if they don't cooperate and they type z or b or something that's not y or n.
- [3:17:15](https://www.youtube.com/watch?v=undefined&t=11835s) All right, let me go ahead now
and compile this code, make agree, ./agree. All right, do I agree? Yes. Let's go with the default. OK, so
that seems to work. No, I don't agree this time. That seems to work. How about my caps lock key is on or
I'm just really yelling, capital Y? It ignores me. Capital N, it ignores me.
- [3:17:38](https://www.youtube.com/watch?v=undefined&t=11858s) So, obviously, a bug, at least if I
want to tolerate uppercase and lowercase, which is kind of reasonable. So what would be the possible
solutions here, do you think? How do I solve this and tolerate both capital and lowercase? Maybe what's
the simplest, most naive implementation? AUDIENCE: [INAUDIBLE] DAVID J.
- [3:17:59](https://www.youtube.com/watch?v=undefined&t=11879s) MALAN: Yeah, so why don't I just
ask two questions? Or you know what, even more simplistic based only on what we've seen before-- if
you will, let me just copy and paste some of this code. Change this to an else-- whoops, not in caps-- else
if "Y". And then I bet I could do the same thing with n. But here too, just like with Scratch, as soon as you
start to find yourself copying and pasting, you're probably doing something wrong.
- [3:18:19](https://www.youtube.com/watch?v=undefined&t=11899s) And what you said verbally, if I
may, was actually better. Because you're implying that I could just say something like OR c == "Y" or,
down here, c == "N". The catch is, you can't use the word OR in C. It's actually two vertical bars. So you
can express one question or another.
- [3:18:43](https://www.youtube.com/watch?v=undefined&t=11923s) You only need one of the answers
to be yes or true, and you use two vertical bars. By contrast, just so you've seen it, if you wanted to check
if something is equal to something AND something else, you could use two ampersands. This logically
would make no sense here, though. Certainly, what the human typed can't both be lowercase and
uppercase.
- [3:19:03](https://www.youtube.com/watch?v=undefined&t=11943s) That just makes no sense. So in
this case, we do want OR. But that allows me to tighten my code up. I don't have to start copying and
pasting whole branches. I can now ask two questions at once. Questions, then, on this variation? Really
good question. Can you convert the input to all lowercase? Absolutely, you could.
- [3:19:21](https://www.youtube.com/watch?v=undefined&t=11961s) We don't have the capability yet.
It turns out that's going to require-- to be easy, another library, though we could do it ourselves knowing
a little bit about ASCII or Unicode from last week. But, yes, that would be an alternative, but more on
that a different time. Other questions? AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Good question.
- [3:19:38](https://www.youtube.com/watch?v=undefined&t=11978s) Unfortunately, you have to be
explicit in C. You can't just say this, even though that's kind of how you might think about it. You have to
ask a complete question using the equality sign twice in this case. Let me ask a question now too. It's not
a typo. I deliberately used single quotes around all of my single letters here.
- [3:19:58](https://www.youtube.com/watch?v=undefined&t=11998s) Why might that be? Previously,
we used double quotes for anything that looked like text. Yeah. Correct, string is double quotes for
multiple characters-- or even one, technically, but yes. And single quotes for single characters. Because
my data type is different. I chose the simple route of just using a single char.
- [3:20:19](https://www.youtube.com/watch?v=undefined&t=12019s) In fact, this program won't work
with Y-E-S or N-O. That's not supported at the moment-- more on that another time. I had to use single
quotes because that's how C does it. If you're dealing with single characters, a.k.a. chars, use single
quotes. If it's a string-- even if it's one single character in a string as though you're starting to write out a
longer word or sentence-- that would be double quotes.
- [3:20:41](https://www.youtube.com/watch?v=undefined&t=12041s) And we'll see why this is before
long too. But, again, just things to keep in mind whenever writing code in this particular language. Yeah,
down here. So, short answer, if I'm understanding correctly, this would be incorrect. And this would be
even more incorrect. But if you don't mind, let me kick the can a couple of weeks on this as to why this
doesn't work.
- [3:21:02](https://www.youtube.com/watch?v=undefined&t=12062s) The most pleasant way to do this
would indeed be to do something like this. But even this is a slippery slope, because what if the user
does something weird, like they capitalize just the Y? You can imagine this getting messy quickly. I like
your idea earlier about just forcing everything to lowercase just to standardize things.
- [3:21:19](https://www.youtube.com/watch?v=undefined&t=12079s) Unfortunately, you cannot
compare strings for equality like this for, again, reasons will come to before long. So for today, we're
keeping it simple, even though, arguably, it's not nearly as user-friendly to only tolerate individual letters.
And there's a question over here. On the US English keyboard it's shift and then the backslash key above
Return, but depending on your keyboard, it will vary.
- [3:21:42](https://www.youtube.com/watch?v=undefined&t=12102s) All right, so let's actually now look
back at something we did a little bit of last week. Let me go ahead and open a file called meow.c,
because, recall, that's what we had Scratch do initially. Let me include not the C50 library this time, but
just stdio.h because I only want printf for this demo.
- [3:21:59](https://www.youtube.com/watch?v=undefined&t=12119s) Let me go ahead now and just
print out meow. And then if I want the cat to meow three times, like it did last week, meow, meow,
meow. Save it. make meow, ./meow. Voila. The program is written-- correct, I claim. It ran. It compiled
OK. But, again, this was the beginning of our conversation last week of not being particularly well-
designed.
- [3:22:21](https://www.youtube.com/watch?v=undefined&t=12141s) And if someone wants to maybe
point out the now obvious, why is this not well-designed, necessarily? Yeah, it's just repetition, right?
Again, I literally resorted to copy-paste. That should be the signal that you're probably doing something
wrong or, at best, just lazy of you, in this case. So the solution, as you might glean from last week, is
probably going to be one of those things called loops.
- [3:22:43](https://www.youtube.com/watch?v=undefined&t=12163s) So let's just take a look at some of
the syntax for loops in C. But, again, no new ideas, it's just some new syntax that'll take some getting
used to. In Scratch, if you wanted to meow forever with something like this, there's not a forever
keyword in C, so this one's a little weird to look at.
- [3:22:58](https://www.youtube.com/watch?v=undefined&t=12178s) But this is the best we can do. It
turns out there is a keyword called while in C. And that kind of has the right semantics, because it's like
while I do something again and again, that's the best I can do. But just like an if condition or an else if
condition, those took a Boolean expression in parentheses, a while loop also takes a Boolean expression
in parentheses.
- [3:23:21](https://www.youtube.com/watch?v=undefined&t=12201s) So I have to ask a question. Now,
if I want to do something forever, I could kind of stupidly just say while 2 is greater than 1, while 3 is
greater than 2, or just something completely arbitrary. But that should rub you the wrong way, because
like, why 2 versus 1? Why 3-- if you want true, just say true.
- [3:23:41](https://www.youtube.com/watch?v=undefined&t=12221s) So it turns out in C, there are
special keywords, true and false, that are literally true and false, respectively. I could also put the number
1 for true and the number 0 for false, but most people would just say true to be explicit. So it's a little
hackish, if you will, but very conventional. There's no forever keyword in C.
- [3:24:04](https://www.youtube.com/watch?v=undefined&t=12244s) If I want to then print meow
forever, I'm going to just use something like printf here. So, again, not perfect translation from one to the
other, but absolutely possible in C. What about this? This is a little more common if you want to do
something a finite number of times, like repeat 3. There's a few different ways we can do this in C. Here's
one approach.
- [3:24:22](https://www.youtube.com/watch?v=undefined&t=12262s) And here's where C-- like a lot of
text-based languages, you kind of have to whip out that toolkit of all of the basic building blocks and
think about, all right, how can I build a little machine in software that does something some number of
times? Well, let me give myself a variable called counter, set it equal to 0.
- [3:24:40](https://www.youtube.com/watch?v=undefined&t=12280s) Let me create a loop whose
Boolean expression is counter less than 3, the idea being here, why don't I just kind of count 1, 2, 3? So
how do I implement this physicality in code? I give myself a variable, set it to 0, 0 fingers up. Now, I ask
the question, is counter less than 3? If so, go ahead and print out meow.
- [3:25:03](https://www.youtube.com/watch?v=undefined&t=12303s) And just intuitively, even if you've
never seen C code or any code before Scratch, what more do I need to do? I've left room here for one
more line of logic. Yeah. We have to increase counter. So I need code like I showed earlier, like counter
equals counter plus 1. And so here's where programming sometimes becomes a bit more like plumbing.
- [3:25:23](https://www.youtube.com/watch?v=undefined&t=12323s) You can't just say what you mean,
like you couldn't Scratch. You have to build a little sort of software machine that initializes a value, does
something, increments it, checks it. And so it's kind of like this software-based machine, but together,
that's just using some familiar building blocks.
- [3:25:37](https://www.youtube.com/watch?v=undefined&t=12337s) But this is pretty common. Just
like in Scratch, you might have used loops a bunch of times, pretty common in C. So can we tighten this
code up? This is correct, but here are some conventions that are popular. If you're going to count, just
say i. A convention in programming-- with, at least, languages like C-- is just use i as an integer if all its
purpose is is to count from like, 0 on up.
- [3:26:00](https://www.youtube.com/watch?v=undefined&t=12360s) Counter is not wrong. It's not bad.
It's just more verbose than you need to be. Just call it i. You don't need more semantics than that. All
right, what else can I do here? There's another opportunity to tighten up this code. Do you recall? Yeah.
Yeah, that syntactic sugar that does nothing new, but it does it more succinctly.
- [3:26:20](https://www.youtube.com/watch?v=undefined&t=12380s) I can change this to either the
intermediate format or even tighter format of just i++. Now, this is pretty canonical. This is how most
people would implement something three times using a loop in C-- using a while loop, that is. Turns out
that it's so common in C and other languages to do something finitely many times, there's a couple of
ways to do it.
- [3:26:43](https://www.youtube.com/watch?v=undefined&t=12403s) In this model, to be clear, the
logic, though, is that we start by initializing the variable, like I've highlighted here. We then ask the
question, is i less than 0? If so, everything that's indented inside the curly braces gets executed-- namely,
meow then the update. Then the computer is going to have to recheck the condition to make sure that i
hasn't gotten so big that it's greater than 3.
- [3:27:07](https://www.youtube.com/watch?v=undefined&t=12427s) But if not, it then does this again
and it does this again. And then it repeats, constantly checking the condition and executing what's in the
block, checking the condition and executing what's in the block. After three times of that, the condition
is going to be false, or a no answer, and that's it for the code.
- [3:27:22](https://www.youtube.com/watch?v=undefined&t=12442s) It just proceeds to whatever's
down here, just like with Scratch. It jumps to the next blocks down below. All right, what's another way,
though, to do this? Well, I've deliberately been counting from 0-- and that's a programming convention,
right? We started last week with all the light bulbs off, which was 0.
- [3:27:37](https://www.youtube.com/watch?v=undefined&t=12457s) So it's pretty reasonable to start
counting at 0's, just like you would here. Like, no fingers are up, this is 0-- fingers on your hand. But if you
prefer, you could start counting at i equals 1. But then you don't want to do it while i is less than 3, you
want to do i is less than or equal to 3.
- [3:27:54](https://www.youtube.com/watch?v=undefined&t=12474s) On most keyboards, there's no
symbol for less than or equal to or greater than or equal to, so in C, you use two characters, less than
and then an equals sign with no spaces in between. That just means less than or equal to. We could
change it to set i to 2 and make this condition be less than or equal to 4.
- [3:28:14](https://www.youtube.com/watch?v=undefined&t=12494s) We could make this be a 10 and
less than or equal to 12. But, again, just stick with the basics. Start at 0 and count on up would be the
convention. Or if you prefer to count down, that's fine too. Set i to 3 and then do this so long as i is
greater than 0, but you have to decrement instead of increment.
- [3:28:35](https://www.youtube.com/watch?v=undefined&t=12515s) So, again, we could do this all day
long. There's literally an infinite number of ways to implement this idea. And that's why I keep
emphasizing convention. Call the variable i for something like this, initialize it to 0 for something like this,
and just generally count up, unless you really prefer to count down.
- [3:28:49](https://www.youtube.com/watch?v=undefined&t=12529s) Again, just certain human
conventions. All right, how about another way to do this? This is what's called a for loop in C, also very
common. It's not quite as straightforward in that it doesn't really read top to bottom in exactly the same
way. This kind of has a lot more logic tucked into its first line.
- [3:29:07](https://www.youtube.com/watch?v=undefined&t=12547s) But it does exactly the same
thing. What happens here is-- notice that inside the parentheses, next to the word for, there's two
semicolons-- which is another weird use of syntax. They're not at the end of the line, now they're in the
middle of the parentheses. But that's what the humans chose years ago.
- [3:29:24](https://www.youtube.com/watch?v=undefined&t=12564s) The first thing before the
semicolons initializes your variable, int i = 0. The next thing is the condition that's going to constantly get
checked every cycle through this loop. And the last thing is going to be what you do after each loop,
which in this case is going to be count up. So, again, if I rewind we initialize i to 0.
- [3:29:46](https://www.youtube.com/watch?v=undefined&t=12586s) We then ask the question, is i less
than 3? If so, execute what's inside of the loop. Then the computer does this, it does the update,
incrementing i by 1. And then it's not going to blindly meow again. It's going to check again the
condition, is i less than 3? Then it's going to meow if so.
- [3:30:06](https://www.youtube.com/watch?v=undefined&t=12606s) Then it might go ahead and
increment i and check the condition again. So, again, this does not read quite the same simple fashion
top to bottom. You kind of read it left to right and then jump around. But, again, the initialization, the
constant Boolean expression being checked, and the update after each time does the exact same thing
as what we saw a moment ago in this while loop format.
- [3:30:33](https://www.youtube.com/watch?v=undefined&t=12633s) Which one is better? Eh, they're
the same. I think most people would probably eventually use a for loop once comfortable, but just
because is really the answer there. All right, any questions, then, on loops as we've translated them to C?
Yeah. AUDIENCE: [INAUDIBLE] DAVID J.
- [3:30:50](https://www.youtube.com/watch?v=undefined&t=12650s) MALAN: A for loop and while loop
can both be used to do exactly the same thing. There are subtle differences with issues of scope, which
we'll discuss before long, where when you create a variable in a for loop-- notice that it was, again, inside
of those parentheses, which technically means it's only going to exist in these four lines of code.
- [3:31:09](https://www.youtube.com/watch?v=undefined&t=12669s) By contrast, with the while loop, I
declared my variable outside of the loop. That variable is going to continue to exist elsewhere in my
program. So that's one of the minor differences there. Good question. But you'll see some others over
time. All right, so we claim then that it's better in some form to do this with loops.
- [3:31:27](https://www.youtube.com/watch?v=undefined&t=12687s) So let's actually jump back to the
code. Let me go ahead and now re-implement meowing with a for loop, for instance. So how about for
int i = 0, i less than 3, i++. Then inside my curly braces, let me go ahead and print out with printf, meow,
with a newline and a semicolon. So I did it pretty quickly just because I've long acquired the muscle
memory.
- [3:31:51](https://www.youtube.com/watch?v=undefined&t=12711s) But if I now make meow, no
errors there. Run ./meow. And I see meow, meow, meow. Well, let's do now what we did last week,
which was to begin to make our own custom functions, if you will, by using our own in C. So here's
where the syntax gets a little funky, but we'll explain over time what each of these keywords is doing.
- [3:32:14](https://www.youtube.com/watch?v=undefined&t=12734s) If I want to create a function
called meow-- because the authors of C did not create a function called meow decades ago-- I need to
give it a name, like meow. I need to specify if it takes any inputs. For now, I'm going to say no. And I'm
going to explicitly say no by writing the special word void.
- [3:32:34](https://www.youtube.com/watch?v=undefined&t=12754s) It's also necessary when
implementing a function in C-- which was not necessary in Scratch-- to specify what its return type is. But
for now, I'm just going to say that meow is the name of the function, it takes no inputs-- and that's what
the void in parentheses means-- and it does not return anything like ask did, or like get_string or get_int
does.
- [3:32:56](https://www.youtube.com/watch?v=undefined&t=12776s) meow's purpose in life is just to
have side effects, visual side effects by printing something on the screen. So what is meow going to do?
I'm going to have it quite simply say printf, quote unquote, "meow", backslash n. And now, just like in
Scratch, I can now just call a brand new function called meow.
- [3:33:17](https://www.youtube.com/watch?v=undefined&t=12797s) And here's where too, if you really
don't like the curly braces, technically speaking, you can get rid of them when there's only one line of
code inside your loop. But, again, stylistically, I would encourage you to preserve them to make super
clear to yourself and others what it is that's going on.
- [3:33:32](https://www.youtube.com/watch?v=undefined&t=12812s) Let me go ahead and save this
and do make meow. Whoops. Darn. All right, what did I do? Something stupid. AUDIENCE: [INAUDIBLE]
DAVID J. MALAN: Yeah, so 0 does not belong there. I meant to hit parenthesis. So let me rerun make
meow. OK, fixed. My mistake. All right, it's still working OK. But recall what I did in Scratch, kind of out of
sight, out of mind.
- [3:33:55](https://www.youtube.com/watch?v=undefined&t=12835s) And just to make a point, let me
just highlight this and move it way down in the file. Because, again, now that meow exists, it's an
abstraction. I just know a meow function exists. I want to be able to use it. So let me scroll back up. My
main function is the same. Let me go ahead and make meow again. And now, just by moving that
function, I've created all these lines of errors.
- [3:34:18](https://www.youtube.com/watch?v=undefined&t=12858s) And let's look at the first. Again,
the rule of thumb here-- it's a little small, but it says meow.c in bold-- which is the name of the file where
the bug is-- 5 is the line number, and 20 is the character. So line number is enough alone. Let's see. Oh,
this is what happens when I scrolled up too far.
- [3:34:37](https://www.youtube.com/watch?v=undefined&t=12877s) Sorry. This is the error we're now
looking at, line 7. I was looking at the old error message from earlier before I fixed the 0. meow.c line 7.
All right, apparently, C does not know what the meow function is. Implicit declaration of function meow
is invalid in C99. Well, what does that mean? Declaration of function means your creation of a function.
- [3:34:58](https://www.youtube.com/watch?v=undefined&t=12898s) Like, I'm declaring that meow
exists, but I haven't apparently defined it yet. And then C99 is the version of C from the year 1999, which
we generally use here, it's one of the more recent versions. So why is that the case? Can you infer from
the mere fact that I just moved meow to the bottom of the file-- which was fine in Scratch but now is
bad-- why is that? AUDIENCE: [INAUDIBLE] DAVID J. MALAN: Yeah, C is just kind of old school.
- [3:35:24](https://www.youtube.com/watch?v=undefined&t=12924s) It reads your code top to bottom.
And if it does not know what meow is when you first try to use it, it just freaks out and prints out these
error messages. So the solution is, quite simply, don't do that, just leave it where it was. But you can
imagine this getting a little annoying over time, if only because main is, by name, the main part of your
program.
- [3:35:47](https://www.youtube.com/watch?v=undefined&t=12947s) And, honestly, it would just be
nice if main were always at the top of your code. Because if you want to understand what a file is doing,
it makes sense to just read it top to bottom. Well, there is a solution to this. You can put functions in
different orders with main at the top so long as you-- and this is perhaps the only time copy paste is
appropriate-- so long as you leave a little breadcrumb for the compiler at the very top of your file that
literally repeats the return value, the name, and the arguments
- [3:36:17](https://www.youtube.com/watch?v=undefined&t=12977s) to that function, semicolon. This
is, so to speak, declaring your function-- and the real fancy way is this is a prototype. It's like, what is this
thing going to look like? But the semicolon means I'm not going to deal with this yet. I'm going to
actually define the function or implement it down below here.
- [3:36:35](https://www.youtube.com/watch?v=undefined&t=12995s) This is kind of a stupid detail.
More recent languages get rid of this need, you can put your functions in any order. But, again, if you just
think about the basics of programming languages like this one here-- and as you noted-- it must just be
reading your code top to bottom. So annoying, yes, but explained, yes too.
- [3:36:53](https://www.youtube.com/watch?v=undefined&t=13013s) So let me go ahead and make
meow one more time, ./meow, still working OK. And let me make one final enhancement to this meow
program here. Let me go ahead now and say something like this. Let me go ahead and say, all right,
wouldn't it be nice if my meow function could do something for me some number of times? So suppose I
want to do this.
- [3:37:15](https://www.youtube.com/watch?v=undefined&t=13035s) This meow function at the
moment is going to meow three times. But suppose I want to meow n times, where n is just some
number provided by the user. Well, just like in Scratch, custom functions can take inputs, I just presently
am saying void. But if I change this to int n, thereby telling the compiler, hey, meow still doesn't return
something, but it does take something as input.
- [3:37:41](https://www.youtube.com/watch?v=undefined&t=13061s) It takes an integer, and I want to
call it n. So this is another way of declaring a variable but a way of declaring a variable that gets handed
into, as input, the function. So now if I tighten up main here, now I can actually do something really cool
just like in Scratch, which is this. If I now look at this code-- let me Zoom in here-- now my main program
is really well-written in the sense that it just says what it does, meow three times.
- [3:38:07](https://www.youtube.com/watch?v=undefined&t=13087s) This works, though, because I
defined meow as now taking an input, an integer called n, and then using n in my now familiar for loop.
There's one change. You might have caught my one mistake. I also have to remind myself up here to
make that change too. Again, this is one of the only redundancies or copy-paste that's sort of
reasonable.
- [3:38:30](https://www.youtube.com/watch?v=undefined&t=13110s) But there, I have now a better
version. So let me go ahead and rerun this, make meow, ./meow. Voila. So, again, no change in
correctness but now, again, we're sort of modularizing our code. And, heck, what you could do now--
and this is just a tease about a feature down the road-- those header files we talked about early, those
libraries, this is the kind of modularization we're talking about.
- [3:38:51](https://www.youtube.com/watch?v=undefined&t=13131s) We, the staff, wrote a function
called get_string, get_int, and so forth, we put it in a file called CS50, and we put little breadcrumbs--
specifically, these things called prototypes-- in cs50.h. So that when you all, as aspiring programmers,
include cs50.h, you are sort of secretly telling the compiler at the very top of your code what the menu
of available functions is.
- [3:39:16](https://www.youtube.com/watch?v=undefined&t=13156s) Why? Because in CS50 is lines like
these-- obviously, not for meow, but for get_string, get_int, and so forth. And stdio.h is the same lines of
code for things like printf. So that's all that's going on there. It's just a way of telling the computer in
advance what functions to expect. All right, any questions, then, on these here? Correct.
- [3:39:44](https://www.youtube.com/watch?v=undefined&t=13184s) So if you don't mind, I want to
continue to wave my hand at that detail for today. Indeed, int main void is a little weird, because what
would the input domain be? We have no mechanism for providing input yet. And what does it mean for
main to return anything? Like, who is it returning to? For another day, if we may.
- [3:40:00](https://www.youtube.com/watch?v=undefined&t=13200s) They're going to come into play
but that, for now, today is just something you should take at face value, as necessary copy-paste to begin
programs. So meow is a function that takes an input, the number of times to meow, but it didn't actually
have a return value, hence the void. But what if we actually want to create our own function that not
only takes 0 or more inputs as arguments but also returns some value, maybe an int, maybe a float,
maybe something else altogether? Well, it turns out, in C, we can do that as well.
- [3:40:28](https://www.youtube.com/watch?v=undefined&t=13228s) Let me go ahead and create a
new file here called discount. And let's implement a quick program via which we can discount some
regular price by some percentage, as though there's a sale going on in a store. Let me go ahead and
include our usual cs50.h followed by stdio.h at the top. Let me give myself int main void as before.