Placeholder Image

字幕列表 影片播放

  • >> [VIDEO PLAYBACK]

  • >> -A triumph of mathematical and mechanical skill

  • is this great new automatic calculator at Harvard University.

  • Intricate problems in mathematics put through the machine in coded form

  • on tape are accurately solved in a minute fraction of the time required

  • for human calculation.

  • Designed to expedite all forms of mathematical and scientific research,

  • the giant mechanical brain will work for the United States Navy until war's end.

  • >> [END PLAYBACK]

  • >> DAVID J. MALAN: This, is CS50.

  • The past was a magical place, but if you've ever

  • wondered what that device is in Harvard Science Center,

  • it is indeed part of the Harvard Mark I, and it's

  • on display there now for the past many years.

  • And what you'll see embedded in problem set

  • one is a look behind the scenes at what that device did, and does,

  • from Professor Harry Lewis, who is in the computer science department here.

  • So more on that in just a bit.

  • >> But today we begin to transition from last week to this,

  • and we'll bring mention of one photograph here

  • that's actually quite famous.

  • Does anyone know what this here is on the screen?

  • At least according to myth.

  • Yeah.

  • AUDIENCE: The first virus.

  • DAVID J. MALAN: So it was supposedly the very first actual bug in a computer.

  • Now in reality the word bug, for a mistake in a program,

  • has been with us for some time in the physical world,

  • and this was one of the first documented times where someone actually

  • plucked a large moth out of the Mark II computer, put it in the binder,

  • and then recorded it as the first known bug

  • to have actually been found physically.

  • So thus was entered that into today's lore.

  • Everything from last week is available online.

  • In particular resources that you might not necessarily notice, especially

  • if coming here in person.

  • So one of the teaching fellows every year makes a canonical set of notes,

  • so you needn't keep your head down, trying

  • to transcribe most anything that might happen here in class.

  • So realize that those so-called [? Scribd ?] notes

  • are online, certainly the slides and source code that we use in class.

  • We make full text transcripts available as well.

  • So if you vaguely remember some term, and want to look it up a little

  • more readily, realize that all of that is searchable online.

  • As well as walkthroughs as well.

  • Indeed, today we begin to look at a lower level programming language,

  • soon to be known to us as C, and it's very easy

  • to sort of get lost after just a few moments time, and then sort of wonder

  • how we actually got to some more complex example from the simplest.

  • And so realize we'll cherry pick during class some

  • of the representative examples, but realize that so much more is also

  • available online so you can work through it at your own pace,

  • however you're most comfortable.

  • >> But we left off with programs that were just this.

  • So I click the green flag, and what does this program do?

  • Says, hello, world.

  • It makes the cat say hello in his little world

  • on the top left hand side of Scratch's user interface.

  • But we started to introduce some terminology last week.

  • And so, things like this we called what?

  • What might be the generic term for this?

  • Yeah.

  • A method or a statement.

  • Sure, it's an action.

  • Do this.

  • And yet there were other shapes to scratch,

  • besides these so-called statement, they were also

  • things like this, which we called what?

  • Yeah.

  • So a loop.

  • And in this case, this thing repeats, obviously, 10 times.

  • There's a forever block.

  • There's a couple of others.

  • And today we're going to see how to translate this to a more modern world,

  • if you will, even though it's going to appear at first glance

  • to be much more arcane.

  • But those were indeed loops.

  • And then we have things like this, which had this shape,

  • and essentially asked questions.

  • And we referred to these as what?

  • >> AUDIENCE: Boolean expression.

  • >> DAVID J. MALAN: Yeah.

  • Boolean expression.

  • Something that is either true or false, yes or no, one or zero,

  • however you want to view the world.

  • But we called those Boolean expressions, and we plug them

  • into conditions, or branches.

  • And in the world of Scratch, everything nicely

  • interlocks in terms of its shape, if it makes logical sense to do so,

  • but you don't have to stop at just doing one thing conditionally,

  • you can have a proverbial fork in the road

  • and do either this or that if the condition is true or not.

  • And then, as you'll see, if you haven't already,

  • you can even nest these blocks.

  • So Scratch grows to fill the shapes that you might want to insert into it,

  • so you can actually express most any logic that you might want to do.

  • >> Now a few words of announcement for both here and New Haven.

  • So sectioning begins today.

  • The URL for this is on the course's website, for those unfamiliar.

  • Sections will be an opportunity to get together

  • in a much more intimate environment than [? Sanders ?] allows,

  • with 10 to 20 classmates with a teaching fellow at different tracks,

  • and different paces, if you so choose.

  • Indeed the course offers different sections

  • for those less comfortable, more comfortable, and somewhere in between,

  • and the role of thumb here, is there's no formal definition,

  • but if you're less comfortable, you kind of know it.

  • You're a bit intrepid about the idea of stepping foot, perhaps, back

  • in the classroom, or showing up to an in between section.

  • If you've been programming since you were six years old, more comfortable,

  • perhaps to you.

  • And if it's kind of a toss-up, that's exactly what the in between

  • is meant to be.

  • >> Now, in the meantime, for a class this size,

  • it takes us quite some time to section everyone, so we have so-called,

  • super sections, coming up this weekend and early next week,

  • so take a look at the course's website for those that are course wide,

  • and also filmed for those who cannot make them.

  • And sections themselves will start in week three of CS50 zero index.

  • Problem set 0 is already on the course's website.

  • And realize that you do have nine late days this semester.

  • So this is an upgrade from last year, when we had five late days.

  • >> So if you're wondering, we have nine problem sets.

  • You have nine late days, each of which you can only spend one per week on.

  • That effectively makes all of the problem sets due Friday,

  • but the goal is to put a little bit of psychological pressure

  • on you to try to get things in on Thursday

  • because as you will soon discover, for better or for worse, most

  • everything in the software world ends up taking much longer than you anticipate.

  • So consider it a sort of opportunity to help

  • yourself to target Thursday, but take comfort in that, yes,

  • everything is due Friday.

  • And if you submit them on Thursday, that's wonderful.

  • There's no bonus points or extra credit for meeting that deadline.

  • It's meant to be a crutch only.

  • >> And office hours, both here and in New Haven,

  • in person, if you'd like to meet with any of our courses

  • teaching fellows and CAs for assistance.

  • >> All right.

  • So now, let's make this more real.

  • And for a moment it's going to start to get a little more arcane, but very

  • quickly on Friday and next week we will be

  • able to do so much more than we could thus far in Scratch.

  • So we introduced code last week, albeit pictoral.

  • It was puzzle pieces.

  • And we formalized that it is something called source code,

  • but would did look like?

  • Well, it looked a little something like this.

  • And, in fact, just to kind of tease where we can go with this.

  • I'm going to go ahead and open up a window here,

  • and I'm just going to choose New File.

  • And I'm going to save this file as something like hello dot c.

  • More on that choice of words in just a moment.

  • And this is how someone might go about writing a program.

  • Include standard I/O dot h, int main void, and then open curly brace, close

  • curly brace, printf hello comma world, backslash n, semicolon, Save, done.

  • OK.

  • I've written my first program, but it's going

  • to be incredibly underwhelmed because when I actually

  • run this program, of course, by first writing make hello,

  • and then dot slash hello, and we'll go through this in much more detail

  • in a moment, that's all it does.

  • Doesn't pop open any windows.

  • It doesn't do anything particularly fancy.

  • All it does is what I told it to do, but how do we get to that?

  • And how do we build upon that?

  • Well, let's introduce a few different concepts here.

  • >> So one, we're about to start using a program called a compiler.

  • A compiler exists for Mac OS, Windows, Linux, any number of operating systems,

  • and we'll soon introduce you to the environment that we will be using,

  • but a compiler is just a general term for a piece of software

  • that takes source code and produces object code from it.

  • In other words, if you have as input your source

  • code, something like what I just typed, it ultimately produces object code.

  • And object code is really just a fancy way of saying it outputs

  • zero's and one's.

  • In other words, source code, even though it's

  • going to look a little arcane, and a little less friendly

  • than Scratch at first glance, it's at least something

  • that we humans can understand.

  • It kind of looks and sounds like English even though it's very succinct.

  • Whereas programming in zero's and one's, as our predecessors had to do,

  • some years ago, was not a particularly fun challenge.

  • And yet, this is what the computer still understands.

  • So compiler gets us from source code to object code.

  • It's like opening a file with the compiler, and when you save it,

  • you get those zeros and ones.

  • So we'll see how to do that in just a moment.

  • >> But let's now try to provide a bridge between last week and this,

  • so that even as we get hung up on things like curly braces,

  • and semicolons, and parentheses, and all of these intellectually interesting

  • aspects of programming, that very understandably trip people up early on,

  • realize that all of the ideas today, are the same as last.

  • So last, we saw this example here, Hello World program.

  • Well, starting today, if we want to implement that same program,

  • it's going to look a little something like this.

  • And we'll explain before long what int, and main, and void, and all of that

  • is, but at least I see here already the familiar Hello World.

  • >> Meanwhile, things like function, say hello,

  • is very simply going to be a very simple statement like this.

  • Printf hello world, with some quotes, with a backslash,

  • with a semicolon, but more on that to come.

  • Loops, meanwhile, this is a loop that, of course, says hello world forever,

  • and this one's going to look a little weirder than in the last,

  • but if I say something like, while true, I

  • can then print out Hello world again and again and again and again.

  • And then as you can kind of start to infer here,

  • this convention of using curly braces is kind

  • of like embracing the indented line of code there.

  • The printf line of code much like the yellow puzzle piece up above.

  • So we're not going to say forever, we're going to say while true,

  • but we'll see why that is in just a bit.

  • >> Meanwhile, if we have a loop like this, where you repeat something a finite

  • number times, this one too it's going to take a little getting used

  • to, but it's going to say for int i gets zero, i less than 10, i++,

  • and then printf again.

  • But there's going to be patterns.

  • And for those of you familiar with AP Computer Science, or Java,

  • or any number of other languages, almost all of these constructs

  • thus far should look mostly familiar.

  • >> So variable.

  • So here's a simple Scratch excerpt that sets a variable called counter to zero,

  • and then forever says the counter, and then it changes the counter by one.

  • So it starts with zero, and then one, two, three.

  • Well, how might we introduce that in this new language a little similarly?

  • Int counter equals zero, while true, which

  • is that same idea from before of doing something forever,

  • and I'm just going to printf.

  • And now it's getting a little strange, percent i, but more on that to come,

  • but these ideas are exactly the same.

  • We just now have to adhere to the syntax that the computer is

  • going to demand of us.

  • >> Boolean expression, something like sx less than y, or x less than y,

  • and y less than z.

  • It's going to look a little something like this, with parentheses just

  • like in math for good measure to just ensure that the order of operations

  • is as we intend.

  • And now, conditions.

  • So if x less than y, say x is less than y, else if x is greater than y,

  • say x is greater than y, otherwise logically, x must be equal to y.

  • So you might implement something like this in Scratch,

  • or you might implement it in this syntax.

  • And again, the curly braces are kind of giving us the same shape as the puzzle

  • pieces, but the syntax, now, is perhaps starting to follow some patterns.

  • >> So, what does this give us here?

  • So we're back then at our main program.

  • Let's actually, now, do something with it.

  • So in this very first line we see sharp include,

  • standard I/O, dot h, and then some weird angle

  • brackets below that. int main void, and then printf hello world.

  • So just take a step.

  • Someone who's never programmed before, at least explain one of these lines.

  • Using perhaps some of the jargon of last week.

  • What is printf, for instance?

  • Yeah.

  • >> AUDIENCE: [INAUDIBLE]

  • >> DAVID J. MALAN: Yeah.

  • It's a function, or a statement, from last week's terminology.

  • And it's an action that's doing something.

  • Now this function, or this statement, does

  • it appear to take some kind of input?

  • And in what sense does it seem to take input, if so?

  • Input, what do I mean by that?

  • Yeah.

  • >> AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Yeah.

  • So Scratch out a few puzzle pieces where you can parametrize it.

  • You could tell the repeat block how many times to repeat,

  • so that was an input of sorts, and just like Scratch

  • had a little white placeholder for the number you would type in,

  • so do we use in this text world parentheses

  • to give us that circular shape into which we can plug our input.

  • After all, tens of years ago when this language, called C,

  • was invented no one anticipated it's one of us,

  • these some years later, we'd want a function that says hello.

  • Right?

  • It'd be nice if there were a puzzle piece, or a key words,

  • that's called hello, that literally just says hello.

  • But we don't need that.

  • We can have something much lower level, like a generic print

  • command, that takes input, and prints whatever we want.

  • So notice now the pattern that will start to emerge.

  • Open paren, close paren is apparently embracing the input,

  • and then I see double quotes on both ends.

  • There is still weird backslash n, but hopefully now you

  • can start to visualize at least that printf is taking in some form of input.

  • Not as user friendly as Scratch, but it's just

  • like having plugged in some value there.

  • >> All right, now let's actually do something with this.

  • So some of you might know from having shopped the class before,

  • chatted with friends, that for many years

  • we used something called the CS50 appliance.

  • This was a term describing a technology called the virtual machine, which

  • is a piece of software that your predecessors would download off

  • the course's website for free.

  • They would also download a free piece of software called a hypervisor,

  • something called like VMware, or VirtualBox, or any number

  • of other tools, and on their Mac or PC or whatever computer they had last year

  • and in years past, they would double click an icon

  • to run this software called the hypervisor.

  • And then inside of that program they would open the CS50 appliance.

  • And the effect was that whether a predecessor of yours

  • was running Mac OS or Windows, he or she then

  • subsequently had a new window on their screen running CS50's environment.

  • So a virtual machine lets you run one operating system inside of another,

  • and that's exactly what we did.

  • Throughout most of the course we use an operating system called

  • Linux, specifically Ubuntu Linux, and this

  • is a very nice way in years past of making sure that everyone in the course

  • just had the same environment.

  • And you can use any kind of computer that you actually want.

  • Among the downsides, though, were it was like two gigabytes

  • to download this thing.

  • It didn't necessarily work super well on people whose computers that maybe only

  • had a gig or so of RAM, or not too many gigahertz of CPU speed,

  • and so this year, for this and other reasons,

  • have we been transitioning to this environment.

  • The so-called CS50 IDE, which is based on open source software,

  • and a supported platform that a company called Cloud9

  • supports in Europe, that allows us to have essentially

  • that exact same environment as in years past, but in the so-called cloud.

  • >> So what is the cloud?

  • You've probably clicked its icon on your Mac or PC.

  • What is the cloud?

  • Yeah?

  • A little louder.

  • Yeah.

  • It's just a bunch of computers elsewhere that store data and run

  • software and the like.

  • So it's really just to the word de jure of describing [? rentable ?]

  • computers that live elsewhere.

  • That you, yourself, don't necessarily own.

  • So when your data, or your programs, are in the cloud,

  • it just means it's on someone else's computers elsewhere,

  • and not, most likely, on your physical laptop or desktop.

  • And so that's what CS50 IDE now is.

  • >> Henceforth you'll go to a certain URL that will introduce you

  • to, in problem set 1, later this week, that will then, upon logging in,

  • give you this environment.

  • And much like Scratch had a few different panels involved,

  • so does CS50's IDE have exactly that.

  • On the top left hand side here, you're going to see a familiar file browser.

  • So any of the files that you put into this environment

  • are just going to be listed there.

  • Files and folders.

  • On the top right here, you're going to see a place

  • where you can actually write your code.

  • Initially in C, later in the term in PHP and JavaScript,

  • and, perhaps for final projects, Ruby or Java or Python

  • or any number of languages are supported in this environment.

  • It's not at all tied to CS50 specifically.

  • >> And then in the bottom corner here, especially for those more comfortable,

  • you have something called a terminal window.

  • And this terminal window allows you to execute any command that you

  • want in this cloud environment.

  • It allows you to install any additional software

  • that you want because you have so-called root, or administrator privileges.

  • So in other words, even though this is ultimately hosted in the cloud,

  • and even though we have simplified it to some extent

  • by default, literally introducing, thanks to Dan Armendariz, and Dino,

  • and Dan Bradley on CS50's team over the past many months, a less comfortable

  • mode that hides a lot of the initially confusing distractions.

  • Everything there exists to represent a typical computer system

  • that you might use in the real world.

  • >> So what does this all mean, and what can we actually do with it?

  • Let's actually take a peek at a program.

  • So I'm going to open up the actual environment here,

  • which is just our Chrome browser, which could be Firefox,

  • could be Internet Explorer, or some other browser as well.

  • It's full screened here for the moment.

  • And I want to actually tease apart what this program was.

  • So let's take a look, by zooming in up top, at what I did, and how I got here.

  • So here's those exact same lines of code.

  • So this is the statement, as you mentioned earlier.

  • The function that says hello.

  • The curly braces, on line four and line six,

  • presumably kind of envelop that line to associate it

  • with the essence of my program.

  • So indeed, this keyword main, that's up there on line three,

  • you can think of that as the equivalent of that when green flag clicked,

  • block in Scratch.

  • So that's all we have here, is when green flag clicked, print, or say,

  • hello world.

  • >> But let's take a guess, now, at what this first, more arcane line, is.

  • Sharp include, open angled bracket, standard I/O dot h,

  • close angled bracket.

  • What is that perhaps doing, especially if you've programmed

  • before in some other language.

  • Yeah?

  • >> AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Yeah.

  • Exactly.

  • So this is giving you access to a library, so to speak, of functions.

  • And a library of functions is functions that someone else

  • wrote, either recently or long ago, so that you

  • don't have to reinvent the wheel.

  • For instance, we don't have to know, in 2015,

  • how to write code that puts a character on my screen.

  • Someone else has figured that out with the printf function

  • so that I can sort of abstract away from that, or layer

  • on top of his or her code, and now just take

  • for granted that any program I write can print something to the screen.

  • So this first line there in step one includes the so-called header file

  • for the standard library.

  • So see, the language in which we're now working

  • had a standard library with lots of functions.

  • Printf is one of them, and that first line of code

  • means give me access to those functions, so that I

  • don't have to reinvent that wheel.

  • >> Now let's go to the bottom window and do exactly what I did before.

  • A moment ago, when I was whizzing through the code,

  • typing it out for the first time, I very quickly typed, make hello.

  • What was that all about would you guess?

  • What was I doing?

  • Yeah?

  • Never mind.

  • It's OK.

  • Yeah.

  • AUDIENCE: Compiling it.

  • DAVID J. MALAN: Yeah.

  • It was just compiling it.

  • Right?

  • Even if this is completely unfamiliar to you, it's pretty English like.

  • It's certainly not zeros and ones, so I still

  • need to run it through that so-called program, a compiler, that's going

  • to take it as input and produce output.

  • And at least, for now, in this Linux environment,

  • but this isn't prerequisite, this is just where we'll begin,

  • you generally interact with computer systems like this

  • by a so-called command line.

  • So with a blinking prompt and your keyboard,

  • and not so much your mouse, and not so much windows and icons and menus,

  • rather everything is textual.

  • And even though this feels, at first glance, perhaps, like a step backward,

  • it's actually a much more powerful and expressive way

  • of controlling a computer especially as you,

  • yourself, become more comfortable from less.

  • It's going to allow you to do a lot more things than just what Microsoft

  • or Apple allows you to do with their graphical operating system.

  • >> So when I hit Enter, now, nothing happens this time

  • because apparently the program hello is up to date.

  • And what was the command I ran after that?

  • Dot slash hello, and hello world.

  • Now let's take a step back for just a moment.

  • It turns out that make hello is just a simplified way of compiling a program.

  • Let's just introduce one piece of jargon,

  • but then move away from it, since it's not

  • all that interesting for today's purposes.

  • The compiler we're going to start using is called Clang, for the C language.

  • And this is a program that's run just like we did at the command line there.

  • And so if I want to compile a program that I

  • have written in a file called hello dot c, I literally just type clang hello c

  • enter.

  • >> The funny thing is, for historical reasons,

  • if I don't specify the name of the program

  • that I want to output, if I don't specify a file name for those zeros

  • and ones, a very weird convention was adopted years ago

  • where it felt kind of a good answer at the time,

  • well let's just arbitrarily call the first program we compile a dot out.

  • A comes first in the alphabet.

  • Out, it's the output.

  • So we will call the program by default a dot out.

  • And that's fine because if I want to run the program in this text based

  • environment, I just type dot slash a dot out enter.

  • And it would run exactly as before.

  • So let's see that real fast.

  • >> So if I go back to my source code in CS50 IDE,

  • and I instead do this, clang hello cot c enter, notice nothing seems to happen,

  • but almost always in this Linux environment,

  • when nothing bad seems to happen, that means something good actually happened.

  • If you don't see an error message, all is probably well.

  • And indeed, if I now do dot slash a dot out enter, I've run my same program.

  • I still have the hello program from earlier because indeed, it's

  • going to be so much nicer just type make instead of remembering

  • these incantations of commands, but let's see how we

  • might improve upon this, nonetheless.

  • >> So, this is the command to compile the code.

  • This is the command to run the compiled code thereafter.

  • How can I specify a file name?

  • Well let's just introduce one more piece of jargon.

  • When you're in this text based environment,

  • you're going to want to specify things called command line arguments.

  • Which is just a fancy way of saying, other words at the prompt that

  • tweak the behavior of whatever program you're running.

  • So specifically the program I'm running, by typing the name clang,

  • is the program called Clang.

  • So this is or the old school equivalent of double-clicking

  • on Microsoft Word or some program's icon.

  • I'm instead just typing its name, but in this version of the command,

  • I then have dash o space hello space hello dot c.

  • So even if you've never played in this world before,

  • just take a guess what does dash o hello do?

  • Or what does it tell clang to do?

  • Yeah.

  • >> AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Yeah.

  • It just sets the file's output to be hello instead of the sillier a dot out.

  • And, indeed, let's try this.

  • So if I now do, first let's do this, r m hello.

  • That's remove.

  • And it's going to say remove regular file hello, which

  • is unnecessarily arcane, but it just means do you want to delete the file,

  • and I type y for yes.

  • And notice, if you were glancing at top left, it did disappear from up there.

  • Let me now do clang dash o, and I'm going to go ahead and do hello,

  • in all caps, hello dot c.

  • Now, if I do dot slash hello, what would you expect?

  • >> Maybe it's case sensitive.

  • Maybe it's not.

  • Let's see.

  • No such file or directory.

  • And we can kind of cheat by just looking at top left.

  • What did I obviously save the program as?

  • All caps hello.

  • So indeed, that's going to make a difference.

  • So now hello world is back.

  • Now this is all fine and good, and all very mind-numbing,

  • to sort of get bogged down in these details, but understanding them

  • is good.

  • >> Typing out these increasingly long incantations

  • is not going to be particularly fun, so people in the world

  • invented another program, some years ago, called

  • make, that quite simply allows you to type make,

  • the name of the program you want to make, and make

  • is smart in that it will look in the current folder, aka directory,

  • and if you have a file called hello dot c,

  • that is the file it will compile for you into a program called hello.

  • So it's a much simpler way of doing the more cryptic commands

  • that we did earlier.

  • And we have pre-configured CS50 IDE to know

  • that it should do dash o and a number of other flags, or command line arguments,

  • that we'll see before long.

  • >> But let's now actually do something more interesting.

  • Just typing out hello world all the time isn't all that compelling.

  • So let me go ahead and change this program and say, hello David.

  • Now I'm going to go down here.

  • I'm going to go ahead and do make hello.

  • And again now you're seeing make is showing you the longer

  • sequence of commands that it's doing because we've pre-configured

  • it that way, but we needn't worry for now

  • what it's doing other than it is compiling dot slash hello, hello world.

  • What's wrong?

  • Yeah.

  • >> AUDIENCE: [INAUDIBLE]

  • >> DAVID J. MALAN: Yes.

  • So, noobie mistake.

  • I notice this is super subtle, but the asterisk here, in the tab,

  • just means you haven't saved it, and browsers and other programs

  • do this sometimes.

  • So control s or command s should save it.

  • Now let me go ahead and re-run the program.

  • Recompiled.

  • Right, so it's a multi-step process.

  • None of which is particularly complex, but any time we change the file,

  • we need to change the resulting zeros and ones, and dot slash hello.

  • Wow.

  • So this is very underwhelming that we just printed out this.

  • Let's make things a little more dynamic, now.

  • >> Well it turns out, that like Scratch, C supports variables.

  • And it turns out if you want to have a variable that stores,

  • not a number like we typically did with Scratch,

  • but like a word, or a phrase, or a name, you

  • can specify a variable to be of type string.

  • So we'll see more of these in just a moment,

  • but it turns out that C, and a lot of languages like it,

  • have very specific types of variables.

  • It's not just like algebra, where you get an x, a y, and z

  • and those are just always numbers, in a programming language

  • you can store numbers, you can store individual characters,

  • you could store whole words, you could store whole images, whole sound files,

  • and the like.

  • So we need to specify what the type of our variable is.

  • And by convention I'm going to call my variable s just because it's succinct,

  • and s stands for string.

  • >> And then I'm going to store inside of this variable,

  • just like with Scratch when I use the set block,

  • I'm going to use the assignment operator in C,

  • which nicely enough is the equal sign, but the equal sign does this.

  • It moves something from the right into the left hand side of the expression.

  • So what I have done, to be clear in line five, is I've declared a variable.

  • I've told the computer, give me memory for a variable, call it s,

  • and know in advance that I'm going to be putting a string in it.

  • And a string is just a sequence of characters.

  • It's a word, or a phrase, or paragraph, but it's a sequence of characters.

  • It's not numbers.

  • What string am I going to be putting into that variable?

  • Well, apparently quote unquote, "Hannah".

  • Now notice the pattern that's emerging.

  • There's a left hand expression.

  • There's a right hand expression, separated by the equal sign.

  • And just take a guess, if unfamiliar nonetheless,

  • with what the semicolon is doing.

  • What's it presumably doing for us?

  • Yeah?

  • >> AUDIENCE: [INAUDIBLE]

  • >> DAVID J. MALAN: Yeah.

  • It's just terminating the line.

  • Right?

  • The computers want to be bluntly told, literally, what to do.

  • Like the peanut butter and jelly sandwich example,

  • if you don't specify exactly what you intend,

  • it might be left to judgment or error.

  • The semicolon means, that's it for this command.

  • It's being super precise, and it's a very easy thing to leave off.

  • So now this program doesn't seem to be behaving any differently until I

  • make it more dynamic.

  • So rather than hard could my own name, let me put a placeholder value.

  • And it turns out that, and I know this from having read the documentation,

  • and used the function for years, that printf was designed to be dynamic.

  • You don't have to know in advance what words you want to put in it.

  • You can instead have a placeholder, like percent s, which

  • is going to tell the computer, put a string here.

  • >> Which string do we want to put there?

  • Will turns out that between these parentheses, which remember, demarcate

  • the input to a function, if you have comma separated list of things here,

  • we actually pass in multiple inputs to a function.

  • So just like some Scratch blocks, you might not

  • have played with them for your projects yet or at all,

  • some Scratch blocks have two inputs.

  • Two white boxes you can type numbers or words into.

  • So to does C have functions like printf that can take two inputs.

  • And again, this comma is separating them.

  • So in English, again even if you've never programmed before,

  • by that logic printf is a function, takes two inputs, one of which

  • is a placeholder, what would you hope printf is going to do with this input?

  • It's going to say hello Hannah, hopefully.

  • So to do this we need to save it.

  • I'm going to zoom out.

  • I'm going to go back down here and say, make hello enter.

  • Oh.

  • Something's wrong, and actually this is quite overwhelming.

  • I have six lines of code and like 12 lines of errors.

  • So, look forward to this.

  • So what's going on here?

  • So the best rule of thumb here, especially when you're starting out,

  • don't get distracted by the stuff at the bottom.

  • In fact, I very deliberately just scrolled up

  • because very often, a computer is not all that bright of a device.

  • It's going to get confused.

  • If it sees one thing that it doesn't understand,

  • it might mistake other lines of code as incorrect,

  • even though they're perfectly fine.

  • So you always want to work top down through the errors.

  • And admittedly this is super arcane at the moment.

  • >> Error, use of undeclared identifier string.

  • Did I mean standard in?

  • No, I meant string.

  • And it's pointing at, maybe it's a typo.

  • Sort of like, spell-checking in a very geeky way, but the reason here,

  • and you'll know this just by practice, is

  • that it turns out that string does not exist in C. In fact, C

  • is a particularly old language, and it's very low level,

  • and it doesn't a lot of features that you might have grown up

  • with if you have, in fact, programmed in Java, as some of you have,

  • but you can teach it new things.

  • And indeed, we have done this CS50, even though by the end of the semester,

  • all of these training wheels will have been taken off,

  • what we do in these first couple of weeks

  • initially is we also have you include CS50 dot h.

  • Which is a header file.

  • More on that in the future.

  • >> It's a file that contains functions that we have written for you.

  • And among those functions is functions for getting input

  • from the user, like strings, and numbers, and real numbers, and more.

  • But it also has a few shortcuts.

  • It defines a Boolean as a data type.

  • C does not have true or false.

  • But we can give it the notion of true or false by way of this library.

  • And it also doesn't have a string built in until we include this line.

  • So this line of code tells the compiler, hey, Clang, look

  • in this file for a whole bunch of code that CS50

  • has written so that anyone using this file

  • can take for granted the features they've added.

  • >> So now let me go ahead and clear the screen,

  • and if curious I'm just saying control l each time to clear the screen,

  • or you can type clear.

  • I'm just going to go ahead now and type make hello, and all is well.

  • Still an arcane output, but it's not an erroneous message.

  • Now if I do dot slash hello, and as an aside, dot slash is unfamiliar to most.

  • It just means, dot means the current directory.

  • So if you've ever used DOS, or Linux, or Unix

  • you might have done something like cd dot dot, where

  • dot dot means go backwards in time, or go backwards, or higher up

  • in your directory.

  • Dot just means look in the current directory.

  • I want hello, which is right here.

  • And indeed, now it says hello Hannah.

  • And if I change it again to Maria, recompile

  • it would make hello, dot slash hello, the program keeps changing.

  • >> But this is still a pretty stupid program.

  • If I want to ship this program, or give it to a friend,

  • or give it to Maria or Hannah for that matter, that's fine.

  • I can customize it for both of them, but it's never going to be dynamic.

  • They can't change it once it's compiled down into those zeros and ones.

  • So what we did something smarter?

  • What if, instead of hard-coding into the program this person's name,

  • what if we just get the string on demand?

  • >> So in other words, let me do this.

  • I'm going to recompile this program.

  • And let me zoom in at the bottom now.

  • Make hello dot slash hello.

  • Now nothing is happening.

  • Can we get a quick volunteer to make something happen?

  • Come on up.

  • What's your name?

  • >> PANCHO: Pancho.

  • >> DAVID J. MALAN: Pacho?

  • Come on up.

  • Once you get up here, this is going to be very underwhelming,

  • but it's more interesting than me typing it.

  • If you want to go ahead and type your name, I made this program just for you.

  • Just type your name.

  • Enter.

  • Wonderful.

  • All right, thank you very much.

  • >> PANCHO: Thank you very much.

  • >> DAVID J. MALAN: OK.

  • >> [APPLAUSE]

  • >> That was very kind.

  • Here you Go.

  • What did we do?

  • So we finally, at least, made this program dynamic.

  • Now it's still certainly by design underwhelming,

  • but we finally have the ability now to write code in advance, much

  • like the authors of printf, and then dynamically

  • adapt based on the input we are handed, either by the programmer,

  • or by the human who's actually interacting with the program.

  • So let's actually now try to do a few other constructs

  • and see if we can't make the programs themselves more sophisticated.

  • So let's go ahead and instead of writing this one from scratch,

  • I'm going to go into today's source code, and all of this

  • is available on the course's website, and you can drag and drop it

  • into your own environment come Friday once problem

  • set 1 gives you the instructions, or you could

  • do it at the command line as well, but more to come in p set 1.

  • >> Let's see what's in this program here called, adder dot c.

  • Now I've already commented it, but what's new?

  • At first glance, what line of code is absolutely new?

  • Probably not fundamentally unfamiliar now.

  • >> AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Yeah, so int x equals get n.

  • We've never seen any of this before, but int is probably

  • the symbol for what kind of variable, [? from ?] an integer.

  • So int for integer.

  • It's just being more succinct. x is just arbitrary.

  • x and y and z are still pretty reasonable for numbers,

  • but generally we'll use words once our programs get more complex.

  • Get int, of course, much like get string,

  • is probably going to get an int from the user,

  • and then we're going to do the same thing with y.

  • And apparently, even though this last line, 24, looks more complex,

  • it's going to say the sum of this and that is this other thing.

  • >> And notice percent i probably denotes what?

  • What role does percent i apparently serve?

  • It's just placeholders.

  • Right?

  • Percent s was for string, percent i is for integer.

  • There's going to be percent f for floating point value, which

  • is just a number with a decimal point.

  • In other words, a real number.

  • So let's actually compile this program real fast and do make adder.

  • Whoops.

  • Good opportunity.

  • So notice here, all this time my user name for this account is jharvard.

  • I am in CS50 IDE, aka IDE50.

  • And now in blue here, is a mention, but from the environment, of where I am.

  • And in fact, if I zoom up to the top here, notice that I have an IDE50

  • and I see this word, if you kind of turn your head to the side, workspace.

  • In a nutshell, the fact that I'm seeing tilde slash workspace

  • means that in this text based command environment,

  • I am in the text equivalent of this graphical folder up here.

  • So when I do dot slash adder, it's saying no rule

  • to make target adder stop.

  • I don't know what that means, but it's bad for some reason.

  • No such file or directory is even more clear.

  • Where is it then?

  • Based on where I clicked to open the file earlier.

  • Where might it be?

  • >> It's probably in source one w, for Wednesday,

  • and I can see it, indeed, if I open that up.

  • There is adder dot c, but in this text-based environment,

  • again, I have to be super explicit.

  • If I want to change directories into source one Wednesday, I need to type,

  • cd space source one Wednesday enter and now

  • my prompt changes to give me a visual cue that

  • I'm, indeed, elsewhere in my environment,

  • but now I can do make adder, it seems to work,

  • dot slash adder, give me an integer.

  • One.

  • Two.

  • The sum of one and two is three.

  • But if I don't cooperate, let's see what get int does.

  • Give me an integer.

  • No.

  • Retry.

  • OK.

  • Fine.

  • 50.

  • 50.

  • All right.

  • So it finally cooperates.

  • And that's some of the functionality that these functions

  • can have built in for you.

  • >> So inside the implementation of get int, what programming

  • construct from last week is probably in there to compel this behavior of retry,

  • retry, retry?

  • Yeah, there's probably some kind of loop.

  • It's going to look syntactically different from Scratch's blocks,

  • but that's indeed what's inside the file.

  • And in a few weeks time we'll actually look at the CS50 library

  • as to what's inside, but there's a loop imposing exactly that.

  • >> Let's play a silly little game here with this file.

  • Condition zero dot c.

  • We're just going to generally start counting at zero.

  • And I keep scrolling past some of the stuff at top.

  • In blue here, demarcated with all of these stars and the slashes,

  • those of you who program before, what is that?

  • Why do we keep skipping over it?

  • What?

  • >> AUDIENCE: It's a comment.

  • >> DAVID J. MALAN: Yeah.

  • It's just a comment.

  • This is a comment for humans to read, that in our case,

  • tells us what the name of the file is, who wrote it, and what it does,

  • so that you don't have to read through the code

  • to know what this file is all about.

  • It's just a description, but functionally it's irrelevant.

  • And I can even delete it from my file and it has no impact.

  • The juicy lines are these first two here, now.

  • int main void is the equivalent of our when green flag clicked,

  • so we're going to leave that be for today, and we'll come back,

  • eventually, as to what int and void are, but now,

  • notice what I've done here too, you can also,

  • if you have an in line comment, if you have

  • a thought you want to share about a particular line of code,

  • convention in C is to do slash slash, and then a few succinct words,

  • not a full sentence, generally not a paragraph,

  • that just explains to you, or your TF, or other readers or colleagues,

  • what your code is doing.

  • So rather than have to look at this and infer from lines

  • nine and 10, what is this doing, in line eight

  • I just give a comment to ask the user for an integer,

  • then I don't have to think too hard about what lines nine and 10 are.

  • I just know that that's what collectively they're doing.

  • >> How?

  • Well, here's the prompt.

  • Here's the getting of the int.

  • And based on the previous behavior, it seems like get int

  • is smart enough to make sure that the human cooperates and gives it

  • an integer.

  • But now we see this.

  • And this is reminiscent of Scratch.

  • I'm claiming that I'm analyzing the user's input,

  • but somewhat inaccurately.

  • And this apparently in C is how we express an if else, a branch,

  • a fork in the road, but I say somewhat inaccurately.

  • What's the bug in this program?

  • About further back?

  • Yeah.

  • >> AUDIENCE: [INAUDIBLE] zero.

  • >> DAVID J. MALAN: Yeah.

  • So if I type in zero, let's consider exactly that case, or corner case,

  • if you will.

  • So if I type in zero, which of the blocks is going to apparently grab it?

  • The if or the else?

  • The else, and it's going to say, you picked a negative number.

  • And indeed, zero is not supposed to be positive or negative,

  • and so that would be erroneously.

  • That would be a bug.

  • So how can we fix this?

  • Someone who has never programmed before?

  • Do you want--

  • >> AUDIENCE: [INAUDIBLE]

  • >> DAVID J. MALAN: Retracting?

  • >> AUDIENCE: [INAUDIBLE]

  • >> DAVID J. MALAN: Oh, you have?

  • Someone who has never programmed before.

  • Want to try your hand at this, in the red shirt?

  • Come on up.

  • Oh yeah, come on up, if you don't mind.

  • So never having programmed before, what's your name?

  • LAUREN: Lauren.

  • DAVID J. MALAN: Lauren, all right.

  • So fix this bug for us.

  • You've not seen C, presumably, and probably no other language.

  • So fix this program for us.

  • Nice to meet ya.

  • No pressure.

  • LAUREN: Yeah.

  • >> DAVID J. MALAN: But again, if this gamble goes according to plan,

  • the takeaway here will be, just again, to start

  • to notice patterns and infer from existing syntax

  • how else you might express yourself, no pun intended, in order

  • to capture some additional scenario.

  • So we're saying else if.

  • printf, good.

  • And notice with CS50 IDE, and frankly-- IDE

  • stands for Integrated Development Environment.

  • It's just a fancy way of saying a program that helps you program.

  • It does user friendly things like when you type one parenthesis,

  • it automatically gives you the other.

  • The presumption being that you're eventually going to want to type that,

  • and it also does it with quotes.

  • >> So I like line 23.

  • 99% of the way there.

  • Someone want to chime in with a tip?

  • I heard semicolon from the audience.

  • Good.

  • So that completes that.

  • And now, I just have a little consternation with line 21.

  • How do we want to express this?

  • What question do we want to ask, do you think?

  • So previously we asked the question, if n is greater than zero, do this.

  • Else, do that.

  • So we need to choose a question to ask here.

  • AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Good.

  • So if n equals zero.

  • Excellent.

  • So let's pause here.

  • Thank you so much for braving this.

  • Let me give you a little well-deserved stress ball, perhaps there.

  • So we're almost there.

  • In fact, Lauren, if you don't mind, let me make one tweak here,

  • just to be consistent even with what we saw in Scratch, what

  • do I want to do with lines 21 and 24, just again based

  • on how we did this in Scratch, perhaps?

  • >> AUDIENCE: Move it up.

  • >> DAVID J. MALAN: Yeah, move it up.

  • So the else condition, the third, or fourth, or fifth, or final branch,

  • should indeed come afterward, and that's just by convention,

  • just because if you will.

  • So I do need to make this tweak.

  • And I need to put my curly brace back here.

  • And now, this program will compile.

  • We're so close, but there's a very, very subtle bug.

  • Let's see what happens.

  • >> First, let me go ahead and zoom out.

  • I'm going to go ahead and type make condition zero.

  • Oh, I lied.

  • Now the compiler is actually pretty smart.

  • And I don't really understand it's error message.

  • Using the result of an assignment as a condition without parentheses,

  • is apparently bad.

  • But what does that mean?

  • >> Well again, looking for patterns in code that we've seen before,

  • what is line 17 actually doing, even though Lauren intended slightly

  • otherwise?

  • Yeah.

  • >> AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Yeah.

  • So remember the equal sign, apparently as we've used it before, is not equals.

  • Its assignment, from right to left.

  • So line 17 at the moment is a condition, and it's

  • executing code inside the parentheses, but when that code executes,

  • what is happening to the value of zero?

  • What is happening to the value of n?

  • Becoming zero.

  • So if n is becoming zero, and again, as per last week,

  • we generally say that zero is false and one is true,

  • or yes is one and no is zero.

  • So true and false, one and 0.

  • If n as of line 17 has been given the value 0, is zero true?

  • No.

  • By definition, zero is false.

  • That's the convention we're going to cling to moving forward.

  • So will lines 18, 19, and 20 ever apply now, logically?

  • They can't.

  • So we asked the question but not quite precisely,

  • and you wouldn't know this unless you've seen it, the solution in C

  • is when you want to express equality, you say equals equals.

  • So equal sign was already used up by assignment, from right to left.

  • Equals equals means equals.

  • And if you really want your mind to be bent, in JavaScript and PHP,

  • there's equals equals equals, which mean something else altogether,

  • but we'll get to that before long.

  • >> So this is a simple fix, but realize super easy to make this mistake,

  • and then wonder and struggle for minutes, if not an hour,

  • why is my code not executing as I intend?

  • It's because you didn't tell it precisely what you meant.

  • So that was almost perfect, and it was great

  • that we stumbled across these two little things because now if we fix them,

  • and I do make condition zero dot slash condition zero,

  • I'd like an integer please.

  • Let's see.

  • No.

  • It's going to force me to do it.

  • So 42.

  • I picked a positive number.

  • So now how do I prove this program correct?

  • If you're a mathematician, you can't really prove this by trial and error,

  • right?

  • You can't just proof by example, but at least

  • as a programmer we're not going to try to prove for now

  • that this is actually correct, but I should probably

  • test some representative cases.

  • >> I've tested 42.

  • I could test 43.

  • I could test 44.

  • Test 45.

  • Right?

  • But I could do this a really long time.

  • What would be some more interesting cases to try?

  • AUDIENCE: [INAUDIBLE] zero.

  • DAVID J. MALAN: So zero.

  • Certainly.

  • And good, we've got it this time, thanks to Lauren's new code.

  • What's another good case to try?

  • AUDIENCE: Negative one.

  • >> DAVID J. MALAN: Negative one, and maybe something a little bigger,

  • so maybe negative 42, but in general, when

  • testing your programs moving forward, you're

  • going to want to think to yourself, like what

  • are the fundamental different cases.

  • And I dare say a negative case, a positive case, and a zero case

  • pretty much rounds these out.

  • Let's look at a variant of this though.

  • Conditions one.

  • This is a correct solution here, so that you'll see two and online,

  • if you walk through the walkthroughs, will generally

  • increment these stepwise until we get exactly the right approach.

  • What if we wanted to ask two questions at once?

  • >> Let me open up this instead.

  • Non-switch, for reasons we'll see another time.

  • What's new syntactically here?

  • I'm currently playing a game where the computer is telling

  • me, give me an integer between one and 10,

  • and then I'm judging the user's input.

  • In English, based on the code you see here

  • in blue, what is this program doing?

  • AUDIENCE: [INAUDIBLE] picked a big or small number.

  • DAVID J. MALAN: Yeah.

  • It's just adjudicating whether I picked a big, or medium,

  • or small number based on a very arbitrary definition of small, medium,

  • and large here, but notice the syntax that's new is this.

  • There's no less than int or equal to sign, or greater than or equal to sign,

  • where one is over the other, that you can express in like Microsoft Word.

  • So in a programming language you typically just do two characters,

  • one after the other.

  • And what does this probably represent, ampersand ampersand.

  • AUDIENCE: [INAUDIBLE]

  • DAVID J. MALAN: Yeah.

  • It just means and because it turns out that the authors of C

  • used a single ampersand, which you might think

  • would be perfect for the notion of and, is already used up,

  • so ampersand ampersand actually captures the case we actually want.

  • >> So let's now stumble across two things.

  • Let me go ahead and open up one.

  • Something that's a little worrisome.

  • Imprecision dot c.

  • Let me introduce two things here.

  • One, notice that we can do math, as just as before when

  • we did plus of two numbers, x and y.

  • What is this program apparently doing?

  • I've not commented it yet, and I've introduced percent f,

  • but I quickly said earlier percent f means floating point

  • value, which is real number.

  • Something with a decimal point, which is not an integer.

  • What do you think this program is trying to show me on the screen?

  • Yeah?

  • AUDIENCE: The result of one over 10.

  • DAVID J. MALAN: Yeah.

  • What's the result of one divided by 10.

  • Right, because all of you have probably grown up knowing that one over 10

  • is what?

  • Yeah, it's just 1/10 or 0.1.

  • Right?

  • So let's see what this actually is.

  • So let me go ahead and save this.

  • Let me go down here.

  • Make imprecision dot slash imprecision, and notice, too,

  • in a Linux environment, you don't have to type out imprecision and take

  • all day long.

  • It generally, if you hit Tab, it will finish your thought for you,

  • based on the words that are available in the current directory.

  • OK.

  • So you were right.

  • One divided by 10 is, in fact, 0.1.

  • But I'm a little curious.

  • And it turns out printf supports some features

  • whereby you can specify more than the default number of decimal points.

  • So if I want to see 10 decimal points I can do percent,

  • for the placeholder symbol, dot 10 for, give me 10 decimal points,

  • and then still floating point value, and, as an aside,

  • what is this backslash n apparently we keep using?

  • It turns out it's a symbol for a new line.

  • It's the very explicit way of saying to the computer,

  • move the cursor to the next line after you're done.

  • And we'll see what happens when we don't do that.

  • >> Let me recompile make imprecision.

  • Now dot slash imprecision.

  • OK.

  • Still right.

  • One divided by 10.

  • Now I'm getting a little curious, though.

  • What if I look 20 decimal points out, recompile, rerun,

  • and as an aside for speed, you can also use

  • your up and down arrows to scroll through your history,

  • so you don't have to keep retyping these commands, enter.

  • Huh.

  • I did not learn that in grade school.

  • All right, so let me see like 29 places out.

  • Maybe it's just a fluke.

  • Maybe my computer is acting weird.

  • That's not a thing, right?

  • It's doing something deterministically.

  • So make imprecision.

  • OK, that's messed up.

  • So what is going on?

  • >> So apparently your teacher has been lying

  • to you all of this time for reasons that we'll see before long.

  • But let's end with one other program.

  • This is a little programs someone wrote some years ago,

  • and it's actually part of a contest.

  • No one actually writes code like this, and if you do,

  • this is stylistically a problem.

  • But people do this for bragging rights.

  • To write the most confusing looking program that

  • nonetheless does something interesting.

  • And the note I thought we'd leave on today, before Colton plays us out here,

  • is to run this program, which, even in just using

  • C in this text-based environment, gives us this.

  • Dammit.

  • >> [LAUGHTER]

  • >> Stand by.

  • [INAUDIBLE] c dash l m.

  • You're going to see the magical incantation that we'll see before long.

  • [INAUDIBLE] All right.

  • And now you can even do things like this.

  • That's it.

  • For CS50 we, will see you on Friday.

  • >> [MUSIC PLAYING]

  • >> [APPLAUSE]

>> [VIDEO PLAYBACK]

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級 美國腔

第一週 (Week 1)

  • 525 41
    Ren-c Huang 發佈於 2021 年 01 月 14 日
影片單字