WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:03.465 [CLASSICAL MUSIC] 00:00:10.395 --> 00:00:12.870 [MUSIC - SESAME STREET'S OSCAR THE GROUCH, "I LOVE TRASH"] 00:00:12.870 --> 00:00:17.830 OSCAR THE GROUCH: (SINGING) Oh, I love trash. 00:00:17.830 --> 00:00:21.850 Anything dirty or dingy or dusty. 00:00:21.850 --> 00:00:26.435 Anything ragged or rotten or rusty. 00:00:26.435 --> 00:00:30.130 Yes, I love trash. 00:00:30.130 --> 00:00:32.980 If you really want to see something trashy, look at this. 00:00:32.980 --> 00:00:37.210 I have here a sneaker that's tattered and worn. 00:00:37.210 --> 00:00:39.400 DAVID J. MALAN: So this is, believe it or not, 00:00:39.400 --> 00:00:43.420 the very first program I wrote using a programming language called Scratch. 00:00:43.420 --> 00:00:45.310 It's a graphical programming language that 00:00:45.310 --> 00:00:48.130 was developed by MIT'S lifelong Kindergarten Group. 00:00:48.130 --> 00:00:50.650 And it was designed, initially, as an environment 00:00:50.650 --> 00:00:55.030 with which to get students excited about computing about technology, especially 00:00:55.030 --> 00:00:56.350 after-school programs. 00:00:56.350 --> 00:00:58.570 But what's wonderfully exciting about it too, 00:00:58.570 --> 00:01:00.380 is that it's also wonderfully instructive 00:01:00.380 --> 00:01:02.692 when it comes to what programming is. 00:01:02.692 --> 00:01:05.650 And indeed, we'll be able to use this programming language, ultimately, 00:01:05.650 --> 00:01:09.040 to explore the whole world of programming. 00:01:09.040 --> 00:01:11.740 Now, what is programming? 00:01:11.740 --> 00:01:14.470 So programming is ultimately about making software. 00:01:14.470 --> 00:01:19.260 And software, of course, is what runs on our hardware, runs on our computers. 00:01:19.260 --> 00:01:22.570 Software is something you double-click and then see and can interact with. 00:01:22.570 --> 00:01:26.560 Software is the files that you open on your computer and interact with. 00:01:26.560 --> 00:01:31.120 And software, ultimately, is what is written using programming code. 00:01:31.120 --> 00:01:34.130 Now, the software itself ultimately runs in any number of devices. 00:01:34.130 --> 00:01:36.340 It might be your desktop PC or your Mac. 00:01:36.340 --> 00:01:38.540 It might be your iPhone or your Android phone. 00:01:38.540 --> 00:01:40.990 But at the end of the day, all of those devices 00:01:40.990 --> 00:01:45.670 are programmed by a human writing, quite simply, code. 00:01:45.670 --> 00:01:47.140 What is code exactly? 00:01:47.140 --> 00:01:51.010 Well, at the end of the day, code is really just a technical implementation 00:01:51.010 --> 00:01:53.470 of algorithms, where algorithms themselves 00:01:53.470 --> 00:01:56.380 are step-by-step instructions for solving some problem. 00:01:56.380 --> 00:01:58.726 Now, what kind of problems might we want to solve? 00:01:58.726 --> 00:02:00.600 Well, let's consider an old-school one first. 00:02:04.360 --> 00:02:08.650 This here is an older technology, an old-school phonebook, inside of which 00:02:08.650 --> 00:02:11.350 are thousands of names and thousands of numbers. 00:02:11.350 --> 00:02:13.840 But it's a wonderful opportunity to think about a problem 00:02:13.840 --> 00:02:16.750 that we might want to solve, like looking someone up in a phonebook 00:02:16.750 --> 00:02:18.550 so that you can actually call them? 00:02:18.550 --> 00:02:21.970 Well, suppose I'm actually looking for someone like Mike Smith. 00:02:21.970 --> 00:02:25.030 Now, I can find Mike Smith in any number of ways in this phonebook, 00:02:25.030 --> 00:02:26.560 assuming he's actually in here. 00:02:26.560 --> 00:02:31.210 I might initially start very correctly and very foolishly, 00:02:31.210 --> 00:02:34.300 perhaps, at the first page, looking down for Mike. 00:02:34.300 --> 00:02:35.020 I don't see him. 00:02:35.020 --> 00:02:37.330 So I move to the second page, look down. 00:02:37.330 --> 00:02:39.961 And I don't see him at the third page, and so forth. 00:02:39.961 --> 00:02:41.710 And as I proceed through the phone book, I 00:02:41.710 --> 00:02:45.659 do claim that this algorithm, this set of instructions, step-by-step, 00:02:45.659 --> 00:02:47.200 for solving this problem, is correct. 00:02:47.200 --> 00:02:50.130 Because if Mike is here, I'm going to find him eventually. 00:02:50.130 --> 00:02:52.200 But of course, this is completely ridiculous. 00:02:52.200 --> 00:02:53.950 This is a completely inefficient algorithm 00:02:53.950 --> 00:02:55.150 for actually finding Mike Smith. 00:02:55.150 --> 00:02:55.690 Why? 00:02:55.690 --> 00:02:57.190 Well, it's going to take me forever to find him 00:02:57.190 --> 00:02:59.398 if he's all the way toward the end of the phone book. 00:02:59.398 --> 00:03:01.170 Wait, I know how to do this faster. 00:03:01.170 --> 00:03:02.980 I remember, from grade school, an old trick 00:03:02.980 --> 00:03:05.479 where, rather than counting by ones, you might count by two. 00:03:05.479 --> 00:03:09.010 So here I might do 2, 4, 6-- 00:03:09.010 --> 00:03:09.730 literally. 00:03:09.730 --> 00:03:11.110 [LAUGHS] 00:03:11.110 --> 00:03:12.910 This algorithm turns out, is much slower. 00:03:12.910 --> 00:03:17.350 6, 8, 10, 12, 14, 16, 18, and so forth. 00:03:17.350 --> 00:03:21.050 And at that rate, I'll certainly find Mike much faster, twice as fast, 00:03:21.050 --> 00:03:21.950 in fact. 00:03:21.950 --> 00:03:24.700 But is that algorithm, are those steps correct? 00:03:24.700 --> 00:03:26.140 Now, they're not quite correct. 00:03:26.140 --> 00:03:29.830 Because what if by chance I happened to be grabbing two pages 00:03:29.830 --> 00:03:32.290 and Mike happens to be sandwiched in between them? 00:03:32.290 --> 00:03:34.610 I might blow right past Mike's name in the phone book. 00:03:34.610 --> 00:03:36.889 So I better have at least a check, a sort of condition 00:03:36.889 --> 00:03:38.680 where I think to myself, wait a minute if I 00:03:38.680 --> 00:03:41.470 hit someone whose name starts with t instead of s, 00:03:41.470 --> 00:03:42.880 maybe I should double back. 00:03:42.880 --> 00:03:44.290 At least one page. 00:03:44.290 --> 00:03:47.989 Of course, no one out there-- if you even still use this technology-- 00:03:47.989 --> 00:03:49.780 is going to solve this problem in that way. 00:03:49.780 --> 00:03:51.140 What are you going to do instead? 00:03:51.140 --> 00:03:52.450 Well, if you're like me, you're probably going 00:03:52.450 --> 00:03:55.000 to instinctively go roughly to the middle of the phone book 00:03:55.000 --> 00:03:57.190 and look down there to see where you are. 00:03:57.190 --> 00:03:59.170 Odds are, if you're like me in this book here, 00:03:59.170 --> 00:04:01.590 you're going to find yourself in the M section. 00:04:01.590 --> 00:04:06.700 But Smith, of course, comes after M. And so we know Mike is to the right side 00:04:06.700 --> 00:04:07.390 here. 00:04:07.390 --> 00:04:11.257 And so that suggests that we could take a pretty big bite out of this problem. 00:04:11.257 --> 00:04:13.340 I don't need to start flipping one page at a time. 00:04:13.340 --> 00:04:16.254 I know Mike is not in this half of the phone book. 00:04:16.254 --> 00:04:19.089 And what's nice about this intuition is that, you know what? 00:04:19.089 --> 00:04:25.390 I can not only figuratively but shall we say, tear this problem in half. 00:04:25.390 --> 00:04:27.370 That wasn't really very hard at all this time. 00:04:27.370 --> 00:04:31.480 Throw 1/2 of the problem away, and I'm left with just 1/2 00:04:31.480 --> 00:04:33.270 of the very same problem, fundamentally. 00:04:33.270 --> 00:04:34.300 Now, what might I do? 00:04:34.300 --> 00:04:36.330 I don't want to resort to the slower approach. 00:04:36.330 --> 00:04:40.210 I want to leverage that intuition, divide and conquer this problem, 00:04:40.210 --> 00:04:42.610 look down and realize, oh, I'm in the T section, 00:04:42.610 --> 00:04:44.650 I went a little too far, but no big deal. 00:04:44.650 --> 00:04:50.230 Let's tear the problem in 2/3 and really, in 1/2. 00:04:50.230 --> 00:04:52.720 So I'm left with just 1/4 of the problem now. 00:04:52.720 --> 00:04:56.770 And I can repeat this algorithm again and again and again, 00:04:56.770 --> 00:05:02.140 until theoretically, I'm left with just Mike's number on this one sheet, 00:05:02.140 --> 00:05:04.255 or in this case, one scrap of paper. 00:05:04.255 --> 00:05:05.630 So what have I really done there? 00:05:05.630 --> 00:05:09.550 So the intuition, ultimately, is that rather than looking for Mike one page 00:05:09.550 --> 00:05:13.750 at a time, I'm instead leveraging the fact that that phone book is sorted. 00:05:13.750 --> 00:05:17.500 And I'm literally dividing and conquering the problem again and again 00:05:17.500 --> 00:05:21.850 and again so that if I started with 1,000 pages, I then go down to 500. 00:05:21.850 --> 00:05:26.230 I then go down to 250, 125, as opposed to starting at 1,000 00:05:26.230 --> 00:05:33.130 and going to 999, then 998, 997, which surely is going to take me much longer. 00:05:33.130 --> 00:05:36.100 Of course, that's not the real way to tear a phone book in half. 00:05:36.100 --> 00:05:40.420 So fortunately there are just two of these books left on earth. 00:05:40.420 --> 00:05:42.770 And we have the second one here. 00:05:42.770 --> 00:05:45.250 So indeed, if I want to go ahead and tear a problem in 1/2, 00:05:45.250 --> 00:05:47.615 the real way you do this-- shall we say? 00:05:47.615 --> 00:05:49.280 There we go-- is-- 00:05:52.432 --> 00:05:54.360 [STRAINS] 00:05:55.330 --> 00:05:56.380 --that way. 00:05:56.380 --> 00:06:01.190 But now back to some programming. 00:06:01.190 --> 00:06:02.910 So I just solved that problem. 00:06:02.910 --> 00:06:06.080 But what was the algorithm exactly that I used to solve it? 00:06:06.080 --> 00:06:08.570 In fact, can we distill some of the key components 00:06:08.570 --> 00:06:11.150 of what I did fairly intuitively and see if we 00:06:11.150 --> 00:06:13.460 can't then later apply those same principles 00:06:13.460 --> 00:06:15.740 to solving other problems altogether? 00:06:15.740 --> 00:06:17.135 Well, let's consider this. 00:06:17.135 --> 00:06:19.760 I propose that the very first thing I did was this in step one, 00:06:19.760 --> 00:06:21.200 I picked up the phone book. 00:06:21.200 --> 00:06:26.150 Step two, I opened to the middle of the phone book, and I looked at the names. 00:06:26.150 --> 00:06:27.650 But then I had to make a decision. 00:06:27.650 --> 00:06:30.620 Because I was, at that point, presented with some information 00:06:30.620 --> 00:06:31.770 as to what is on the page. 00:06:31.770 --> 00:06:37.240 And so step four, if Smith is among the names, on the page, then, step five, 00:06:37.240 --> 00:06:38.710 I was going to call Mike. 00:06:38.710 --> 00:06:41.990 Else if, step six, Smith is earlier in the book, that is, 00:06:41.990 --> 00:06:44.810 he's to the left alphabetically, well then, logically I 00:06:44.810 --> 00:06:48.980 should open to the middle of the left half of the book 00:06:48.980 --> 00:06:53.090 and then go back to step three, thereby looking at names again and continuing 00:06:53.090 --> 00:06:54.650 down this same set of steps. 00:06:54.650 --> 00:06:59.240 However, if after step six, I decide no, Smith is not earlier in the book 00:06:59.240 --> 00:07:01.550 and therefore, I should not do step seven and eight, 00:07:01.550 --> 00:07:05.000 well then, let me ask the question else if Smith is later in the book, 00:07:05.000 --> 00:07:07.790 then is he to the right of where I'm currently looking? 00:07:07.790 --> 00:07:11.420 And if so, opens in the middle of the right half of the book 00:07:11.420 --> 00:07:13.340 and then go back to step three. 00:07:13.340 --> 00:07:16.950 Because now I'm looking at a different page and a different set of names. 00:07:16.950 --> 00:07:19.767 However, if that's not the case that Mike's among the names, 00:07:19.767 --> 00:07:22.100 and it's not the case that Smith is earlier in the book, 00:07:22.100 --> 00:07:25.430 and it's not the case that Smith is later in the book, then what should 00:07:25.430 --> 00:07:27.290 I do, perhaps, in step 12? 00:07:27.290 --> 00:07:31.880 Well else or otherwise, I should incept their team just quit. 00:07:31.880 --> 00:07:35.900 Because logically, if Mike's not on this page, and he's not to the left, 00:07:35.900 --> 00:07:39.750 and he's not to the right, that he must not be in the book at all. 00:07:39.750 --> 00:07:41.700 And so what do we have here? 00:07:41.700 --> 00:07:44.000 Well, this is something that I would call code. 00:07:44.000 --> 00:07:48.057 It's not written in C or C++ or Java, or some other programming language you 00:07:48.057 --> 00:07:48.890 might have heard of. 00:07:48.890 --> 00:07:52.850 It's written in English of some sort, pretty succinct, terse English. 00:07:52.850 --> 00:07:55.340 And indeed, it doesn't follow any particular vocabulary. 00:07:55.340 --> 00:07:58.910 I just tried to say each of these lines as succinctly as I could. 00:07:58.910 --> 00:08:01.940 So we'll call it pseudo code, sort of code-like syntax 00:08:01.940 --> 00:08:04.670 that happens to be written in English, so you could write it 00:08:04.670 --> 00:08:05.960 in most any other language. 00:08:05.960 --> 00:08:08.210 But what's important is a few characteristics. 00:08:08.210 --> 00:08:11.420 One, just to keep things nice and orderly, I've numbered the lines. 00:08:11.420 --> 00:08:12.800 Now, I did that manually. 00:08:12.800 --> 00:08:15.140 Software can certainly do that automatically for us. 00:08:15.140 --> 00:08:18.020 But that helps me make sense of what comes after what. 00:08:18.020 --> 00:08:21.080 And in particular, it also allows me, in steps 8 and 11 00:08:21.080 --> 00:08:23.360 to make these back references and sort of go back 00:08:23.360 --> 00:08:26.240 to and re-execute some earlier lines. 00:08:26.240 --> 00:08:30.976 But there's also some similarities, I'd argue, among some of the verbs 00:08:30.976 --> 00:08:32.850 and some of the language here that I've used. 00:08:32.850 --> 00:08:35.419 Let's see if we can't highlight a few of those principles. 00:08:35.419 --> 00:08:38.570 So one, notice here in yellow, are a whole bunch 00:08:38.570 --> 00:08:41.090 of actions, a whole bunch of verbs, that are 00:08:41.090 --> 00:08:45.500 telling me to go do something, pick up, open to, look at, call, open, 00:08:45.500 --> 00:08:47.210 go back, quit. 00:08:47.210 --> 00:08:51.830 Those are all very specific procedures, or we'll call them functions, 00:08:51.830 --> 00:08:53.240 shall we say? 00:08:53.240 --> 00:08:55.790 What else is similar among these lines? 00:08:55.790 --> 00:08:58.610 Well, here I have if, else if, else if, and else. 00:08:58.610 --> 00:09:02.640 So let's henceforth call those conditions, or branches. 00:09:02.640 --> 00:09:04.010 Really, they're decision points. 00:09:04.010 --> 00:09:06.295 This is really a four-way decision point. 00:09:06.295 --> 00:09:08.420 And I have to go down either this road or that road 00:09:08.420 --> 00:09:12.230 or this other road or this fourth road when making a decision. 00:09:12.230 --> 00:09:15.130 But what do I use as input to make that decision? 00:09:15.130 --> 00:09:18.410 Well, let's consider these questions, really, that we're asking ourself. 00:09:18.410 --> 00:09:21.350 Is Smith among the names on the page in front of me? 00:09:21.350 --> 00:09:22.730 Is Smith earlier in the book? 00:09:22.730 --> 00:09:23.960 Is Smith later in the book? 00:09:23.960 --> 00:09:27.830 Well, a programmer would call those three phrases Boolean 00:09:27.830 --> 00:09:31.300 expressions, named after someone by the last name of Boole. 00:09:31.300 --> 00:09:35.780 And a Boolean expression, by definition, is either true or false, yes or no. 00:09:35.780 --> 00:09:39.060 So it's really a question that has a yes-no, or true-false answer. 00:09:39.060 --> 00:09:42.440 And indeed, each of these yellow phrases has an answer. 00:09:42.440 --> 00:09:45.320 Because I use that answer, yes or no, to decide 00:09:45.320 --> 00:09:48.170 whether or not to execute some subsequent lines. 00:09:48.170 --> 00:09:52.610 And in particular, notice that only if those Boolean expressions, and ergo, 00:09:52.610 --> 00:09:57.890 the whole branch is true and is to be taken, only then should 00:09:57.890 --> 00:10:05.270 I execute lines 5 and 7 and 8, and 10 and 11 and 13. 00:10:05.270 --> 00:10:07.700 And notice, per the indentation, that those lines are very 00:10:07.700 --> 00:10:10.850 deliberately associated with exactly those conditions. 00:10:10.850 --> 00:10:16.250 So you only do them if the line above them is true, or is yes. 00:10:16.250 --> 00:10:18.050 Lastly, there is this curiosity. 00:10:18.050 --> 00:10:21.580 And both line 8 and line 11 go back to step three. 00:10:21.580 --> 00:10:25.264 Well, henceforth, let's start calling this a loop or a cycle. 00:10:25.264 --> 00:10:27.680 Because conceptually, that's pretty much what's happening. 00:10:27.680 --> 00:10:31.640 I'm being told, in steps 8 and 11, to go back to some earlier step. 00:10:31.640 --> 00:10:33.800 But given that I came from that step, this 00:10:33.800 --> 00:10:36.080 is a loop, in the sense that I'm going to keep doing, 00:10:36.080 --> 00:10:38.120 perhaps, the same thing again and again. 00:10:38.120 --> 00:10:40.040 Maybe going left maybe going right, hopefully 00:10:40.040 --> 00:10:43.460 eventually exiting or calling Mike, because I've deliberately 00:10:43.460 --> 00:10:46.250 induced some kind of loop here. 00:10:46.250 --> 00:10:49.850 But these aren't the only ideas that we might see in a computer program. 00:10:49.850 --> 00:10:53.720 And indeed, that's what that was, finding Mike Smith in that phone book, 00:10:53.720 --> 00:10:57.890 that was, effectively, a computer program being executed by me, albeit, 00:10:57.890 --> 00:10:58.520 a human. 00:10:58.520 --> 00:11:02.320 But the code, or the pseudo-code that drove my thinking 00:11:02.320 --> 00:11:04.780 and drove my actions is exactly the same kind of logic 00:11:04.780 --> 00:11:08.260 that you might feed to an actual computer, whether it is a PC or a Mac 00:11:08.260 --> 00:11:10.360 or an iPhone or Android phone. 00:11:10.360 --> 00:11:15.010 Because what computer programs tend to share in common, no matter the language 00:11:15.010 --> 00:11:18.070 they're written in, are things like functions and loops 00:11:18.070 --> 00:11:20.080 and conditions and Boolean expressions. 00:11:20.080 --> 00:11:25.030 And even more features still, things called variables and threads and events 00:11:25.030 --> 00:11:26.080 and even more features. 00:11:26.080 --> 00:11:28.660 And so that's what we'll do here in on out, 00:11:28.660 --> 00:11:31.300 is explore not just programming fundamentals, 00:11:31.300 --> 00:11:33.820 but explore some of these fundamentals in the context 00:11:33.820 --> 00:11:35.710 of some actual languages. 00:11:35.710 --> 00:11:38.620 First, one of the oldest languages someone might know, 00:11:38.620 --> 00:11:42.250 and we're not going to go with it as far back as FORTRAN and COBOL 00:11:42.250 --> 00:11:45.910 or binary or familiar, rather, we'll start with this one here. 00:11:45.910 --> 00:11:48.830 This is a small program, just a few lines of code, 00:11:48.830 --> 00:11:52.290 so to speak, written in an older language called C. 00:11:52.290 --> 00:11:55.270 A language that's still very much in use but it's older, 00:11:55.270 --> 00:11:57.722 in that it's been around for some decades now. 00:11:57.722 --> 00:11:59.680 And it doesn't tend to have that many features. 00:11:59.680 --> 00:12:03.100 But it's nonetheless a very powerful language, on top of which, so to speak, 00:12:03.100 --> 00:12:05.740 more recent, more modern languages have been built. 00:12:05.740 --> 00:12:08.290 It looks pretty cryptic, I'll grant you that. 00:12:08.290 --> 00:12:11.470 There's like, this hashtag over there and these angled brackets 00:12:11.470 --> 00:12:14.770 and curly braces, parentheses, semi-colon, backslash n 00:12:14.770 --> 00:12:17.740 and a few other syntactic curiosities. 00:12:17.740 --> 00:12:19.600 But what do all those mean? 00:12:19.600 --> 00:12:23.470 Well, even if unsure at first glance, like odds are you are, 00:12:23.470 --> 00:12:26.780 you can probably take a guess as to what this program does. 00:12:26.780 --> 00:12:30.190 Suppose I did write this program as such and then 00:12:30.190 --> 00:12:34.780 somehow ran this program on my Mac or PC or phone, what's it going to do? 00:12:34.780 --> 00:12:39.190 Probably, yeah, I mean, even if you don't recognize all the syntax-- 00:12:39.190 --> 00:12:42.320 I do recognize the word Print or Print-F, whatever that is. 00:12:42.320 --> 00:12:45.280 But printing I'm familiar with. 00:12:45.280 --> 00:12:47.360 And I do see a phrase here-- "hello, world," 00:12:47.360 --> 00:12:49.360 but I'm not quite sure what that backslash n is. 00:12:49.360 --> 00:12:53.970 So I'm going to guess this program, when run, is going to print hello, world. 00:12:53.970 --> 00:12:55.960 And indeed, that's what it does. 00:12:55.960 --> 00:12:57.490 There's a bit of overhead here. 00:12:57.490 --> 00:13:00.520 Not just syntactically, but sort of conceptually. 00:13:00.520 --> 00:13:04.120 Because you, as a programmer, need to understand, what does this line do? 00:13:04.120 --> 00:13:05.200 What does this line do? 00:13:05.200 --> 00:13:06.580 What do these curly braces mean? 00:13:06.580 --> 00:13:08.830 Why is there the backslash n there and so forth? 00:13:08.830 --> 00:13:10.900 But those are all just things that you learn. 00:13:10.900 --> 00:13:14.190 For instance, if you've not spoken Japanese 00:13:14.190 --> 00:13:18.130 as a language or Spanish, or any number of other languages, the very first time 00:13:18.130 --> 00:13:22.630 you look at that written language, it might look like Greek to you, 00:13:22.630 --> 00:13:23.470 so to speak. 00:13:23.470 --> 00:13:26.445 And might, indeed, be Greek if you don't speak Greek. 00:13:26.445 --> 00:13:29.320 But that's just because you haven't recognized those patterns before. 00:13:29.320 --> 00:13:31.414 You haven't recognized those symbols before. 00:13:31.414 --> 00:13:33.580 And indeed, if right now you're staring at something 00:13:33.580 --> 00:13:37.120 that's completely unfamiliar and while you do recognize some patterns, 00:13:37.120 --> 00:13:38.954 it's not all very straightforward, that's 00:13:38.954 --> 00:13:40.870 just because you don't yet speak the language. 00:13:40.870 --> 00:13:42.340 You don't yet know the language. 00:13:42.340 --> 00:13:45.670 You don't yet know how to program in this particular language. 00:13:45.670 --> 00:13:48.160 But what's exciting about programming in general 00:13:48.160 --> 00:13:52.540 is that even though there are dozens, if not hundreds, of programming languages, 00:13:52.540 --> 00:13:56.290 in use today, they're much more accessible, 00:13:56.290 --> 00:14:00.040 I dare say, than written and spoken human languages. 00:14:00.040 --> 00:14:03.490 Rather, you'll find many, many, many similarities among programming 00:14:03.490 --> 00:14:06.400 languages such that once you know one and a few, 00:14:06.400 --> 00:14:08.530 can you relatively easily bootstrap yourself, 00:14:08.530 --> 00:14:12.190 teach yourself other languages that might have newer and fancier features 00:14:12.190 --> 00:14:14.680 and then just, therefore, make you more productive? 00:14:14.680 --> 00:14:17.513 Because at the end of the day, that's what programming is all about. 00:14:17.513 --> 00:14:20.950 It's about writing software and therefore, controlling hardware with it 00:14:20.950 --> 00:14:22.940 to solve problems on your behalf. 00:14:22.940 --> 00:14:26.510 Whether that problem is to make a telephone call on a mobile device, 00:14:26.510 --> 00:14:29.590 whether that problem is to send an email or search for something 00:14:29.590 --> 00:14:32.620 on the internet, all of those problems are solved 00:14:32.620 --> 00:14:34.740 by controlling hardware with software. 00:14:34.740 --> 00:14:38.650 And the means by which you do that is by programming writing code 00:14:38.650 --> 00:14:42.790 like this, that ultimately runs on those devices. 00:14:42.790 --> 00:14:47.380 And yet-- and yet, I can't help but recall 00:14:47.380 --> 00:14:51.730 that the computers I know or grew up with, somehow or other, only 00:14:51.730 --> 00:14:54.190 understand zeros and ones, right? 00:14:54.190 --> 00:14:57.100 They only understand the so-called binary system. 00:14:57.100 --> 00:15:00.220 And yet, this does not quite look like zeros and ones. 00:15:00.220 --> 00:15:03.910 So how is it that I, who can just barely understand this as a human, 00:15:03.910 --> 00:15:07.060 can program a computer using this and have it understand this, 00:15:07.060 --> 00:15:09.560 even though these, themselves, are not zeros and ones? 00:15:09.560 --> 00:15:14.110 In other words, how do we get this to that instead? 00:15:14.110 --> 00:15:16.300 Well, it turns out when writing a computer program, 00:15:16.300 --> 00:15:17.874 there's often multiple steps. 00:15:17.874 --> 00:15:19.540 And sometimes these steps are automated. 00:15:19.540 --> 00:15:22.840 But sometimes you have to type, you have to make them happen manually. 00:15:22.840 --> 00:15:26.560 And in general, the process of converting this code 00:15:26.560 --> 00:15:30.580 here, or source code, which tends to be English-like syntax that 00:15:30.580 --> 00:15:35.530 does follow a set of standards and use a certain vocabulary, this being C, 00:15:35.530 --> 00:15:41.710 the language known as C, I can convert this source code to machine code, that 00:15:41.710 --> 00:15:45.310 is zeros and ones that look like this by running this source 00:15:45.310 --> 00:15:49.000 code through a special type of program quite simply called a Compiler. 00:15:49.000 --> 00:15:52.300 A compiler is simply a program that compiles code, 00:15:52.300 --> 00:15:56.830 converts it from source code to machine code so that the human can write it, 00:15:56.830 --> 00:16:00.960 but the machine can ultimately understand it. 00:16:00.960 --> 00:16:02.610 Consider another example here. 00:16:02.610 --> 00:16:07.740 This program looks almost kind of-- well, not really the same. 00:16:07.740 --> 00:16:10.819 But it's written in a very similar language called C++. 00:16:10.819 --> 00:16:13.860 Now, here too, you might not recognize some of the symbols on the screen. 00:16:13.860 --> 00:16:16.800 But you probably do recognize hello world, this time 00:16:16.800 --> 00:16:18.600 without the backslash n. 00:16:18.600 --> 00:16:22.980 And even though we have a different word up here, I/O stream and standard, endl 00:16:22.980 --> 00:16:26.940 here, odds are it's a pretty good bet that this program too 00:16:26.940 --> 00:16:28.110 does print hello world. 00:16:28.110 --> 00:16:31.260 And this is already, in simplest form, testament to the fact 00:16:31.260 --> 00:16:36.330 that many programming languages do the same things differently. 00:16:36.330 --> 00:16:41.670 Indeed, whether you know C or C++ or something like Java or PHP or Python 00:16:41.670 --> 00:16:44.100 or Ruby, or any number of other languages, odds are, 00:16:44.100 --> 00:16:47.700 at the end of the day, you can actually solve the same problems using any 00:16:47.700 --> 00:16:50.850 number of different languages, much like you, as a human, 00:16:50.850 --> 00:16:55.080 can express yourself verbally in any number of spoken languages. 00:16:55.080 --> 00:16:57.630 But it might just be easier to communicate 00:16:57.630 --> 00:16:59.880 with other humans in one language. 00:16:59.880 --> 00:17:02.220 It might just be easier and less time consuming 00:17:02.220 --> 00:17:05.577 to solve problems using one programming language over another. 00:17:05.577 --> 00:17:08.160 And this indeed, in part at least, explains why there are just 00:17:08.160 --> 00:17:09.450 so many programming languages. 00:17:09.450 --> 00:17:10.932 We've seen just two so far. 00:17:10.932 --> 00:17:12.390 But many different languages exist. 00:17:12.390 --> 00:17:14.973 Because as more and more time passes, and more and more humans 00:17:14.973 --> 00:17:18.150 use these languages, do they realize, wow, I could do this better. 00:17:18.150 --> 00:17:20.770 Or gee, I wish I had this feature instead or wow, 00:17:20.770 --> 00:17:24.210 I wish I could type fewer keys on the keyboard in order just 00:17:24.210 --> 00:17:26.520 to have my computer say hello world. 00:17:26.520 --> 00:17:29.550 And so you'll find that different languages do things differently 00:17:29.550 --> 00:17:31.710 even if the end result is the same. 00:17:31.710 --> 00:17:35.580 For instance, here is a language called Python. 00:17:35.580 --> 00:17:39.030 And thank God-- this is the simplest example of the ones 00:17:39.030 --> 00:17:42.000 we've seen thus far-- it really just says what it means. 00:17:42.000 --> 00:17:45.510 No hashtags up top, no curly braces, apparently. 00:17:45.510 --> 00:17:47.790 Just print hello world. 00:17:47.790 --> 00:17:51.270 And that's with this program in Python does. 00:17:51.270 --> 00:17:53.820 Now Python, it's worth noting, is a different type 00:17:53.820 --> 00:17:57.390 of language in most implementations. 00:17:57.390 --> 00:18:00.750 That is to say, you typically don't write Python source code 00:18:00.750 --> 00:18:04.410 and then manually convert it to machine code, zeros and ones. 00:18:04.410 --> 00:18:08.490 Rather, there is a special program that you use that's not called a compiler, 00:18:08.490 --> 00:18:10.200 but called an interpreter. 00:18:10.200 --> 00:18:13.110 Because what's also the case, with some languages, 00:18:13.110 --> 00:18:15.630 is that rather than convert them directly to zeros and ones, 00:18:15.630 --> 00:18:20.000 that your CPU, your computer's brain understands directly, 00:18:20.000 --> 00:18:23.370 you instead convert them to some intermediate language, generally known 00:18:23.370 --> 00:18:24.360 as bytecode. 00:18:24.360 --> 00:18:27.280 Java is the same way, as are some other languages. 00:18:27.280 --> 00:18:29.490 And so with Python, before running this, I 00:18:29.490 --> 00:18:34.290 might actually compile it or convert it to this much more 00:18:34.290 --> 00:18:35.670 cryptic-looking format. 00:18:35.670 --> 00:18:38.550 This is not code that I, myself, would particularly enjoy writing. 00:18:38.550 --> 00:18:42.330 But this is what's outputted when you write Python code. 00:18:42.330 --> 00:18:44.790 And it's outputted as something called bytecode. 00:18:44.790 --> 00:18:47.810 So this is just an intermediate format that still is somewhat readable. 00:18:47.810 --> 00:18:50.380 It's at least alphabetical characters and not zeros and ones. 00:18:50.380 --> 00:18:52.860 So as a human I kind of sort of can read it. 00:18:52.860 --> 00:18:55.180 But my computer can absolutely read this. 00:18:55.180 --> 00:18:58.830 Specifically, a program called an Interpreter on my computer, 00:18:58.830 --> 00:19:01.860 that, I could download to my Mac or PC or some other device 00:19:01.860 --> 00:19:04.660 can understand these lines one at a time. 00:19:04.660 --> 00:19:06.810 And so generally, a language like Python might 00:19:06.810 --> 00:19:10.800 again, be compiled to an intermediate language like this here, this bytecode. 00:19:10.800 --> 00:19:14.760 And then it's this code, ultimately, that's read by the machine. 00:19:14.760 --> 00:19:17.670 But the takeaway for us is really, quite simply, 00:19:17.670 --> 00:19:20.340 that there are different ways of doing the same thing 00:19:20.340 --> 00:19:21.750 in any number of languages. 00:19:21.750 --> 00:19:25.210 And even among those languages, do they get used in different ways? 00:19:25.210 --> 00:19:29.040 Sometimes you have to compile your code and then run the resulting machine 00:19:29.040 --> 00:19:31.410 code, the zeros and ones, by double clicking an icon 00:19:31.410 --> 00:19:32.940 or typing some command. 00:19:32.940 --> 00:19:36.420 Or might you be able to write your code and not explicitly compile 00:19:36.420 --> 00:19:38.190 it, just instead interpret it. 00:19:38.190 --> 00:19:40.320 The steps might feel or be a little different. 00:19:40.320 --> 00:19:44.850 But at the end of the day, most users experience the software 00:19:44.850 --> 00:19:47.400 in a familiar way, by double-clicking an icon 00:19:47.400 --> 00:19:49.290 by running in the name of a command. 00:19:49.290 --> 00:19:52.110 And for all intents and purposes, your users or your customers 00:19:52.110 --> 00:19:55.540 don't need to know or care what language something is written in, 00:19:55.540 --> 00:20:00.930 so long as it is created for your particular type of computer 00:20:00.930 --> 00:20:03.257 or your particular operating system. 00:20:03.257 --> 00:20:06.090 This is why, if you have a Mac, for instance, and a friend has a PC, 00:20:06.090 --> 00:20:10.680 you can't necessarily just share software and run the same programs 00:20:10.680 --> 00:20:11.816 on multiple computers. 00:20:11.816 --> 00:20:14.190 You might each have to buy or download different versions 00:20:14.190 --> 00:20:17.370 of the software, one of which is for Mac OS, one of which is for Windows. 00:20:17.370 --> 00:20:21.100 And even though those programs might be written in the same language, 00:20:21.100 --> 00:20:25.770 they generally have been compiled for or packaged for different computers, CPUs, 00:20:25.770 --> 00:20:28.170 or really, different operating systems. 00:20:28.170 --> 00:20:31.590 But this is just a decision that the software author, the software 00:20:31.590 --> 00:20:34.290 developer, or software engineer, so to speak, 00:20:34.290 --> 00:20:38.250 simply has to make on your behalf before shipping his or her software to you. 00:20:38.250 --> 00:20:39.540 What about other languages? 00:20:39.540 --> 00:20:41.040 This one here is Java. 00:20:41.040 --> 00:20:42.630 It too has some syntax of its own. 00:20:42.630 --> 00:20:45.690 But at the end of the day, it's going to do the same thing, 00:20:45.690 --> 00:20:48.690 print out the screen just hello world. 00:20:48.690 --> 00:20:50.820 This example here is another language called 00:20:50.820 --> 00:20:55.140 Ruby, which is commonly used as an alternative to some of these languages 00:20:55.140 --> 00:20:55.770 here as well. 00:20:55.770 --> 00:20:59.100 And it is even more simple syntactically, 00:20:59.100 --> 00:21:01.880 in the particular example, in that it's going to put or print 00:21:01.880 --> 00:21:04.790 on the screen, hello world as well. 00:21:04.790 --> 00:21:07.130 This one here is written in a language called LISP. 00:21:07.130 --> 00:21:09.440 The salient difference here being these parentheses 00:21:09.440 --> 00:21:12.354 on the outside, which are just hinting at a syntactic feature 00:21:12.354 --> 00:21:13.520 of this particular language. 00:21:13.520 --> 00:21:16.550 But this is quite simply how you might print hello world. 00:21:16.550 --> 00:21:18.810 And then in another language altogether, JavaScript 00:21:18.810 --> 00:21:23.720 is this how you might print hello world to the screen or browser window 00:21:23.720 --> 00:21:24.750 as well? 00:21:24.750 --> 00:21:28.640 Now, we've certainly only just scratched the surface of these several languages. 00:21:28.640 --> 00:21:30.830 And surely these languages can do much more. 00:21:30.830 --> 00:21:33.410 And odds are, it's going to take many more lines of code. 00:21:33.410 --> 00:21:36.830 But throughout each of these language are some commonalities, 00:21:36.830 --> 00:21:40.520 support for things like functions and loops 00:21:40.520 --> 00:21:43.310 and conditions and Boolean expressions, and maybe 00:21:43.310 --> 00:21:46.280 things like events and threads and variables and more. 00:21:46.280 --> 00:21:50.030 And so rather than get too much into the weeds of very specific languages, 00:21:50.030 --> 00:21:52.370 let's focus here on the ideas, and let's focus 00:21:52.370 --> 00:21:57.020 on these ideas as graphically and as pleasantly 00:21:57.020 --> 00:22:02.000 as we can by revisiting that first programming environment called Scratch. 00:22:02.000 --> 00:22:05.180 In particular, you'll recall that Scratch, just a moment ago, 00:22:05.180 --> 00:22:08.570 was raining trash down on my screen in synchronization 00:22:08.570 --> 00:22:11.810 with the old Sesame Street song that you might remember. 00:22:11.810 --> 00:22:13.850 But how was that program working? 00:22:13.850 --> 00:22:16.340 Well, consider how we might have gone about implementing 00:22:16.340 --> 00:22:18.380 some of these basic ideas. 00:22:18.380 --> 00:22:20.724 There was trash falling from the screen. 00:22:20.724 --> 00:22:22.640 But what does it mean to fall from the screen? 00:22:22.640 --> 00:22:26.180 Well, a screen, recall, is just a grid of dots, a grid of pixels, 00:22:26.180 --> 00:22:29.540 a bunch of pixels left-right, a bunch of pixels top-down. 00:22:29.540 --> 00:22:33.410 And so to move on the screen or to animate something on the screen really 00:22:33.410 --> 00:22:37.220 means just to take an image, whether it's a .png or .gif or .jpg or some 00:22:37.220 --> 00:22:40.610 other format and just move it slightly, slightly. 00:22:40.610 --> 00:22:43.470 Maybe one pixel downward at a time. 00:22:43.470 --> 00:22:48.080 And if you move that image downward one pixel at a time quickly enough, 00:22:48.080 --> 00:22:50.435 it doesn't look like some very jagged movement. 00:22:50.435 --> 00:22:54.290 It, to the human eyes, actually looks like a very clear movement. 00:22:54.290 --> 00:22:56.390 And so indeed what I was probably doing in order 00:22:56.390 --> 00:23:00.680 to make trash fall from the sky here is it started off screen. 00:23:00.680 --> 00:23:04.100 And then in some kind of loop, moved some number of pixels 00:23:04.100 --> 00:23:05.900 every second or every split second. 00:23:05.900 --> 00:23:07.580 And the result is some animation. 00:23:07.580 --> 00:23:10.400 Well, why does the trash, like the shoe and the piece of trash, 00:23:10.400 --> 00:23:12.920 actually stop at the bottom of the screen? 00:23:12.920 --> 00:23:16.700 Well, odds are, I could solve that problem with some kind of condition, 00:23:16.700 --> 00:23:18.680 some kind of logic or Boolean expression where 00:23:18.680 --> 00:23:23.930 I ask the computer if you're touching the bottom of the screen, sneaker, 00:23:23.930 --> 00:23:25.550 then stop moving. 00:23:25.550 --> 00:23:29.570 Or conversely, only if you're not touching the bottom of the screen, 00:23:29.570 --> 00:23:31.400 should you be moving top to bottom. 00:23:31.400 --> 00:23:33.740 So you can effectively stop the animation 00:23:33.740 --> 00:23:37.250 by just using some additional logic, and asking a question, 00:23:37.250 --> 00:23:39.900 are you at the bottom of the screen, yes or no? 00:23:39.900 --> 00:23:41.450 True or false? 00:23:41.450 --> 00:23:45.020 Meanwhile, every time I click and drag the piece of trash 00:23:45.020 --> 00:23:47.600 into the trashcan, what was happening while Oscar 00:23:47.600 --> 00:23:50.970 was emerging from his trash can, which was another form of animation 00:23:50.970 --> 00:23:51.650 altogether. 00:23:51.650 --> 00:23:53.720 But he was also shouting out, in a little cartoon 00:23:53.720 --> 00:23:57.680 bubble, the number of pieces of trash that I'd already deposited in his can, 00:23:57.680 --> 00:24:00.020 starting at one, then two, then three. 00:24:00.020 --> 00:24:02.120 Well, he would appear to be using something 00:24:02.120 --> 00:24:06.890 you might recall from algebra, being a variable like x or y or z. 00:24:06.890 --> 00:24:12.290 And this variable was probably initialized, or set initially to 0, 00:24:12.290 --> 00:24:14.090 because we'd put no pieces of trash. 00:24:14.090 --> 00:24:17.550 And every time Oscar received a new piece of trash, 00:24:17.550 --> 00:24:21.600 he probably incremented, or added 1, to that variable. 00:24:21.600 --> 00:24:25.296 So 0 becomes 1, which then becomes 2 or 3. 00:24:25.296 --> 00:24:26.920 Now, how did I increment that variable? 00:24:26.920 --> 00:24:29.730 Odds are, there is some kind of condition, 00:24:29.730 --> 00:24:34.100 saying if piece of trash added to trash can, then 00:24:34.100 --> 00:24:37.580 increase the variable's value by 1. 00:24:37.580 --> 00:24:41.550 If it's called x make x 1, then 2, than 3. 00:24:41.550 --> 00:24:43.272 So again, a basic building block. 00:24:43.272 --> 00:24:46.480 Now, there is more and more trash that would eventually fall from the screen. 00:24:46.480 --> 00:24:48.813 And my god, there's that song that I still can't get out 00:24:48.813 --> 00:24:50.600 of my head, 10 years later. 00:24:50.600 --> 00:24:54.500 But that, too, was playing on some kind of loop or some kind of synchronization 00:24:54.500 --> 00:24:55.790 with the game at hand. 00:24:55.790 --> 00:24:58.826 So even if, at first glance, this game seems pretty daunting. 00:24:58.826 --> 00:25:00.950 And you might think, that must have taken you like, 00:25:00.950 --> 00:25:02.510 what, David, eight hours? 00:25:02.510 --> 00:25:03.500 Well, that is true. 00:25:03.500 --> 00:25:04.970 It did take me at least that long. 00:25:04.970 --> 00:25:08.900 And I swear, by far the most challenging aspect of this game 00:25:08.900 --> 00:25:13.290 wasn't getting the logic right, but was getting that song in of and henceforth, 00:25:13.290 --> 00:25:14.880 never out of my head. 00:25:14.880 --> 00:25:20.000 But the key takeaway is, is that all of this seeming complexity, 00:25:20.000 --> 00:25:22.340 if you kind of zoom in and focus conceptually 00:25:22.340 --> 00:25:25.430 on just individual aspects of this game or this computer program, 00:25:25.430 --> 00:25:31.010 it can be reduced to just conditions and loops and variables 00:25:31.010 --> 00:25:32.412 and some basic building blocks. 00:25:32.412 --> 00:25:35.120 And I daresay, if you think about the software you, yourself, use 00:25:35.120 --> 00:25:39.200 every day on your phone, on your laptop or your desktop, odds are, 00:25:39.200 --> 00:25:42.380 even if you might have no idea how a browser is implemented 00:25:42.380 --> 00:25:45.650 or no idea how Excel or Word or Keynote or Numbers 00:25:45.650 --> 00:25:48.110 or any number of other programs are written, 00:25:48.110 --> 00:25:53.210 if you really focus on just small pieces of that software, individual features, 00:25:53.210 --> 00:25:59.200 could you probably start to glean exactly how the software developers who 00:25:59.200 --> 00:26:02.929 made the software actually implemented those features? 00:26:02.929 --> 00:26:05.470 So let's now break down Scratch into some of its fundamentals 00:26:05.470 --> 00:26:08.830 and start applying some of these very ideas. 00:26:08.830 --> 00:26:12.820 Here is perhaps the simplest of programs that you might 00:26:12.820 --> 00:26:15.130 implement with Scratch as a language. 00:26:15.130 --> 00:26:19.330 Surprise, surprise, this is how, in Scratch, you would say, hello world. 00:26:19.330 --> 00:26:23.380 Now, in the world of Scratch, you have a graphical environment 00:26:23.380 --> 00:26:26.851 where there is, as we'll soon see, a cat that actually performs all the action. 00:26:26.851 --> 00:26:30.100 So it's much like I, the human who was performing these actions with the phone 00:26:30.100 --> 00:26:33.070 book just a bit ago, in the world of Scratch, by default, 00:26:33.070 --> 00:26:38.115 is a cat going to perform any of the commands that you provide in software. 00:26:38.115 --> 00:26:39.740 We can change the cat to anything else. 00:26:39.740 --> 00:26:43.250 Indeed, I turned him into Oscar in that example. 00:26:43.250 --> 00:26:48.190 But this is what the commands themselves are going to look like. 00:26:48.190 --> 00:26:51.880 And Scratch, as we'll see, has a whole bunch of fundamentals. 00:26:51.880 --> 00:26:55.834 For instance, Scratch is going to have functions or actions or verbs that 00:26:55.834 --> 00:26:57.250 look a little something like this. 00:26:57.250 --> 00:27:00.250 This is a puzzle piece, purple in fact, that's 00:27:00.250 --> 00:27:03.620 going to specify that Scratch should say hello world. 00:27:03.620 --> 00:27:06.340 Here's another puzzle piece that, as you might guess, 00:27:06.340 --> 00:27:11.440 is going to tell Scratch to say hello world forever, again and again 00:27:11.440 --> 00:27:13.240 and again, in some kind of cycle. 00:27:13.240 --> 00:27:15.250 Meanwhile in Scratch, this is how you're going 00:27:15.250 --> 00:27:20.110 to repeat something a fixed or finite number of times, 50 in this case. 00:27:20.110 --> 00:27:22.540 This in Scratch, though a much bigger block, 00:27:22.540 --> 00:27:25.780 is how we're going to specify things conditionally. 00:27:25.780 --> 00:27:28.570 If x is less than y, then do the following. 00:27:28.570 --> 00:27:32.120 Else, if x is greater than y, do the following instead. 00:27:32.120 --> 00:27:38.660 Else, third fork in the road, do this, some other thing, altogether. 00:27:38.660 --> 00:27:40.750 And so indeed, you can really see in this example 00:27:40.750 --> 00:27:45.700 that Scratch, being graphical in nature, actually has you programming. 00:27:45.700 --> 00:27:48.250 Not by typing words alone on the keyboard 00:27:48.250 --> 00:27:52.450 and numbering your lines, as I did earlier with the phone book algorithm. 00:27:52.450 --> 00:27:58.120 But rather, putting together puzzle pieces whose shape kind of 00:27:58.120 --> 00:27:59.470 implies what they do. 00:27:59.470 --> 00:28:02.160 The fact that this if block kind of looks like this. 00:28:02.160 --> 00:28:04.600 And inside of that block you can put other blocks, 00:28:04.600 --> 00:28:08.350 apparently, implies that you should only say that x is less than y 00:28:08.350 --> 00:28:11.380 if it's inside of this if else block. 00:28:11.380 --> 00:28:14.290 And now, notice we have apparently crammed another if else block 00:28:14.290 --> 00:28:18.010 inside of this to effectively create a threw-way fork in the road. 00:28:18.010 --> 00:28:21.190 And in green here, do we see some Boolean expressions? 00:28:21.190 --> 00:28:25.560 Asking the question is x less than y or greater than y, in these two lines 00:28:25.560 --> 00:28:26.060 here. 00:28:26.060 --> 00:28:28.960 And then in orange we have variables, x and y themselves, 00:28:28.960 --> 00:28:30.790 which are probably just numbers. 00:28:30.790 --> 00:28:34.570 So how do we go about assembling all of these building blocks 00:28:34.570 --> 00:28:37.436 into actual programs written in this language called Scratch? 00:28:37.436 --> 00:28:39.810 Well, let's go ahead and run the Scratch software itself. 00:28:39.810 --> 00:28:42.035 Scratch is not only a programming language. 00:28:42.035 --> 00:28:43.785 It's also a whole programming environment. 00:28:43.785 --> 00:28:46.180 It's a piece of software unto itself that's 00:28:46.180 --> 00:28:50.440 going to allow us, by dragging and dropping these puzzle pieces, 00:28:50.440 --> 00:28:51.640 to program the computer. 00:28:51.640 --> 00:28:54.280 Well, let's take a look at the User Interface, or UI. 00:28:54.280 --> 00:28:56.860 At the top left here is Scratch himself. 00:28:56.860 --> 00:29:00.490 He lives in this two-dimensional world that has a height and has a width 00:29:00.490 --> 00:29:02.950 and currently, by default, has just this blank backdrop. 00:29:02.950 --> 00:29:05.650 And he can move up, down, left, right, and do many more things 00:29:05.650 --> 00:29:07.060 in that environment as well. 00:29:07.060 --> 00:29:08.710 What kinds of things can he do? 00:29:08.710 --> 00:29:10.780 Well, in the middle of this user interface 00:29:10.780 --> 00:29:14.590 do you see a whole bunch of palettes or categories, these being the scripts? 00:29:14.590 --> 00:29:17.680 And these blue blocks here all fall under the header of motion. 00:29:17.680 --> 00:29:19.720 So these are all puzzle pieces or blocks that 00:29:19.720 --> 00:29:21.641 somehow relate to Scratch's motion. 00:29:21.641 --> 00:29:24.640 Meanwhile, if we click through Costumes, we could change the aesthetics. 00:29:24.640 --> 00:29:27.790 And in fact, we could turn Scratch into a dog or to a grouch. 00:29:27.790 --> 00:29:32.200 And under the sounds tab, could we actually explore sounds and multimedia 00:29:32.200 --> 00:29:34.030 that we can integrate into Scratch? 00:29:34.030 --> 00:29:38.800 But the blank slate with which we begin is this big area over here 00:29:38.800 --> 00:29:42.550 on the right, to which I can drag and drop these puzzle pieces 00:29:42.550 --> 00:29:47.950 and actually interconnect them in order to instruct Scratch to do things. 00:29:47.950 --> 00:29:49.450 For instance, let's try. 00:29:49.450 --> 00:29:51.580 Now, I just know, from having used Scratch before, 00:29:51.580 --> 00:29:52.890 where some of these blocks are. 00:29:52.890 --> 00:29:55.600 And the first one I'm going to grab is under Events. 00:29:55.600 --> 00:29:58.870 And I'm going to click and drag and grab this one here, which 00:29:58.870 --> 00:30:01.009 is titled when green flag clicked. 00:30:01.009 --> 00:30:02.800 Because if you didn't notice before, notice 00:30:02.800 --> 00:30:05.980 now that above Scratch's two-dimensional world here 00:30:05.980 --> 00:30:08.950 is not only this blank background, but also this green flag 00:30:08.950 --> 00:30:12.850 and green, signifying go, and this red stop sign, signifying stop. 00:30:12.850 --> 00:30:16.600 And it's by clicking either of those buttons that I can start or stop. 00:30:16.600 --> 00:30:19.300 Respectively, any program here that I write. 00:30:19.300 --> 00:30:21.060 Very similar in spirit to double-clicking 00:30:21.060 --> 00:30:24.070 an icon on your desktop, or typing a command 00:30:24.070 --> 00:30:27.010 in a prompt on a typical computer. 00:30:27.010 --> 00:30:30.310 So meanwhile, I have now this when green flag clicked block. 00:30:30.310 --> 00:30:32.770 And I want Scratch to say something. 00:30:32.770 --> 00:30:36.130 Well, I know from having used Scratched before that the Say block happens 00:30:36.130 --> 00:30:39.710 to be under Looks, where a whole bunch of puzzle pieces are as well. 00:30:39.710 --> 00:30:42.850 And notice what I can do now is click on say hello, 00:30:42.850 --> 00:30:44.830 where hello is just the default word. 00:30:44.830 --> 00:30:49.240 And then notice here, if I drag, notice how it turns white and kind of glows 00:30:49.240 --> 00:30:52.060 when I move the block close enough, as though they're magnetized? 00:30:52.060 --> 00:30:55.780 Well, that's because these two blocks want to logically snap together. 00:30:55.780 --> 00:31:00.459 So as soon as I release my mouse button, will they lock together as such? 00:31:00.459 --> 00:31:02.500 And now I can double-click on the text box there, 00:31:02.500 --> 00:31:06.810 and type hello comma world, and actually type and any number of words 00:31:06.810 --> 00:31:08.640 that I might want Scratch to say. 00:31:08.640 --> 00:31:13.810 Well, let's zoom out, move my cursor over to the green flag and click. 00:31:13.810 --> 00:31:16.710 And voila, the first of my programs. 00:31:16.710 --> 00:31:18.390 It's doing exactly what I said. 00:31:18.390 --> 00:31:22.359 And if you notice, it's not stopping to say hello world anymore, 00:31:22.359 --> 00:31:24.150 because I never actually had a command that 00:31:24.150 --> 00:31:26.760 said hey, Scratch, stop saying hello. 00:31:26.760 --> 00:31:29.820 And so I'm just going to manually stop this program for now, 00:31:29.820 --> 00:31:31.825 with the red stop sign up there. 00:31:31.825 --> 00:31:33.450 All right, so that's all fine and good. 00:31:33.450 --> 00:31:35.820 But it's not all that realistic. 00:31:35.820 --> 00:31:38.490 Cats do not have speech bubbles coming out of their mouths. 00:31:38.490 --> 00:31:42.840 So let's make it a little more realistic by moving up here to my blocks, 00:31:42.840 --> 00:31:45.112 clicking and dragging and pulling away the Say block. 00:31:45.112 --> 00:31:47.070 And I can just let it go anywhere in the middle 00:31:47.070 --> 00:31:49.050 and it will just disappear or be deleted. 00:31:49.050 --> 00:31:53.040 And now this time let me go up to Sound. 00:31:53.040 --> 00:31:54.420 And oh, this is promising. 00:31:54.420 --> 00:31:57.064 Play sound meow is the purple block here. 00:31:57.064 --> 00:31:58.230 Let me go ahead and do this. 00:31:58.230 --> 00:32:00.300 And notice it too has a very similar shape, 00:32:00.300 --> 00:32:01.740 even though it's a little longer. 00:32:01.740 --> 00:32:04.724 And it wants to interconnect as soon as I let go. 00:32:04.724 --> 00:32:07.140 Let me go ahead and zoom out and click now the green flag. 00:32:07.140 --> 00:32:09.120 [MEOW] 00:32:09.620 --> 00:32:10.612 Aw. 00:32:10.612 --> 00:32:11.610 [MEOW] 00:32:11.610 --> 00:32:13.112 Aw. 00:32:13.112 --> 00:32:14.540 [MEOW] 00:32:14.540 --> 00:32:15.900 Aw. 00:32:15.900 --> 00:32:18.010 So Scratch has just meowed three separate times 00:32:18.010 --> 00:32:22.510 because I ran the program three times by clicking on the green flag three times. 00:32:22.510 --> 00:32:23.830 Now, that's a little tedious. 00:32:23.830 --> 00:32:26.020 And it's also not very lifelike if every time I 00:32:26.020 --> 00:32:29.470 want to hear Scratch I have to breathe new life into him by clicking 00:32:29.470 --> 00:32:30.790 the green flag. 00:32:30.790 --> 00:32:33.160 Wouldn't this be an opportunity to leverage one 00:32:33.160 --> 00:32:37.570 of those principles we looked at already, that of a loop or a cycle, 00:32:37.570 --> 00:32:39.880 if I want Scratch to do something again and again? 00:32:39.880 --> 00:32:43.540 So for instance, let me go ahead and poke around under the control box, 00:32:43.540 --> 00:32:44.330 perhaps. 00:32:44.330 --> 00:32:46.120 And oh, there's that repeat block. 00:32:46.120 --> 00:32:49.130 It defaults to 10, but we can change that to anything I want. 00:32:49.130 --> 00:32:53.950 And if I click and drag here, now, this isn't quite right logically. 00:32:53.950 --> 00:32:56.560 Because I don't want to play the sound meow 00:32:56.560 --> 00:32:59.410 when the green flag is clicked and then repeat something 10 times. 00:32:59.410 --> 00:33:00.118 So you know what? 00:33:00.118 --> 00:33:02.950 I'm going to just leave this in no-man's land for just a moment. 00:33:02.950 --> 00:33:06.970 I'm going to click and drag this purple block away so it becomes detached. 00:33:06.970 --> 00:33:09.910 And now, notice, you might think that it definitely 00:33:09.910 --> 00:33:12.580 is not going to fit inside this repeat block that's just not 00:33:12.580 --> 00:33:13.730 too tall of a space. 00:33:13.730 --> 00:33:16.000 But notice that the whiteness is, in fact, 00:33:16.000 --> 00:33:18.250 illuminated, kind of saying yes, this is OK. 00:33:18.250 --> 00:33:20.290 And the shapes do kind of match. 00:33:20.290 --> 00:33:22.510 And so what's nice about Scratch is that so long 00:33:22.510 --> 00:33:26.110 as the shapes are in accordance, can you let go? 00:33:26.110 --> 00:33:28.970 And the containing block will grow to fill that particular shape. 00:33:28.970 --> 00:33:31.660 And even though it looks like we're now completely out of room 00:33:31.660 --> 00:33:33.400 within that Repeat block, we can actually 00:33:33.400 --> 00:33:37.600 drag and squeeze more and more blocks there, and the block will grow to fill. 00:33:37.600 --> 00:33:40.020 So let's just do this three times. 00:33:40.020 --> 00:33:44.230 And now let's go ahead and connect this to when the green flag clicked. 00:33:44.230 --> 00:33:46.960 Zoom out, click the green flag, and-- 00:33:46.960 --> 00:33:48.442 [MEOW] 00:33:52.400 --> 00:33:53.780 Huh. 00:33:53.780 --> 00:33:57.440 Now, I am pretty sure logically this says when the green flag is clicked, 00:33:57.440 --> 00:33:59.420 repeat the following three times. 00:33:59.420 --> 00:34:01.190 Play sound, meow. 00:34:01.190 --> 00:34:07.280 And yet, unless I'm not hearing things right, I only heard one meow. 00:34:07.280 --> 00:34:08.480 So what's wrong? 00:34:08.480 --> 00:34:10.010 Is this a bug? 00:34:10.010 --> 00:34:11.270 A mistake in Scratch? 00:34:11.270 --> 00:34:13.130 Did MIT screw up? 00:34:13.130 --> 00:34:15.949 Or is this a bug in my code somehow? 00:34:15.949 --> 00:34:19.270 Did David screw up? 00:34:19.270 --> 00:34:21.810 Well, let's consider the options. 00:34:21.810 --> 00:34:26.719 Let me go back to that sound category, and see if there's maybe 00:34:26.719 --> 00:34:29.179 a different block I should be using. 00:34:29.179 --> 00:34:31.370 Now, I'm only just now reasoning through this. 00:34:31.370 --> 00:34:31.870 Right? 00:34:31.870 --> 00:34:34.670 Because I've written a program that I think should do one thing. 00:34:34.670 --> 00:34:37.620 I have realized it did not do what I intended. 00:34:37.620 --> 00:34:38.496 So is it my fault? 00:34:38.496 --> 00:34:39.620 Is it the software's fault? 00:34:39.620 --> 00:34:43.870 And maybe am I misunderstanding the functionality of some of these blocks? 00:34:43.870 --> 00:34:46.940 Now, what do you notice here? 00:34:46.940 --> 00:34:50.170 Scratch supports drumming, apparently, and other notes. 00:34:50.170 --> 00:34:53.239 But up above, it can play sound meow. 00:34:53.239 --> 00:34:59.420 Or more precisely, it can play sound meow until done. 00:34:59.420 --> 00:35:01.190 What happens if I use that one instead? 00:35:01.190 --> 00:35:01.910 Let me try. 00:35:01.910 --> 00:35:06.290 Let me go ahead and drag this one away to effectively delete it. 00:35:06.290 --> 00:35:10.572 And then drag this one in its place so it too still fits. 00:35:10.572 --> 00:35:12.530 Now, let me go ahead and click that green flag. 00:35:12.530 --> 00:35:15.860 [THREE MEOWS] 00:35:15.860 --> 00:35:16.700 Interesting. 00:35:16.700 --> 00:35:18.470 That time I did hear it all three times. 00:35:18.470 --> 00:35:19.310 Let's just be sure. 00:35:19.310 --> 00:35:22.410 [THREE MEOWS] 00:35:22.410 --> 00:35:23.420 OK. 00:35:23.420 --> 00:35:25.850 I definitely heard it all three times that time. 00:35:25.850 --> 00:35:26.870 But why? 00:35:26.870 --> 00:35:30.350 Well, with the previous version, which I'll restore here 00:35:30.350 --> 00:35:35.630 by going back to the way it is, notice that the program was actually 00:35:35.630 --> 00:35:38.090 performing correctly. 00:35:38.090 --> 00:35:40.610 Correctly, so far as the definition of these blocks go. 00:35:40.610 --> 00:35:44.510 Just incorrectly, with respect to my own expectations. 00:35:44.510 --> 00:35:46.500 Computers, recall, are really fast. 00:35:46.500 --> 00:35:47.000 Right? 00:35:47.000 --> 00:35:50.310 This Mac here might be a gigahertz or two gigahertz fast, 00:35:50.310 --> 00:35:53.630 which means it can do one or two billion things per second. 00:35:53.630 --> 00:35:54.590 And that's a lot. 00:35:54.590 --> 00:35:58.440 I mean, I am, by far, the limiting factor as the human in this equation. 00:35:58.440 --> 00:36:01.400 So maybe, just maybe, Scratch was literally 00:36:01.400 --> 00:36:05.180 doing what I told it to do which was when the green flag is clicked 00:36:05.180 --> 00:36:06.950 repeat the following three times 00:36:06.950 --> 00:36:09.740 But Scratch, being a computer program, is just so fast. 00:36:09.740 --> 00:36:13.010 It played the song or the sound three times. 00:36:13.010 --> 00:36:16.250 But it played it so fast within this Repeat block 00:36:16.250 --> 00:36:19.940 that all three of those meows were effectively overlapping. 00:36:19.940 --> 00:36:22.730 So we only heard one of them because all three 00:36:22.730 --> 00:36:25.070 were being played pretty much at the same time, 00:36:25.070 --> 00:36:27.470 because that loop was going around so quickly. 00:36:27.470 --> 00:36:32.570 But by contrast, with this other puzzle piece, play sound meow until done, 00:36:32.570 --> 00:36:36.200 now I'm compelling the program to just give me 00:36:36.200 --> 00:36:39.050 a moment to listen to the entirety of that sound 00:36:39.050 --> 00:36:42.650 before you proceed to the next iteration, or the next cycle. 00:36:42.650 --> 00:36:45.440 And then one more pause until the next cycle, 00:36:45.440 --> 00:36:49.820 so that I can hear it three and exactly three times. 00:36:49.820 --> 00:36:53.100 So such a simple program and such a simple example. 00:36:53.100 --> 00:36:55.610 And odds are, whenever you're writing code 00:36:55.610 --> 00:36:58.820 and it doesn't behave as you intend and you look at it and you think, 00:36:58.820 --> 00:37:00.693 no, that is right. 00:37:00.693 --> 00:37:03.570 Eh, that's not necessarily the case. 00:37:03.570 --> 00:37:06.680 And indeed, bugs, or mistakes in one's code, 00:37:06.680 --> 00:37:10.880 are going to be perhaps one of the most common and one 00:37:10.880 --> 00:37:15.816 of the most frustrating experiences one has when first learning to program. 00:37:15.816 --> 00:37:18.440 But these are the kinds of things that, with practice, go away. 00:37:18.440 --> 00:37:21.920 And so like I just did, you reach instinctively, here on out, 00:37:21.920 --> 00:37:25.760 for the right block, as opposed to the incorrect one. 00:37:25.760 --> 00:37:28.830 Now let's see what else we can do here when it comes to sound. 00:37:28.830 --> 00:37:32.970 What about not just playing a sound, but adding a bit of, 00:37:32.970 --> 00:37:34.700 shall we say, animation here? 00:37:34.700 --> 00:37:37.190 Well how might it go about making this cat moo? 00:37:37.190 --> 00:37:40.672 Well, let me go ahead under Control and do something forever. 00:37:40.672 --> 00:37:42.380 Because I just want this cat, ultimately, 00:37:42.380 --> 00:37:44.887 to move back and forth, back and forth forever. 00:37:44.887 --> 00:37:46.220 That of course, involves motion. 00:37:46.220 --> 00:37:47.434 So let me go to motion. 00:37:47.434 --> 00:37:49.100 And all right, I like the sound of this. 00:37:49.100 --> 00:37:51.200 Move 10 steps. 00:37:51.200 --> 00:37:55.280 And 10 steps is probably going to be 10 dots or 10 pixels on the screen. 00:37:55.280 --> 00:37:57.420 Well, then what do I want him to do? 00:37:57.420 --> 00:38:01.740 Well, let's see what happens first if I click the green flag. 00:38:01.740 --> 00:38:02.660 OK. 00:38:02.660 --> 00:38:04.490 So not quite what I intended. 00:38:04.490 --> 00:38:06.650 Because he's not really moving anymore. 00:38:06.650 --> 00:38:11.720 And this is actually a feature, being designed for aspiring programmers 00:38:11.720 --> 00:38:13.400 and not to mention, children. 00:38:13.400 --> 00:38:17.240 Scratch realized, or rather, MIT realized, that in designing Scratch, 00:38:17.240 --> 00:38:20.030 probably shouldn't let the cat go 100% off the screen. 00:38:20.030 --> 00:38:23.530 So we can just click and drag him back into view here if we want. 00:38:23.530 --> 00:38:25.740 But of course, program's still running. 00:38:25.740 --> 00:38:28.370 So he's just going to run away from us anyway. 00:38:28.370 --> 00:38:29.460 So how do we fix this? 00:38:29.460 --> 00:38:32.210 How do we make him go back and forth and back and forth without me 00:38:32.210 --> 00:38:33.570 having to manually intervene? 00:38:33.570 --> 00:38:35.900 Well, what about some kind of condition? 00:38:35.900 --> 00:38:39.260 Let me poke around the Control blocks. 00:38:39.260 --> 00:38:42.200 And let me actually grab one of these conditions. 00:38:42.200 --> 00:38:46.970 And instead of moving 10 steps forever again and again, 00:38:46.970 --> 00:38:50.630 after making 10 steps let me ask a question. 00:38:50.630 --> 00:38:52.220 Am I touching the edge? 00:38:52.220 --> 00:38:54.350 Because if so I don't want to keep going. 00:38:54.350 --> 00:38:58.100 So to ask that question, let me go under sensing here. 00:38:58.100 --> 00:38:59.510 And indeed, let's see. 00:38:59.510 --> 00:39:02.210 This one here, Touching, sounds about right. 00:39:02.210 --> 00:39:04.100 But I don't want this to have anything to do 00:39:04.100 --> 00:39:07.280 with the mouse pointer or the cursor, but rather, with the edge. 00:39:07.280 --> 00:39:11.440 So by this drop-down, let me change this Boolean expression to Touching Edge. 00:39:11.440 --> 00:39:12.650 Drag and drop this over here. 00:39:12.650 --> 00:39:16.610 And this certainly doesn't seem to fit size-wise, but shape-wise, it does. 00:39:16.610 --> 00:39:20.750 So if I let go, the block, as expected, grows to fill that particular Boolean 00:39:20.750 --> 00:39:21.500 expression. 00:39:21.500 --> 00:39:23.390 And now, what do I want to do? 00:39:23.390 --> 00:39:26.660 Well, now, maybe if I'm touching the edge, 00:39:26.660 --> 00:39:28.710 I should turn some number of degrees. 00:39:28.710 --> 00:39:29.960 15 doesn't feel quite right. 00:39:29.960 --> 00:39:31.430 I want to turn all the way around. 00:39:31.430 --> 00:39:35.810 And I know that to be 180 out of 360 total possible degrees. 00:39:35.810 --> 00:39:39.800 And now let's see what happens when I click the green flag. 00:39:39.800 --> 00:39:40.850 OK. 00:39:40.850 --> 00:39:41.545 So better. 00:39:41.545 --> 00:39:43.290 He's not just running off the screen. 00:39:43.290 --> 00:39:45.890 And now, I don't have to click and drag him back into view. 00:39:45.890 --> 00:39:51.227 But of course, turning 180 degrees isn't quite the visual I want. 00:39:51.227 --> 00:39:53.810 We don't really intend for the cat to be flipping upside down. 00:39:53.810 --> 00:39:55.890 So maybe there's a better way. 00:39:55.890 --> 00:40:00.177 This, definitely a bug aesthetically, but perhaps not functionally. 00:40:00.177 --> 00:40:01.760 But there's still an opportunity here. 00:40:01.760 --> 00:40:03.600 Let's leave that bug alone. 00:40:03.600 --> 00:40:06.360 Let's pause the program with the red stop sign here. 00:40:06.360 --> 00:40:08.330 Let me go into sounds, just for fun. 00:40:08.330 --> 00:40:11.180 It's hard to justify this academically here. 00:40:11.180 --> 00:40:13.770 But let me go into this sound bar, where you can hear-- 00:40:13.770 --> 00:40:14.270 [MEOW] 00:40:14.270 --> 00:40:18.080 --not just the default meow, But you can also click on the microphone. 00:40:18.080 --> 00:40:22.460 And let me go ahead and give my best example of cat 00:40:22.460 --> 00:40:25.740 running into the side of a screen. 00:40:25.740 --> 00:40:27.480 Ouch! 00:40:27.480 --> 00:40:28.470 And stop that. 00:40:28.470 --> 00:40:31.050 You can now see a waveform, or a graphical depiction 00:40:31.050 --> 00:40:33.090 of what my voice apparently sounds like. 00:40:33.090 --> 00:40:38.250 Let me go ahead and click and Delete the moments of silence beforehand and also 00:40:38.250 --> 00:40:39.330 a little after. 00:40:39.330 --> 00:40:42.179 And interestingly, it seems like I've really mustered some energy 00:40:42.179 --> 00:40:42.970 and then said ouch. 00:40:42.970 --> 00:40:45.400 But we'll leave the rest of the wave together. 00:40:45.400 --> 00:40:47.640 I'm going to go ahead and name this recording ouch. 00:40:47.640 --> 00:40:52.170 And now notice if I go back to Scripts, and I go to Sound, 00:40:52.170 --> 00:40:55.050 now ouch is available from the drop-down as one of my Sounds. 00:40:55.050 --> 00:40:59.010 And let me go ahead and put that not outside the condition, but inside. 00:40:59.010 --> 00:41:01.350 So that now, if I'm touching the edge, not only 00:41:01.350 --> 00:41:06.090 do I want to wrap around 180 degrees, I also want to play this sound. 00:41:06.090 --> 00:41:09.240 Let's see what my newest program now sounds like. 00:41:09.240 --> 00:41:10.510 Ouch! 00:41:10.510 --> 00:41:11.070 Ouch! 00:41:11.070 --> 00:41:11.810 OK. 00:41:11.810 --> 00:41:12.364 Ouch! 00:41:12.364 --> 00:41:13.530 OK, maybe not very cat-like. 00:41:13.530 --> 00:41:15.020 [OUCHING CONTINUES] 00:41:15.020 --> 00:41:18.810 A bit more like human hitting the edge of a screen, 00:41:18.810 --> 00:41:23.460 but at least correct, that 180-degree detail aside. 00:41:23.460 --> 00:41:26.460 So notice what's guided my building of this program. 00:41:26.460 --> 00:41:29.220 I didn't set out and drag all the possible puzzle pieces at once. 00:41:29.220 --> 00:41:31.320 I didn't record the sound in advance all at once. 00:41:31.320 --> 00:41:34.549 I instead took the proverbial baby steps, very small, 00:41:34.549 --> 00:41:36.840 incremental steps, starting with something very simple, 00:41:36.840 --> 00:41:38.970 just a forever loop, and just have the cat move 00:41:38.970 --> 00:41:40.604 all the way to the side of the screen. 00:41:40.604 --> 00:41:43.020 Then did I add to that the feature where he would actually 00:41:43.020 --> 00:41:46.500 wrap around conditionally, in the form of that If block. 00:41:46.500 --> 00:41:50.370 And then did I go in and add the third feature, which was this 00:41:50.370 --> 00:41:53.850 a silly human ouch-like sound. 00:41:53.850 --> 00:41:57.085 But the key takeaway there is that it's much easier, 00:41:57.085 --> 00:42:01.200 I'd argue, to write complex programs if you start off by breaking them down 00:42:01.200 --> 00:42:05.520 into those component parts and consider individual milestones for yourself, 00:42:05.520 --> 00:42:08.580 so how you might set out implementing just one feature at a time, 00:42:08.580 --> 00:42:09.780 as opposed to everything. 00:42:09.780 --> 00:42:12.738 And indeed, if you consider real-world software that use on your phones 00:42:12.738 --> 00:42:16.020 or your computers, certainly did Microsoft not set out to build, 00:42:16.020 --> 00:42:19.230 in one day, an entire word processing program 00:42:19.230 --> 00:42:22.320 called Word or an entire spreadsheet program called Excel. 00:42:22.320 --> 00:42:24.630 Odds are, their software developers back in the day, 00:42:24.630 --> 00:42:29.799 the very first version of the software, did they write just one small feature? 00:42:29.799 --> 00:42:31.590 Maybe you double-click the icon and it just 00:42:31.590 --> 00:42:34.092 opens a big rectangular window that does nothing, 00:42:34.092 --> 00:42:36.300 but at least they had the beginnings of the software. 00:42:36.300 --> 00:42:39.840 Then maybe they added support for just a column, maybe two columns, 00:42:39.840 --> 00:42:42.410 maybe any number of columns in any number of rows. 00:42:42.410 --> 00:42:45.150 And so there too, might Microsoft, among other companies, 00:42:45.150 --> 00:42:48.420 have set milestones for themselves such that the end result is 00:42:48.420 --> 00:42:50.560 impressive and amazingly complex. 00:42:50.560 --> 00:42:55.110 Maybe even millions of lines of code in C or C++ or Java, 00:42:55.110 --> 00:42:56.460 or some other language. 00:42:56.460 --> 00:42:59.637 But they didn't start out by trying to tackle that all at once. 00:42:59.637 --> 00:43:01.470 Just like I, when starting out this program, 00:43:01.470 --> 00:43:05.170 did not set out to implement it all at once. 00:43:05.170 --> 00:43:07.740 Now, with that said, let's open a couple of examples 00:43:07.740 --> 00:43:11.790 that have already been written and see what they do themselves. 00:43:11.790 --> 00:43:14.220 This, then, is Pet the Cat, a program I wrote 00:43:14.220 --> 00:43:16.830 in advance that still stars Scratch and already has 00:43:16.830 --> 00:43:19.290 these puzzle pieces assembled. 00:43:19.290 --> 00:43:20.340 So what does this do? 00:43:20.340 --> 00:43:22.090 Well, lets go about reading code, which is 00:43:22.090 --> 00:43:23.941 another side of software development. 00:43:23.941 --> 00:43:26.190 Most software programs, certainly the largest of them, 00:43:26.190 --> 00:43:28.890 are not written by single humans, but rather 00:43:28.890 --> 00:43:31.042 by teams of humans who are collaborating. 00:43:31.042 --> 00:43:33.750 Indeed, even though you might have the mindset or the expectation 00:43:33.750 --> 00:43:37.050 that programming is largely a solitary exercise done late 00:43:37.050 --> 00:43:41.850 at night with lots of caffeine and darkness and a flickering computer 00:43:41.850 --> 00:43:46.200 screen, the reality is it's an incredibly collaborative profession. 00:43:46.200 --> 00:43:51.930 It's an incredibly collaborative hobby, for some, whereby you work either 00:43:51.930 --> 00:43:54.657 alongside a whole team of individuals or remotely 00:43:54.657 --> 00:43:57.240 with a whole team of individuals collaborating at whiteboards, 00:43:57.240 --> 00:44:00.900 collaborating virtually, and ultimately reading each other's code. 00:44:00.900 --> 00:44:03.120 Not only to understand what someone else has written, 00:44:03.120 --> 00:44:05.910 but to enhance what he or she has done by building 00:44:05.910 --> 00:44:09.577 atop their own lives of code or in this case their own puzzle pieces. 00:44:09.577 --> 00:44:10.660 So let's try exactly that. 00:44:10.660 --> 00:44:13.312 And let's read this code that I've already written. 00:44:13.312 --> 00:44:14.520 When the green klag flicked-- 00:44:14.520 --> 00:44:15.780 [LAUGHS] 00:44:15.780 --> 00:44:19.050 When green flag clicked, forever do the following. 00:44:19.050 --> 00:44:22.500 If touching the mouse pointer, then play sound meow, 00:44:22.500 --> 00:44:27.880 and then wait for two seconds if touching mouse pointer, then play 00:44:27.880 --> 00:44:29.870 sound meow, then wait for two seconds. 00:44:29.870 --> 00:44:31.790 So what's this program going to do? 00:44:31.790 --> 00:44:34.050 Well, its title kind of gives it away. 00:44:34.050 --> 00:44:38.442 But if I click the green flag, nothing seems to be happening. 00:44:38.442 --> 00:44:40.400 But notice that all these puzzle pieces are now 00:44:40.400 --> 00:44:44.480 highlighted in yellow, to indicate that the program is indeed running. 00:44:44.480 --> 00:44:47.240 But nothing's happening until-- 00:44:47.240 --> 00:44:50.661 let me move my mouse pointer over the cat. 00:44:50.661 --> 00:44:51.623 [MEOW] 00:44:51.623 --> 00:44:53.070 Ah. 00:44:53.070 --> 00:44:55.310 Let's try that again. 00:44:55.310 --> 00:44:56.180 Interesting. 00:44:56.180 --> 00:44:59.230 So this time, rather than meowing forever, 00:44:59.230 --> 00:45:02.780 Scratch is only meowing when my mouse pointer is touching him, just 00:45:02.780 --> 00:45:03.401 like this. 00:45:03.401 --> 00:45:05.756 [TWO MEOWS] 00:45:05.756 --> 00:45:09.360 So this is kind of like the digital equivalent of petting the cat. 00:45:09.360 --> 00:45:11.060 Well, what about the opposite? 00:45:11.060 --> 00:45:15.740 What might it be like to write a program where you don't want to pet the cat? 00:45:15.740 --> 00:45:21.420 Well, for that example let's open Don't Pet the Cat, which now looks like this. 00:45:21.420 --> 00:45:23.480 So this program's a little more complicated 00:45:23.480 --> 00:45:25.970 in that it has not just an If block, which asks one 00:45:25.970 --> 00:45:29.300 question, but an If Else block, which asks a question 00:45:29.300 --> 00:45:32.900 and then behaves one way if it's true and another way if it's false. 00:45:32.900 --> 00:45:35.660 In this case, we forever if touching the mouse pointer, 00:45:35.660 --> 00:45:38.600 then play sound lion five-- 00:45:38.600 --> 00:45:40.730 so like the fifth of our lion sounds-- 00:45:40.730 --> 00:45:41.420 until done. 00:45:41.420 --> 00:45:45.620 Otherwise play sound meow, and then wait for two seconds 00:45:45.620 --> 00:45:48.050 to give the human a chance to sort of orient 00:45:48.050 --> 00:45:50.750 himself or herself and pet the cat again. 00:45:50.750 --> 00:45:52.280 So let's see what happens here. 00:45:52.280 --> 00:45:57.205 But consider that I've induced, this time, an infinite loop forever. 00:45:57.205 --> 00:46:00.276 [MEOWING] 00:46:02.500 --> 00:46:03.250 Every two seconds. 00:46:06.270 --> 00:46:08.200 What about not touching the cat? 00:46:08.200 --> 00:46:10.630 What was the imperative there? 00:46:10.630 --> 00:46:16.470 Let's try moving the cursor closer, closer. 00:46:16.470 --> 00:46:17.930 Closer. 00:46:17.930 --> 00:46:19.830 [LION ROARS] 00:46:19.830 --> 00:46:22.050 That is why you don't want to pet the cat. 00:46:22.050 --> 00:46:24.660 So in that case, did the lion sound only play 00:46:24.660 --> 00:46:28.020 when the cat was actually touched with the mouse pointer? 00:46:28.020 --> 00:46:29.280 Well, what about variables? 00:46:29.280 --> 00:46:31.350 What about programs in which you actually 00:46:31.350 --> 00:46:34.920 want to count something, as in the case of the sheep? 00:46:34.920 --> 00:46:38.760 Of course, in the human world it's often we humans who are doing the counting. 00:46:38.760 --> 00:46:42.480 But let me propose that it's sheep who deserve a chance to count themselves 00:46:42.480 --> 00:46:43.050 sometimes. 00:46:43.050 --> 00:46:47.160 And so here we have counting sheep. 00:46:47.160 --> 00:46:50.730 And indeed, this is literally a sheep counting. 00:46:50.730 --> 00:46:52.140 And how is he doing that? 00:46:52.140 --> 00:46:54.970 Well, over here, since I've already clicked the green flag, 00:46:54.970 --> 00:46:57.180 do you see highlighted the program that he's running? 00:46:57.180 --> 00:47:00.030 When green flag clicked, he said counter to zero. 00:47:00.030 --> 00:47:02.160 So this orange puzzle piece is like a custom piece 00:47:02.160 --> 00:47:04.950 that I made in advance via the Data Palette. 00:47:04.950 --> 00:47:09.450 And it allows me to create a variable called anything I want, x or y or z, 00:47:09.450 --> 00:47:12.270 or frankly, more descriptively in English, counter. 00:47:12.270 --> 00:47:15.030 Because what it is, I want a variable-- 00:47:15.030 --> 00:47:16.590 something that stores a value-- 00:47:16.590 --> 00:47:17.855 that does counting. 00:47:17.855 --> 00:47:20.730 I'm going to call it counter instead of the more generic x or y or z. 00:47:20.730 --> 00:47:22.688 And then I'm forever going to do the following. 00:47:22.688 --> 00:47:25.230 I'm going to say not hello world or some other phrase, 00:47:25.230 --> 00:47:27.570 but rather, the value of the counter. 00:47:27.570 --> 00:47:31.410 So I seem to have dragged and dropped the variable's puzzle piece right 00:47:31.410 --> 00:47:33.480 on top of the Say block for one second. 00:47:33.480 --> 00:47:35.176 Then I'm going to wait for a second. 00:47:35.176 --> 00:47:37.050 Then I'm going to increment the counter by 1, 00:47:37.050 --> 00:47:40.680 so change it by 1, by adding 1 to its value, 00:47:40.680 --> 00:47:43.060 and then just keep doing this forever. 00:47:43.060 --> 00:47:47.850 And so the sheep is now going to count up and up and up 00:47:47.850 --> 00:47:50.940 and up and up, seemingly forever. 00:47:50.940 --> 00:47:55.710 If that gets a little boring, I suppose we could always remove the weight block 00:47:55.710 --> 00:47:59.620 and just have the sheep count again and again and again and again. 00:47:59.620 --> 00:48:01.800 And so now there's no blinking effect. 00:48:01.800 --> 00:48:03.790 Because the speech bubble isn't going away, 00:48:03.790 --> 00:48:06.570 but the number's indeed counting a little faster. 00:48:06.570 --> 00:48:09.630 Because we're saying it for one second at a time. 00:48:09.630 --> 00:48:12.780 But it's one thing to use puzzle pieces that MIT 00:48:12.780 --> 00:48:15.990 has created for us, these conditions, these loops, these variables. 00:48:15.990 --> 00:48:18.930 What if my programs start to get sophisticated enough 00:48:18.930 --> 00:48:22.170 and my ideas start to get grand enough that I'd really 00:48:22.170 --> 00:48:24.720 like to start creating my own puzzle pieces, 00:48:24.720 --> 00:48:28.760 puzzle pieces that maybe MIT didn't envision programmers needing? 00:48:28.760 --> 00:48:32.790 Well, turns out in most programming languages you can do exactly this. 00:48:32.790 --> 00:48:36.720 You can create your own custom functions or procedures 00:48:36.720 --> 00:48:41.540 by assembling new functionality out of other existing puzzle pieces. 00:48:41.540 --> 00:48:45.510 And so in this way, can we use existing puzzle pieces, kind of wrap them up 00:48:45.510 --> 00:48:49.590 with a new name, with a new shape, and actually then give that shape a name 00:48:49.590 --> 00:48:52.170 so that henceforth, I don't have to worry about using 00:48:52.170 --> 00:48:53.940 all of those individual puzzle pieces. 00:48:53.940 --> 00:48:57.750 I can use just one newly-named one to achieve some goal. 00:48:57.750 --> 00:49:01.140 Let's consider the following example, cough 0. 00:49:01.140 --> 00:49:04.110 This is an example, per the puzzle pieces up here, 00:49:04.110 --> 00:49:06.300 that simply instruct the cat to cough three 00:49:06.300 --> 00:49:08.130 times, waiting a second in between. 00:49:08.130 --> 00:49:09.980 Say cough for a second, then wait. 00:49:09.980 --> 00:49:11.616 Say cough for a second, then wait. 00:49:11.616 --> 00:49:12.990 Say cough for a second then wait. 00:49:12.990 --> 00:49:15.615 Of course, if I click green flag on here, you're going to see-- 00:49:15.615 --> 00:49:16.740 as you might expect-- 00:49:16.740 --> 00:49:20.620 the cat is unfortunately coughing not once, not twice, 00:49:20.620 --> 00:49:22.620 but three times in total. 00:49:22.620 --> 00:49:25.500 This is clearly an opportunity for better design. 00:49:25.500 --> 00:49:32.850 And you can see the surface of an opportunity for better programming. 00:49:32.850 --> 00:49:34.650 This is not a good design. 00:49:34.650 --> 00:49:38.400 It's a correct design in that it will say cough three times 00:49:38.400 --> 00:49:39.730 and wait a second in between. 00:49:39.730 --> 00:49:42.146 But it's not a very good design because it kind of sort of 00:49:42.146 --> 00:49:44.880 looks like I copied and pasted these puzzle pieces, much 00:49:44.880 --> 00:49:47.580 like you might do with your own processing program or email 00:49:47.580 --> 00:49:48.690 client or the like. 00:49:48.690 --> 00:49:52.122 I've just taken a couple of puzzle pieces and said, copy, paste, paste. 00:49:52.122 --> 00:49:53.830 And maybe I'd do it even more if I wanted 00:49:53.830 --> 00:49:56.130 Scratch to cough more than three times. 00:49:56.130 --> 00:49:58.200 But we can do better than that with programming. 00:49:58.200 --> 00:50:00.240 We can use loops instead. 00:50:00.240 --> 00:50:04.350 So in cough one do we have an alternative example, 00:50:04.350 --> 00:50:06.600 wherein the cat now is going to cough three times, 00:50:06.600 --> 00:50:10.480 but by way of a repeat block, a loop. 00:50:10.480 --> 00:50:12.390 Now, the code is functionally the same. 00:50:12.390 --> 00:50:15.350 But this would be arguably better designed. 00:50:15.350 --> 00:50:18.600 Because now if I want to change what the cat is saying, or when he's coughing, 00:50:18.600 --> 00:50:20.190 maybe I want to spell it differently. 00:50:20.190 --> 00:50:23.410 Or I want him to wait some number of seconds that aren't just one. 00:50:23.410 --> 00:50:25.980 Well, I can just make a change in one place 00:50:25.980 --> 00:50:28.620 rather than in like, six different puzzle pieces. 00:50:28.620 --> 00:50:32.100 I can focus on just the one instance here. 00:50:32.100 --> 00:50:36.570 But suppose that it's coughing that is the puzzle piece that I 00:50:36.570 --> 00:50:39.780 want to create and use here on out, so I don't 00:50:39.780 --> 00:50:41.760 have to remember that that is how you make 00:50:41.760 --> 00:50:44.190 a sprite, a character on the screen, cough. 00:50:44.190 --> 00:50:46.290 Because in addition to having one cat like this, 00:50:46.290 --> 00:50:49.134 we can actually have multiple cats, or a cat and a dog or a cat 00:50:49.134 --> 00:50:51.550 and a dog and a grouch, or any number of other characters. 00:50:51.550 --> 00:50:54.390 So this is to say, it would be nice, once I figure out 00:50:54.390 --> 00:50:57.120 how to do something in a programming language like Scratch, 00:50:57.120 --> 00:51:01.080 I'd really like to save that functionality in a custom function, 00:51:01.080 --> 00:51:03.960 in a puzzle piece that I have made so I can use it elsewhere. 00:51:03.960 --> 00:51:06.720 Not just for this sprite or cat, but for others. 00:51:06.720 --> 00:51:13.410 So in cough two do we now have an example that defines 00:51:13.410 --> 00:51:15.390 a new puzzle piece called cough? 00:51:15.390 --> 00:51:16.860 It's purple in nature. 00:51:16.860 --> 00:51:19.184 I grabbed it from the More Blocks Palette. 00:51:19.184 --> 00:51:21.600 And you'll notice that I defined, with this special puzzle 00:51:21.600 --> 00:51:25.080 piece, a new piece called cough. 00:51:25.080 --> 00:51:27.990 Beneath that block is a number of statements. 00:51:27.990 --> 00:51:32.010 Say cough for one second, wait for one second, and that's it. 00:51:32.010 --> 00:51:34.690 But now, above this is my new program. 00:51:34.690 --> 00:51:36.690 And this is where things get really interesting. 00:51:36.690 --> 00:51:39.360 Because now I can drag and drop when green flag clicked. 00:51:39.360 --> 00:51:42.810 I can still grab a Repeat block and do something three times. 00:51:42.810 --> 00:51:44.610 But what do I want to do three times? 00:51:44.610 --> 00:51:47.970 This time I just very simply want to cough. 00:51:47.970 --> 00:51:51.750 And so now, thanks to my Custom puzzle piece being defined here, 00:51:51.750 --> 00:51:52.950 I now have access-- 00:51:52.950 --> 00:51:57.570 under More Blocks-- to a custom puzzle piece called cough that not only says 00:51:57.570 --> 00:51:58.600 what it means-- 00:51:58.600 --> 00:52:00.570 which is nice and descriptive and easy to use-- 00:52:00.570 --> 00:52:03.840 it also hides or, so to speak, abstracts away 00:52:03.840 --> 00:52:08.370 all of the underlying implementation details or the complexity, relatively 00:52:08.370 --> 00:52:09.930 speaking, of what it means to cough. 00:52:09.930 --> 00:52:12.600 So that all I or a friend who is using this puzzle piece 00:52:12.600 --> 00:52:15.840 has to worry about here on out is that the puzzle piece is called cough. 00:52:15.840 --> 00:52:19.380 Does not matter how it's actually implemented. 00:52:19.380 --> 00:52:21.480 And it turns out that in programming languages, 00:52:21.480 --> 00:52:26.220 just like Scratch, you can make one additional improvement on this. 00:52:26.220 --> 00:52:30.380 Rather than have to rely on your own repeat block, 00:52:30.380 --> 00:52:32.130 notice what else you can do. 00:52:32.130 --> 00:52:35.940 In this case, I've refined my implementation of this custom puzzle 00:52:35.940 --> 00:52:39.600 piece, cough, to take what we'll call and what a programmer would call 00:52:39.600 --> 00:52:42.900 an argument or a parameter, some number of inputs 00:52:42.900 --> 00:52:45.270 that influence the behavior of the puzzle piece. 00:52:45.270 --> 00:52:48.900 Now, we've seen arguments or inputs all over the place thus far. 00:52:48.900 --> 00:52:50.670 Move some number of steps. 00:52:50.670 --> 00:52:52.320 That's an argument or a parameter. 00:52:52.320 --> 00:52:53.326 Say something. 00:52:53.326 --> 00:52:55.200 That's an argument or a parameter, because it 00:52:55.200 --> 00:53:00.070 influences the behavior of the Moo block or the Say block, respectively. 00:53:00.070 --> 00:53:01.180 So how do I do that? 00:53:01.180 --> 00:53:03.480 How do I make a cough puzzle piece that doesn't just 00:53:03.480 --> 00:53:07.320 cough once, but coughs 2 times, 3 times, 10 times, 00:53:07.320 --> 00:53:10.740 however many times the user of that puzzle piece wants? 00:53:10.740 --> 00:53:13.060 Well, would use the puzzle piece like this. 00:53:13.060 --> 00:53:16.260 And notice there's now a little white box where I can type a number. 00:53:16.260 --> 00:53:20.070 But it turns out you can parameterize your own custom functions 00:53:20.070 --> 00:53:22.830 by using a special puzzle piece like this, that has not just 00:53:22.830 --> 00:53:25.950 the name of your custom function, but also 00:53:25.950 --> 00:53:28.200 a placeholder for a variable, really. 00:53:28.200 --> 00:53:33.720 A variable being another way of thinking about a parameter or argument. 00:53:33.720 --> 00:53:36.870 And so what I've done with n-- and I'm just calling it n for number, 00:53:36.870 --> 00:53:40.620 but I could have called it anything I want, x, y, or z or something else. 00:53:40.620 --> 00:53:45.150 I can now repeat in the implementation of cough that many times. 00:53:45.150 --> 00:53:46.980 I don't have to hard code 3 anymore. 00:53:46.980 --> 00:53:48.540 I don't have to hard code any number. 00:53:48.540 --> 00:53:51.660 I can drag and drop a copy of n here. 00:53:51.660 --> 00:53:56.580 So that whatever number the user of my puzzle piece types in, 00:53:56.580 --> 00:53:58.800 that's what will be used for the Repeat block. 00:53:58.800 --> 00:54:01.170 And that's how many times this puzzle piece 00:54:01.170 --> 00:54:05.130 will say cough and wait one second. 00:54:05.130 --> 00:54:08.970 And so this now is just a small example of what it's really 00:54:08.970 --> 00:54:11.190 like to program and solve problems. 00:54:11.190 --> 00:54:13.320 The problem, to be fair, is relatively small here, 00:54:13.320 --> 00:54:16.890 in that we just want the cat to cough visually on the screen. 00:54:16.890 --> 00:54:19.470 But the opportunities that problem affords really 00:54:19.470 --> 00:54:21.260 are captured in these several examples. 00:54:21.260 --> 00:54:23.490 With these examples, do you see me starting out 00:54:23.490 --> 00:54:26.154 in the simplest, most naive and correct way possible? 00:54:26.154 --> 00:54:28.320 But it wasn't very well designed, because I was just 00:54:28.320 --> 00:54:31.442 kind of duplicating the Say block and the Wait block 00:54:31.442 --> 00:54:33.150 and then the Say block and the Wait block 00:54:33.150 --> 00:54:34.200 and the Say block and the Wait block. 00:54:34.200 --> 00:54:35.910 It's just, it already sounds tedious. 00:54:35.910 --> 00:54:37.565 And indeed, it's poor form. 00:54:37.565 --> 00:54:39.690 But then I factored that out with the repeat block. 00:54:39.690 --> 00:54:43.410 And then I introduced a custom puzzle piece whose second version 00:54:43.410 --> 00:54:45.660 also allowed me to parameterize it. 00:54:45.660 --> 00:54:50.310 And so the fun in programming, and the opportunities 00:54:50.310 --> 00:54:55.140 to become a better programmer are illuminated with examples like this. 00:54:55.140 --> 00:54:58.290 To be good at programming is to notice opportunities like this, 00:54:58.290 --> 00:55:00.360 to actually take opportunities like this, 00:55:00.360 --> 00:55:05.880 to factor certain common code out, to abstract it away or bundle it inside 00:55:05.880 --> 00:55:09.220 of custom puzzle pieces or black boxes, so to speak, 00:55:09.220 --> 00:55:13.020 is what's evidence, generally, of not just correct code, 00:55:13.020 --> 00:55:16.800 but good design and well-designed code. 00:55:16.800 --> 00:55:19.749 Now, these are just a few of the features you might see in a language. 00:55:19.749 --> 00:55:21.290 There are certainly a few others too. 00:55:21.290 --> 00:55:24.810 Let me go ahead and open up an example here called Thread. 00:55:24.810 --> 00:55:29.040 And you'll see, as promised, Scratch's support for multiple sprites, 00:55:29.040 --> 00:55:30.364 multiple characters. 00:55:30.364 --> 00:55:31.530 And this is the funny thing. 00:55:31.530 --> 00:55:36.150 Because thus far, almost all of our programs or indeed, 00:55:36.150 --> 00:55:40.140 all of our programs, have had just one cat or one sprite, and therefore, 00:55:40.140 --> 00:55:42.330 really one thread, as we'll call it. 00:55:42.330 --> 00:55:44.490 They're only doing one thing at a time. 00:55:44.490 --> 00:55:47.340 With this example, per the cat and the bird, 00:55:47.340 --> 00:55:49.320 this example will actually have two things 00:55:49.320 --> 00:55:51.160 happening at once simultaneously. 00:55:51.160 --> 00:55:54.340 And there's some kind of interrelation between them. 00:55:54.340 --> 00:55:57.450 Notice that the cat is kind of stealthily, 00:55:57.450 --> 00:56:00.960 and very predictably, following the bird. 00:56:00.960 --> 00:56:02.130 This is not random. 00:56:02.130 --> 00:56:05.040 But oh, no, please, getting-- 00:56:05.040 --> 00:56:06.430 [LIONS ROARS] 00:56:07.480 --> 00:56:09.610 Not exactly what we were hoping for there. 00:56:09.610 --> 00:56:14.110 So there was clearly some kind of interaction or dependency there. 00:56:14.110 --> 00:56:15.500 So what was going on? 00:56:15.500 --> 00:56:19.000 Well, notice that the bird is highlighted in the bottom left, 00:56:19.000 --> 00:56:21.020 as has been our cat previously. 00:56:21.020 --> 00:56:25.090 And so this, at top right, is the program driving the bird's behavior. 00:56:25.090 --> 00:56:28.830 When the green flag was clicked it went to negative 150 and positive 150. 00:56:28.830 --> 00:56:33.730 It turns out that blank world in which Scratch lives can 00:56:33.730 --> 00:56:36.790 be addressed with coordinates, x and y. 00:56:36.790 --> 00:56:38.530 So some number of pixels left or right. 00:56:38.530 --> 00:56:40.090 Some number of pixels top and bottom. 00:56:40.090 --> 00:56:43.510 And I just decided arbitrarily to start it at negative 150, 50 00:56:43.510 --> 00:56:44.710 with some trial and error. 00:56:44.710 --> 00:56:46.990 I then had it then point in a 45-degree angle, 00:56:46.990 --> 00:56:49.170 just because it starts off in an interesting way. 00:56:49.170 --> 00:56:52.300 And then I forever told the bird do the following-- 00:56:52.300 --> 00:56:57.070 if not touching the cat, the other sprite, then move three steps. 00:56:57.070 --> 00:56:58.480 And if on edge, bounce. 00:56:58.480 --> 00:57:02.710 Which is a nice one-line version, one single-puzzle-piece version of the if 00:57:02.710 --> 00:57:06.560 construct we did earlier where bounce means turn 180 degrees. 00:57:06.560 --> 00:57:08.770 But I've used a puzzle piece that comes with Scratch 00:57:08.770 --> 00:57:10.670 to achieve the same result. 00:57:10.670 --> 00:57:12.280 So that's it. 00:57:12.280 --> 00:57:15.724 If the bird is not touching the cat, then just move three steps. 00:57:15.724 --> 00:57:17.140 And if you're on the edge, bounce. 00:57:17.140 --> 00:57:18.460 So just keep bouncing around. 00:57:18.460 --> 00:57:19.230 Just keep moving. 00:57:19.230 --> 00:57:23.800 So the bird doesn't put all that much thought into what it's doing. 00:57:23.800 --> 00:57:26.500 The cat, meanwhile, is kind of clever. 00:57:26.500 --> 00:57:30.630 The cat, when the green flag is clicked, goes to negative 160, negative 160. 00:57:30.630 --> 00:57:33.830 So I made sure that they don't start too close to one another. 00:57:33.830 --> 00:57:35.770 I then have them point in a random direction. 00:57:35.770 --> 00:57:39.010 Scratch, like a lot of languages, supports random or pseudo 00:57:39.010 --> 00:57:39.760 random numbers. 00:57:39.760 --> 00:57:44.240 Where just like a human can pick a number, like 50, that's sort of random, 00:57:44.240 --> 00:57:48.300 so can a computer pick a number between 91 and 179. 00:57:48.300 --> 00:57:52.890 And that, you might notice, is between essentially a 90-degree window. 00:57:52.890 --> 00:57:55.480 And just because I wanted the cat to at least start off 00:57:55.480 --> 00:57:57.430 at some range of angles. 00:57:57.430 --> 00:58:00.220 Then I wanted the cat forever to do the following. 00:58:00.220 --> 00:58:04.330 If you're touching the bird, place sound lion 4 this time. 00:58:04.330 --> 00:58:05.467 Then stop this script. 00:58:05.467 --> 00:58:07.300 So I wanted to make sure that the cat didn't 00:58:07.300 --> 00:58:10.210 keep moving if it touched the bird. 00:58:10.210 --> 00:58:13.310 I wanted the game just to stop or the animation to stop. 00:58:13.310 --> 00:58:15.910 And so I explicitly say stop, so that the human doesn't 00:58:15.910 --> 00:58:18.790 have to click the red stop sign. 00:58:18.790 --> 00:58:20.560 But then here's the cleverness. 00:58:20.560 --> 00:58:25.810 Forever point toward the bird and move one step. 00:58:25.810 --> 00:58:28.022 So the cat has this very stealthy advantage 00:58:28.022 --> 00:58:29.980 that no matter where the bird is, and remember, 00:58:29.980 --> 00:58:33.100 the bird is kind of bouncing all every which way, 00:58:33.100 --> 00:58:41.560 bouncing randomly off the walls, ultimately, the cat 00:58:41.560 --> 00:58:45.160 is slowly but surely tracking the bird by always pointing toward it 00:58:45.160 --> 00:58:46.480 and moving one step. 00:58:46.480 --> 00:58:49.480 And so even as the bird is sort of aimlessly flying around, 00:58:49.480 --> 00:58:54.250 the cat is very slowly but very methodically, very algorithmically, 00:58:54.250 --> 00:58:56.530 if you will, pointing itself toward the cat. 00:58:56.530 --> 00:59:00.820 So if we rerun this program, you'll see the cat's indeed 00:59:00.820 --> 00:59:03.280 exercising exactly that logic. 00:59:03.280 --> 00:59:05.130 And the bird's kind of getting away. 00:59:05.130 --> 00:59:07.960 But again, the cat is pivoting to follow him. 00:59:07.960 --> 00:59:10.390 And he keeps going and keeps going and keeps going. 00:59:10.390 --> 00:59:12.250 And we'll again find him right-- 00:59:12.250 --> 00:59:13.300 [LION ROARS] 00:59:13.300 --> 00:59:14.110 There. 00:59:14.110 --> 00:59:17.920 Now, we could give maybe the bird a bit more of an advantage. 00:59:17.920 --> 00:59:19.660 Let me go ahead and click on the bird. 00:59:19.660 --> 00:59:21.730 And how about instead of moving three steps, 00:59:21.730 --> 00:59:24.760 let me have him move six steps, twice as fast? 00:59:24.760 --> 00:59:27.250 Click the green flag. 00:59:27.250 --> 00:59:28.660 Very nice. 00:59:28.660 --> 00:59:31.100 So the cat is still moving at the same rate. 00:59:31.100 --> 00:59:33.700 Unfortunately, it's still moving toward the bird. 00:59:33.700 --> 00:59:36.110 And the bird is not taking that into account. 00:59:36.110 --> 00:59:39.490 We could give the advantage to the cat instead. 00:59:39.490 --> 00:59:42.700 So if I go back to the cat script and instead of moving one step, 00:59:42.700 --> 00:59:46.000 how about 10 steps, and let's see what happens now. 00:59:46.000 --> 00:59:47.360 [ROARS] 00:59:47.360 --> 00:59:49.480 Cat pretty much seeks out the bird. 00:59:49.480 --> 00:59:52.130 A bird-seeking cat. 00:59:52.130 --> 00:59:56.500 How about one more example that speaks to the height 00:59:56.500 --> 00:59:59.560 of the ceiling with an environment like Scratch, just how much 00:59:59.560 --> 01:00:02.230 you can do using some basic fundamentals. 01:00:02.230 --> 01:00:05.890 This time we have a couple of our puppets here to star. 01:00:05.890 --> 01:00:08.500 Notice one is blue, one is orange. 01:00:08.500 --> 01:00:13.630 And one is called playing Marco Polo, it would seem, if familiar. 01:00:13.630 --> 01:00:17.170 This is a game that kids might play whereby if one person says Marco, 01:00:17.170 --> 01:00:18.940 the other kids are supposed to say Polo. 01:00:18.940 --> 01:00:22.360 And then one is supposed to find the other in that particular game. 01:00:22.360 --> 01:00:27.250 But we're just using it as an example of something called Events. 01:00:27.250 --> 01:00:30.550 So it turns out, per the last example, that a computer can effectively 01:00:30.550 --> 01:00:33.970 do multiple things at a time, thanks to multithreading, 01:00:33.970 --> 01:00:37.120 where a thread is an independent thread, so to speak, 01:00:37.120 --> 01:00:40.750 of execution, that is happening in parallel with something 01:00:40.750 --> 01:00:41.650 else on the computer. 01:00:41.650 --> 01:00:45.190 And especially nowadays, now that computers have multiple cores or CPUs, 01:00:45.190 --> 01:00:47.260 they can literally do multiple things at once. 01:00:47.260 --> 01:00:51.220 And even if they can't, computers are so much faster than use humans 01:00:51.220 --> 01:00:55.091 that even if the cat and bird aren't technically moving at the same time, 01:00:55.091 --> 01:00:58.090 even if the cat gets to move a bit and then the bird cat, then the bird, 01:00:58.090 --> 01:00:59.798 the fact that that's happening so quickly 01:00:59.798 --> 01:01:01.930 means my human eyes probably won't even notice. 01:01:01.930 --> 01:01:04.780 So for all intents and purposes, a multithreaded program 01:01:04.780 --> 01:01:08.580 is indeed doing multiple things at once, just like our past example. 01:01:08.580 --> 01:01:12.120 But sometimes those threads might want to inter-communicate, 01:01:12.120 --> 01:01:15.930 not just pointing themselves at each other, but actually sending messages. 01:01:15.930 --> 01:01:17.460 And notice how this can work. 01:01:17.460 --> 01:01:22.110 When green flag is clicked, let's have this puppet forever do the following. 01:01:22.110 --> 01:01:23.780 If the key space-- 01:01:23.780 --> 01:01:25.350 so the space bar is pressed-- 01:01:25.350 --> 01:01:28.770 then say Marco for two seconds, as per this game. 01:01:28.770 --> 01:01:31.200 And then broadcast something called an event. 01:01:31.200 --> 01:01:35.220 An event is like sending a message wirelessly 01:01:35.220 --> 01:01:36.870 that only computers can hear. 01:01:36.870 --> 01:01:39.480 So it's not saying just the word, Marco. 01:01:39.480 --> 01:01:42.490 It's also transmitting a signal called an Event. 01:01:42.490 --> 01:01:48.240 And so long as the other puppet has been configured to listen for that event, 01:01:48.240 --> 01:01:51.450 then it can respond to the first puppet's action. 01:01:51.450 --> 01:01:54.540 So the orange puppet is going to say Marco and then broadcast 01:01:54.540 --> 01:01:55.890 this so-called event. 01:01:55.890 --> 01:01:58.650 The blue puppet, quite simply, is going to do this. 01:01:58.650 --> 01:02:03.100 When I receive Event, say Polo, for two seconds. 01:02:03.100 --> 01:02:06.720 So in other words, this blue Muppet is not 01:02:06.720 --> 01:02:09.530 going to do anything when the green flag is clicked. 01:02:09.530 --> 01:02:11.190 There is no such block there. 01:02:11.190 --> 01:02:16.530 Rather, when I receive Event, that secret wireless signal from some other 01:02:16.530 --> 01:02:18.570 sprite-- in this case, the orange Muppet-- 01:02:18.570 --> 01:02:22.170 the blue Muppet is going to say Polo for two seconds. 01:02:22.170 --> 01:02:26.010 And so notice the dependency we've created between these two guys. 01:02:26.010 --> 01:02:29.770 When I now click on the green flag, nothing appears to happen. 01:02:29.770 --> 01:02:33.570 But the orange Muppet, remember, is listening for my space bar. 01:02:33.570 --> 01:02:37.710 And so as soon as I hit it, he says Marco and the blue guy 01:02:37.710 --> 01:02:40.650 hears it and says Polo. 01:02:40.650 --> 01:02:45.210 But the only thing listening for my key press is the orange puppet. 01:02:45.210 --> 01:02:48.240 The blue puppet, meanwhile, is only responding to the Event 01:02:48.240 --> 01:02:50.340 emitted by the orange puppet. 01:02:50.340 --> 01:02:54.510 Now, this allows us to implement this simple little game with puppets. 01:02:54.510 --> 01:02:58.797 But this idea that one thread can convey a message to another 01:02:58.797 --> 01:03:00.130 is what's ultimately compelling. 01:03:00.130 --> 01:03:03.150 Surely I could use that in more complicated programs 01:03:03.150 --> 01:03:06.750 as a mechanism for having two sprites interact somehow. 01:03:06.750 --> 01:03:09.750 Not to follow each other around on the screen like the cat and the bird, 01:03:09.750 --> 01:03:13.470 but maybe exchange messages of some sort. 01:03:13.470 --> 01:03:16.320 So that something only happens to one sprite 01:03:16.320 --> 01:03:21.630 if something has just happened to another sprite. 01:03:21.630 --> 01:03:22.210 Woo. 01:03:22.210 --> 01:03:23.520 Well, that was Scratch. 01:03:23.520 --> 01:03:27.000 This is just one programming language, and it's graphical at that. 01:03:27.000 --> 01:03:28.920 And indeed, programmers in the real world 01:03:28.920 --> 01:03:32.440 don't typically write software by dragging and dropping puzzle pieces. 01:03:32.440 --> 01:03:34.800 Rather, they write textual lines of code, 01:03:34.800 --> 01:03:39.120 like the C and C++ and Java and Python and Ruby code that we saw earlier. 01:03:39.120 --> 01:03:43.800 But the ideas that they write down and the ideas that they leverage 01:03:43.800 --> 01:03:46.500 are functionally identical to so many of the ideas 01:03:46.500 --> 01:03:48.000 that we've seen here in Scratch. 01:03:48.000 --> 01:03:50.940 What Scratch does for us, for our purposes 01:03:50.940 --> 01:03:54.032 here, is just get rid of things like the hashtag symbol 01:03:54.032 --> 01:03:55.740 and the curly braces and the parentheses, 01:03:55.740 --> 01:03:59.040 and so many other syntactic features that are really just distractions. 01:03:59.040 --> 01:04:01.350 And necessary for the language as vocabulary, 01:04:01.350 --> 01:04:03.910 but not necessary to understand the ideas. 01:04:03.910 --> 01:04:07.620 So if you walk away today with an understanding of what functions are 01:04:07.620 --> 01:04:10.980 and loops and conditions, Boolean expressions, variables, 01:04:10.980 --> 01:04:13.830 and perhaps threads and events, and some of the other features 01:04:13.830 --> 01:04:17.880 we've seen evinced by Scratch, do you have really a fundamental understanding 01:04:17.880 --> 01:04:22.390 of what it tends to be like to program in fairly common languages? 01:04:22.390 --> 01:04:24.640 Indeed, we focused on one type of programming language 01:04:24.640 --> 01:04:26.692 here, imperative or procedural programming. 01:04:26.692 --> 01:04:28.150 And there's other worlds out there. 01:04:28.150 --> 01:04:29.920 There's object-oriented programming. 01:04:29.920 --> 01:04:31.480 There is functional programming. 01:04:31.480 --> 01:04:35.850 But the end of the day, the key takeaway is that in those and these languages, 01:04:35.850 --> 01:04:38.980 are there are generally these principles, these fundamentals, 01:04:38.980 --> 01:04:43.980 these building blocks as embodied by the literal puzzle pieces here, 01:04:43.980 --> 01:04:47.880 with which you can assemble solutions to problems by using 01:04:47.880 --> 01:04:52.470 very small problems solved with these puzzle pieces and build, 01:04:52.470 --> 01:04:56.820 on top of those solutions, even more and more sophisticated solutions. 01:04:56.820 --> 01:05:00.540 Still consider, after all, where we began, looking at Oscar Time. 01:05:00.540 --> 01:05:02.960 That is a game where you have to rack up a high 01:05:02.960 --> 01:05:05.670 as high a score as possible by clicking and dragging and dropping 01:05:05.670 --> 01:05:09.570 trash that's falling from the sky into the trash before the game ends. 01:05:09.570 --> 01:05:11.220 But what does that even mean? 01:05:11.220 --> 01:05:16.140 Again, if you zoom in to the lowest level at the puzzle pieces 01:05:16.140 --> 01:05:18.930 and the building blocks that compose that program, 01:05:18.930 --> 01:05:21.110 there is just a loop that was making the trash fall. 01:05:21.110 --> 01:05:23.970 And there is just a variable keeping track of the score. 01:05:23.970 --> 01:05:26.760 And there was an If condition raising the lid of the trash when 01:05:26.760 --> 01:05:28.020 the cursor got close enough. 01:05:28.020 --> 01:05:31.290 And so all of the complexity of that particular game, at the end of the day, 01:05:31.290 --> 01:05:35.430 can be reduced to these and other basic primitives. 01:05:35.430 --> 01:05:37.350 As to what other languages are out there, 01:05:37.350 --> 01:05:40.380 this is just a slice of the possible list. 01:05:40.380 --> 01:05:43.380 But on the screen here, would you see some of those familiar ones again, 01:05:43.380 --> 01:05:48.960 C and C++, but so many others that are being used currently or have been used 01:05:48.960 --> 01:05:49.710 in the past. 01:05:49.710 --> 01:05:52.260 And the dot, dot, dot suggests that there's so many more. 01:05:52.260 --> 01:05:54.343 In fact, if you'd like to take a look in Wikipedia 01:05:54.343 --> 01:05:57.630 and be completely overwhelmed by the many languages out there, 01:05:57.630 --> 01:06:01.560 you'll be impressed by just how many we humans have created over time. 01:06:01.560 --> 01:06:04.800 But at the end of the day, there tend to be trends in this industry. 01:06:04.800 --> 01:06:07.767 And when you are a programmer or a software developer 01:06:07.767 --> 01:06:10.350 or a software engineer in the real world working for a company 01:06:10.350 --> 01:06:13.260 or really just working for a hobby, will you, as a programmer, 01:06:13.260 --> 01:06:17.250 tend to have just one or a few languages that you tend to reach for? 01:06:17.250 --> 01:06:19.890 So becoming a programmer is not about learning 01:06:19.890 --> 01:06:21.600 that list of programming languages. 01:06:21.600 --> 01:06:24.660 Even I only know a tiny fraction, probably count them 01:06:24.660 --> 01:06:26.610 on one or two hands in total. 01:06:26.610 --> 01:06:28.410 But you tend to want to introduce yourself 01:06:28.410 --> 01:06:30.480 to different types of programming languages. 01:06:30.480 --> 01:06:34.080 So that when something new comes along or some new device comes along 01:06:34.080 --> 01:06:37.950 that you want to program for, you have the basic ideas and principles in mind 01:06:37.950 --> 01:06:40.409 and simply need to pick up a bit of new syntax. 01:06:40.409 --> 01:06:42.450 And as such, it's a little bit easier, I daresay, 01:06:42.450 --> 01:06:45.150 than learning a new spoken or written human language. 01:06:45.150 --> 01:06:48.990 Because these languages tend to be more narrowly defined with fewer keywords. 01:06:48.990 --> 01:06:53.970 But the ideas persist, even as some of those particulars changed. 01:06:53.970 --> 01:06:58.520 To wrap for now, I thought we'd take a look at an oldie but goodie, 01:06:58.520 --> 01:07:01.230 a program written by one of our own former students 01:07:01.230 --> 01:07:03.840 that synthesized music, synthesized loops, 01:07:03.840 --> 01:07:07.290 then synthesized conditions, all to create 01:07:07.290 --> 01:07:11.800 a wonderfully-animated conclusion to this look here, at programming. 01:07:11.800 --> 01:07:41.400 [MUSIC - THE WEATHER GIRLS, "IT'S RAINING MEN"] 01:07:41.400 --> 01:07:42.400 WEATHER GIRL ICON 1: Hi! 01:07:42.400 --> 01:07:43.399 WEATHER GIRL ICON 2: Hi! 01:07:43.399 --> 01:07:44.500 We're your weather girls. 01:07:44.500 --> 01:07:45.666 WEATHER GIRL ICON 3: Uh-huh. 01:07:45.666 --> 01:07:48.100 WEATHER GIRL ICON 2: And have we got news for you! 01:07:48.100 --> 01:07:49.725 WEATHER GIRL ICON 1: You better listen. 01:07:49.725 --> 01:07:52.000 WEATHER GIRL ICON 2: Get ready, all you lonely girls, 01:07:52.000 --> 01:07:54.100 and leave those umbrellas at home. 01:07:54.100 --> 01:07:55.600 WEATHER GIRL ICON 3: All right. 01:07:55.600 --> 01:07:57.700 ALL (SINGING): Humidity's rising. 01:07:57.700 --> 01:07:59.200 WEATHER GIRL ICON 2: Mm, rising. 01:07:59.200 --> 01:08:00.400 ALL (SINGING): Barometer's getting low. 01:08:00.400 --> 01:08:01.600 WEATHER GIRL ICON 2: How low, girl? 01:08:01.600 --> 01:08:02.800 WEATHER GIRL ICON 1: Uh-oh. 01:08:02.800 --> 01:08:04.600 ALL (SINGING): According to all sources-- 01:08:04.600 --> 01:08:06.100 WEATHER GIRL ICON 1: What sources, now? 01:08:06.100 --> 01:08:07.933 ALL (SINGING): The street's the place to go. 01:08:07.933 --> 01:08:09.700 WEATHER GIRL ICON 2: We better hurry up. 01:08:09.700 --> 01:08:15.434 'Cause tonight, for the first time, just about half past 10. 01:08:15.434 --> 01:08:16.600 ALL (SINGING): Half past 10. 01:08:16.600 --> 01:08:20.200 WEATHER GIRL ICON 2: For the first time in history, 01:08:20.200 --> 01:08:22.000 it's going to start raining men. 01:08:22.000 --> 01:08:24.100 WEATHER GIRL ICONS 2 AND 3: Start raining men. 01:08:24.100 --> 01:08:30.400 WEATHER GIRL ICON 2: It's raining men, hallelujah, it's raining men. 01:08:30.400 --> 01:08:32.200 Amen. 01:08:32.200 --> 01:08:34.000 I'm going to go out. 01:08:34.000 --> 01:08:36.100 I'm going to let myself get-- 01:08:36.100 --> 01:08:40.000 ALL: Absolutely soaking wet! 01:08:40.000 --> 01:08:42.700 WEATHER GIRL ICON 2: It's raining men! 01:08:42.700 --> 01:08:44.850 Hallelujah!