1 00:00:08,483 --> 00:00:11,477 [ROCK MUSIC] 2 00:00:32,435 --> 00:00:35,429 [MUSIC - "HAPPY TOGETHER" THE TURTLES] 3 00:01:27,330 --> 00:01:30,170 DAVID MALAN: Today, we begin our exploration of the 4 00:01:30,170 --> 00:01:32,710 fundamentals of computer science and our introduction, 5 00:01:32,710 --> 00:01:35,070 now, to the art of programming, of which that is 6 00:01:35,070 --> 00:01:36,260 just one example. 7 00:01:36,260 --> 00:01:39,600 But in the same vein of problem solving, know that 8 00:01:39,600 --> 00:01:42,080 CS50's traditional Puzzle Day is tomorrow. 9 00:01:42,080 --> 00:01:45,190 If you've not yet RSVPed for this event, you can go to 10 00:01:45,190 --> 00:01:47,510 cs50.net/rsvp. 11 00:01:47,510 --> 00:01:50,940 You can bring teams of two or three or four. 12 00:01:50,940 --> 00:01:53,080 You will be challenged with problems like the one you saw 13 00:01:53,080 --> 00:01:54,190 up there in the beginning. 14 00:01:54,190 --> 00:01:57,000 All new problems this year if you partook last year, and you 15 00:01:57,000 --> 00:01:59,680 will have a chance then to win some fabulous prizes. 16 00:01:59,680 --> 00:02:02,750 Among them, a Wii and some gift certificates and like, 17 00:02:02,750 --> 00:02:05,650 all while hanging out with CS50 students and classmates 18 00:02:05,650 --> 00:02:07,720 and pizza and Facebook. 19 00:02:07,720 --> 00:02:10,430 So more on that tomorrow if you would like. 20 00:02:10,430 --> 00:02:13,330 >> This then is CS50, for those of you joining us for the 21 00:02:13,330 --> 00:02:14,510 first time. 22 00:02:14,510 --> 00:02:18,030 And know that this course is particularly determined to get 23 00:02:18,030 --> 00:02:22,605 you through this course to its final end, at which point you 24 00:02:22,605 --> 00:02:24,270 will have not only an understanding of these 25 00:02:24,270 --> 00:02:26,450 fundamentals of computer science, but you will have 26 00:02:26,450 --> 00:02:29,120 this very practical skill set that you can then take back to 27 00:02:29,120 --> 00:02:31,370 your own department, whether it's engineering sciences, 28 00:02:31,370 --> 00:02:33,570 applied mathematics, the physical sciences, natural 29 00:02:33,570 --> 00:02:34,780 science, or the like. 30 00:02:34,780 --> 00:02:37,030 Indeed, what's so exciting about computer science these 31 00:02:37,030 --> 00:02:39,900 days is that it is just so applicable to 32 00:02:39,900 --> 00:02:40,990 all sorts of fields. 33 00:02:40,990 --> 00:02:46,210 And even though today, we will only scratch the surface of a 34 00:02:46,210 --> 00:02:49,020 very traditional programming language known as C, we'll 35 00:02:49,020 --> 00:02:51,040 instead look at something called Scratch, something with 36 00:02:51,040 --> 00:02:53,420 which that cookie love story was implemented by one of your 37 00:02:53,420 --> 00:02:57,020 predecessors in years past, to tell stories, to create games, 38 00:02:57,020 --> 00:03:00,060 to create interactive art, and to actually start to wrap our 39 00:03:00,060 --> 00:03:02,240 minds around some of the fundamental constructs that 40 00:03:02,240 --> 00:03:05,390 underlie programming but in a context, as you'll see, that's 41 00:03:05,390 --> 00:03:09,140 much less scary, that's much less arcane, than you will see 42 00:03:09,140 --> 00:03:10,140 before long. 43 00:03:10,140 --> 00:03:12,410 And realize, too, for those of you just joining us this 44 00:03:12,410 --> 00:03:16,170 semester, the phrases on which we ended Wednesday's lecture, 45 00:03:16,170 --> 00:03:20,260 it is not so important where you end up relative to your 46 00:03:20,260 --> 00:03:22,950 classmates in this class, but where you, by semester's end 47 00:03:22,950 --> 00:03:27,190 in week 11, end up relative to yourself right this very day. 48 00:03:27,190 --> 00:03:32,000 >> So without further ado, this is also worth noting that 49 00:03:32,000 --> 00:03:34,450 those less comfortable, those somewhere in between, are 50 00:03:34,450 --> 00:03:36,825 also, by design of this course, not at any disadvantage 51 00:03:36,825 --> 00:03:38,760 vis-a-vis those more comfortable 52 00:03:38,760 --> 00:03:39,920 coming into this class. 53 00:03:39,920 --> 00:03:41,810 As you'll see over the weeks to come, we have different 54 00:03:41,810 --> 00:03:44,600 tracks for disparate back background, sections for those 55 00:03:44,600 --> 00:03:46,500 less comfortable, more comfortable, those somewhere 56 00:03:46,500 --> 00:03:47,250 in between. 57 00:03:47,250 --> 00:03:49,520 As you'll see this weekend and next week, two versions of 58 00:03:49,520 --> 00:03:52,410 most problem sets in both standard and hacker edition so 59 00:03:52,410 --> 00:03:55,390 that you all can self-select down the path with which you 60 00:03:55,390 --> 00:03:56,615 are most comfortable. 61 00:03:56,615 --> 00:03:58,770 >> So today, we dive into this programming 62 00:03:58,770 --> 00:04:00,060 language called Scratch. 63 00:04:00,060 --> 00:04:02,250 It's a graphical programming language, and yet it has the 64 00:04:02,250 --> 00:04:04,500 same features of some of the higher level languages we'll 65 00:04:04,500 --> 00:04:07,050 explore later in the semester, among them C and 66 00:04:07,050 --> 00:04:08,670 JavaScript and PHP. 67 00:04:08,670 --> 00:04:11,890 But it's going to allow us to avoid some of the distractions 68 00:04:11,890 --> 00:04:14,890 early on of syntax, things like semicolons, parentheses, 69 00:04:14,890 --> 00:04:17,720 and other arcane details that, at first glance, are just not 70 00:04:17,720 --> 00:04:19,916 at all intellectually interesting and tend to get in 71 00:04:19,916 --> 00:04:22,310 the way from some fundamental understanding. 72 00:04:22,310 --> 00:04:24,870 In terms of now the support structure ahead, realized this 73 00:04:24,870 --> 00:04:27,570 tool, CS50 Discuss, which will be linked on the course's 74 00:04:27,570 --> 00:04:30,320 website later today, is the online discussion forum in 75 00:04:30,320 --> 00:04:32,480 which you'll be able to ask questions of each other and 76 00:04:32,480 --> 00:04:35,020 staff, and it's also a tool, as we'll see next week, 77 00:04:35,020 --> 00:04:37,240 that we'll use quite integratedly in 78 00:04:37,240 --> 00:04:38,780 office hours as well. 79 00:04:38,780 --> 00:04:41,670 Indeed, office hours begin on Monday, and frankly, the first 80 00:04:41,670 --> 00:04:43,930 week of office hours is fairly low key. 81 00:04:43,930 --> 00:04:46,820 >> I don't think you'll find Scratch all that inaccessible. 82 00:04:46,820 --> 00:04:48,930 It's rather self-explanatory, as we'll see, and so what 83 00:04:48,930 --> 00:04:51,380 we'll use it for today is to extract some of the 84 00:04:51,380 --> 00:04:53,990 fundamental ideas that will, then, persist throughout the 85 00:04:53,990 --> 00:04:54,900 rest of the semester. 86 00:04:54,900 --> 00:04:57,980 But starting Monday, at 8:00 PM through 11:00 PM will be 87 00:04:57,980 --> 00:05:01,530 office hours in Annenberg over brain break. 88 00:05:01,530 --> 00:05:04,490 Sectioning two will begin this evening, so sometime this 89 00:05:04,490 --> 00:05:07,260 weekend, go to cs50.net/section, and you'll 90 00:05:07,260 --> 00:05:08,530 be asked a number of questions. 91 00:05:08,530 --> 00:05:10,290 Among them, how would you describe your 92 00:05:10,290 --> 00:05:12,040 comfort level as of today? 93 00:05:12,040 --> 00:05:14,450 There's no hard, fast rule as to who's in which bucket. 94 00:05:14,450 --> 00:05:17,170 It's just the sort of thing that you probably know if you 95 00:05:17,170 --> 00:05:20,280 are among those the less comfortable or otherwise. 96 00:05:20,280 --> 00:05:21,350 >> And now, walkthroughs. 97 00:05:21,350 --> 00:05:24,020 The problem set specification for this week, both standard 98 00:05:24,020 --> 00:05:28,560 and hacker edition alike, is online at cs50.net as of now. 99 00:05:28,560 --> 00:05:31,500 And you'll see that the first of these editions, the 100 00:05:31,500 --> 00:05:33,940 standard edition each week, is accompanied by something we 101 00:05:33,940 --> 00:05:36,550 call a code walkthrough, a session led by one of the 102 00:05:36,550 --> 00:05:39,100 course's teaching fellows to guide you through, provide 103 00:05:39,100 --> 00:05:42,290 tips on, and get you down a particular path when it comes 104 00:05:42,290 --> 00:05:44,150 to starting these problem sets. 105 00:05:44,150 --> 00:05:46,790 So with each of these problem sets, if you're ever wanting 106 00:05:46,790 --> 00:05:50,250 for yourself where do I begin, you begin with these 107 00:05:50,250 --> 00:05:50,890 walkthroughs. 108 00:05:50,890 --> 00:05:53,450 And in fact, allow me to introduce Zamyla Chan, this 109 00:05:53,450 --> 00:05:55,870 year's teaching fellow who will be leading each and every 110 00:05:55,870 --> 00:05:56,910 one of these walkthroughs. 111 00:05:56,910 --> 00:05:57,961 Zamyla? 112 00:05:57,961 --> 00:06:03,370 >> [APPLAUSE] 113 00:06:03,370 --> 00:06:03,975 ZAMYLA CHAN: Oh, hi, everyone. 114 00:06:03,975 --> 00:06:05,130 My name is Zamyla. 115 00:06:05,130 --> 00:06:08,250 I'm a junior studying engineering in Winthrop House. 116 00:06:08,250 --> 00:06:10,890 But I try and fit in as many computer science courses as I 117 00:06:10,890 --> 00:06:13,350 can, which is why I'm really excited to be leading this 118 00:06:13,350 --> 00:06:14,590 year's walkthroughs. 119 00:06:14,590 --> 00:06:16,950 Walkthroughs, for me, were an essential part of my CS50 120 00:06:16,950 --> 00:06:17,910 experience. 121 00:06:17,910 --> 00:06:22,710 During the walkthrough, for every p-set, we'll go through 122 00:06:22,710 --> 00:06:25,580 the problem set together, look over the problems, kind of 123 00:06:25,580 --> 00:06:27,360 divide them up into manageable bites. 124 00:06:27,360 --> 00:06:28,990 I'll give you tips, techniques, for getting 125 00:06:28,990 --> 00:06:30,820 through and getting started. 126 00:06:30,820 --> 00:06:33,510 I hope to see you all at the walkthroughs. 127 00:06:33,510 --> 00:06:34,720 If you can't make it in person, then 128 00:06:34,720 --> 00:06:36,580 please do tune in online. 129 00:06:36,580 --> 00:06:36,840 >> DAVID MALAN: Excellent. 130 00:06:36,840 --> 00:06:37,950 Thank you, Zamyla. 131 00:06:37,950 --> 00:06:40,870 So realize that walkthroughs are, indeed, on a Friday 132 00:06:40,870 --> 00:06:43,110 afternoon, but this is deliberately by design so that 133 00:06:43,110 --> 00:06:45,750 even if you'd rather not attend class on a Friday 134 00:06:45,750 --> 00:06:48,490 afternoon, the videos will be up all the more quickly over 135 00:06:48,490 --> 00:06:48,990 the weekend. 136 00:06:48,990 --> 00:06:51,880 So realize that cs50.net will be all of Zamyla's 137 00:06:51,880 --> 00:06:53,130 walkthroughs. 138 00:06:54,800 --> 00:06:56,890 And that there is today's date and time, 3:00 PM, 139 00:06:56,890 --> 00:06:58,370 Harberd Hall, 104. 140 00:06:58,370 --> 00:07:00,080 And some of the teaching fellows have also prepared 141 00:07:00,080 --> 00:07:03,250 some remarks for you in absentia to give you a bit of 142 00:07:03,250 --> 00:07:06,760 perspective as to what their experience coming into and 143 00:07:06,760 --> 00:07:08,990 going out of CS50 was like. 144 00:07:08,990 --> 00:07:11,250 So if we could dim the lights for just a moment, I give you 145 00:07:11,250 --> 00:07:18,100 some of CS50's staff. 146 00:07:18,100 --> 00:07:20,440 >> JACKSON STEINKAMP: I took CS50 last year as a freshman in the 147 00:07:20,440 --> 00:07:22,880 fall, and it absolutely blew my mind. 148 00:07:22,880 --> 00:07:25,090 I had never taken any programming classes before, 149 00:07:25,090 --> 00:07:26,380 and never came in with any computer science 150 00:07:26,380 --> 00:07:27,320 experience at all. 151 00:07:27,320 --> 00:07:29,240 And just, I heard the buzz about the class and 152 00:07:29,240 --> 00:07:30,050 decided to take it. 153 00:07:30,050 --> 00:07:31,780 >> JULIA MITELMAN: It was really fun, really engaging. 154 00:07:31,780 --> 00:07:33,480 I still, to this day-- 155 00:07:33,480 --> 00:07:36,560 I just recently saw I have the floppy disk that David gave us 156 00:07:36,560 --> 00:07:38,140 on our first day of lecture. 157 00:07:38,140 --> 00:07:40,020 I had hung it on my wall. 158 00:07:40,020 --> 00:07:42,410 This is probably a little nerdy, but I hung in on my 159 00:07:42,410 --> 00:07:46,930 wall during the class as a reminder of how cool it was. 160 00:07:46,930 --> 00:07:49,240 >> TRAVIS DOWNS: It's so embarrassing. 161 00:07:49,240 --> 00:07:50,770 >> JACKSON STEINKAMP: Computer science is something you 162 00:07:50,770 --> 00:07:52,370 should try even if you're not one for 163 00:07:52,370 --> 00:07:54,980 the traditional sciences. 164 00:07:54,980 --> 00:07:58,510 It's its own experience, and CS50 will make sure you're 165 00:07:58,510 --> 00:08:01,520 supported well through it with its veritable army of TFs. 166 00:08:01,520 --> 00:08:03,700 >> ALI NAHM: I took it as a freshman, and so I made a lot 167 00:08:03,700 --> 00:08:04,215 of new friends. 168 00:08:04,215 --> 00:08:07,170 I also got introduced to this entire concentration and 169 00:08:07,170 --> 00:08:10,770 entire school of engineering, and so I highly recommend it 170 00:08:10,770 --> 00:08:14,360 and welcome you to our CS50 family. 171 00:08:14,360 --> 00:08:17,510 >> YANIV YACOBY: CS50 just teaches you how to use tools 172 00:08:17,510 --> 00:08:19,210 that are widely accessible. 173 00:08:19,210 --> 00:08:21,840 You just need a laptop, you need a web browser, and you 174 00:08:21,840 --> 00:08:26,030 need to learn to write some code, and you can really build 175 00:08:26,030 --> 00:08:27,400 neat things. 176 00:08:27,400 --> 00:08:31,660 >> KAREN XIAO: It's just so cool to be able to make something 177 00:08:31,660 --> 00:08:34,580 and have people use it and have people see it, and that's 178 00:08:34,580 --> 00:08:35,900 what I really love about it. 179 00:08:35,900 --> 00:08:37,429 >> TIM MCLAUGHLIN: --a sense of community, I think, in this 180 00:08:37,429 --> 00:08:39,470 course more than any other course I've taken so far. 181 00:08:39,470 --> 00:08:41,350 You're not just taking another-- 182 00:08:41,350 --> 00:08:42,740 you're not filling another requirement. 183 00:08:42,740 --> 00:08:45,310 You're not just going to lectures and going to section. 184 00:08:45,310 --> 00:08:49,630 But you're doing tons of things that are all about 185 00:08:49,630 --> 00:08:51,560 programming and all about technology, but it doesn't 186 00:08:51,560 --> 00:08:53,090 really feel like a class most of the time. 187 00:08:53,090 --> 00:08:55,012 >> TRAVIS DOWNS: And on the first day, they handed out cake, and 188 00:08:55,012 --> 00:08:56,215 I was instantly sold. 189 00:08:56,215 --> 00:08:57,822 >> JACOB PRITT: Free candy and pizza. 190 00:08:57,822 --> 00:08:59,820 >> ZAMYLA CHAN: --regardless of your interest level, I think 191 00:08:59,820 --> 00:09:05,170 that CS50, you'll have fun, and you'll be intellectually 192 00:09:05,170 --> 00:09:06,070 stimulated. 193 00:09:06,070 --> 00:09:07,926 >> MARK GROZEN-SMITH: It's always a party in class, and it's a 194 00:09:07,926 --> 00:09:09,990 party every night working on your p-set. 195 00:09:09,990 --> 00:09:11,880 >> JACKSON STEINKAMP: Each time you finish a problem set, you 196 00:09:11,880 --> 00:09:13,710 will feel like you've finished a project. 197 00:09:13,710 --> 00:09:16,580 >> MELISSA NIU: It was freshman year, and I was done shopping. 198 00:09:16,580 --> 00:09:19,030 I had my four classes ready, and I was in Annenberg. 199 00:09:19,030 --> 00:09:21,670 And I bump into a friend, and he says, hey, I'm shopping 200 00:09:21,670 --> 00:09:24,330 this class called CS50, and you should come with me. 201 00:09:24,330 --> 00:09:28,150 Ended up taking it that fall, and after that class, I 202 00:09:28,150 --> 00:09:30,510 thought maybe I'm going to minor in CS. 203 00:09:30,510 --> 00:09:33,760 But here I am, three years later, still studying computer 204 00:09:33,760 --> 00:09:36,680 science and actually doing it as a major, and I loved every 205 00:09:36,680 --> 00:09:37,780 moment of it. 206 00:09:37,780 --> 00:09:39,030 >> ROB BOWDEN: I have no idea. 207 00:09:46,408 --> 00:09:48,060 >> ALI NAHM: Let's see. 208 00:09:48,060 --> 00:09:49,200 >> MELISSA NIU: Craziness. 209 00:09:49,200 --> 00:09:50,610 >> SPEAKER 1: I feel like I'd be very cliche. 210 00:09:50,610 --> 00:09:52,530 I'd just say, like, awesome. 211 00:09:52,530 --> 00:09:53,540 >> YANIV YACOBY: Accessibility. 212 00:09:53,540 --> 00:09:55,080 >> JACOB PRITT: Free candy and pizza. 213 00:09:55,080 --> 00:09:57,310 >> TRAVIS DOWNS: Can't turn down a class that hands out cake. 214 00:09:57,310 --> 00:09:58,510 >> TIM MCLAUGHLIN: Energetic. 215 00:09:58,510 --> 00:10:00,040 >> VIPUL SHEKHAWAT: Essential. 216 00:10:00,040 --> 00:10:02,636 >> ROB BOWDEN: Let me think of an answer. 217 00:10:02,636 --> 00:10:04,480 I think I got something. 218 00:10:04,480 --> 00:10:07,092 Oh, god. 219 00:10:07,092 --> 00:10:13,370 Yeah, my name's Rob Bowden, and this is CS50. 220 00:10:13,370 --> 00:10:16,340 >> [APPLAUSE] 221 00:10:20,810 --> 00:10:23,390 DAVID MALAN: All right, so let's start to paint a picture 222 00:10:23,390 --> 00:10:26,230 of the direction in which we can go, and let's introduce 223 00:10:26,230 --> 00:10:28,190 this concept here known as pseudocode. 224 00:10:28,190 --> 00:10:29,560 So pseudocode is not a programming 225 00:10:29,560 --> 00:10:30,810 language unto itself. 226 00:10:30,810 --> 00:10:33,250 It's nothing technical per se, but it's just sort of a 227 00:10:33,250 --> 00:10:36,490 general way of expressing yourself fairly precisely, 228 00:10:36,490 --> 00:10:39,730 fairly algorithmically, fairly procedurally, but without 229 00:10:39,730 --> 00:10:42,570 having to worry about what language you're expressing 230 00:10:42,570 --> 00:10:43,150 yourself in. 231 00:10:43,150 --> 00:10:46,280 It's some model of English and programming languages with 232 00:10:46,280 --> 00:10:48,790 which you happen to be familiar, so we can start 233 00:10:48,790 --> 00:10:50,600 writing this sort of thing as we go. 234 00:10:50,600 --> 00:10:54,450 And in fact, Joseph, could I borrow you up on stage to be 235 00:10:54,450 --> 00:10:55,490 scribe here? 236 00:10:55,490 --> 00:11:00,130 I've gone ahead here in advance and forgotten to put 237 00:11:00,130 --> 00:11:02,920 on some socks today, and this'll be among our more 238 00:11:02,920 --> 00:11:03,780 ridiculous examples. 239 00:11:03,780 --> 00:11:04,580 >> Now, I need you over here. 240 00:11:04,580 --> 00:11:05,630 I'll do the socks part. 241 00:11:05,630 --> 00:11:08,040 So here we have a little scratch pad. 242 00:11:08,040 --> 00:11:09,940 This is literally just TextEdit in a Mac. 243 00:11:09,940 --> 00:11:12,270 We're not actually going to write a runnable program, but 244 00:11:12,270 --> 00:11:14,220 we're just going to start sketching out pseudocode based 245 00:11:14,220 --> 00:11:17,830 on some of the counsel you provide to me here. 246 00:11:17,830 --> 00:11:20,760 So here is my pile of socks at home. 247 00:11:20,760 --> 00:11:23,320 I have no socks on when I wake up in the morning, and we now 248 00:11:23,320 --> 00:11:26,280 need to write a program, an algorithm of sorts, with which 249 00:11:26,280 --> 00:11:27,900 to get these socks on my feet. 250 00:11:27,900 --> 00:11:30,860 And along the way, let's see if we trip over, or encounter, 251 00:11:30,860 --> 00:11:32,860 some of the ideas that you're going to have to start 252 00:11:32,860 --> 00:11:35,470 thinking about much more seriously when programming 253 00:11:35,470 --> 00:11:37,950 lest your programs don't behave quite as intended. 254 00:11:37,950 --> 00:11:39,580 >> So I sit down here. 255 00:11:39,580 --> 00:11:41,180 I've got my pile of socks. 256 00:11:41,180 --> 00:11:43,780 What's the first thing a reasonable human being would 257 00:11:43,780 --> 00:11:46,600 do when the goal is to put on a pair of socks? 258 00:11:46,600 --> 00:11:49,190 Someone give me one step, and only one step. 259 00:11:49,190 --> 00:11:49,796 Yeah? 260 00:11:49,796 --> 00:11:50,690 AUDIENCE: Bend down. 261 00:11:50,690 --> 00:11:54,290 DAVID MALAN: Bend down, Okay. 262 00:11:54,290 --> 00:11:55,540 Step two. 263 00:11:59,975 --> 00:12:00,645 Step two. 264 00:12:00,645 --> 00:12:01,840 AUDIENCE: Pick up your sock. 265 00:12:01,840 --> 00:12:03,220 DAVID MALAN: Pick up your sock. 266 00:12:03,220 --> 00:12:07,610 Okay, so slight ambiguity here, and this is one of the first 267 00:12:07,610 --> 00:12:09,380 stumbling blocks that we're supposed to deliberately 268 00:12:09,380 --> 00:12:10,290 encounter here. 269 00:12:10,290 --> 00:12:14,110 It's a little ambiguous, so pick up your sock. 270 00:12:14,110 --> 00:12:17,425 Fine, I'll take this one, but a computer, realize in just a 271 00:12:17,425 --> 00:12:20,420 bit, is not going to have that sort of human instinct to just 272 00:12:20,420 --> 00:12:21,490 pick the nearest one. 273 00:12:21,490 --> 00:12:23,590 We're going to have to start, before long, expressing 274 00:12:23,590 --> 00:12:24,610 ourselves more precisely. 275 00:12:24,610 --> 00:12:26,600 >> All right, so step two is pick up your sock. 276 00:12:26,600 --> 00:12:27,520 We'll take it. 277 00:12:27,520 --> 00:12:28,620 Step three. 278 00:12:28,620 --> 00:12:29,532 In the back. 279 00:12:29,532 --> 00:12:30,900 AUDIENCE: Find a matching pair. 280 00:12:30,900 --> 00:12:32,060 DAVID MALAN: Find matching pair. 281 00:12:32,060 --> 00:12:34,740 Okay, so this is good. 282 00:12:34,740 --> 00:12:35,650 I had to choose this sock. 283 00:12:35,650 --> 00:12:39,200 So the goal is to find a matching sock, now, but what 284 00:12:39,200 --> 00:12:39,990 does that mean? 285 00:12:39,990 --> 00:12:42,130 A reasonable human being, much like on Wednesday when I just 286 00:12:42,130 --> 00:12:44,700 knew how to find Mike Smith in a phone book, just kind of 287 00:12:44,700 --> 00:12:45,640 went with their instinct. 288 00:12:45,640 --> 00:12:49,220 But here, it's obviously this sock here, but a computer's 289 00:12:49,220 --> 00:12:51,350 not going to be so instinctive. 290 00:12:51,350 --> 00:12:53,830 A computer is going to have a collection of bits, as we 291 00:12:53,830 --> 00:12:55,380 discussed on Wednesday, and those are 292 00:12:55,380 --> 00:12:56,710 organized somehow in memory. 293 00:12:56,710 --> 00:13:00,400 But the point is that a computer has only the ability 294 00:13:00,400 --> 00:13:03,390 to look at things one at a time, and in fact, even we 295 00:13:03,390 --> 00:13:05,980 humans-- even though it feels like I glanced down and a split 296 00:13:05,980 --> 00:13:08,900 second later I know where the sock is, my brain and my eyes 297 00:13:08,900 --> 00:13:12,000 presumably did a quick skim of those socks, and then latched 298 00:13:12,000 --> 00:13:13,400 on to the one in question. 299 00:13:13,400 --> 00:13:15,780 >> So if we be all the more deliberate now as a computer, 300 00:13:15,780 --> 00:13:17,780 how do I find this matching pair? 301 00:13:17,780 --> 00:13:18,890 Well, we have to iterate. 302 00:13:18,890 --> 00:13:21,440 We have to perhaps loop over this mess of socks on the 303 00:13:21,440 --> 00:13:25,760 floor whereby I say something like FOR EACH sock, pick it 304 00:13:25,760 --> 00:13:30,195 up, AND IF the same shape and size as the other one, THEN 305 00:13:30,195 --> 00:13:32,150 dot, dot, dot, we'll continue the story. 306 00:13:32,150 --> 00:13:34,430 So for each sock, so I pick up this one. 307 00:13:34,430 --> 00:13:36,520 I check is this equal to this one. 308 00:13:36,520 --> 00:13:38,250 It's not, so I put aside side. 309 00:13:38,250 --> 00:13:39,220 Then I iterate again. 310 00:13:39,220 --> 00:13:40,340 Is this one equal? 311 00:13:40,340 --> 00:13:42,010 No, it's not, so I put it aside. 312 00:13:42,010 --> 00:13:42,710 Is this one? 313 00:13:42,710 --> 00:13:43,200 No. 314 00:13:43,200 --> 00:13:43,670 This one? 315 00:13:43,670 --> 00:13:44,520 No, and so forth. 316 00:13:44,520 --> 00:13:48,730 And then finally, hopefully, I will encounter this sock here. 317 00:13:48,730 --> 00:13:51,350 So if it's the same shape, size, take it. 318 00:13:51,350 --> 00:13:53,980 >> And now, what would be our next step here? 319 00:13:53,980 --> 00:13:54,402 Yeah? 320 00:13:54,402 --> 00:13:56,090 AUDIENCE: Identify right from left. 321 00:13:56,090 --> 00:13:58,980 DAVID MALAN: Okay, identify right and left, so 322 00:13:58,980 --> 00:14:01,870 fortunately, that kind of works. 323 00:14:01,870 --> 00:14:04,020 A little symmetric, or I've just worn them that way. 324 00:14:04,020 --> 00:14:07,020 All right, so I've identified the right. 325 00:14:07,020 --> 00:14:09,980 And now, before we proceed, let me point out what Joseph's 326 00:14:09,980 --> 00:14:12,890 been doing here as sort of a versed programmer. 327 00:14:12,890 --> 00:14:15,800 So again, there's no one way of doing this, but beyond just 328 00:14:15,800 --> 00:14:18,480 numbering the lines, Joseph has already started to do this 329 00:14:18,480 --> 00:14:19,460 sort of indentation. 330 00:14:19,460 --> 00:14:21,950 Indeed, this is a very common convention in programming, 331 00:14:21,950 --> 00:14:26,110 whereby when you do something iterative, looping style, as 332 00:14:26,110 --> 00:14:28,480 we're implying with the English phrase "FOR EACH 333 00:14:28,480 --> 00:14:31,090 sock," the convention in pseudocode and, as we'll see, 334 00:14:31,090 --> 00:14:33,410 normal programming languages, is to just indent. 335 00:14:33,410 --> 00:14:36,240 Hit the space bar a few times, hit the Tab key or the like, 336 00:14:36,240 --> 00:14:40,660 so that nested underneath "FOR EACH sock" is the chunk of 337 00:14:40,660 --> 00:14:43,430 stuff that you need to do as a result of that loop. 338 00:14:43,430 --> 00:14:45,940 So that's all that's conveying semantically. 339 00:14:45,940 --> 00:14:48,620 Now meanwhile, the "If it's the same shape and size," the 340 00:14:48,620 --> 00:14:51,590 fact that "Take it" is indented further just means 341 00:14:51,590 --> 00:14:54,370 that's the only thing you should do if that condition, 342 00:14:54,370 --> 00:14:59,770 if that branch, that fork in the road, is in fact true. 343 00:14:59,770 --> 00:15:02,130 >> So now here, we're on step four, identify right and left. 344 00:15:02,130 --> 00:15:03,410 I've identified right. 345 00:15:03,410 --> 00:15:05,150 Give me step five. 346 00:15:05,150 --> 00:15:07,880 And technically, we could really call 347 00:15:07,880 --> 00:15:09,510 the FOR EACH thing-- 348 00:15:09,510 --> 00:15:11,450 you should probably number all the lines if 349 00:15:11,450 --> 00:15:12,190 we're going to do this. 350 00:15:12,190 --> 00:15:14,006 JOSEPH: [INAUDIBLE] 351 00:15:14,006 --> 00:15:14,610 DAVID MALAN: [INAUDIBLE]? 352 00:15:14,610 --> 00:15:16,070 Okay, fine, all right, we'll do it your way. 353 00:15:16,070 --> 00:15:19,590 All right, so step five, how do we do the right sock? 354 00:15:19,590 --> 00:15:22,310 How do we proceed next, here? 355 00:15:22,310 --> 00:15:22,730 Yes? 356 00:15:22,730 --> 00:15:24,300 AUDIENCE: Lift up right leg. 357 00:15:24,300 --> 00:15:27,100 DAVID MALAN: Lift up right leg, Okay. 358 00:15:27,100 --> 00:15:29,330 Step six? 359 00:15:29,330 --> 00:15:30,728 Quickly. 360 00:15:30,728 --> 00:15:31,202 Yeah. 361 00:15:31,202 --> 00:15:33,100 AUDIENCE: Find an open end of the sock. 362 00:15:33,100 --> 00:15:34,440 DAVID MALAN: Okay, find the open end of the sock. 363 00:15:34,440 --> 00:15:34,840 So good. 364 00:15:34,840 --> 00:15:37,180 So here, honestly, a very common instinct would just be 365 00:15:37,180 --> 00:15:39,880 put on right sock, but that too is fairly ambiguous. 366 00:15:39,880 --> 00:15:42,750 Unless the computer or human knows exactly what that means, 367 00:15:42,750 --> 00:15:45,160 it's not going to be to execute that, so here, I've 368 00:15:45,160 --> 00:15:46,520 found the opening of the stock. 369 00:15:46,520 --> 00:15:48,222 Step seven? 370 00:15:48,222 --> 00:15:49,700 AUDIENCE: Touch your toes. 371 00:15:49,700 --> 00:15:51,070 DAVID MALAN: Touch toes. 372 00:15:51,070 --> 00:15:53,546 Okay, so now we go-- 373 00:15:53,546 --> 00:15:56,480 All right, I'm going to take some liberties here. 374 00:15:56,480 --> 00:15:57,110 Thanks. 375 00:15:57,110 --> 00:15:59,210 Step eight? 376 00:15:59,210 --> 00:16:00,060 Put on sock. 377 00:16:00,060 --> 00:16:02,060 Okay, so now I, think we're close enough to sort of take 378 00:16:02,060 --> 00:16:03,480 this one at home. 379 00:16:03,480 --> 00:16:05,980 All right, so this goes up. 380 00:16:05,980 --> 00:16:07,950 I'll take some liberties with what it means, actually. 381 00:16:07,950 --> 00:16:08,920 Put the sock on. 382 00:16:08,920 --> 00:16:10,853 All right, now step nine? 383 00:16:10,853 --> 00:16:11,740 AUDIENCE: Put foot down. 384 00:16:11,740 --> 00:16:12,510 DAVID MALAN: Thank you. 385 00:16:12,510 --> 00:16:15,610 Step nine, put foot down, and now, we can repeat. 386 00:16:15,610 --> 00:16:19,540 So presumably, we can now go into step 10 and say identify 387 00:16:19,540 --> 00:16:21,960 left sock, but that's presumably already done. 388 00:16:21,960 --> 00:16:24,100 >> And so then, I can sort of repeat these steps. 389 00:16:24,100 --> 00:16:25,260 But this sort of begs the question. 390 00:16:25,260 --> 00:16:28,100 Before, the last time we wanted to repeat something, we 391 00:16:28,100 --> 00:16:30,730 did it iteratively, again and again, a FOR 392 00:16:30,730 --> 00:16:32,660 EACH loop so to speak. 393 00:16:32,660 --> 00:16:36,420 Would it make sense to use a loop in order to handle both 394 00:16:36,420 --> 00:16:38,470 the left and the right sock? 395 00:16:38,470 --> 00:16:40,460 Because it feels like these operations are pretty much 396 00:16:40,460 --> 00:16:43,090 identical except for the fact that one starts here and one 397 00:16:43,090 --> 00:16:44,850 starts here? 398 00:16:44,850 --> 00:16:47,400 Do we loop, or do we just keep writing steps 399 00:16:47,400 --> 00:16:48,745 10 and 11 and 12? 400 00:16:48,745 --> 00:16:50,540 AUDIENCE: Loop. 401 00:16:50,540 --> 00:16:52,240 >> DAVID MALAN: Okay, so loop. 402 00:16:52,240 --> 00:16:54,730 I actually might have said just keep going. 403 00:16:54,730 --> 00:16:55,720 So why is this the case? 404 00:16:55,720 --> 00:16:57,940 Well, this is actually the first of our non-obvious 405 00:16:57,940 --> 00:17:01,210 design decisions, and in fact, one of the metrics with which 406 00:17:01,210 --> 00:17:03,990 we'll begin to evaluate, for ourselves and for you, the 407 00:17:03,990 --> 00:17:06,780 quality of a program is just how well designed it is. 408 00:17:06,780 --> 00:17:09,619 Have you done the minimal amount of work necessary to 409 00:17:09,619 --> 00:17:12,480 get the job done the most quickly, either in terms of 410 00:17:12,480 --> 00:17:15,430 your time or in terms of the computer's running time? 411 00:17:15,430 --> 00:17:17,589 How many operations does it takes to execute? 412 00:17:17,589 --> 00:17:20,180 So arguably, this is an opportunity for a loop because 413 00:17:20,180 --> 00:17:22,829 as soon as I start copying and pasting, as Joseph effectively 414 00:17:22,829 --> 00:17:25,010 would start doing in a moment, you're kind of wasting your 415 00:17:25,010 --> 00:17:27,140 time, and you're being doubly expressive. 416 00:17:27,140 --> 00:17:30,260 >> But at the same time, these are really just two special 417 00:17:30,260 --> 00:17:31,890 cases, left and right. 418 00:17:31,890 --> 00:17:34,930 And whereas before, I might have 10 or 20 or more socks in 419 00:17:34,930 --> 00:17:37,960 a pile, it definitely makes sense not to have 50 lines of 420 00:17:37,960 --> 00:17:40,180 code saying check this sock, then the next one, 421 00:17:40,180 --> 00:17:41,300 then the next one. 422 00:17:41,300 --> 00:17:43,450 Here, it's a little less obvious, and I would proposed 423 00:17:43,450 --> 00:17:44,390 that we could go either way. 424 00:17:44,390 --> 00:17:46,720 We could either have that loop, although it only loops 425 00:17:46,720 --> 00:17:50,620 twice, or we could simply copy and paste just a little bit 426 00:17:50,620 --> 00:17:53,270 here in order to get the job done. 427 00:17:53,270 --> 00:17:55,210 But this program is buggy, so to speak. 428 00:17:55,210 --> 00:17:59,120 It might have some mistakes, errors, or corner cases, so to 429 00:17:59,120 --> 00:18:00,860 speak, that we didn't really anticipate. 430 00:18:00,860 --> 00:18:03,400 Nothing went wrong this time, but what could have gone wrong 431 00:18:03,400 --> 00:18:06,100 while executing this program? 432 00:18:06,100 --> 00:18:07,040 AUDIENCE: You don't have any socks. 433 00:18:07,040 --> 00:18:08,980 >> DAVID MALAN: So there could be no socks there whatsoever. 434 00:18:08,980 --> 00:18:10,380 So let's consider that corner case. 435 00:18:10,380 --> 00:18:14,670 So if we could scroll back up to step one, so step one was 436 00:18:14,670 --> 00:18:16,540 bend down, so that checks out. 437 00:18:16,540 --> 00:18:21,250 Step two, pick up your sock, but then find matching sock, 438 00:18:21,250 --> 00:18:22,420 identify right. 439 00:18:22,420 --> 00:18:25,050 We kind of made a whole bunch of assumptions, and this is, 440 00:18:25,050 --> 00:18:27,720 frankly, why program sometimes crash. 441 00:18:27,720 --> 00:18:30,240 If you, the programmer, have made certain assumptions, like 442 00:18:30,240 --> 00:18:33,000 surely there's going to be socks or surely there's going 443 00:18:33,000 --> 00:18:35,310 to be memory left in the computer, surely there's going 444 00:18:35,310 --> 00:18:37,420 to be disk space left on the hard drive-- 445 00:18:37,420 --> 00:18:39,040 Well, if you make these assumptions, and that's not, 446 00:18:39,040 --> 00:18:41,880 in fact, reality, who knows sometimes what the computer's 447 00:18:41,880 --> 00:18:42,520 going to do? 448 00:18:42,520 --> 00:18:44,450 And sometimes, when you get the spinning beach ball or the 449 00:18:44,450 --> 00:18:47,750 frozen Windows or the like, that's precisely because some 450 00:18:47,750 --> 00:18:50,980 programmer did not anticipate those so-called corner cases. 451 00:18:50,980 --> 00:18:53,050 What else could have gone wrong in this program? 452 00:18:53,050 --> 00:18:53,330 Yeah? 453 00:18:53,330 --> 00:18:55,950 >> AUDIENCE: You don't have a right leg and a left leg. 454 00:18:55,950 --> 00:18:58,920 DAVID MALAN: Okay, might not have both a right leg and a 455 00:18:58,920 --> 00:19:01,940 left leg, and so this program might not be universally 456 00:19:01,940 --> 00:19:02,590 applicable. 457 00:19:02,590 --> 00:19:03,658 Others? 458 00:19:03,658 --> 00:19:05,650 AUDIENCE: You might have picked up an orphan sock. 459 00:19:05,650 --> 00:19:07,970 DAVID MALAN: I might have picked up an orphan sock, so a 460 00:19:07,970 --> 00:19:11,250 non-matching sock that just has no siblings because I've 461 00:19:11,250 --> 00:19:13,650 lost it, it's torn, it's in the wash still, or the like. 462 00:19:13,650 --> 00:19:15,550 So that, too, hasn't really been handled. 463 00:19:15,550 --> 00:19:15,790 Yeah? 464 00:19:15,790 --> 00:19:17,510 AUDIENCE: You might already have socks on. 465 00:19:17,510 --> 00:19:19,050 DAVID MALAN: I might already have socks on. 466 00:19:19,050 --> 00:19:20,080 I didn't actually check. 467 00:19:20,080 --> 00:19:23,350 IF you don't have socks on, THEN proceed to do 468 00:19:23,350 --> 00:19:24,330 line one and two. 469 00:19:24,330 --> 00:19:24,980 And that could happen. 470 00:19:24,980 --> 00:19:26,880 You fall asleep with your socks on and the like, so 471 00:19:26,880 --> 00:19:28,860 that, too, a very reasonable corner case. 472 00:19:28,860 --> 00:19:29,630 And maybe one other? 473 00:19:29,630 --> 00:19:31,310 AUDIENCE: The sock is inside out. 474 00:19:31,310 --> 00:19:34,000 DAVID MALAN: So the sock is inside out, so we did no error 475 00:19:34,000 --> 00:19:35,330 checking, in short. 476 00:19:35,330 --> 00:19:38,550 We didn't check if the state of the world is as we expect. 477 00:19:38,550 --> 00:19:41,310 We didn't check if we actually found what we're looking for. 478 00:19:41,310 --> 00:19:43,310 >> And even though this is sort of a ridiculous example 479 00:19:43,310 --> 00:19:46,110 involving socks, at the end of the day, this is exactly the 480 00:19:46,110 --> 00:19:48,740 sort of mindset you need to have while writing programs, 481 00:19:48,740 --> 00:19:51,740 even in Scratch as well as in C, in JavaScript, in PHP, 482 00:19:51,740 --> 00:19:53,920 because otherwise, your programs will exhibit the 483 00:19:53,920 --> 00:19:56,980 equivalent of that spinning beach ball or just yield 484 00:19:56,980 --> 00:19:58,210 inaccurate results. 485 00:19:58,210 --> 00:20:00,250 So many thanks here to our scribe Joseph. 486 00:20:00,250 --> 00:20:05,060 [APPLAUSE] 487 00:20:05,060 --> 00:20:06,850 >> DAVID MALAN: All right, so what is, in 488 00:20:06,850 --> 00:20:08,230 fact, a computer program? 489 00:20:08,230 --> 00:20:09,670 Well, let's take a quick glance at a 490 00:20:09,670 --> 00:20:11,100 representative one here. 491 00:20:11,100 --> 00:20:15,260 So this is a program written in a language called C. C is 492 00:20:15,260 --> 00:20:17,830 fairly old these days, but many newer languages are built 493 00:20:17,830 --> 00:20:18,460 on top of it. 494 00:20:18,460 --> 00:20:21,180 Indeed, PHP, one of the web-centric languages we'll 495 00:20:21,180 --> 00:20:24,050 use toward term's end, itself has what's called an 496 00:20:24,050 --> 00:20:27,550 interpreter, a program that's written in C, but more on that 497 00:20:27,550 --> 00:20:28,690 in many weeks from now. 498 00:20:28,690 --> 00:20:30,840 But this program, and this is what it means to write a 499 00:20:30,840 --> 00:20:32,880 program, albeit a very simple one. 500 00:20:32,880 --> 00:20:35,540 We have some fairly cryptic syntax here, but you can 501 00:20:35,540 --> 00:20:38,340 probably guess, even if you've never programmed before, what 502 00:20:38,340 --> 00:20:39,750 this program does. 503 00:20:39,750 --> 00:20:43,640 Indeed, I don't know what printf is, but print certainly 504 00:20:43,640 --> 00:20:45,700 conjures up the idea of printing something out. 505 00:20:45,700 --> 00:20:48,590 And so yes, this program is ultimately going to print out 506 00:20:48,590 --> 00:20:50,250 the words "Hello, World." 507 00:20:50,250 --> 00:20:53,390 >> Now, whether you have a Mac or a PC or a Linux computer, odds 508 00:20:53,390 --> 00:20:55,550 are, at least if you downloaded some freely 509 00:20:55,550 --> 00:20:58,000 available software, you could have been writing programs on 510 00:20:58,000 --> 00:21:00,310 your own laptop for quite some time now. 511 00:21:00,310 --> 00:21:03,040 On Mac OS, for instance, there's this program called 512 00:21:03,040 --> 00:21:05,340 Terminal that comes with a Mac, that's usually in your 513 00:21:05,340 --> 00:21:07,550 Utilities folder, and it generally opens a black and 514 00:21:07,550 --> 00:21:09,820 white or a white and black window at which you have a 515 00:21:09,820 --> 00:21:11,410 prompt at which you can type commands. 516 00:21:11,410 --> 00:21:13,060 So this is actually reminiscent of what computers 517 00:21:13,060 --> 00:21:15,100 used to be before graphical user 518 00:21:15,100 --> 00:21:16,710 interfaces, GUIs, came along. 519 00:21:16,710 --> 00:21:19,200 Now, in Windows, you have a similar mechanism in the form 520 00:21:19,200 --> 00:21:20,130 of the command prompt. 521 00:21:20,130 --> 00:21:23,430 >> But what I'm going to do here is open up, let's say, 522 00:21:23,430 --> 00:21:26,580 TextEdit again, so the same program we were using for 523 00:21:26,580 --> 00:21:28,870 pseudocode a moment ago, and I'm going to go ahead and 524 00:21:28,870 --> 00:21:30,080 write my first program. 525 00:21:30,080 --> 00:21:34,030 Include stdio.h, whatever that means, int main 526 00:21:34,030 --> 00:21:36,880 void, whatever that means, and then in the middle here, 527 00:21:36,880 --> 00:21:40,560 printf("hello, world."). And then close quote, 528 00:21:40,560 --> 00:21:42,340 close paren, semicolon. 529 00:21:42,340 --> 00:21:44,460 Now I'm going to go ahead and just hit Command-S. I'm going 530 00:21:44,460 --> 00:21:47,650 to go ahead and save this as hello.c, so the convention in 531 00:21:47,650 --> 00:21:50,530 the world of C programming is name the file dot c. 532 00:21:50,530 --> 00:21:52,880 I'm going to just put in John Harvard's Home directory, 533 00:21:52,880 --> 00:21:56,370 here, click Save, and now I'm going to go over to this 534 00:21:56,370 --> 00:21:59,110 terminal window, which again is this black and white prompt 535 00:21:59,110 --> 00:22:00,860 where I can execute commands. 536 00:22:00,860 --> 00:22:04,550 >> I can run programs by typing their name, not by 537 00:22:04,550 --> 00:22:06,710 double-clicking icons in the usual sense. 538 00:22:06,710 --> 00:22:10,590 But the thing is about C is that a language like C first 539 00:22:10,590 --> 00:22:13,150 comes in this form, something called source code. 540 00:22:13,150 --> 00:22:15,400 Something that looks a little like English but is 541 00:22:15,400 --> 00:22:17,980 definitely less like English than Joseph's pseudocode a 542 00:22:17,980 --> 00:22:18,930 moment ago. 543 00:22:18,930 --> 00:22:20,560 It's a little more arcane. 544 00:22:20,560 --> 00:22:22,830 It seems to follow some patterns or rules. 545 00:22:22,830 --> 00:22:25,300 The fact that I have curly braces, semicolons, quotes, 546 00:22:25,300 --> 00:22:28,240 angle braces, feels like a computer came up with this 547 00:22:28,240 --> 00:22:29,250 sort of language. 548 00:22:29,250 --> 00:22:32,010 But if I go, now, to this terminal window, I can run a 549 00:22:32,010 --> 00:22:35,250 command that's going to convert that source code is 550 00:22:35,250 --> 00:22:36,610 something called object code. 551 00:22:36,610 --> 00:22:39,690 That's going to convert English-like syntax to zeros 552 00:22:39,690 --> 00:22:41,760 and ones, the same sorts of zeros and ones we 553 00:22:41,760 --> 00:22:42,820 talked about Wednesday. 554 00:22:42,820 --> 00:22:44,810 Now, I'm going to run a command called Clang. 555 00:22:44,810 --> 00:22:47,750 More on this in the weeks to come, but it's a program with 556 00:22:47,750 --> 00:22:52,330 which I can convert hello.c into a whole bunch 557 00:22:52,330 --> 00:22:53,640 of zeros and ones. 558 00:22:53,640 --> 00:22:54,950 >> Now, I've run this command. 559 00:22:54,950 --> 00:22:57,940 I've run Clang, and then I said run yourself on this file 560 00:22:57,940 --> 00:23:00,630 called hello.c, which I created a moment ago, and 561 00:23:00,630 --> 00:23:02,090 nothing seems to happen. 562 00:23:02,090 --> 00:23:05,550 But indeed, if I poked around my home directory, I would see 563 00:23:05,550 --> 00:23:09,620 that this stupidly named program a.out now exists. 564 00:23:09,620 --> 00:23:12,250 This is just the default name for a program when writing in 565 00:23:12,250 --> 00:23:15,140 C. We can override this eventually, but a.out is the 566 00:23:15,140 --> 00:23:18,340 name of the program I just converted into zeros and ones. 567 00:23:18,340 --> 00:23:21,490 And now that it's zeros and ones, my Mac, in this case, or 568 00:23:21,490 --> 00:23:24,670 your Windows PC, can understand those bits, those 569 00:23:24,670 --> 00:23:25,410 zeros and ones. 570 00:23:25,410 --> 00:23:29,130 And so when I hit Enter, I see "hello, world!" 571 00:23:29,130 --> 00:23:31,270 >> But it's a bit buggy. 572 00:23:31,270 --> 00:23:34,110 I didn't quite say "hello, world!:air:- jharvard." Air is 573 00:23:34,110 --> 00:23:35,020 the name of my computer. 574 00:23:35,020 --> 00:23:37,730 Jharvard's the name of the account, so what did I clearly 575 00:23:37,730 --> 00:23:41,070 omit from the program? 576 00:23:41,070 --> 00:23:42,480 Some kind of line break. 577 00:23:42,480 --> 00:23:45,150 I didn't hit the equivalent of Enter or the carriage return, 578 00:23:45,150 --> 00:23:47,760 and this is, again, testament to the fact that computers can 579 00:23:47,760 --> 00:23:49,800 only do what you tell them to do. 580 00:23:49,800 --> 00:23:52,400 And the fact that I didn't tell the computer move the 581 00:23:52,400 --> 00:23:53,860 cursor to the next line-- 582 00:23:53,860 --> 00:23:55,200 well, it's certainly not going to just do it 583 00:23:55,200 --> 00:23:56,750 presumptuously for me. 584 00:23:56,750 --> 00:24:00,860 So if I go back to my program, and I say \n-- 585 00:24:00,860 --> 00:24:03,180 So \n, as we'll soon see, is the way of 586 00:24:03,180 --> 00:24:06,910 representing weird things like new line characters, things 587 00:24:06,910 --> 00:24:08,840 that would otherwise be the result of 588 00:24:08,840 --> 00:24:09,940 hitting the Enter key. 589 00:24:09,940 --> 00:24:11,730 But for now, just know that hitting the Enter key would 590 00:24:11,730 --> 00:24:14,170 just make our code look odd, so the world decided, you know 591 00:24:14,170 --> 00:24:16,580 what, to keep things prettier, to keep it on one line, let's 592 00:24:16,580 --> 00:24:19,070 just say \n represents a new line. 593 00:24:19,070 --> 00:24:22,260 Let me resave my file, go back to the terminal window, and 594 00:24:22,260 --> 00:24:25,290 re-run a.out, Enter. 595 00:24:25,290 --> 00:24:28,374 Still buggy, but why? 596 00:24:28,374 --> 00:24:29,750 >> AUDIENCE: [INAUDIBLE] 597 00:24:29,750 --> 00:24:31,540 DAVID MALAN: Yeah, so I need to recompile it. 598 00:24:31,540 --> 00:24:34,610 So to compile a program just means convert it from source 599 00:24:34,610 --> 00:24:37,660 code to object code, source code to zeros and ones. 600 00:24:37,660 --> 00:24:40,830 Now, the mere fact that I hit Save in this TextEdit has no 601 00:24:40,830 --> 00:24:43,450 bearing on those zeros and ones because I first need to 602 00:24:43,450 --> 00:24:48,260 tell Clang hey, I've changed those lines of code, the 603 00:24:48,260 --> 00:24:49,040 source code. 604 00:24:49,040 --> 00:24:51,010 You need to regenerate a.out. 605 00:24:51,010 --> 00:24:52,990 Nothing appears to have happened, but in a computer, 606 00:24:52,990 --> 00:24:55,820 at a command line, so to speak, when nothing happens, 607 00:24:55,820 --> 00:24:57,510 that usually means all is well. 608 00:24:57,510 --> 00:24:59,040 When something does happen, it means 609 00:24:59,040 --> 00:25:00,420 you messed up, generally. 610 00:25:00,420 --> 00:25:04,020 >> So let's now go to a.out, and indeed now, I have "hello, 611 00:25:04,020 --> 00:25:04,440 world." 612 00:25:04,440 --> 00:25:05,990 And now, what about these zeros and ones? 613 00:25:05,990 --> 00:25:07,520 Where, in fact, are those? 614 00:25:07,520 --> 00:25:10,710 Well, I can't really just kind of poke around very 615 00:25:10,710 --> 00:25:11,210 effectively. 616 00:25:11,210 --> 00:25:12,650 Let me open up TextEdit. 617 00:25:12,650 --> 00:25:14,060 Here's a.out. 618 00:25:14,060 --> 00:25:17,390 Let me go ahead and open this, and this is apparently what my 619 00:25:17,390 --> 00:25:19,080 program looks like. 620 00:25:19,080 --> 00:25:21,570 So I've opened, not hello.c, but a.out. 621 00:25:21,570 --> 00:25:23,960 But this is actually not what my program really is. 622 00:25:23,960 --> 00:25:26,450 Clearly, this is some kind of alphabetical characters. 623 00:25:26,450 --> 00:25:28,670 I see no zeros and ones, but this is 624 00:25:28,670 --> 00:25:30,780 because a.out is a program. 625 00:25:30,780 --> 00:25:33,540 Zeros and ones-- but TextEdit, as the name suggests, it's 626 00:25:33,540 --> 00:25:37,130 just like Notepad on Windows, is just a text editor, so it's 627 00:25:37,130 --> 00:25:39,730 confusing all of those zeros and ones as 628 00:25:39,730 --> 00:25:41,870 though they were, what? 629 00:25:41,870 --> 00:25:43,380 ASCII characters. 630 00:25:43,380 --> 00:25:45,020 >> So recall on Wednesday, we just came up with this 631 00:25:45,020 --> 00:25:48,740 arbitrary mapping of numbers, or bits, to letters of the 632 00:25:48,740 --> 00:25:51,690 alphabet and punctuation symbols and the like. 633 00:25:51,690 --> 00:25:54,920 So TextEdit, that it's a text editor is misinterpreting 634 00:25:54,920 --> 00:25:57,270 those patterns of zeros and ones that are supposed to be 635 00:25:57,270 --> 00:25:59,990 printing words, like "hello, world." It's displaying them 636 00:25:59,990 --> 00:26:02,150 as ASCII, and that's why it looks a little messy. 637 00:26:02,150 --> 00:26:04,910 Now, there are some hints of correctness in here. 638 00:26:04,910 --> 00:26:08,090 Notice if I highlight, there is a hint of actually "hello, 639 00:26:08,090 --> 00:26:10,810 world," so somewhere in that program is 640 00:26:10,810 --> 00:26:12,120 the sentence I wrote. 641 00:26:12,120 --> 00:26:14,610 But let's go ahead and now see with a different program. 642 00:26:14,610 --> 00:26:17,420 This is not one we'll use that often, but it also comes with 643 00:26:17,420 --> 00:26:19,970 a Mac and will be inside of the CS50 appliance. 644 00:26:19,970 --> 00:26:23,940 Let me go ahead and open with a program called XXD. 645 00:26:23,940 --> 00:26:26,530 >> Back in the day, most programs were named fairly cryptically, 646 00:26:26,530 --> 00:26:28,200 and so the trend continues. 647 00:26:28,200 --> 00:26:31,160 But -b means spit this program out as binary. 648 00:26:31,160 --> 00:26:32,330 Don't run it. 649 00:26:32,330 --> 00:26:35,770 Display it to me as zeros and ones, and this is the C 650 00:26:35,770 --> 00:26:37,190 program we just wrote. 651 00:26:37,190 --> 00:26:39,920 Now, I, as a human here, I honestly have no idea what 652 00:26:39,920 --> 00:26:42,230 these various patterns of zeros and ones represent. 653 00:26:42,230 --> 00:26:45,320 Back in the day, I, with my punch cards or the like, would 654 00:26:45,320 --> 00:26:48,080 actually have to look up what these various patterns of 655 00:26:48,080 --> 00:26:51,560 01111000, actually represent. 656 00:26:51,560 --> 00:26:54,390 Or worse, I would need to do the punching or the creation 657 00:26:54,390 --> 00:26:56,130 of these patterns of zeros and ones. 658 00:26:56,130 --> 00:27:00,410 But for now, take on faith that a CPU, Intel inside, so 659 00:27:00,410 --> 00:27:03,440 to speak, inside of all of our computers these days, knows 660 00:27:03,440 --> 00:27:05,100 how to interpret these zeros and ones. 661 00:27:05,100 --> 00:27:07,010 And some zeros and ones mean print. 662 00:27:07,010 --> 00:27:09,040 Some zeros and ones mean play a sound. 663 00:27:09,040 --> 00:27:13,630 Some zeroes and ones mean take user input from a keyboard. 664 00:27:13,630 --> 00:27:15,960 There's all sorts of different patterns, but we thankfully, 665 00:27:15,960 --> 00:27:19,680 as humans, only generally need to worry about programming at 666 00:27:19,680 --> 00:27:21,270 this fairly higher level. 667 00:27:21,270 --> 00:27:24,320 And in other CS classes can you delve down deeper and look 668 00:27:24,320 --> 00:27:26,820 at things like those zeros and ones, or yet 669 00:27:26,820 --> 00:27:28,000 other things still. 670 00:27:28,000 --> 00:27:29,210 >> So now let's convert this. 671 00:27:29,210 --> 00:27:32,550 Let's move very quickly away from C and move to something a 672 00:27:32,550 --> 00:27:36,390 little more comforting, a little more exciting, in that 673 00:27:36,390 --> 00:27:39,340 we can get back our animations and sounds and the like that 674 00:27:39,340 --> 00:27:40,940 clearly have escaped us in this 675 00:27:40,940 --> 00:27:42,480 fairly primitive interface. 676 00:27:42,480 --> 00:27:46,430 So this same program in C can be represented now in this 677 00:27:46,430 --> 00:27:50,060 programming language called Scratch as follows. 678 00:27:50,060 --> 00:27:54,230 This is the equivalent of this hello world program written in 679 00:27:54,230 --> 00:27:57,370 this puzzle piece style language called Scratch. 680 00:27:57,370 --> 00:28:00,600 So let me go ahead and open up this very program. 681 00:28:00,600 --> 00:28:01,850 It's again called Scratch. 682 00:28:01,850 --> 00:28:03,880 It's freely available, and this is the same thing we 683 00:28:03,880 --> 00:28:05,120 started today on. 684 00:28:05,120 --> 00:28:07,790 >> So this here is Scratch, and it's broken up into a few 685 00:28:07,790 --> 00:28:09,700 different pieces. 686 00:28:09,700 --> 00:28:12,690 On the very top right, we have the so-called stage, and 687 00:28:12,690 --> 00:28:13,670 indeed, that's where the cookies 688 00:28:13,670 --> 00:28:15,440 performed just a bit ago. 689 00:28:15,440 --> 00:28:19,660 And on that stage are things called sprites, characters, or 690 00:28:19,660 --> 00:28:21,030 objects, or entities. 691 00:28:21,030 --> 00:28:23,320 It doesn't really matter how you think of them, but they 692 00:28:23,320 --> 00:28:26,740 are programmable, movable things, and in this case, this 693 00:28:26,740 --> 00:28:30,730 program that our student wrote has a couple of gingerbread 694 00:28:30,730 --> 00:28:32,990 cookies, a couple of circular cookies, a whole bunch of 695 00:28:32,990 --> 00:28:35,430 hearts, a whole bunch of eyeglasses. 696 00:28:38,170 --> 00:28:42,420 Because of this, he or she is able to program each of those 697 00:28:42,420 --> 00:28:44,720 individual characters separately. 698 00:28:44,720 --> 00:28:46,480 >> Now, what does it mean to program these characters? 699 00:28:46,480 --> 00:28:49,440 Well, let me go ahead and click on this left hand cookie 700 00:28:49,440 --> 00:28:51,830 and scroll over to the top left here. 701 00:28:51,830 --> 00:28:53,670 In the top left of my screen now is the 702 00:28:53,670 --> 00:28:55,280 so-called scripts area. 703 00:28:55,280 --> 00:28:58,790 This is sort of a blank slate, initially, onto which I can 704 00:28:58,790 --> 00:29:01,540 drag and drop puzzle pieces that, frankly, do 705 00:29:01,540 --> 00:29:03,020 exactly what they say. 706 00:29:03,020 --> 00:29:05,820 At the very top of this stack of puzzle pieces is the word 707 00:29:05,820 --> 00:29:09,340 When Green Flag Clicked, and if you didn't notice before, 708 00:29:09,340 --> 00:29:12,300 the way I started that cookie song was clicking, literally, 709 00:29:12,300 --> 00:29:13,320 a green flag. 710 00:29:13,320 --> 00:29:16,590 So that puzzle piece at top left there means when the 711 00:29:16,590 --> 00:29:19,780 human clicks the green flag, proceeded to do 712 00:29:19,780 --> 00:29:21,110 the following things. 713 00:29:21,110 --> 00:29:23,330 >> Now, what did they cookie proceed to do? 714 00:29:23,330 --> 00:29:25,810 I don't really know how to interpret this yet, but the 715 00:29:25,810 --> 00:29:29,220 cookie apparently set its groove to zero, then it waited 716 00:29:29,220 --> 00:29:31,740 three seconds, then it changed its group to one, then it 717 00:29:31,740 --> 00:29:33,590 waited a second, then it changed its 718 00:29:33,590 --> 00:29:35,100 groove back to one. 719 00:29:35,100 --> 00:29:37,373 And then this actually looks like a bit of a bug, shouldn't 720 00:29:37,373 --> 00:29:39,440 have to change its groove again and again unless it's 721 00:29:39,440 --> 00:29:43,380 being changed elsewhere, but this series of steps is what's 722 00:29:43,380 --> 00:29:46,800 dictating the behavior of this particular cookie. 723 00:29:46,800 --> 00:29:49,180 So let's actually scroll back and not look at something 724 00:29:49,180 --> 00:29:50,430 quite so complex yet. 725 00:29:50,430 --> 00:29:52,260 Let me go ahead and go to File, New, 726 00:29:52,260 --> 00:29:53,560 and get a clean slate. 727 00:29:53,560 --> 00:29:56,590 >> So now, I indeed have an empty script area, an empty stage, 728 00:29:56,590 --> 00:29:59,380 with our default sprite Scratch, and at the top left 729 00:29:59,380 --> 00:30:01,670 on my screen do I have the pallet of all of their 730 00:30:01,670 --> 00:30:03,400 available puzzle pieces. 731 00:30:03,400 --> 00:30:06,300 And we won't go through nearly that many of these things 732 00:30:06,300 --> 00:30:09,470 today because, again, most are self-explanatory, but we will 733 00:30:09,470 --> 00:30:12,370 try to categorize them and point out the similarities 734 00:30:12,370 --> 00:30:15,380 with these future languages to which we will dive. 735 00:30:15,380 --> 00:30:19,180 And at top left here is the first When Green Flag Clicked, 736 00:30:19,180 --> 00:30:22,170 so let me drag this over here, zoom out a bit. 737 00:30:22,170 --> 00:30:25,780 And if I click the green flag, nothing really happens because 738 00:30:25,780 --> 00:30:29,360 I haven't attached any logic, any statement so to speak, to 739 00:30:29,360 --> 00:30:31,040 that green flag, so let me go up to the 740 00:30:31,040 --> 00:30:32,500 categories over here. 741 00:30:32,500 --> 00:30:34,840 I'm currently in the Control category. 742 00:30:34,840 --> 00:30:37,630 I'm instead going to go down to the Looks category, and 743 00:30:37,630 --> 00:30:39,970 there's a whole bunch of things here that say Say, 744 00:30:39,970 --> 00:30:43,110 Think, Change Color, Switch Costume. 745 00:30:43,110 --> 00:30:44,860 >> So you can do silly things with costumes and 746 00:30:44,860 --> 00:30:45,870 sounds and the like. 747 00:30:45,870 --> 00:30:49,970 Let me go ahead and just say Say, and now notice as I drag 748 00:30:49,970 --> 00:30:52,940 and drop this puzzle piece, it's going to want to latch 749 00:30:52,940 --> 00:30:55,010 into the corresponding shape. 750 00:30:55,010 --> 00:30:58,020 So when I go ahead and let go of my mouse, they lock 751 00:30:58,020 --> 00:31:01,770 together, and now if I go over here and click the green flag, 752 00:31:01,770 --> 00:31:05,910 the cat does in fact say hello because that's what is inside 753 00:31:05,910 --> 00:31:06,980 of this white box. 754 00:31:06,980 --> 00:31:08,900 We'll soon see that this white box is what's called an 755 00:31:08,900 --> 00:31:10,250 argument, or a parameter. 756 00:31:10,250 --> 00:31:12,560 It's a way of changing the behavior of, in this case, a 757 00:31:12,560 --> 00:31:15,070 puzzle piece, but if I want to say exactly what I said 758 00:31:15,070 --> 00:31:18,610 before, say hello, world, I can now go back over here, 759 00:31:18,610 --> 00:31:21,990 click play, and "hello, world" is what's said. 760 00:31:21,990 --> 00:31:24,340 >> So we are literally programming now. 761 00:31:24,340 --> 00:31:27,540 It's not all that compelling of a program, but at least 762 00:31:27,540 --> 00:31:30,140 it's a little more compelling than something that looks, at 763 00:31:30,140 --> 00:31:31,300 first glance, like this. 764 00:31:31,300 --> 00:31:34,190 And we can very quickly get all the more expressive 765 00:31:34,190 --> 00:31:36,600 because in Scratch, like in other languages, there's all 766 00:31:36,600 --> 00:31:40,010 sorts of statements, not just Say or printing something, but 767 00:31:40,010 --> 00:31:42,650 you can do things like waiting, as we just saw with 768 00:31:42,650 --> 00:31:44,240 the cookie, some number of seconds. 769 00:31:44,240 --> 00:31:46,440 You can play sounds in the environment of Scratch just 770 00:31:46,440 --> 00:31:49,050 like you can in a normal computer program play sound. 771 00:31:49,050 --> 00:31:52,440 You can check what are called Boolean expressions. 772 00:31:52,440 --> 00:31:55,630 So now, let's start to add to our toolkit some terminology 773 00:31:55,630 --> 00:31:58,840 that actually relates to the example that Joseph and I did 774 00:31:58,840 --> 00:32:00,080 here with the socks. 775 00:32:00,080 --> 00:32:03,040 So statements are just statements of fact. 776 00:32:03,040 --> 00:32:04,160 Do this. 777 00:32:04,160 --> 00:32:07,370 A directive for the sprite, or me the human, to do something. 778 00:32:07,370 --> 00:32:10,420 A Boolean expression is something that has a value, a 779 00:32:10,420 --> 00:32:14,580 so-called truth value, that's either a zero or one, false or 780 00:32:14,580 --> 00:32:17,890 true, off or on, no or yes. 781 00:32:17,890 --> 00:32:19,730 Doesn't really matter how you think of this, but it's a 782 00:32:19,730 --> 00:32:20,700 binary state. 783 00:32:20,700 --> 00:32:22,610 As Nate discussed in Wednesday's video, two 784 00:32:22,610 --> 00:32:23,570 different things. 785 00:32:23,570 --> 00:32:25,740 >> So in Scratch, Boolean expressions happen to look 786 00:32:25,740 --> 00:32:28,910 like these blue objects here, and in this case, the question 787 00:32:28,910 --> 00:32:30,960 mark implies that you're asking a question. 788 00:32:30,960 --> 00:32:34,250 Is the cat, or the sprite, touching the mouse pointer? 789 00:32:34,250 --> 00:32:36,580 So this is just one example of a Scratch block that's going 790 00:32:36,580 --> 00:32:39,950 to allow us to check yes or no, is the mouse touching the 791 00:32:39,950 --> 00:32:40,790 sprite on the screen? 792 00:32:40,790 --> 00:32:42,840 And this can be useful if you actually want to do things 793 00:32:42,840 --> 00:32:43,820 with your mouse. 794 00:32:43,820 --> 00:32:46,470 In addition to Boolean expressions, we have things 795 00:32:46,470 --> 00:32:49,110 like is the mouse down, so you can detect that kind of 796 00:32:49,110 --> 00:32:50,200 question as well. 797 00:32:50,200 --> 00:32:52,590 We can do mathematics if you actually want, and there's 798 00:32:52,590 --> 00:32:55,170 actually more compelling uses for this than just pure of 799 00:32:55,170 --> 00:32:56,400 arithmetic, as we'll see. 800 00:32:56,400 --> 00:32:59,690 Pseudo randomness and making your program appear to think 801 00:32:59,690 --> 00:33:03,490 or behave differently based on some seemingly random values, 802 00:33:03,490 --> 00:33:06,610 and then we have things like Boolean expressions like AND. 803 00:33:06,610 --> 00:33:08,830 So if you actually want to check two values, we'll see in 804 00:33:08,830 --> 00:33:11,110 Scratch that we can actually test if this is 805 00:33:11,110 --> 00:33:13,190 true and this is true. 806 00:33:13,190 --> 00:33:15,300 For instance, in the case of my socks, I could've at the 807 00:33:15,300 --> 00:33:19,045 very end asked the question if left sock is on and right sock 808 00:33:19,045 --> 00:33:20,610 is on, quit. 809 00:33:20,610 --> 00:33:22,710 You're all done for the day, so that would be an 810 00:33:22,710 --> 00:33:24,300 opportunity for that. 811 00:33:24,300 --> 00:33:27,740 >> So let's go ahead and try to piece some of these together 812 00:33:27,740 --> 00:33:31,570 and go into a couple of examples more 813 00:33:31,570 --> 00:33:32,610 compelling than this one. 814 00:33:32,610 --> 00:33:35,770 So let me go ahead here and open up some of the examples 815 00:33:35,770 --> 00:33:38,350 that will always be on the course's website as well, and 816 00:33:38,350 --> 00:33:39,870 open up hello2. 817 00:33:39,870 --> 00:33:42,910 So in hello2 here, we have a program that's doing a few 818 00:33:42,910 --> 00:33:46,290 things, but it's not doing it as effectively as we might. 819 00:33:46,290 --> 00:33:48,900 So here it says "hello, world" for one second and then waits 820 00:33:48,900 --> 00:33:49,450 for a second. 821 00:33:49,450 --> 00:33:51,720 And then does it again, and then does it again. 822 00:33:51,720 --> 00:33:55,530 So if I click the green flag, Scratch says "Hello, world. 823 00:33:55,530 --> 00:33:57,160 Hello, world. 824 00:33:57,160 --> 00:34:00,080 Hello, world." And this is obviously candidate now for 825 00:34:00,080 --> 00:34:00,860 improvement. 826 00:34:00,860 --> 00:34:03,070 >> What's the marginal improvement, hopefully, we can 827 00:34:03,070 --> 00:34:07,610 now make if Scratch supports the concept? 828 00:34:07,610 --> 00:34:08,310 Some kind of loop. 829 00:34:08,310 --> 00:34:10,960 Some kind of repetition, now, would be nice, so let me 830 00:34:10,960 --> 00:34:11,920 actually try that. 831 00:34:11,920 --> 00:34:13,870 Let me actually go and move this. 832 00:34:13,870 --> 00:34:16,449 So notice can detach blocks as easily as you can add to 833 00:34:16,449 --> 00:34:17,420 attach them. 834 00:34:17,420 --> 00:34:20,730 Let me go under Control, scroll down here, and indeed, 835 00:34:20,730 --> 00:34:24,000 there's this puzzle piece here, Repeat and Forever and 836 00:34:24,000 --> 00:34:24,870 Forever If. 837 00:34:24,870 --> 00:34:28,500 So there's a number of ways of expressing looping constructs 838 00:34:28,500 --> 00:34:29,340 in Scratch. 839 00:34:29,340 --> 00:34:31,730 The one I probably want here is not Forever because I only 840 00:34:31,730 --> 00:34:34,639 want this to happen three times, but probably Repeat. 841 00:34:34,639 --> 00:34:38,449 So let me drag Repeat over here, drag and drop it, and 842 00:34:38,449 --> 00:34:42,530 now instead of saying "hello, world" three separate times, 843 00:34:42,530 --> 00:34:44,230 let me drag this puzzle piece in here. 844 00:34:44,230 --> 00:34:46,610 And even though it doesn't seem to fit, the program is smart 845 00:34:46,610 --> 00:34:48,920 enough to realize it will grow to fill, so it's the 846 00:34:48,920 --> 00:34:51,270 shapes that matter and not the absolute size. 847 00:34:51,270 --> 00:34:54,030 Let me change the repetition to three, and now let me go 848 00:34:54,030 --> 00:34:56,560 ahead and drag Wait One Second in there as well. 849 00:34:56,560 --> 00:34:59,220 It's going to snap in as well, and so now I'm going to drag 850 00:34:59,220 --> 00:35:01,370 these guys over here and just throw them away because I 851 00:35:01,370 --> 00:35:02,710 don't need them anymore. 852 00:35:02,710 --> 00:35:05,920 Let me zoom out and click the green flag now, and we have 853 00:35:05,920 --> 00:35:09,600 the same program but, as I predicted before, better 854 00:35:09,600 --> 00:35:12,200 designed because you can imagine how bad this program 855 00:35:12,200 --> 00:35:14,410 would get, certainly aesthetically, if you had to 856 00:35:14,410 --> 00:35:16,640 start copying and pasting, copying and pasting, or 857 00:35:16,640 --> 00:35:20,020 dragging and dropping the same darn things again and again. 858 00:35:20,020 --> 00:35:22,260 >> Now, simply saying stuff on the screen, printing to the 859 00:35:22,260 --> 00:35:25,350 screen, really all not that exciting, so let's open a 860 00:35:25,350 --> 00:35:26,580 third variance here. 861 00:35:26,580 --> 00:35:29,810 And now, as you'll see, this'll quickly get annoying-- 862 00:35:29,810 --> 00:35:30,850 [MEOW] 863 00:35:30,850 --> 00:35:31,940 DAVID MALAN: --but it's also kind of cute. 864 00:35:31,940 --> 00:35:35,244 [MEOW] 865 00:35:35,244 --> 00:35:37,460 DAVID MALAN: Okay, so better, and we can certainly use that 866 00:35:37,460 --> 00:35:41,060 same transition of chunking this up into a looping 867 00:35:41,060 --> 00:35:43,520 structure, but let's make it more interesting still. 868 00:35:43,520 --> 00:35:47,480 >> Let me go ahead and open up a fourth variance here, where I 869 00:35:47,480 --> 00:35:50,020 take things one step further. 870 00:35:50,020 --> 00:35:54,450 So according to this, silly though this is at first 871 00:35:54,450 --> 00:35:58,530 glance, what is this program going to do? 872 00:35:58,530 --> 00:35:59,790 It's going to meow once. 873 00:35:59,790 --> 00:36:00,160 Why? 874 00:36:00,160 --> 00:36:03,340 Well, one is, as far as I know, always less than two. 875 00:36:03,340 --> 00:36:04,890 There's no notion of randomness here. 876 00:36:04,890 --> 00:36:07,240 I've literally hard-coded one and two, but this is an 877 00:36:07,240 --> 00:36:10,370 example now of actually using a Boolean expression. 878 00:36:10,370 --> 00:36:13,650 Much like as Joseph did in his pseudocode, the indentation IF 879 00:36:13,650 --> 00:36:17,170 you find matching socks THEN do the following, here we have 880 00:36:17,170 --> 00:36:19,750 an expression IF one is less than two THEN-- 881 00:36:19,750 --> 00:36:21,820 and in fact we even have a little bit of indentation, 882 00:36:21,820 --> 00:36:24,480 where the purple is slightly indented to the right-- 883 00:36:24,480 --> 00:36:26,600 THEN you're going to play the sound meow. 884 00:36:26,600 --> 00:36:30,060 Now, in this case, that one is always less than two, so this 885 00:36:30,060 --> 00:36:31,970 is kind of a waste of a condition. 886 00:36:31,970 --> 00:36:34,890 But we'll be able, as we'll see, to plug other things into 887 00:36:34,890 --> 00:36:37,420 these placeholders where one and two now are. 888 00:36:37,420 --> 00:36:41,340 So let's now advance to example five of these several 889 00:36:41,340 --> 00:36:45,850 hellos and look at what this program's going to do. 890 00:36:45,850 --> 00:36:48,710 >> So now, in an English sentence, how does this 891 00:36:48,710 --> 00:36:51,460 program behave? 892 00:36:51,460 --> 00:36:52,950 AUDIENCE: Meows half the time. 893 00:36:52,950 --> 00:36:55,380 DAVID MALAN: Meow's half of the time, so this is a way of 894 00:36:55,380 --> 00:36:56,700 conveying a very simple idea. 895 00:36:56,700 --> 00:36:59,400 Even though we happen to be using some inequalities here 896 00:36:59,400 --> 00:37:01,860 in some numbers, this is really just a programmatical 897 00:37:01,860 --> 00:37:09,490 way, a precise way, of saying if the coin comes up heads, go 898 00:37:09,490 --> 00:37:10,270 ahead and meow. 899 00:37:10,270 --> 00:37:13,540 Or conversely, if the coin comes up tails, don't meow. 900 00:37:13,540 --> 00:37:15,110 And in this case, how do we express that? 901 00:37:15,110 --> 00:37:17,950 We'll pick a random number from 1 to 10, and if that 902 00:37:17,950 --> 00:37:21,110 number is less than 6, go ahead and meow. 903 00:37:21,110 --> 00:37:22,260 And how did this get in here? 904 00:37:22,260 --> 00:37:24,620 Well again, notice just the dragging and dropping and 905 00:37:24,620 --> 00:37:26,190 things latches into place. 906 00:37:26,190 --> 00:37:28,020 >> So now let's see if this randomness works. 907 00:37:28,020 --> 00:37:29,845 Let me go ahead and click the green flag. 908 00:37:29,845 --> 00:37:31,120 [MEOW] 909 00:37:31,120 --> 00:37:32,000 DAVID MALAN: Okay. 910 00:37:32,000 --> 00:37:33,430 [MEOW] 911 00:37:33,430 --> 00:37:35,260 DAVID MALAN: Okay. 912 00:37:35,260 --> 00:37:38,510 Okay, good, so we got heads, heads, tails effectively. 913 00:37:38,510 --> 00:37:39,295 Tails. 914 00:37:39,295 --> 00:37:39,590 [MEOW] 915 00:37:39,590 --> 00:37:41,829 DAVID MALAN: Heads. 916 00:37:41,829 --> 00:37:43,220 [MEOW] 917 00:37:43,220 --> 00:37:44,040 DAVID MALAN: Excellent. 918 00:37:44,040 --> 00:37:46,220 It's always awkward when just statistically you get a bad 919 00:37:46,220 --> 00:37:48,510 run, and it's all heads, and the program actually doesn't 920 00:37:48,510 --> 00:37:49,230 work as you'd hope. 921 00:37:49,230 --> 00:37:51,210 But this time, it worked, and we seem to have, if we did 922 00:37:51,210 --> 00:37:53,830 this an infinite number of times, 50% odds. 923 00:37:53,830 --> 00:37:56,950 Now again, not all that interesting, just making cats 924 00:37:56,950 --> 00:37:59,840 meow, so let's see if we can't advance this a bit further 925 00:37:59,840 --> 00:38:01,820 here in version six. 926 00:38:01,820 --> 00:38:05,620 So now, we have really annoying version-- 927 00:38:05,620 --> 00:38:08,215 [MEOWS EVERY FEW SECONDS] 928 00:38:08,215 --> 00:38:10,640 DAVID MALAN: --and this is what's known, general, as an 929 00:38:10,640 --> 00:38:11,670 infinite loop. 930 00:38:11,670 --> 00:38:14,190 >> So infinite loop in this case feels bad. 931 00:38:14,190 --> 00:38:16,970 It's definitely going to start sounding bad, and yet infinite 932 00:38:16,970 --> 00:38:18,740 loops aren't always bad. 933 00:38:18,740 --> 00:38:20,910 Can you think of context in computer programs where you'd 934 00:38:20,910 --> 00:38:22,910 actually want an infinite loop? 935 00:38:22,910 --> 00:38:23,691 Yeah? 936 00:38:23,691 --> 00:38:26,520 AUDIENCE: When you want to check a condition. 937 00:38:26,520 --> 00:38:28,240 DAVID MALAN: Okay, when you want to keep checking a condition? 938 00:38:28,240 --> 00:38:29,190 Like what? 939 00:38:29,190 --> 00:38:30,440 AUDIENCE: [INAUDIBLE] 940 00:38:36,060 --> 00:38:38,170 DAVID MALAN: Okay, good, so if you had some program, some 941 00:38:38,170 --> 00:38:40,440 kind of home automation thing, where you want to constantly 942 00:38:40,440 --> 00:38:41,980 monitor is something the case. 943 00:38:41,980 --> 00:38:42,850 Are the lights on? 944 00:38:42,850 --> 00:38:45,020 Are the lights on because maybe you have a timer, and 945 00:38:45,020 --> 00:38:47,360 you want them to go off, you might need to do something 946 00:38:47,360 --> 00:38:48,290 again and again. 947 00:38:48,290 --> 00:38:50,540 And in fact, speaking of timers, any of you who have 948 00:38:50,540 --> 00:38:53,100 clocks on your computer or digital watches, that's an 949 00:38:53,100 --> 00:38:54,210 infinite loop. 950 00:38:54,210 --> 00:38:56,980 It continues to update the time because it's constantly 951 00:38:56,980 --> 00:38:59,750 checking and checking and checking has the time changed, 952 00:38:59,750 --> 00:39:01,000 and if so, oh, my god. 953 00:39:03,670 --> 00:39:04,740 The clock's finally changed. 954 00:39:04,740 --> 00:39:07,020 It needs display that value to you. 955 00:39:07,020 --> 00:39:10,880 So whereas most of the time infinite loops are a mistake, 956 00:39:10,880 --> 00:39:13,950 or at least a poor design decision, sometimes they do 957 00:39:13,950 --> 00:39:14,785 have their value. 958 00:39:14,785 --> 00:39:18,170 >> Well, let's advance further here to hello7. 959 00:39:18,170 --> 00:39:20,410 So now the program will get a little more interactive. 960 00:39:20,410 --> 00:39:23,120 Let me zoom in here, and again, this is what's nice 961 00:39:23,120 --> 00:39:23,640 about Scratch. 962 00:39:23,640 --> 00:39:26,760 And we'll use it to be clear, Scratch, just today and into 963 00:39:26,760 --> 00:39:27,780 next week's problem set. 964 00:39:27,780 --> 00:39:31,920 But on Monday, we dive into C. In this program here, it does, 965 00:39:31,920 --> 00:39:33,190 forever, the following. 966 00:39:33,190 --> 00:39:35,890 IF touching the mouse pointer-- now, who's the 967 00:39:35,890 --> 00:39:36,420 context here? 968 00:39:36,420 --> 00:39:39,670 Well, and notice that who's selected down here at bottom 969 00:39:39,670 --> 00:39:43,060 right is the cat, sprite one, so these scripts, this 970 00:39:43,060 --> 00:39:45,720 program, applies to him specifically. 971 00:39:45,720 --> 00:39:48,950 So if that cat is touching the mouse pointer, then it's going 972 00:39:48,950 --> 00:39:51,350 to play this down and wait two seconds, and 973 00:39:51,350 --> 00:39:53,030 then repeat ad nauseum. 974 00:39:53,030 --> 00:39:55,360 >> So let's go ahead and hit play. 975 00:39:55,360 --> 00:39:57,580 Nothing happens, but if I want to pet the 976 00:39:57,580 --> 00:39:59,310 cat now, I can simply. 977 00:39:59,310 --> 00:40:01,130 [MEOW] 978 00:40:01,130 --> 00:40:02,244 DAVID MALAN: Adorable. 979 00:40:02,244 --> 00:40:05,956 [MEOW] 980 00:40:05,956 --> 00:40:08,750 DAVID MALAN: Okay, less annoying, but also gets dull, 981 00:40:08,750 --> 00:40:11,480 so let's move on and see if we can't inject 982 00:40:11,480 --> 00:40:12,460 a little more logic. 983 00:40:12,460 --> 00:40:14,300 That was example seven. 984 00:40:14,300 --> 00:40:16,090 >> Here in example eight, we're going to 985 00:40:16,090 --> 00:40:18,670 introduce an ELSE condition. 986 00:40:18,670 --> 00:40:21,440 So much like a literal fork in the road, in which you can go 987 00:40:21,440 --> 00:40:24,260 left or you can go right, a condition in a programming 988 00:40:24,260 --> 00:40:28,120 language like Scratch, or as we'll see C, can allow you to 989 00:40:28,120 --> 00:40:32,230 go in one direction or another via an IF ELSE construct. 990 00:40:32,230 --> 00:40:35,160 So quite literally, IF touching mouse pointer, this 991 00:40:35,160 --> 00:40:38,680 will play some sound, ELSE it's going to play this other 992 00:40:38,680 --> 00:40:39,800 sound, meow. 993 00:40:39,800 --> 00:40:42,510 Now, if you can infer from the name of these sounds, you can 994 00:40:42,510 --> 00:40:45,310 probably guess what this program's meant to conjure up 995 00:40:45,310 --> 00:40:46,250 the idea of. 996 00:40:46,250 --> 00:40:48,742 This cat is meowing happily. 997 00:40:48,742 --> 00:40:49,230 [MEOW] 998 00:40:49,230 --> 00:40:51,410 DAVID MALAN: Happily, but doesn't 999 00:40:51,410 --> 00:40:52,926 quite like to be touched. 1000 00:40:52,926 --> 00:40:54,550 [ROARS] 1001 00:40:54,550 --> 00:40:57,500 DAVID MALAN: So now we have a cat who will yell at you. 1002 00:40:57,500 --> 00:41:00,570 >> All right, well, one last example with cats here, and 1003 00:41:00,570 --> 00:41:02,920 let's open version nine of this here. 1004 00:41:02,920 --> 00:41:06,190 So now, we have the next most annoying sound that I could 1005 00:41:06,190 --> 00:41:11,630 find, so we have a walrus or sea lion here who's going to 1006 00:41:11,630 --> 00:41:13,222 do the following. 1007 00:41:13,222 --> 00:41:20,870 [SEAL BARK] 1008 00:41:20,870 --> 00:41:23,310 DAVID MALAN: Okay, so this will go on until you figure out how 1009 00:41:23,310 --> 00:41:24,640 this program works. 1010 00:41:24,640 --> 00:41:28,560 So this time, this animal has two scripts, and what's 1011 00:41:28,560 --> 00:41:31,050 interesting here is that these scripts are going to execute 1012 00:41:31,050 --> 00:41:32,260 in parallel. 1013 00:41:32,260 --> 00:41:35,340 So because they both start with one green flag clicked, 1014 00:41:35,340 --> 00:41:38,080 it's like going like this, and both programs start running it 1015 00:41:38,080 --> 00:41:41,370 once even if they're looping forever. 1016 00:41:41,370 --> 00:41:44,420 So in the top script, I have some logic. 1017 00:41:44,420 --> 00:41:49,275 What features does that provide up there? 1018 00:41:49,275 --> 00:41:50,740 [SEAL BARK] 1019 00:41:50,740 --> 00:41:51,670 AUDIENCE: [INAUDIBLE] 1020 00:41:51,670 --> 00:41:53,295 DAVID MALAN: If it's what? 1021 00:41:53,295 --> 00:41:53,760 [SEAL BARK] 1022 00:41:53,760 --> 00:41:55,852 AUDIENCE: If muted to zero, it's going to keep playing the 1023 00:41:55,852 --> 00:41:58,410 sound. [SEAL BARK] 1024 00:41:58,410 --> 00:41:58,940 DAVID MALAN: Okay, good. 1025 00:41:58,940 --> 00:42:01,960 So IF muted, whatever this is, this orange thing is zero, 1026 00:42:01,960 --> 00:42:05,370 THEN play the sea lion sound and think "Oh, 1027 00:42:05,370 --> 00:42:06,770 hi," for two seconds. 1028 00:42:06,770 --> 00:42:09,380 Now, I don't know what muted is, but zero conjures up the 1029 00:42:09,380 --> 00:42:11,230 idea of false or off. 1030 00:42:11,230 --> 00:42:14,490 So if muted is false, so if not muted, 1031 00:42:14,490 --> 00:42:15,660 keep playing the sound. 1032 00:42:15,660 --> 00:42:17,350 All right, well, how do we disable this thing? 1033 00:42:17,350 --> 00:42:19,310 >> Well, let's look at the second script down there. 1034 00:42:19,310 --> 00:42:22,130 The second script says set muted to zero. 1035 00:42:22,130 --> 00:42:25,180 Notice it's also orange, so what Scratch does is it colors 1036 00:42:25,180 --> 00:42:27,400 blocks in the same shade if it's sort 1037 00:42:27,400 --> 00:42:28,850 of logically related. 1038 00:42:28,850 --> 00:42:32,850 So just as muted up top was orange, so is muted down here 1039 00:42:32,850 --> 00:42:34,060 mentioned in orange block. 1040 00:42:34,060 --> 00:42:36,170 But this is a variable assignments, so just like in 1041 00:42:36,170 --> 00:42:39,170 algebra, you have x and y and z, in programming you have 1042 00:42:39,170 --> 00:42:41,440 variables, but they're generally-- 1043 00:42:41,440 --> 00:42:42,580 let's pause for a moment and figure how 1044 00:42:42,580 --> 00:42:44,280 to stop this barking. 1045 00:42:44,280 --> 00:42:45,480 How do I do this? 1046 00:42:45,480 --> 00:42:46,480 [SEAL BARK] 1047 00:42:46,480 --> 00:42:47,920 DAVID MALAN: Okay. 1048 00:42:47,920 --> 00:42:49,490 It stopped. Okay. 1049 00:42:49,490 --> 00:42:52,230 >> So just as in algebra you have variables x, y, and z, but in 1050 00:42:52,230 --> 00:42:55,260 programming, having variables like x, y, and z is generally 1051 00:42:55,260 --> 00:42:57,330 frowned upon because they're not at all expressive. 1052 00:42:57,330 --> 00:43:00,070 They have no semantic meaning whatsoever, so in most 1053 00:43:00,070 --> 00:43:02,110 programming languages, variables can have full 1054 00:43:02,110 --> 00:43:05,270 fledged names or words or phrases, like muted, to say 1055 00:43:05,270 --> 00:43:06,030 what they do. 1056 00:43:06,030 --> 00:43:09,380 So this second script also was listening forever, and it said 1057 00:43:09,380 --> 00:43:12,100 if the key, the keyboard key Space, is 1058 00:43:12,100 --> 00:43:13,370 pressed, question mark. 1059 00:43:13,370 --> 00:43:16,300 So there's a condition with a Boolean expression that's 1060 00:43:16,300 --> 00:43:19,010 going to answer a question either truthfully or false, 1061 00:43:19,010 --> 00:43:22,880 then I have inside of it IF the space key is pressed AND 1062 00:43:22,880 --> 00:43:28,020 IF muted is zero, set muted to one, ELSE set muted to zero. 1063 00:43:28,020 --> 00:43:30,510 So this other fork in the road, and notice how I've 1064 00:43:30,510 --> 00:43:33,500 nested the two IF conditions, is a way of checking is the 1065 00:43:33,500 --> 00:43:36,610 Space Bar pressed because if so, I either want to go this 1066 00:43:36,610 --> 00:43:38,000 way or that way. 1067 00:43:38,000 --> 00:43:40,210 And how do I invert the value of muted? 1068 00:43:40,210 --> 00:43:41,410 I have to check is it zero? 1069 00:43:41,410 --> 00:43:45,890 If so, make it one, else make it zero to therefore toggle 1070 00:43:45,890 --> 00:43:47,670 its two states. 1071 00:43:47,670 --> 00:43:50,030 >> All right, so we have then some of these fundamental 1072 00:43:50,030 --> 00:43:50,540 constructs. 1073 00:43:50,540 --> 00:43:52,810 We have Boolean expressions, and realize, too, these are 1074 00:43:52,810 --> 00:43:54,050 not all that unfamiliar. 1075 00:43:54,050 --> 00:43:56,730 In fact, here's a quick screenshot of Harvard course's CS50 1076 00:43:56,730 --> 00:43:59,740 shopping tool, and any website out there that has checkboxes 1077 00:43:59,740 --> 00:44:02,420 and drop downs have really, all this time, been using 1078 00:44:02,420 --> 00:44:03,430 Boolean expressions. 1079 00:44:03,430 --> 00:44:05,760 In this case here, if you click the checkbox next to 1080 00:44:05,760 --> 00:44:08,620 course greater than or equal to 4.5, or the same next to 1081 00:44:08,620 --> 00:44:11,200 faculty, you're specifying a Boolean expression. 1082 00:44:11,200 --> 00:44:14,780 Show me courses for which that expression is true. 1083 00:44:14,780 --> 00:44:17,080 Or to the right, doesn't conflict with courses I'm 1084 00:44:17,080 --> 00:44:20,440 taking, if that is checked, then yes, you want to check 1085 00:44:20,440 --> 00:44:22,890 that condition, else you want to ignore it. 1086 00:44:22,890 --> 00:44:25,170 So Boolean expressions are sort of all around, but when 1087 00:44:25,170 --> 00:44:28,080 we put them in conditions, whether IF conditions, IF 1088 00:44:28,080 --> 00:44:30,480 ELSEs, or we can even simulate deeper 1089 00:44:30,480 --> 00:44:32,830 levels, IF ELSE IF ELSE-- 1090 00:44:32,830 --> 00:44:34,430 so that's sort of a ternary state. 1091 00:44:34,430 --> 00:44:36,920 You can go this way or this way or this way. 1092 00:44:36,920 --> 00:44:39,680 We can keep nesting things to go in different directions. 1093 00:44:39,680 --> 00:44:41,800 >> So Scratch has these loops, like Forever. 1094 00:44:41,800 --> 00:44:44,750 It has these features like Repeat 10, some 1095 00:44:44,750 --> 00:44:46,260 finite number of times. 1096 00:44:46,260 --> 00:44:49,040 We have the ability now to set variables, so in this case 1097 00:44:49,040 --> 00:44:51,810 I've declared, for instance, a variable called socks. 1098 00:44:51,810 --> 00:44:54,340 I've initialized to 0, and that's yet another direction 1099 00:44:54,340 --> 00:44:56,940 we could have taken up here with Joseph, whereby maybe I 1100 00:44:56,940 --> 00:45:00,150 just keep track of how many socks I have on and terminate 1101 00:45:00,150 --> 00:45:03,490 the program when that variable's value is 2. 1102 00:45:03,490 --> 00:45:05,660 That would be another way of sort of generalizing that 1103 00:45:05,660 --> 00:45:08,820 problem and doing something again and again. 1104 00:45:08,820 --> 00:45:12,080 Well, let's go ahead and now introduce a couple new things. 1105 00:45:12,080 --> 00:45:14,100 So those of you with prior programing experience will 1106 00:45:14,100 --> 00:45:17,120 know that a lot of languages have arrays, or vectors or 1107 00:45:17,120 --> 00:45:19,970 lists, and indeed, Scratch has something like this, too. 1108 00:45:19,970 --> 00:45:22,510 So let's see if we can't take things to the next level here. 1109 00:45:22,510 --> 00:45:24,920 If I have the ability, now, with these puzzle pieces to 1110 00:45:24,920 --> 00:45:29,080 add something, like a word or number, to a variable, I can 1111 00:45:29,080 --> 00:45:30,550 start to accumulate things. 1112 00:45:30,550 --> 00:45:33,630 And this is actually pretty apropos for things like games, 1113 00:45:33,630 --> 00:45:35,810 role-playing games where you're kind of walking around 1114 00:45:35,810 --> 00:45:38,600 some fantasy world collecting things, picking things up, 1115 00:45:38,600 --> 00:45:39,900 earning points, or the like. 1116 00:45:39,900 --> 00:45:42,310 You might want to keep track of some kind of inventory, and 1117 00:45:42,310 --> 00:45:45,180 indeed, that's what one of our former students here did with 1118 00:45:45,180 --> 00:45:47,750 something called Fruitcraft RPG. 1119 00:45:47,750 --> 00:45:51,820 >> So let me go ahead and open up this thing here, and in 1120 00:45:51,820 --> 00:45:56,260 Fruitcraft, we have this world up at top. 1121 00:45:56,260 --> 00:45:58,820 So let me go ahead and click the green flag. 1122 00:45:58,820 --> 00:46:01,040 Notice at top left is some kind of inventory. 1123 00:46:01,040 --> 00:46:04,110 That's implemented in Scratch as what we'll call an array or 1124 00:46:04,110 --> 00:46:06,890 a list, and now we have this little animation. 1125 00:46:06,890 --> 00:46:10,440 So just as we started earlier with this cookie love story, 1126 00:46:10,440 --> 00:46:13,320 and then we advanced to cats and sea lions, now we can have 1127 00:46:13,320 --> 00:46:15,100 things that are even more interactive. 1128 00:46:15,100 --> 00:46:18,250 And this little blue guy, I can start to move around his 1129 00:46:18,250 --> 00:46:19,500 little home here. 1130 00:46:19,500 --> 00:46:22,570 So it looks like he's got an exit down here, so I'm using 1131 00:46:22,570 --> 00:46:25,260 the arrows keys, up, down, left right. 1132 00:46:25,260 --> 00:46:28,370 And now I'm outside, so let's what I've got here. 1133 00:46:28,370 --> 00:46:30,920 Looks like an orange, and indeed, as soon as I touch the 1134 00:46:30,920 --> 00:46:33,110 orange, it gets plopped into my inventory. 1135 00:46:33,110 --> 00:46:35,640 If I go over here to the cherries, now I have something 1136 00:46:35,640 --> 00:46:36,760 else in my inventory. 1137 00:46:36,760 --> 00:46:39,100 And this is all nice and cute, but think about how, now, this 1138 00:46:39,100 --> 00:46:39,840 is implemented. 1139 00:46:39,840 --> 00:46:42,490 Well, we have this notion of a list, and that's apparently a 1140 00:46:42,490 --> 00:46:45,470 puzzle piece that you just say what you want add to it, add 1141 00:46:45,470 --> 00:46:46,950 orange, add cherries. 1142 00:46:46,950 --> 00:46:48,540 Now, what is this little blue guy doing? 1143 00:46:48,540 --> 00:46:49,810 Well, he's a sprite. 1144 00:46:49,810 --> 00:46:52,190 And presumably, the orange and the cherries-- 1145 00:46:52,190 --> 00:46:54,140 they themselves were separate sprites. 1146 00:46:54,140 --> 00:46:56,980 And using conditions in Boolean expressions, the 1147 00:46:56,980 --> 00:47:00,530 student was probably able to express IF blue guy is 1148 00:47:00,530 --> 00:47:04,590 touching cherries THEN add the word cherries to his 1149 00:47:04,590 --> 00:47:07,890 inventory, and then also hide the cherry sprite. 1150 00:47:07,890 --> 00:47:10,600 So underneath the hood, there probably still is a cherry 1151 00:47:10,600 --> 00:47:11,350 sprite there. 1152 00:47:11,350 --> 00:47:14,160 We've just told it to become effectively invisible. 1153 00:47:14,160 --> 00:47:16,340 >> Now, if I keep walking over here, we can also do this 1154 00:47:16,340 --> 00:47:19,150 proximal thing, where I can go and read the sign. 1155 00:47:19,150 --> 00:47:23,170 So if blue guy touching sign, we can have this Say block 1156 00:47:23,170 --> 00:47:26,455 just like the cat spoke to us in words, hello, earlier, "Got 1157 00:47:26,455 --> 00:47:26,880 some fruit? 1158 00:47:26,880 --> 00:47:29,290 Bring it to the fruit place." All right, so now, apparently, 1159 00:47:29,290 --> 00:47:30,550 I have my directions. 1160 00:47:30,550 --> 00:47:33,050 I can go over here to the fruit place, line myself up 1161 00:47:33,050 --> 00:47:33,740 with the door. 1162 00:47:33,740 --> 00:47:35,390 Now, I'm in here. 1163 00:47:35,390 --> 00:47:38,120 I can go up to the man at the counter. 1164 00:47:38,120 --> 00:47:40,330 He detects that I'm close to him, so it doesn't have to be 1165 00:47:40,330 --> 00:47:46,780 quite identically touching, and I have won the game. 1166 00:47:46,780 --> 00:47:49,890 So there we have Fruitcraft RPG. 1167 00:47:49,890 --> 00:47:52,200 >> So we can do things even more advanced than this. 1168 00:47:52,200 --> 00:47:53,200 We can add sounds. 1169 00:47:53,200 --> 00:47:54,610 We can add pseudo randomness. 1170 00:47:54,610 --> 00:47:56,260 We can add complexity. 1171 00:47:56,260 --> 00:47:58,490 Let me go ahead here, and rather than do this myself, 1172 00:47:58,490 --> 00:48:01,240 let me show you one of the more sophisticated submissions 1173 00:48:01,240 --> 00:48:06,640 we got last year from a certain someone named Blake. 1174 00:48:06,640 --> 00:48:08,910 Can we have one volunteer who is comfortable appearing on 1175 00:48:08,910 --> 00:48:12,600 camera and is up for playing a game? 1176 00:48:12,600 --> 00:48:13,450 How about right there? 1177 00:48:13,450 --> 00:48:15,380 Come on up. 1178 00:48:15,380 --> 00:48:19,680 All right, so the game that you have just unknowingly 1179 00:48:19,680 --> 00:48:20,780 volunteered to play-- 1180 00:48:20,780 --> 00:48:25,910 [APPLAUSE] 1181 00:48:25,910 --> 00:48:28,610 DAVID MALAN: --is something from yesteryear called 1182 00:48:28,610 --> 00:48:30,100 "Frogger." What is your name? 1183 00:48:30,100 --> 00:48:30,730 RENDA: Renda. 1184 00:48:30,730 --> 00:48:31,050 DAVID MALAN: Redna? 1185 00:48:31,050 --> 00:48:32,530 RENDA: It's like Brenda with a b. 1186 00:48:32,530 --> 00:48:33,190 DAVID MALAN: Okay, Renda. 1187 00:48:33,190 --> 00:48:33,590 David. 1188 00:48:33,590 --> 00:48:34,580 Nice to meet you. 1189 00:48:34,580 --> 00:48:38,690 >> So here in "Frogger," and if we could raise the volume just 1190 00:48:38,690 --> 00:48:39,780 a little bit, you are this little 1191 00:48:39,780 --> 00:48:41,010 green frog on the bottom. 1192 00:48:41,010 --> 00:48:43,940 You can use left, right, up and down, and your goal is to 1193 00:48:43,940 --> 00:48:46,510 cross the street, cross the river, and touch the lily pads 1194 00:48:46,510 --> 00:48:47,760 at the top. 1195 00:48:52,090 --> 00:48:53,440 Aw. 1196 00:48:53,440 --> 00:48:54,630 One more t-- 1197 00:48:54,630 --> 00:48:56,400 redo, all right? 1198 00:48:56,400 --> 00:48:58,890 Let's hit stop. 1199 00:48:58,890 --> 00:49:00,140 No one saw that. 1200 00:49:10,470 --> 00:49:12,930 [APPLAUSE] 1201 00:49:17,023 --> 00:49:17,850 [APPLAUSE] 1202 00:49:17,850 --> 00:49:19,341 DAVID MALAN: Yeah. 1203 00:49:19,341 --> 00:49:20,832 Very well done. 1204 00:49:20,832 --> 00:49:22,082 Excellent, thank you. 1205 00:49:27,300 --> 00:49:29,430 >> So that there was Frogger. Now that you know what you've 1206 00:49:29,430 --> 00:49:32,170 gotten yourselves into, one more volunteer for a different 1207 00:49:32,170 --> 00:49:34,910 game submitted by another student. 1208 00:49:34,910 --> 00:49:35,560 You want to come on up? 1209 00:49:35,560 --> 00:49:36,576 What's your name? 1210 00:49:36,576 --> 00:49:37,000 RICHARD: Richard. 1211 00:49:37,000 --> 00:49:37,420 DAVID MALAN: Richard. 1212 00:49:37,420 --> 00:49:39,732 All right, Richard, come on up. 1213 00:49:39,732 --> 00:49:42,187 [APPLAUSE] 1214 00:49:45,140 --> 00:49:47,030 DAVID MALAN: You have something that'll sound 1215 00:49:47,030 --> 00:49:54,180 familiar soon, so here are your instructions. 1216 00:49:54,180 --> 00:49:56,580 So in a moment, some puzzle pieces are going to scroll up 1217 00:49:56,580 --> 00:49:58,150 from the screen that look either left 1218 00:49:58,150 --> 00:49:59,450 or right up or down. 1219 00:49:59,450 --> 00:50:01,230 You're going to have to hit the arrow keys in such a way 1220 00:50:01,230 --> 00:50:04,540 that it corresponds to those puzzle pieces lining up with 1221 00:50:04,540 --> 00:50:05,720 the placeholders at top. 1222 00:50:05,720 --> 00:50:07,500 So when you see a left arrow, and it lines up with the left 1223 00:50:07,500 --> 00:50:09,260 arrow, hit the left arrow. 1224 00:50:09,260 --> 00:50:10,866 You may begin. 1225 00:50:10,866 --> 00:50:13,674 [MUSIC - "STRONGER" KAYNE WEST] 1226 00:51:21,222 --> 00:51:23,470 DAVID MALAN: All right, big round of applause for Richard. 1227 00:51:23,470 --> 00:51:26,082 [APPLAUSE] 1228 00:51:32,426 --> 00:51:33,720 DAVID MALAN: Very well done. 1229 00:51:33,720 --> 00:51:34,110 Thank you. 1230 00:51:34,110 --> 00:51:37,104 [APPLAUSE] 1231 00:51:41,600 --> 00:51:44,330 >> DAVID MALAN: So fun and, seemingly, sort of 1232 00:51:44,330 --> 00:51:46,590 intimidating as it might be to implement something as 1233 00:51:46,590 --> 00:51:49,550 seemingly sophisticated as this, realize that the student 1234 00:51:49,550 --> 00:51:51,450 didn't set out and just write this all at once. 1235 00:51:51,450 --> 00:51:54,200 Rather, you can break down a problem as seemingly complex 1236 00:51:54,200 --> 00:51:56,840 as this into much smaller pieces, and this, too, is 1237 00:51:56,840 --> 00:51:57,560 going to be a theme. 1238 00:51:57,560 --> 00:52:00,490 The worst thing you can do in writing a program in most any 1239 00:52:00,490 --> 00:52:03,480 language is to sit down, get really excited, write the 1240 00:52:03,480 --> 00:52:07,200 whole damn thing, and then just hope that it works by the 1241 00:52:07,200 --> 00:52:08,510 time you're finished writing. 1242 00:52:08,510 --> 00:52:10,920 Rather, the process of programming should generally 1243 00:52:10,920 --> 00:52:13,440 be very deliberate, very iterative, whereby you just 1244 00:52:13,440 --> 00:52:16,955 set very small steps for yourselves, bite-size pieces 1245 00:52:16,955 --> 00:52:20,070 do you want to bite off, and so that you have these sanity 1246 00:52:20,070 --> 00:52:21,740 checks, little milestones you can meet. 1247 00:52:21,740 --> 00:52:24,170 And then you build on top of those to create more 1248 00:52:24,170 --> 00:52:25,400 sophisticated things still. 1249 00:52:25,400 --> 00:52:26,820 >> So for instance, how could we go about 1250 00:52:26,820 --> 00:52:28,380 implementing a game like this? 1251 00:52:28,380 --> 00:52:30,160 Well, frankly, I would certainly start by just 1252 00:52:30,160 --> 00:52:32,260 supporting one key at a time. 1253 00:52:32,260 --> 00:52:34,840 Let's just implement support for the left arrow. 1254 00:52:34,840 --> 00:52:37,610 So the student had to somehow create in Photoshop, or in 1255 00:52:37,610 --> 00:52:40,340 Scratch itself using the little graphical editor, an 1256 00:52:40,340 --> 00:52:42,680 arrow key that looks like the one at top left there, just 1257 00:52:42,680 --> 00:52:43,910 the gray placeholder. 1258 00:52:43,910 --> 00:52:46,610 Then the student had to figure out what x, y coordinate to 1259 00:52:46,610 --> 00:52:48,380 put it, where to put it in the window. 1260 00:52:48,380 --> 00:52:50,730 0, 0 is up here, so you have to figure out the number of 1261 00:52:50,730 --> 00:52:53,690 pixels, or dots, to offset that arrow from 1262 00:52:53,690 --> 00:52:54,800 the top of the screen. 1263 00:52:54,800 --> 00:52:57,040 And then once that's in place, your program doesn't actually 1264 00:52:57,040 --> 00:53:00,380 do anything yet, so you then need a second sprite, for 1265 00:53:00,380 --> 00:53:04,470 instance a green arrow that's also pointing left, and you 1266 00:53:04,470 --> 00:53:06,710 then need to start writing some scripts for it. 1267 00:53:06,710 --> 00:53:09,310 >> And you notice, perhaps, that these things started coming at 1268 00:53:09,310 --> 00:53:11,470 different speeds, and the colors were in different 1269 00:53:11,470 --> 00:53:13,750 locations, and that's because the student used a bit of 1270 00:53:13,750 --> 00:53:14,990 pseudo randomness. 1271 00:53:14,990 --> 00:53:17,440 And by pseudo randomness, I just mean pick a number 1272 00:53:17,440 --> 00:53:20,230 between something and something because you can 1273 00:53:20,230 --> 00:53:22,890 start to map things, like if the number is between one and 1274 00:53:22,890 --> 00:53:24,940 five, well, let's make the thing green. 1275 00:53:24,940 --> 00:53:28,290 If it's between 6 and 10, let's make the puzzle piece 1276 00:53:28,290 --> 00:53:29,730 red instead. 1277 00:53:29,730 --> 00:53:31,840 So long as you have a way of generating some kind of 1278 00:53:31,840 --> 00:53:34,730 randomness, you can then make decisions based on that 1279 00:53:34,730 --> 00:53:35,260 randomness. 1280 00:53:35,260 --> 00:53:37,490 And I keep saying pseudo random because there's a 1281 00:53:37,490 --> 00:53:38,390 little dirty secret. 1282 00:53:38,390 --> 00:53:41,130 Computers can't come up with random numbers. 1283 00:53:41,130 --> 00:53:42,800 They can only do what they're told because 1284 00:53:42,800 --> 00:53:44,110 they're man-made devices. 1285 00:53:44,110 --> 00:53:45,880 They can't just guess a number like we humans 1286 00:53:45,880 --> 00:53:46,960 feel like we can. 1287 00:53:46,960 --> 00:53:50,970 A computer has to do something mathematical to conjure up the 1288 00:53:50,970 --> 00:53:53,970 illusion of mathematical number, sometimes using the 1289 00:53:53,970 --> 00:53:57,420 current day of time as an input to figuring out what 1290 00:53:57,420 --> 00:53:59,570 number to return, but more on that another time. 1291 00:53:59,570 --> 00:54:02,060 For now, just know that we can generate pseudo randomness. 1292 00:54:02,060 --> 00:54:04,610 >> So once I have the ability for the left arrow to start 1293 00:54:04,610 --> 00:54:07,990 appearing at different times and at different speeds, then 1294 00:54:07,990 --> 00:54:10,230 I can go back and add some of these IF conditions. 1295 00:54:10,230 --> 00:54:14,950 IF this sprite is touching the other one, AND the left arrow 1296 00:54:14,950 --> 00:54:16,330 key has been pressed-- 1297 00:54:16,330 --> 00:54:17,900 so three conditions in that case. 1298 00:54:17,900 --> 00:54:20,750 I can use that AND block perhaps, in that case-- 1299 00:54:20,750 --> 00:54:23,210 THEN I want to go ahead and increment the score. 1300 00:54:23,210 --> 00:54:25,130 And at top, we have a score, we have Awesome, 1301 00:54:25,130 --> 00:54:26,510 Cool, Good, and Boo. 1302 00:54:26,510 --> 00:54:29,810 So there's apparently five variables that this student 1303 00:54:29,810 --> 00:54:32,220 used to keep track of these various metrics. 1304 00:54:32,220 --> 00:54:34,180 So in short, the end result is amazing. 1305 00:54:34,180 --> 00:54:37,320 It's fun, it's fun to play, it's engaging, but this is not 1306 00:54:37,320 --> 00:54:38,380 where the student began. 1307 00:54:38,380 --> 00:54:42,210 He or she started at a much smaller set of steps. 1308 00:54:42,210 --> 00:54:44,470 >> So what are some other building blocks that we can 1309 00:54:44,470 --> 00:54:45,760 weave into these programs? 1310 00:54:45,760 --> 00:54:48,750 Well, there's this other concept in most languages, 1311 00:54:48,750 --> 00:54:50,730 Scratch among them, known as threads. 1312 00:54:50,730 --> 00:54:54,660 So a computer can actually not really do multiple things at a 1313 00:54:54,660 --> 00:54:56,730 time, at least not usually. 1314 00:54:56,730 --> 00:54:59,930 Rather, a computer generally has just one CPU, and even 1315 00:54:59,930 --> 00:55:02,230 though computers are super fast and can, therefore, 1316 00:55:02,230 --> 00:55:04,740 create the illusion of doing multiple things at once-- 1317 00:55:04,740 --> 00:55:06,180 checking your mail, getting an instant 1318 00:55:06,180 --> 00:55:07,770 message, printing a document-- 1319 00:55:07,770 --> 00:55:11,530 really, a computer is just jumping from printing to IMing 1320 00:55:11,530 --> 00:55:15,160 to emailing, back and back and back and forth so fast that we 1321 00:55:15,160 --> 00:55:18,170 slow witted humans just don't realize that it's actually 1322 00:55:18,170 --> 00:55:21,240 running those programs a little bit at a time. 1323 00:55:21,240 --> 00:55:23,960 Now, this a bit of a white lie these days because, nowadays, 1324 00:55:23,960 --> 00:55:26,740 many our computers are what are called multi-core, so you 1325 00:55:26,740 --> 00:55:29,760 have one CPU but multiple cores, which is kind of like 1326 00:55:29,760 --> 00:55:31,760 having multiple CPUs. 1327 00:55:31,760 --> 00:55:35,020 >> And so sometimes, computers can truly do multiple things 1328 00:55:35,020 --> 00:55:38,500 at a time, but generally within a program, programs 1329 00:55:38,500 --> 00:55:40,200 rely on these things called threads. 1330 00:55:40,200 --> 00:55:43,160 So a thread is sort of like a miniature program that can 1331 00:55:43,160 --> 00:55:46,540 exist alongside another miniature program and can run 1332 00:55:46,540 --> 00:55:51,250 in parallel, or at least can run under the illusion that 1333 00:55:51,250 --> 00:55:52,990 they're running simultaneously. 1334 00:55:52,990 --> 00:55:55,060 So Scratch supports these things called threads. 1335 00:55:55,060 --> 00:55:57,580 You can have multiple scripts executing at once, just as we 1336 00:55:57,580 --> 00:56:00,220 did with the sea lion, and this allows us to actually 1337 00:56:00,220 --> 00:56:03,620 then have interactions among these sprites. 1338 00:56:03,620 --> 00:56:09,910 Let me go ahead here and pull up, let's say, threads, and 1339 00:56:09,910 --> 00:56:11,870 play this as follows. 1340 00:56:11,870 --> 00:56:13,950 We have two sprites, each of whom we'll see 1341 00:56:13,950 --> 00:56:15,720 has just one script. 1342 00:56:15,720 --> 00:56:18,000 And you notice there seems to be some intelligence in the 1343 00:56:18,000 --> 00:56:21,335 cat in this one because he's getting closer-- 1344 00:56:21,335 --> 00:56:21,730 [ROARS] 1345 00:56:21,730 --> 00:56:24,540 DAVID MALAN: --and closer to the little bird. 1346 00:56:24,540 --> 00:56:26,570 >> So how is the bird operating? 1347 00:56:26,570 --> 00:56:28,200 Well, let's take a look at the bird first. 1348 00:56:28,200 --> 00:56:31,220 The bird script said when the green flag is clicked, go to x 1349 00:56:31,220 --> 00:56:34,020 equals negative 115 and y equals 150. 1350 00:56:34,020 --> 00:56:37,010 So I just figured out a random location where I wanted the 1351 00:56:37,010 --> 00:56:39,580 bird to start, and I just plopped him there by default. 1352 00:56:39,580 --> 00:56:42,800 Then Forever IF not touching cat, so this is a different 1353 00:56:42,800 --> 00:56:45,080 kind of looping construct, but same idea, do this again and 1354 00:56:45,080 --> 00:56:45,780 again and again. 1355 00:56:45,780 --> 00:56:49,410 So long as you're not touching the cat, move three steps, and 1356 00:56:49,410 --> 00:56:51,680 if you're on the edge, bounce, where it's sort of a 1357 00:56:51,680 --> 00:56:53,320 reflection in the billiard sense. 1358 00:56:53,320 --> 00:56:56,140 So that's how the bird is moving around kind of 1359 00:56:56,140 --> 00:56:58,890 seemingly randomly, but it's just because it's bouncing off 1360 00:56:58,890 --> 00:57:00,170 the walls in this case. 1361 00:57:00,170 --> 00:57:03,170 >> Now the cat, meanwhile, is kind of cheating. 1362 00:57:03,170 --> 00:57:05,520 The cat, when the green flag is clicked, yes, starts in 1363 00:57:05,520 --> 00:57:07,390 some location, a random location-- 1364 00:57:07,390 --> 00:57:09,590 at least in part as per the pick 1365 00:57:09,590 --> 00:57:11,430 random green block there-- 1366 00:57:11,430 --> 00:57:14,660 and then Forever IF touching the bird, play the lion sound, 1367 00:57:14,660 --> 00:57:15,790 and then stop script. 1368 00:57:15,790 --> 00:57:18,560 So when I said terminate, or exit before, there's a puzzle 1369 00:57:18,560 --> 00:57:20,600 piece in Scratch that will just kill the program at that 1370 00:57:20,600 --> 00:57:22,900 point because it's kind of logically done. 1371 00:57:22,900 --> 00:57:25,030 But otherwise, here, notice what's going to happen. 1372 00:57:25,030 --> 00:57:28,470 Point toward the bird and move one step. 1373 00:57:28,470 --> 00:57:31,390 So this point toward bird is kind of an advantage the cat 1374 00:57:31,390 --> 00:57:34,640 has in that it's homing in on the moving bird, and we can 1375 00:57:34,640 --> 00:57:37,370 now make this program all the more interesting. 1376 00:57:37,370 --> 00:57:41,520 Instead of moving one step at a time per CPU cycle, per 1377 00:57:41,520 --> 00:57:44,280 strike toll of the bell, so to speak, let me go ahead and 1378 00:57:44,280 --> 00:57:47,350 move, let's say, five steps at a time as the cat. 1379 00:57:47,350 --> 00:57:51,120 Click run, and now he really finds him quickly. 1380 00:57:51,120 --> 00:57:55,620 If we double this further to 10 steps, it kind of goes 1381 00:57:55,620 --> 00:57:56,370 right for him. 1382 00:57:56,370 --> 00:57:57,750 Now, we can give the bird, 1383 00:57:57,750 --> 00:57:59,540 perhaps, a bit of an advantage. 1384 00:57:59,540 --> 00:58:02,370 Let's go to the bird and say instead of moving three steps, 1385 00:58:02,370 --> 00:58:07,140 let's move him 30 steps. 1386 00:58:07,140 --> 00:58:08,700 But he still got caught in the end. 1387 00:58:08,700 --> 00:58:10,430 >> So here, we have two threads. 1388 00:58:10,430 --> 00:58:13,230 It's incarcerated in Scratch with two scripts and two 1389 00:58:13,230 --> 00:58:16,150 sprites, but the idea in other languages is that you can 1390 00:58:16,150 --> 00:58:19,220 write, essentially, too many programs like this and have 1391 00:58:19,220 --> 00:58:22,950 them run truly, or imaginarily, in parallel. 1392 00:58:22,950 --> 00:58:25,280 >> Now, there's also this concept in programming known as 1393 00:58:25,280 --> 00:58:28,010 events, and this is something we won't see in C, the 1394 00:58:28,010 --> 00:58:30,500 language known as C. We will see it towards semester's end 1395 00:58:30,500 --> 00:58:33,240 in web programming, when we introduce JavaScript and the 1396 00:58:33,240 --> 00:58:36,970 notion of building web pages that are dynamic and interact 1397 00:58:36,970 --> 00:58:37,710 with users. 1398 00:58:37,710 --> 00:58:40,430 So in this case, we have a very simple example of two 1399 00:58:40,430 --> 00:58:43,860 sprites, boy and girl, each of whom have their respective 1400 00:58:43,860 --> 00:58:48,040 threads, but somehow these two are inter-communicating by way 1401 00:58:48,040 --> 00:58:49,490 of something called events. 1402 00:58:49,490 --> 00:58:52,750 So let me go ahead here and zoom in on the boy's script, 1403 00:58:52,750 --> 00:58:54,050 which looks like this. 1404 00:58:54,050 --> 00:58:57,510 When green flag clicked, forever do the following. 1405 00:58:57,510 --> 00:59:01,510 If the key Space, or the Space Bar, is pressed, say 1406 00:59:01,510 --> 00:59:03,900 Marco for two seconds, that's purely aesthetic on the 1407 00:59:03,900 --> 00:59:06,860 screen, a little speech bubble, but then broadcast. 1408 00:59:06,860 --> 00:59:09,240 >> So broadcast is another Scratch piece that's 1409 00:59:09,240 --> 00:59:12,050 representative of a class of functionality in programming 1410 00:59:12,050 --> 00:59:15,080 that allows different programs, different threads, 1411 00:59:15,080 --> 00:59:17,120 to inter-communicate, to somehow send 1412 00:59:17,120 --> 00:59:18,520 messages, one to another. 1413 00:59:18,520 --> 00:59:21,930 Passing a piece of paper in class is sort of the low tech 1414 00:59:21,930 --> 00:59:22,700 equivalent. 1415 00:59:22,700 --> 00:59:24,300 So broadcast event. 1416 00:59:24,300 --> 00:59:26,210 I can send this message, and the word event 1417 00:59:26,210 --> 00:59:27,270 is completely arbitrary. 1418 00:59:27,270 --> 00:59:30,000 Scratch sometimes has these drop downs, so I just came up 1419 00:59:30,000 --> 00:59:33,230 with a random word like event because now, what the boy does 1420 00:59:33,230 --> 00:59:36,600 when I press the key is he broadcasts this event. 1421 00:59:36,600 --> 00:59:39,270 And if I look now at the girl's script, her script is 1422 00:59:39,270 --> 00:59:42,690 super simple because all she needs to do is not act when 1423 00:59:42,690 --> 00:59:44,100 the green flag is clicked. 1424 00:59:44,100 --> 00:59:47,910 She is designed to action when she receives quote, unquote, 1425 00:59:47,910 --> 00:59:51,210 "event", and at that point, she's listening therefore for 1426 00:59:51,210 --> 00:59:52,960 the so-called event again and again. 1427 00:59:52,960 --> 00:59:55,790 As soon as she receives that event, she's going to shout 1428 00:59:55,790 --> 00:59:57,680 Polo for two seconds. 1429 00:59:57,680 --> 01:00:01,090 >> And so you can perhaps infer from this exactly what the 1430 01:00:01,090 --> 01:00:03,190 next result is going to be. 1431 01:00:03,190 --> 01:00:04,910 Let me click the green flag. 1432 01:00:04,910 --> 01:00:06,610 Nothing happens because I need to do what? 1433 01:00:06,610 --> 01:00:07,310 AUDIENCE: Space Bar. 1434 01:00:07,310 --> 01:00:08,630 DAVID MALAN: Space Bar. 1435 01:00:08,630 --> 01:00:11,990 Boy says Marco, girl says Polo. 1436 01:00:11,990 --> 01:00:13,670 But that's not hard coded per se. 1437 01:00:13,670 --> 01:00:16,740 That's inter-communication between scripts, so now we 1438 01:00:16,740 --> 01:00:19,240 have the ability to make even more complex programs where 1439 01:00:19,240 --> 01:00:22,830 these two are somehow inter-communicating. 1440 01:00:22,830 --> 01:00:25,260 >> So in what directions can we take this? 1441 01:00:25,260 --> 01:00:28,260 Well, in problem set 0, really, the objective is to 1442 01:00:28,260 --> 01:00:29,830 have fun with Scratch. 1443 01:00:29,830 --> 01:00:32,440 For the hacker edition, you'll instead have fun with a more 1444 01:00:32,440 --> 01:00:35,850 sophisticated version of Scratch called BYOB, Build 1445 01:00:35,850 --> 01:00:38,040 Your Own Blocks, but the idea is the same. 1446 01:00:38,040 --> 01:00:38,780 You'll be able-- 1447 01:00:38,780 --> 01:00:39,710 Yes, that was deliberate. 1448 01:00:39,710 --> 01:00:42,450 It came from Berkeley. 1449 01:00:42,450 --> 01:00:45,200 BYOB is the hacker edition version of this, but both 1450 01:00:45,200 --> 01:00:48,180 demographics, standard edition and hacker edition alike, the 1451 01:00:48,180 --> 01:00:50,940 goal at hand for the coming week is really just to dive in 1452 01:00:50,940 --> 01:00:53,050 deep, get your hands dirty with programming, and make 1453 01:00:53,050 --> 01:00:55,240 something interesting, make something interactive, make 1454 01:00:55,240 --> 01:00:58,020 something artistic, make something fun to actually 1455 01:00:58,020 --> 01:01:01,020 demonstrate, so that by week's end, you'll have a project, 1456 01:01:01,020 --> 01:01:03,770 not only for your first CS50 pset, but you'll have a 1457 01:01:03,770 --> 01:01:05,620 little something that you can show off in Annenberg to 1458 01:01:05,620 --> 01:01:08,830 friends or even family by uploading it to MIT's website. 1459 01:01:08,830 --> 01:01:11,760 And so as I said on Wednesday, we expect 90% of the class, 1460 01:01:11,760 --> 01:01:13,970 generally, to do the standard editions. 1461 01:01:13,970 --> 01:01:16,660 Realize that there's also this outlet for those of you who 1462 01:01:16,660 --> 01:01:18,990 might otherwise find yourself a bit bored with the basics 1463 01:01:18,990 --> 01:01:21,650 and really want to dive in and craft a vision you already 1464 01:01:21,650 --> 01:01:25,380 have with prior background in this more friendly 1465 01:01:25,380 --> 01:01:26,240 environment. 1466 01:01:26,240 --> 01:01:29,030 >> So let me pull up one other example that one of our former 1467 01:01:29,030 --> 01:01:33,830 students here did and tell a little something through song. 1468 01:01:33,830 --> 01:01:37,430 That similarly, as this plays, think about how you go about 1469 01:01:37,430 --> 01:01:40,520 implementing this program using precisely these same 1470 01:01:40,520 --> 01:01:44,180 building blocks, a little bit of pseudo randomness, and a 1471 01:01:44,180 --> 01:01:46,010 bit of familiar song. 1472 01:01:46,010 --> 01:01:47,910 If we could raise the volume just a little bit? 1473 01:01:47,910 --> 01:01:49,160 [MUSIC - "IT'S RAINING MEN" THE WEATHER GIRLS] 1474 01:03:02,261 --> 01:03:03,930 >> DAVID MALAN: That's it for CS50. 1475 01:03:03,930 --> 01:03:05,860 We will see you on Monday. 1476 01:03:05,860 --> 01:03:21,152 [APPLAUSE]