Placeholder Image

字幕列表 影片播放

  • This is CS 50 Harvard University's introduction to the Intellectual Enterprises of Computer Science and the Arts of Programming.

  • Ah, my name is David Malin, and I actually took this course myself sophomore year some years ago, but but I almost didn't At the time, I was quite uneasy with the idea, frankly, of taking a computer science course, let alone this course.

  • And so my freshman year, at least, I really gravitated towards courses and departments with which I was much more familiar.

  • Computer science was well beyond my comfort zone at the time, and it was really these unfamiliar waters that I wasn't quite ready to shop even my first year.

  • But sophomore year, I finally got up the nerve to come, like through the door of CS 50 and on Lee, because the professor at the time let me take the course pass fail.

  • I was that uneasy, and if only I had known at the time what I now know, which is.

  • The 2/3 of CS 50 students have never taken a computer science course before, so if you are feeling similarly uneasy with the idea of trying something new, or even if you have prior background but are looking to fill in gaps in your knowledge.

  • If you're particularly self taught, realize you are very much in good company.

  • And ultimately, what matters in this course is not so much where you end up relative to your classmates, but where you end up relative to yourself when you began.

  • Which is of course, today.

  • So what, then, is computer science and what was it that I was so uneasy with?

  • Well, I dare say we can describe computer science is this?

  • It's just the process of solving problems and what does it mean to solve a problem?

  • You've got some input, and the goal is to get some output.

  • The solution to that particular problem and in between really is computer science, and we'll see what's in this literal black box on the screen as we begin to fill in some of those blanks.

  • But when we consider ah problem to be solved, we have to all agree from the gecko, especially for using machines.

  • How are we going to represent these inputs and outputs?

  • And so one of the first concept we explore in computer science is how you represent information itself and odds are, you probably know, coming into this course that's computers only Speak what language.

  • So to speak S O'Brien.

  • Aaron, if you've never heard that term before, Fine as well, But zeros in one somehow.

  • But how do we get to that point?

  • Well, even if you kind of know the computer, speak binary, whatever that means.

  • But you haven't necessarily thought about how that works.

  • Will consider how most of us learn Tout accounts.

  • Maybe first on our hands, right.

  • If I want to count the number of people in the room, I might do 12345 putting up one finger for every person in the room.

  • So that's what's known as you Neri notation you are, you know, implying one, because I'm just putting up a finger or not to count people in the room.

  • Of course, I can only count so high on just one hand, but thankfully, we have many symbols in our so called human world of decimal dec, meaning 10.

  • We have digits zero through nine, and from there we can express any numbers with which we're all familiar.

  • But computers don't have that many digits.

  • They only have zeros and ones.

  • And yet somehow they're able to store not only numbers, but letters and images and videos and sounds, and so much more.

  • So how does that work?

  • Well, let's take a quick look at just this to get everyone on the same page as to how you can build the phones in our pockets.

  • The laptops on our desk today using just zeros and ones.

  • Well, this are just symbols on the screen here.

  • What is this, of course, represent 123.

  • But why?

  • I mean, these literally are just symbols, or GL ifs on the screen, sort of strokes of a pen that we all ascribe meaning to.

  • We just see this as 123.

  • But why is that?

  • Well, if you're like me, you probably learned way back when to think of the right most digit as being in the ones place with one's Call them.

  • The middle digit is being in the tens place, and the left most digit is being in the hundreds place.

  • So how do we get from this pattern of 1 to 3 to the number 123?

  • Well, it's, um, quick mental math that we all just do instinctively.

  • It's 100 times one plus two times, 10 times two plus one times three.

  • That, of course, gives us 100 plus 20 plus three or 123.

  • So all of us just kind of take that process for granted now.

  • But it turns out that there's a system at play here, a system such that we can figure out any number in the same way.

  • So if we've got our one's place tens place in hundreds place, of course, in the decimal world, this is the number one.

  • This is the number 23456789 Something interesting happens, Of course.

  • After nine, you sort of carry the one.

  • And why then does 010?

  • Or if we ignore the leading zeros as being insignificant?

  • Why does 10 represent the number?

  • Well, no, obviously, as 10.

  • Well, just cause it's 10 times one plus one time zero.

  • So that's the system we've been using for years, and it turns out that these columns realize are just powers of 10.

  • So 10 to the zero is one tend to the one is 10 and tend to the two is 100.

  • And that's how we got ones tens and hundreds.

  • Place.

  • Now, in the computer world, you don't have 10 digits.

  • Zero through nine.

  • You have just two digits, zero and one.

  • So we can just make a little tweak to our mental model here, so to speak.

  • And just now, use two powers of two instead of powers of 10.

  • So that means the right hand column is gonna be one.

  • The middle column is gonna be to the left column is gonna be four.

  • And if we kept going, it be 8 16 32 instead of 1000 10,000, 100,000.

  • But the idea is exactly the same.

  • So I propose that computers on Lee speak zeros and ones, of course.

  • But how do they represent larger numbers than zero and one?

  • Well, this is representing zero.

  • This is representing one.

  • What pattern of symbols on the screen would represent the number we humans know is too.

  • S 010 So you just spoke binary.

  • So even if you just generally knew what buying that binder is spoken by computers, it just means that a computer to represent the number we know is to somehow stores a pattern of symbols of 010 How do they represent three 011 for the same reason?

  • It's two times one plus one times one gives us, of course, three.

  • And now, just as before when we went 9 to 10.

  • Now we actually go from 3 to 4 by carrying the one now.

  • So in binary by meaning to hence, the zero and 1100 is not 100 per se.

  • It is literally the number we know it's for, and we can keep going on up.

  • This, of course, now is five.

  • This would be six.

  • This would be seven.

  • And what happens if we want to represent eight s?

  • So we kind of have to solve this somehow.

  • We need another digit, and that's fine, right?

  • In math class, you would just add another place, another column in order to get back the value that you want.

  • So we need more zeroes or ones otherwise now known as bit.

  • If you've ever heard of the phrase bit, it just means binary digit and a zero or a one.

  • It's just something we know is a bit And why is this remain to computers?

  • Well, it's fancy is our Macs and PCs and phones are today.

  • Consider.

  • After all that, at the end of the day, what do you have to do typically with your your laptop, your desktop, your phone these days?

  • What do you do with the very end of the day s So you charge it right, you plug it into some physical resource.

  • That is the Onley physical resource into these computers that we use today.

  • And that's kind of nice, because, of course, the court can either be plugged in or not plugged in.

  • Or maybe we could call that a one or a zero, True or false, on or off.

  • Which is to say, if electricity is our only input and there's either yes, electricity or no electricity will.

  • That actually maps quite nicely to the idea of binary, because we need just two symbols zero and one often on to represent that idea.

  • Of course, with a single bit, you can't do all that much.

  • You can only count from zero toe one, and that's it.

  • So computers tend to use larger numbers of bits to represent information, and they do this ultimately, physically.

  • So it turns out that with both plug in the wall or even a light bulb or switch thereof, you can represent a zero or one.

  • But if you want to represent Maur zeros and ones while we just need maybe eight bits at a time.

  • And if you've ever heard of the expression bite Aah!

  • Bite is just a term describing eight bits, eight zeros and ones and more useful measure instead.

  • So given that I think it's time perhaps toe to see if we can't solve the problem ourselves, Perhaps we could get, say, two volunteers.

  • Come on.

  • Okay, so one hand here and one hand over here.

  • Come on down.

  • If you want to meet me on the other end of the stage.

  • Come on over here.

  • Come on down.

  • What's your name?

  • Priyanka.

  • Nice to meet you, David.

  • Come on over and say if you want to wait right here and what's your name?

  • Calvin.

  • David.

  • Nice to meet you.

  • Come on over here.

  • Where?

  • Crown kiss.

  • So proud how you raise your hand first.

  • So you get to choose.

  • Do you want to go first or second in this little challenge?

  • ahead.

  • Okay, So Priyanka is gonna go first if you want to stand over there.

  • Kalman.

  • So the challenge ahead here is Could you go ahead and represent for us in binary, using each of these light bulbs and in turn, switches a zeros and ones say the number 50 they might turn one light bulb on representing the 30 two's place might turn a light.

  • Boban, representing the eights place or total count now is 32 plus not eight plus 16 I think.

  • Which is going to give us 32 plus 16 which is 48.

  • And so we get now a round of applause if we could, Priyanka, thanks very much.

  • Give us just a moment.

  • So each of these lightbulbs then represents just a switch orbit and inside of your computer.

  • If you've ever heard the phrase transistor, a transistor is just a tiny little switch and our computers that they have millions or billions of these switches that they use physically to represent information in store values, just like Priyanka did here.

  • So if a computer were to represent the number 50 it would literally turn on three switches of sorts.

  • Store a little bit of electricity here, here and here to represent the number 50.

  • And it would leave off all of the other switches.

  • The other five.

  • In this case, if we're using eight bits or one bite Calvin, you raise your hand seconds.

  • And so we have one other challenge ahead.

  • Fortunately, these things are magnetic.

  • So let's take things up a notch.

  • And if you would Calvin, how about the number 13 if you will?

  • How would a computer represent the number 13 where each of these light bulbs from one toe 128 represents a bit?

  • We had, of course, the ones place over here.

  • The tooth place 48 16 and so forth.

  • So we can ask the audience.

  • Should we turn on for instance, this bulb here.

  • No way to big.

  • How about this one?

  • Okay.

  • And you're in charge as the audience, This one.

  • Okay, so we have 1248 four gives us eight plus four is 12 and another round of applause.

  • Thank Thank you.

  • Got tougher job.

  • Thanks to you both.

  • So, at the end of the day, while this is a very large physical incarnation of the notion of minor.

  • That's all that's going on inside of our computers every day when they represent information.

  • But we only thus far have the ability to represent numbers.

  • It seems.

  • So.

  • How, then, does a computer allow you to send text messages and e mails and composed documents and so forth?

  • We need to all agree how we're going to go about representing characters or letters of an alphabet piot, English or something else instead, and any intuition for how, if a computer only has millions or billions of switches that can be turned on and off there by representing numbers, we could possibly go about representing something other than numbers like the letter A sagan by a digit.

  • So we may be just need to agree.

  • As a group.

  • You know what?

  • Let's all agree that the letter A in the context of a word processing program or a text message or email just needs to be represented by a number, and we all need to agree what that number is.

  • So maybe super simply, Let's just say a is one B is to see is three and so forth, and you could imagine them having a computer turn on lots of little transistors to represent A and B and C, so long as the software on that computer as well see, knows that it's a word processing program and not, say, a calculator or something that's meant to use numbers alone.

  • So it turns out that computers don't actually use the number one for a or two.

  • For B.

  • It's a little bigger than that.

  • The world decided decades ago that the Capital Letter A is gonna be represented by the number 65 65 which is to say, if you have a bite of information in the computer and this is the ones, place twos for a 16 30 to 64.

  • If a computer were to store the capital letter a using eight bits or switches, it would just turn on those to the 64th place and the ones place.

  • And so when you have actually received a message in a text message or an email with the Capital Letter A.

  • You have just received a pattern of zeros and ones somehow wirelessly or via wires representing that pattern.

  • Specifically, this pattern here, if we draw it, not his light bulbs, but zeros and ones So it turns out there are certainly more numbers than just A's and B's and C's.

  • We have the whole alphabet plus punctuation, thanks to a system called Asking the American Standard Code for Information Interchange, which is just a fancy way of saying that there is a well defined map that humans around the world agreed on years ago.

  • That looks a little something like this.

  • So say a 65 and Iess 73 and dot, dot, dot to both ends.

  • So that is to say, if you were to receive a message from a computer or from a friend saying 72 73 33 or the pattern of zeros and ones representing those digits, What message did you perhaps just receive?

  • 72?

  • 73?

  • 33 s so high?

  • So, quite literally, if you were to text a friend high, they would receive a message that's essentially a pattern of zeros on 1 72 73 then something.

  • It's actually not obvious from the chart what they are, but it turns out, 33 was the number humans gave years ago to represents an exclamation point instead, and so any of the other punctuation symbols you might see on your keyboard similarly, have numbers that a computer would use that all of us agreed on years ago to represent that value.

  • But of course, this is a very American centric at the moment.

  • And indeed the acronym Ask AE has American in it, and it's It was biased early on to American English, for instance.

  • But there's so many more characters in the world, of course, such as accented characters and other languages altogether.

  • And frankly, there's also these things these days, which even though they look like pictures, you access them via your keyboard because they are indeed just symbols in an alphabet.

  • There are zeros and ones, patterns of zeros and ones that represent those characters as well.

  • That's what's known as Unicode, and that's like a super set of asking because asking way back when used only eight bits, which is not terribly many to represent characters, Unicode uses eight or 16 or 24 or even 32 which means we have way many more possible patterns of zeros and ones with which to represent things like this.

  • So this is face with tears of joy.

  • As of 2019.

  • It is the most popular emoji sent on IOS devices, at least does anyone once a hazard, a guess as to what the decimal number is.

  • That represents a face with tears of joy, not 65.

  • It's not 72 not 73 800 higher than that 10,000 higher than that.

  • It's 128,514.

  • That's how many Emojis await us down the road because the numbers are now getting this big or equivalently this pattern of zeros and ones.

  • So at the risk of taking all of the fun out of ever sending someone emoji emoji when you send that face with tears of joy, you're literally just somehow having your phone send a friend's phone.

  • This pattern of zeros and ones and android or IOS are presenting it as that yellow picture.

  • But that yellow picture, of course, itself is an image, and it's composed of lots of little dots, and odds are coming into this class.

  • You're generally familiar with using images, certainly, and seeing the little dots that compose an image, especially if they're of low quality.

  • You can really see those dots and so in a face like the emoji.

  • We have lots of yellow dots.

  • How does a computer represent each of those dots?

  • Well, according to a system called RGB Red, green, blue.

  • So again, decades ago, people in a room decided, You know what, just represent colors.

  • We still have to use just zeros and ones little switches mechanically.

  • But let's just all agree what numbers represent what colors.

  • So we just need another system for that now.

  • It turns out that RGB essentially composes any color of the rainbow by mixing together or some read some green, some blue, and by combining those colors one on top of the other, you can get any color of the rainbow you want.

  • So this is to say, to store any dot on the screen one pixel, so to speak, in an image that you might take us a photo or sent to a friend.

  • You are actually storing three values.

  • 1233 numbers.

  • Really, that, of course, at the end of the day, or zeroes and ones, those three numbers just tell the computer how much red, how much green and how much blue to use to represent some dot on a screen.

  • So suppose that, coincidentally ah, computer were storing the pattern.

  • 72 73 33 albeit with zeros and ones or light bulbs back to back to back like this.

  • Well, in the context of photo shop or a browser or apple photos or the like in the context of a graphical program, your computer is gonna interpret this same pattern of digits and in turn bits not as high exclamation point but as some amount of red, some amount of green, some amount of blue.

  • And it turns out, if you combine this much red 72 this much green 73 followed by this much blue 33.

  • What you get when you combine all three of those is that shade of yellow.

  • So for every single dot in that emoji that face with tears of joy every dot in this image and we can see it if we really zoom in is stored using three values.

  • Some amount of red, some amount of green, some amount of blue that we combined give you yellow or black or gray or anything else, depending on how you encode those values.

  • Meanwhile, images or videos things like this that you might see on the Internet these days in the form of means or anything else, are actually just images, but they're multiple images in the same file.

  • If you've ever seen what's called an animated GIF, which this happens to be, that happens to be looping, all our human eyes are seeing is one image after another after another after another, really quickly creating the illusion therefore off movement.

  • But this cat is not actually moving these air, like five or 10 photographs of a cat in different positions just being looped endlessly.

  • And that, too, is all a video is.

  • Ah, video is just a sequence of images flying past your eyes so quickly we humans perceive it as actual movement.

  • And so that's like almost everything.

  • We used to represent information today and our computers, where we have zeros and ones from which we get finer, to which we then get decimal digits.

  • But we can use those digits to represent, say, characters on the screen or colors on the screen and in turn now, even them or interactive.

  • Now, what about something like music in a computer?

  • Another type of information you might want to represent?

  • Well, how if we had, like, a panel on the stage here, we could.

  • Kwan ties the notes here, playing using just numbers as well, Right?

  • What note you're playing?

  • Maybe it's a B, C, D e f or G or some variant thereof.

  • Maybe how long you hold the key down four.

  • And if you hit it really hard, maybe how loud it is.

  • So you could imagine using three values the notes, the duration and the volume just to represent something like a piano song might be one encoding of a song, and that might use different values if I want to play it even louder and longer on so forth.

  • Which is to say that at the end of the day, no matter what media we used to represent information, it all reduces two zeros and ones.

  • So once we have the ability to represent inputs, Beit numbers or letters or colors or videos, now we can talk about outputs.

  • So how do we get from inputs?

  • Toe outputs?

  • That's what's inside this so called black box.

  • And this is where computer science comes.

  • In an algorithm.

  • Anyone know what this term is?

  • What's an algorithm.

  • You sort of read about it all the time these days, especially the context of self driving cars and cirie and Alexa and so forth.

  • Yeah, a set way to do something.

  • Yeah, in the context of problem solving an algorithm mistress, step by step instructions for solving some problems.

  • So what might an algorithm be for a problem that we might want to solve?

  • Well, consider this.

  • This is an old school problem where you might have lots and lots of names and lots of knots of numbers.

  • And those names air hopefully sorted alphabetically from a through Z in a book like this.

  • And even though most of us don't really reach for this technology anymore, consider that it's really the same as your iPhone or Android phone or other device, which has all of your contacts top to bottom, and you can scroll through them from a to Z, or you can search for them by typing into the little auto complete box.

  • How is even your phone solving this problem?

  • Well, let's consider a simple approach.

  • I'm gonna look at the first page and look for someone on the phone book.

  • Supposed that person's name is Mike Smith.

  • Last name starting with s.

  • Let me go ahead and look down.

  • He's not here.

  • Let me turn the page.

  • Let me turn the page.

  • Let me turn the page.

  • This is an algorithm.

  • It's a step by step process for solving a problem Finding Mike Smith.

  • Is this algorithm correct?

  • Would you say Yeah, I mean, it's pretty slow.

  • It's pretty stupid in that it's gonna take my God forever, like hundreds of page turns to find Mike Smith.

  • But if he's there, I will find him according to this step by step approach.

  • What if I speed things up a bit just because it's tedious?

  • Otherwise, So I do.

  • 2468 10 12 14 16 and so forth.

  • Is that algorithm faster?

  • Absolutely.

  • Literally, twice as fast.

  • Is it correct?

  • No.

  • Why?

  • Yeah, we might skip it.

  • I might get unlucky.

  • And eventually I might get to like the esses.

  • But darn it, if Mike wasn't in between two pages as I turned them at once.

  • So it's not a fatal flaw.

  • That algorithm I could fix by just saying if you hit the t section or maybe s n instead of s m just back up one or so pages just to fix that, that bug or mistakes, so to speak.

  • But these days, twice as fast.

  • But if this phone book has, like 1000 something pages, it's still gonna take me.

  • Maybe like 500 pair wise turns just defined Mike Smith.

  • That's a while, just to look someone up.

  • But most of us, if you've used this technology instead, did what back in the day, go like roughly to the middle, right, if there aren't little letters on the side of which the cheat so roughly into the middle, I'm in the M section now.

  • The EMS off course mean that Mike is not this way, which would be the A's.

  • He's probably this way toward disease, because s, of course, is between the M and the sea.

  • So at this point, I can literally tear the problem in half, throw half of the problem away very dramatically and unnecessarily making the point that we've now gone from 1000 some odd pages to what 500 I could do it again.

  • I went a little too far now in the tea section so I could tear the problem in half again.

  • Throw that half away, and now I'm down to from 1000 2 500 to 250 pages on Lee after just two steps in this step by step process.

  • And if I repeat this again and again and again, hopefully I'll be left ultimately with, say, just one page on which Mike Smith either is or is not.

  • And I can call him or quits.

  • So that algorithm would get me to the solution so much faster.

  • And we can appreciate this even if we just look at some of the numbers, ultimately as follows.

  • So if I start with, say, 1000 24 pages total in the phone book and I'm looking for Mike Smith and I divide and conquer this problem, spitting the problem in half in and half in and half I go to 5 12 I got 22 56 1 28 64 30 to 16 842 and one.

  • After just 10 steps, I have found Mike Smith's page.

  • By contrast, that first algorithm where I just did one page at a time, how many steps maybe might have taken me to find Mike Smith like 708 100 roughly where the essence might be.

  • So in the worst case, 1000 pages.

  • If I look through the whole thing, the second algorithm, maybe 500 pages, because I'm going twice at a time.

  • But my God, 10 steps with this algorithm here and odds are that would be the algorithm most of us in this room would reach for by default.

  • Which is to say that a lot of problem solving really is will find is just about harnessing your existing intuition and comfort with ideas that now you just need to translate in such a way that machines and other humans can understand.

  • So how might we just think about how much better that algorithm is?

  • Well, consider this first line here on this y axis or vertical axis.

  • Let me describe this is the time to solve some problems and on the horizontal or X axis the size of the problem.

  • So the number of pages in the phone book would get bigger as you go to the right, and the number of seconds or page turns required would go up along the Y axis here, so that first algorithm depicted here in red suggests, like a 1 to 1 relationship between the number of pages in the book and the number of seconds to find someone.

  • So you have this straight line a slope of 1/1, if you will.

  • And so if we consider the second algorithm, the second algorithm is also going to be a straight line.

  • But that straight line is gonna be lower on the graph.

  • Why?

  • Because for any size problem, it's gonna take me half as much time to search that phone book.

  • Because, of course I'm going to pages at a time.

  • So if we see this, for instance, if this dash line represents some number of pages in the phone book, maybe 1000 24 well, you can see that it might take this many seconds or page turns to actually find Mike Smith with that second algorithm.

  • But in the first algorithm, that same number of pages would take way more time to solve literally twice as much time.

  • In this case, what about the third algorithm?

  • Well, even if your memory of what a longer them is is a little hazy, it just describes a fundamentally different shape.

  • The Green Line describes that third and final algorithm whereby you divided the problem, not one page at a time or two pages at a time, but 50% again and again and again.

  • You have did it again, again and again and notice that is the number of pages in the phone book.

  • It's really large.

  • You barely make an impact on how many how much time it takes to solve that problem.

  • For instance, if a Cambridge in Allston, two towns here in Massachusetts merge next year in their phone books, become one phone book that's twice as big.

  • So not 1000 pages each, but 2000 pages total.

  • How many more steps might it take us to find Mike Smith in next year's phone book?

  • If it's got 2000 pages instead of 1000 just one more step.

  • But the 1st 2 algorithms that's another 1000 steps, maybe, Or another 500.

  • These air fundamentally big differences in efficiency, so to speak.

  • So let's translate this idea this intuition into the first example of code.

  • This is pseudo code, and there's no one formal definition of this pseudo code is like code like syntax that you write in English or your own spoken language that represents your ideas, but in a succinct way.

  • And so I might propose that this algorithm for finding Mike Smith might be written in pseudo code English Lakes and Texas follows Step one pick up phone book, which was indeed the first thing I did step to open to the middle of phone book, which is the next thing I did.

  • Step three might be Look at the page to see if someone's there.

  • And if Smith is on that page, what do I want to d'oh?

  • Well, my code's gonna look a little different now, and I'm gonna deliberately in Dent to make clear that there's a dependency of some sort.

  • I'm gonna go ahead and step five and call Mike on Lee.

  • If line four is true that he's on the page else.

  • If Smith is earlier in the book to the left, so to speak, I'm gonna go ahead and open to the middle of the left half of the book, and then what am I probably gonna want to do next?

  • So, yeah, this ultimately, how do I do this again and again?

  • Well, I already have some code, if you will, on Line three that does that.

  • Look at Page and then make a decision to go left or go right.

  • So I'll just say Go back toe line three after going to the middle of the left half of the book if Smith is indeed earlier in the book.

  • The other scenario, of course, is the opposite.

  • So if else if Smith is later in book, let's open to the middle of the right half of the book and then let's go to Line three.

  • Else there's 1/4 possible scenario or case what else might happen in this algorithm?

  • He's not there.

  • And so I probably want to anticipate that and just say, Quit if indeed he's not on the page to the left of the page or to the right of the page.

  • So there is one way of expressing this pseudo code, albeit now in just this English like syntax.

  • But in this syntax alone, there's some commonalities, some features we're going to see over the next several weeks, not only in pseudo code but in a language called scratch, a language called CIA language called Python and Maur there are certain constructs in programming, procedural programming, so to speak, that are gonna be common among all of these languages highlighted in yellow here or what?

  • Henceforth, today and onward, we're just gonna call functions these air verbs or actions that just tell the computer or in this case, the human What to d'oh.

  • So those air functions now highlighted in yellow instead, or what we're gonna call conditions.

  • These air branches sort of forks in the road.

  • So you can either do this or you could do that or maybe this other thing as well.

  • But to make those decisions, you have to ask yourself a question.

  • And those questions and computer science are called boolean expressions after a mathematician named Bull and a Boolean expression highlighted now in yellow are just questions that have yes or no answers or if you will, true or false answers or heck, now that we know binary one or zero answer.

  • So even in code, we see a hint off why ones and zeroes are helpful.

  • Lastly, there's this thing here.

  • Go back to line three reviews did in two places that refers to something we'll call a loop.

  • It's a cycle that does something again and again.

  • So beyond those four ideas, functions, conditions, bullying expressions and loops were about to see ah, bunch of others as well, variables reminiscent of what you might recall from algebra but more powerful now in this context of programming, something called Threads and Events.

  • And we're gonna do this by way of an actual programming language.

  • But it's not yet going to be this one.

  • Indeed, just a week from now, will you actually understand what this means?

  • Odds are 2/3 of you have no idea what this actually is, and that's okay.

  • But it's a program written in a language called See Maur on that next time.

  • That quite simply says hello to the world, but it wouldn't do it today in the context of a graphical language from M.

  • I T.

  • Media Lab called Scratch.

  • This is a language via which you can program a computer by dragging and dropping blocks or things that look like puzzle pieces that interlock together so that you can tell the computer exactly what to do.

  • Step by step and we'll see in this language today.

  • And for the first problem set or programming assignment that you can express all of these ideas from pseudo code in an actual language like scratch.

  • So what lies ahead?

  • What scratch dot M I t dot video, which I'm about to pull up myself, is going to be where you spend time in the first problem set, and it's gonna look quite like this when you open it up on the left hand side of this website.

  • This Web based programming environment from M.

  • I t.

  • You're going to see on the left hand side a whole bunch of puzzle pieces, so to speak, and they're categorized according to some different colors there on the left in the middle.

  • There's just gonna be a big white canvas initially onto which you can drag and drop those puzzle pieces and lock them together to make the program do something.

  • What can you make it do well on the top, right here is the so called stage in scratch.

  • By default, there's only one character or sprite on that stage.

  • Scratch himself, and he can move up, down, left, right and do any number of other things.

  • If you tell him what to do by dragging and dropping those puzzle pieces.

  • If you don't like that cattle, you want many others.

  • You can also add multiple sprites or characters by creating on them in that area.

  • So let's actually do this now with some sample programs such that we can actually begin programming using this environment.

  • I'm going to go ahead now and open up a browser here and in just a moment you'll see on the screen scratch dot m i t dot edu itself.

  • So if I go ahead and create by simply clicking up there, we'll see this editor ultimately that by default has this tutorial that I'm gonna go ahead and dismiss.

  • But now we see that same environment, and you'll see that above Scratch your over a top, right?

  • There's this green flag and the stop sign The green flag is what you can.

  • Click is the human To make the program go.

  • The stop sign will make any program stop.

  • So it turns out, among all of these categories here, there's, for instance, some orange ones called control.

  • There's some yellow ones called events and will focus on this ladder one first if I go ahead and drag from events.

  • This puzzle piece here.

  • This is how I could begin programming when the green flag is clicked.

  • Do the following.

  • What do I want to do?

  • Well, let's have this catches say hello and from having used this program before, I know that under looks I could go ahead and say something like Say hello and notice.

  • Not only does the shape match the orange puzzle piece or yellow puzzle piece notice that it magnetically wants to snap together, and if I let go, it will.

  • And I'm gonna say perhaps one of the most canonical things to say in your very first program, which is just conventionally hello world.

  • And leave it at that.

  • I'm gonna Now go ahead and click the green flag and voila, our very first program the same effect.

  • Thank you.

  • Thank you.

  • Very low bar so far.

  • But we'll we'll improve upon this next because this cat is pretty in an emits and it's just now saying hello.

  • What if we wanted him to do more than that and say not?

  • Hello?

  • But hello to me or hello to you.

  • Well, turns out we can do this a little differently as well.

  • I'm gonna go ahead and throw this away went by just dragging it over on the left and the puzzle piece just goes away.

  • And if I poke around a little further under these blue blocks, for instance, sensing there's a bunch of puzzle pieces related to scratches, environments and one of those is this one here, ask what's your name and wait, but that What's your name?

  • Expression is in a white box that I can actually change.

  • You can ask any questions you want, but I'll go ahead and use that default.

  • And now notice.

  • Also over here in blue is answer, which is another circular shape, which represents what we're going to start calling Variable.

  • That variable is gonna store whatever the human types in when asked for his or her name.

  • So what do I want to do when I actually get the person's name?

  • Let me go back to looks.

  • Let me go ahead and say hello and I'll go ahead and say hello and then another one say hello again.

  • But instead of this, I wanted to say my name, but it would be incorrect to just tight my name, because then my name is always gonna be David no matter who plays this game or is asked the question.

  • So I don't want to do that.

  • If I go instead to sensing and then drag and drop this puzzle piece, notice this.

  • It wants to magnetically snap in there, and it will grow to fit it.

  • So now I can say hello and then I can say name.

  • So let me go ahead and hit.

  • Stop and play again.

  • What's your name?

  • I'll go ahead and type in.

  • David.

  • Enter.

  • And now David, huh?

  • I feel like we forgot the hello.

  • This is my first bug.

  • And he thoughts as to Why?

  • What did I do wrong?

  • Yeah, yeah.

  • I mean, I told the computer via this algorithm to do tooth to do three things.

  • Ask what's your name and wait.

  • But then I just say said hello saying answer in such rapid succession And my God, art Macs and PCs and phones these days you're so fast, they're gonna do so many things so quickly that it did say hello.

  • Just none of us really saw it because my name immediately overrode it.

  • So I can fix this in a couple of ways.

  • Let me go ahead and go back to looks for just a moment.

  • Get rid of this puzzle piece and maybe say hello for two seconds.

  • Then let me go ahead and say my name or the user's name for two seconds as by dragging answer now into here.

  • So that was my first bug in code, so to speak.

  • Let me go ahead and play again.

  • Type in David.

  • And now it's Hello, David.

  • All right, so better.

  • It's a little weird because, you know, if if you typically don't greet someone by saying hello, David.

  • Two seconds later.

  • So what if we kind of combine this expressions and say not Hello world.

  • But hello, comma David.

  • What?

  • We can do this in a different way.

  • Let me go back over here and, for instance, get just one of those say blocks like this.

  • I'm not gonna worry about seeing it for some number of seconds, cause I'm only gonna say one thing now, but I was somehow want to say, hello, comma.

  • And then the user's name.

  • Well, if I poke around further and you would only know this by having seen it before, it turns out there's this puzzle piece down here called Join and it's a little weird that the default words air, apple and banana.

  • But those air just placeholders.

  • If I go ahead and drag this over here, it grows to fill and it overwrites what was there, I can say Hello, comma.

  • And then, instead of hello, Banana, let me go back to sensing dragged the user's answer.

  • And now we say, hello, comma So and so So let's try this now.

  • Instead, Green flag type in my name.

  • Enter.

  • Hello, Comma David.

  • So now the program isn't all that much more complicated.

  • But notice that we're starting to nest.

  • These verbs thes functions were asking for a name.

  • And then we're saying the result of joining the following two things.

  • So there's a few ideas to keep track of at a time.

  • But this is actually quite like what we were doing from the get go earlier as follows.

  • For instance, if we want to just say hello world with this puzzle piece here, this actually maps perfectly to our fundamental definition of what problem solving is the input to that puzzle pieces.

  • Of course.

  • Just hello world, the function or algorithm that you wanna execute step by step instructions were saying something.

  • Scratch calls say in purple and the output you want, of course, is scratched, saying Hello world.

  • Now in the more sophisticated example, consider this.

  • We asked, What's your name?

  • And then we waited.

  • Well, that picture would look like this instead.

  • The input to that question is, of course, what's your name, the algorithm, or in this case, function via which were solving that problem is to ask and wait.

  • Taking in that input and the output we get back is now answer.

  • And then, lastly in that third example where things got a little fancier and you had a start dragging and dropping in nesting these puzzle pieces notice that this is the same idea.

  • The input now is two things.

  • Hello and answer.

  • Those go into a function called Join, the output of which should be Hello, comma David.

  • But now we want to pass that output as the input to the same block so that the final result is hello, David, right out of the cat's mouth.

  • So, in this way, even with the simplest of puzzle pieces, does all everything fit into this same model of problem solving but let's now make things a little more interesting rather than just talking text with this cat's let's go ahead and haven't do some other things.

  • Instead, let me go ahead under sound, for instance, and it looks like there's a block in pink hair called Play sound Miao until done.

  • And now let me stop the old program and start the new Okay, little piercing, but adorable.

  • And if you want to hear it again, I can click the green flag on I can click the green flag.

  • Of course, this is a pretty boring cat.

  • If it goes lifeless after it said just one.

  • Now, how would we get it, too?

  • Perpetuate that sound in just now every few seconds.

  • What construct her idea might we want to use here?

  • Eso some kind of loop so it turns out in scratch.

  • There's a few ways to do this, but if I go down to control, you'll see a couple of blocks here, repeat some number of times by default, 10 or forever.

  • I'm gonna go ahead and maybe do this forever, okay?

  • And I'm going to drag and drop this now into here.

  • Notice I can move things around wherever I want, and now I can reattach this up here and now play very agitated cats.

  • At the moment, I can calm him down, perhaps by dragging and dropping this and notice.

  • Even if there's no room, it will grow to fill one second.

  • Now it's a happier cap, if you will.

  • But now that I have the ability to have the cat do something forever, it doesn't have to just do the same thing again and again.

  • It can interact with me or its environment.

  • So instead so instead of playing the sound cheer someone following along at home.

  • So instead of playing the sound here, let me go ahead and try this.

  • Let me go to censor sensing and go ahead, Thio, which what might I want to do here?

  • Let's go ahead and under motion rather under here.

  • Point toward mouse pointer So point towards mouse pointer and then go ahead and you know what moves?

  • Some number of steps and I'm gonna have it move one step or one dot or pixel at a time.

  • And here now we'll get this effect.

  • It looks as though now what kind of follow me, right and as I move my cursor, he is forever pointing toward the cursor than moving.

  • One step, of course, is a little slow.

  • And let me go ahead and stop.

  • If I make him take 10 steps at a time, or 10 dots or pixels on the screen now it seems a little harder to avoid and you concerned he'll literally do as you as you move.

  • Your cars are here, all right, so now that we have the ability to do something again and again, let's have something counts up for this one.

  • Let me go ahead and grab something from online.

  • So when C.

  • S fifties website will always make available the examples and source code from each class and this time I want to go ahead and get this sheep here.

  • So it turns out you don't have to just use a cat.

  • You can use a sheep if you prefer, and let's see by looking at this code first.

  • What is this sheep going to do when the green flag is clicked?

  • There's this orange puzzle piece called Set counter toe one.

  • What is that?

  • That's an example of what might we call this?

  • It's called a variable now an algebra we typically used like X and Y and Z for variables and programming.

  • When you want to store some value in a place holder like a variable, you typically give it more descriptive words.

  • X, Y and Z don't really tell the programmer or someone reading it what it's doing.

  • So we called this variable counter and we said it equal to one.

  • Now forever.

  • The sheep is going to do what it's going to say the counter for one second.

  • It's gonna then wait for one second and then it's gonna change or add one to the counter as its final step.

  • So here we have, if you will, a counting sheep whereby it will now just perpetually count up from 1 to 2 to three.

  • And probably if we let this thing go chuck to infinity.

  • So once we have the ability now to do variables, what if we start to make our programs Maur Interactive and start to remember information?

  • Let me go ahead and into our examples from before and grab another example here Now, instead, I'm going to go back to what's called a studio and scratch where all of these examples that currently are, and I'm gonna go and open up a example called PET zero Computer scientist typically start counting from zero because that's symbolic of, like, all of lightbulbs being off.

  • And so here's a program now that if I hit play does not seem to do anything at once at first, but notice as I move my cursor over.

  • I'm kind of petting the cat now, if you will.

  • So how is this working?

  • Well, he, too, is just forever listening or waiting for something to happen.

  • It's forever doing this if touching Mouse pointer, then play sound now until done.

  • So now the cat is being responsive to the user input, not following my cursor, but responding Just if I'm actually hovering over him, we can, of course, change this up a little bit in version two of this, otherwise known as pet one.

  • Because we started counting at zero.

  • What should I not dio with this program?

  • Uh uh.

  • Maybe don't pet this cat.

  • Why?

  • Well, this time it is forever checking if I'm touching the mouse pointer, and if so, it's going to apparently play a sound more else.

  • It plays the sound sound now, which seems to be happening.

  • So here we go.

  • Uh, so don't let the cat and so that's just a condition asking a question of so called Boolean expression.

  • If touching mouse pointer will Now let's do something a little fancier.

  • Still, let me go ahead here and do this.

  • Let me go ahead and make this one from scratch.

  • No pun intended.

  • And I'm gonna go ahead and control start with an event when the green flag is clicked.

  • Let me go ahead and grab some motion.

  • Here, let me go ahead and grab set, rotation style toe left, right.

  • Just because this will make sure that he ultimately does as we intend I'm gonna go to control.

  • I'm gonna do the following forever.

  • Suppose I want the cat to just bounce back and forth on the screen.

  • Well, the first thing I need to do is animate him and actually make him start move.

  • And the best way to do that, frankly, was just being go to motion and move some number of steps.

  • Maybe 10 steps to go quickly or one step to go slowly.

  • But I can ask a question every time the cat moves I can go and ask something like this.

  • If something is true, go ahead and maybe do something else, so maybe bounce off the wall.

  • So how do I say if you're touching the edge, go ahead and bounce backwards?

  • Well, it turns out, if you poke around, you'll see something like this.

  • If touching, not mo

This is CS 50 Harvard University's introduction to the Intellectual Enterprises of Computer Science and the Arts of Programming.

字幕與單字

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

A2 初級

CS50 VR 2019 - Lecture 0 - Computational Thinking,Scratch (CS50 VR 2019 - Lecture 0 - Computational Thinking, Scratch)

  • 4 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字