1 00:00:00,000 --> 00:00:49,610 [Introduction] 2 00:00:49,610 --> 00:00:52,910 DAVID MALAN: This is CS50, Harvard University's introduction 3 00:00:52,910 --> 00:00:55,310 to the intellectual enterprises of computer science 4 00:00:55,310 --> 00:00:56,810 and the art of programming. 5 00:00:56,810 --> 00:00:59,480 My name is David Malan, and if you are among those 6 00:00:59,480 --> 00:01:03,300 in the room who are thinking, why am I in a class of computer science, 7 00:01:03,300 --> 00:01:05,750 realize that I too felt that exact same way. 8 00:01:05,750 --> 00:01:07,460 In fact, my freshman year, I didn't quite 9 00:01:07,460 --> 00:01:10,820 get up the nerve to take this class or computer science more generally, 10 00:01:10,820 --> 00:01:13,530 and that was largely because I was intimidated by it. 11 00:01:13,530 --> 00:01:14,680 I was a little nervous. 12 00:01:14,680 --> 00:01:16,590 It felt well out of my comfort zone. 13 00:01:16,590 --> 00:01:19,860 And I really didn't know at the end of the day what it actually was. 14 00:01:19,860 --> 00:01:22,280 But realize if you, too, are feeling a little bit of that, 15 00:01:22,280 --> 00:01:24,140 or even if you're among those more comfortable who 16 00:01:24,140 --> 00:01:26,270 have dabbled in computer science or programming, 17 00:01:26,270 --> 00:01:29,180 realize that there's so many blanks that we can fill in along the way 18 00:01:29,180 --> 00:01:31,510 so that ultimately, at the end of the semester, everyone 19 00:01:31,510 --> 00:01:34,280 will feel themselves on the same page. 20 00:01:34,280 --> 00:01:38,870 And until then, rest assured that 68% of the people sitting to your left 21 00:01:38,870 --> 00:01:40,940 and to your right and behind and in front 22 00:01:40,940 --> 00:01:45,230 have never taken a CS course before, which may very well be 23 00:01:45,230 --> 00:01:47,000 the demographic into which you fit. 24 00:01:47,000 --> 00:01:50,330 But realize, too, that with such an amazing support 25 00:01:50,330 --> 00:01:53,820 structure with so many office hours and sections and materials and beyond, 26 00:01:53,820 --> 00:01:56,120 realize that what's ultimately important in this course 27 00:01:56,120 --> 00:01:59,180 is not so much where you end up relative to your classmates 28 00:01:59,180 --> 00:02:03,530 in week 10, our final week, but where you end up relative to yourself 29 00:02:03,530 --> 00:02:04,400 in week zero. 30 00:02:04,400 --> 00:02:06,050 And indeed, that is where we now are. 31 00:02:06,050 --> 00:02:08,810 And as it turns out, computer scientists start counting at zero. 32 00:02:08,810 --> 00:02:11,690 And so over the next 11 weeks, we will take you 33 00:02:11,690 --> 00:02:14,060 from being among those less comfortable or perhaps 34 00:02:14,060 --> 00:02:16,120 somewhere in between less comfortable and more 35 00:02:16,120 --> 00:02:20,510 to feeling much more comfortable and confident and capable than that. 36 00:02:20,510 --> 00:02:23,990 But to get there, we need to understand what computer science really is. 37 00:02:23,990 --> 00:02:26,870 And this was something I didn't understand until I set foot in a room 38 00:02:26,870 --> 00:02:27,530 like this. 39 00:02:27,530 --> 00:02:31,160 And I dare say we can distill computer science into just this picture. 40 00:02:31,160 --> 00:02:33,290 Computer science is about problem solving. 41 00:02:33,290 --> 00:02:36,440 And I know that high school courses typically do kind of paint 42 00:02:36,440 --> 00:02:38,270 a misleading picture that it's only about 43 00:02:38,270 --> 00:02:40,910 and it's entirely about programming and people with their heads 44 00:02:40,910 --> 00:02:44,270 down in the computer lab working fairly anti-socially on code, 45 00:02:44,270 --> 00:02:47,270 but the reality is it's all about solving problems, and very often, 46 00:02:47,270 --> 00:02:51,140 solving problems collaboratively either in person or by leveraging code, 47 00:02:51,140 --> 00:02:53,240 programs that others have written in the past. 48 00:02:53,240 --> 00:02:54,980 And what does it mean to solve a problem? 49 00:02:54,980 --> 00:02:56,440 Well, you need inputs. 50 00:02:56,440 --> 00:02:58,280 So there's a problem you're trying to solve. 51 00:02:58,280 --> 00:02:59,150 That is the input. 52 00:02:59,150 --> 00:02:59,990 And you want output. 53 00:02:59,990 --> 00:03:01,800 You want the solution to that problem. 54 00:03:01,800 --> 00:03:03,830 And the sort of secret sauce of computer science 55 00:03:03,830 --> 00:03:06,140 is going to be everything in this proverbial black box 56 00:03:06,140 --> 00:03:08,440 in the middle over the next several weeks, 57 00:03:08,440 --> 00:03:12,090 where you begin to understand exactly what you can do with that. 58 00:03:12,090 --> 00:03:15,050 But in order to start solving problems, we kind of just 59 00:03:15,050 --> 00:03:18,290 need to decide as a group how we're going to represent these problems 60 00:03:18,290 --> 00:03:19,780 and what might a problem be. 61 00:03:19,780 --> 00:03:21,350 Well, in this room, there's a whole bunch of people. 62 00:03:21,350 --> 00:03:24,650 If we wanted to take attendance or count the number of people in this room, 63 00:03:24,650 --> 00:03:28,040 I might need to start keeping track of how many people I see. 64 00:03:28,040 --> 00:03:30,700 But how do I represent the number of people I see? 65 00:03:30,700 --> 00:03:34,070 Well, I can do it sort of old school and I can just take out a piece of chalk 66 00:03:34,070 --> 00:03:35,320 or whatnot and say, all right. 67 00:03:35,320 --> 00:03:38,260 I see 1, 2, 3, 4, 5. 68 00:03:38,260 --> 00:03:40,220 I can do little stylistic conventions like that 69 00:03:40,220 --> 00:03:42,290 to save space and remind myself. 70 00:03:42,290 --> 00:03:45,840 6, 7, 8, 9, 10, and so forth. 71 00:03:45,840 --> 00:03:48,860 Or I can, of course, just do that on my own hand. 72 00:03:48,860 --> 00:03:52,730 So 1, 2, 3, 4, 5, and so forth. 73 00:03:52,730 --> 00:03:57,350 But obviously, how high can I count on just one hand? 74 00:03:57,350 --> 00:04:00,110 So 5 you would think, but that's just because we haven't really 75 00:04:00,110 --> 00:04:01,760 thought hard enough about this problem. 76 00:04:01,760 --> 00:04:04,850 It turns out that with just these five fingers, let alone these five more, 77 00:04:04,850 --> 00:04:08,360 I can actually count rather higher because after all, the system 78 00:04:08,360 --> 00:04:10,310 I'm using of hashmarks on the board or just 79 00:04:10,310 --> 00:04:13,550 now with my fingers is just kind of keeping my fingers down and putting 80 00:04:13,550 --> 00:04:16,190 them up to represent ones, really. 81 00:04:16,190 --> 00:04:19,160 But what if I actually took into account the order of my fingers 82 00:04:19,160 --> 00:04:22,860 and sort of permuted them, so to speak, so that it's really patterns of fingers 83 00:04:22,860 --> 00:04:24,860 that represent the number of people in the room, 84 00:04:24,860 --> 00:04:27,660 and not just the mere presence of a finger going up or down. 85 00:04:27,660 --> 00:04:29,480 In other words, this can remain zero. 86 00:04:29,480 --> 00:04:31,130 This could still be one. 87 00:04:31,130 --> 00:04:34,250 But what if two is not just this, the obvious? 88 00:04:34,250 --> 00:04:36,140 But what if it's just this? 89 00:04:36,140 --> 00:04:38,660 So raising just one, my second finger. 90 00:04:38,660 --> 00:04:41,180 What if, then, three is this? 91 00:04:41,180 --> 00:04:44,420 So we have 0, 1, 2, 3. 92 00:04:44,420 --> 00:04:47,150 That's going to lead us to four somewhat offensively. 93 00:04:47,150 --> 00:04:51,140 But if we begin to jump ahead to five, I might now 94 00:04:51,140 --> 00:04:52,790 permute this finger and this finger up. 95 00:04:52,790 --> 00:04:56,120 And if I want to now represent six, I could do this. 96 00:04:56,120 --> 00:04:56,910 And now seven. 97 00:04:56,910 --> 00:04:59,870 In other words, I've expressed so many more patterns on my hand already 98 00:04:59,870 --> 00:05:01,910 and if we keep doing this, I think I can actually 99 00:05:01,910 --> 00:05:06,740 represent painfully perhaps like 32 different patterns, and therefore 100 00:05:06,740 --> 00:05:09,380 32 different people, on my hands alone. 101 00:05:09,380 --> 00:05:11,690 Or 31 people if I start counting at zero. 102 00:05:11,690 --> 00:05:13,750 So what is that-- what's the relationship 103 00:05:13,750 --> 00:05:14,960 and how did we even get here? 104 00:05:14,960 --> 00:05:17,810 Well, it turns out that computers are kind of simplistic, 105 00:05:17,810 --> 00:05:19,400 much like our hands here. 106 00:05:19,400 --> 00:05:21,940 At the end of the day, your computer is plugged into the wall 107 00:05:21,940 --> 00:05:24,980 or it's got a battery, so it either has or it does not have electricity. 108 00:05:24,980 --> 00:05:27,140 At the end of the day, that is the physical resource 109 00:05:27,140 --> 00:05:30,030 that drives these things and our phones and all of technology today. 110 00:05:30,030 --> 00:05:33,650 So if there is either electricity or not, that kind of maps nicely 111 00:05:33,650 --> 00:05:35,810 to no finger or yes finger. 112 00:05:35,810 --> 00:05:39,350 And indeed, computers, as you probably know, only speak what language? 113 00:05:39,350 --> 00:05:40,980 What alphabet, so to speak? 114 00:05:40,980 --> 00:05:41,480 Yeah. 115 00:05:41,480 --> 00:05:41,990 Binary. 116 00:05:41,990 --> 00:05:42,890 Bi meaning two. 117 00:05:42,890 --> 00:05:46,160 And indeed, that refers to the fact that in binary in computers, 118 00:05:46,160 --> 00:05:48,050 you only have two digits-- 119 00:05:48,050 --> 00:05:49,200 zero and one. 120 00:05:49,200 --> 00:05:53,440 We humans, of course, have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 121 00:05:53,440 --> 00:05:55,570 and then we can combine those to count even higher. 122 00:05:55,570 --> 00:05:57,950 But computers only have 0, 1, and then that's it. 123 00:05:57,950 --> 00:05:59,910 Because at the end of the day, there's actually 124 00:05:59,910 --> 00:06:04,290 a direct mapping between power being off and it being a zero or power being on 125 00:06:04,290 --> 00:06:07,770 and it being one, or some electrons or whatever flowing from your battery 126 00:06:07,770 --> 00:06:08,980 or from the wall. 127 00:06:08,980 --> 00:06:12,240 So this is why computers tend to speak only binary, 128 00:06:12,240 --> 00:06:14,760 because at the end of the day, it just maps really cleanly 129 00:06:14,760 --> 00:06:18,360 to what it is that's powering them in the first place. 130 00:06:18,360 --> 00:06:19,920 But how is this actually useful? 131 00:06:19,920 --> 00:06:23,280 If computers only have zeros and ones, how can they do anything useful? 132 00:06:23,280 --> 00:06:28,030 Well, think about our human world, where you might have this pattern of symbols. 133 00:06:28,030 --> 00:06:31,050 This is decimal, dec meaning 10 because you have 0 through 9. 134 00:06:31,050 --> 00:06:33,270 And this is, of course, 123. 135 00:06:33,270 --> 00:06:33,960 But why? 136 00:06:33,960 --> 00:06:36,330 If you haven't thought about this in quite some time, 137 00:06:36,330 --> 00:06:40,200 this is really just a pattern of three symbols, one and two and three shapes, 138 00:06:40,200 --> 00:06:41,520 or glyphs, on the screen. 139 00:06:41,520 --> 00:06:44,850 But we humans, ever since grade school, have started ascribing meaning 140 00:06:44,850 --> 00:06:46,310 to each of these numbers, right? 141 00:06:46,310 --> 00:06:49,710 If you think back, this is the ones column, this is the tens column, 142 00:06:49,710 --> 00:06:53,300 this is the hundreds column, and so forth, and we could keep going. 143 00:06:53,300 --> 00:06:57,450 And so why does this pattern-- one, two, three-- mean 123? 144 00:06:57,450 --> 00:06:59,620 Well, it's because all of us sort of intuitively 145 00:06:59,620 --> 00:07:04,860 nowadays are just quickly in our head doing 100 times 1 plus 10 times 146 00:07:04,860 --> 00:07:11,430 2 plus 1 times 3, which of course gives us 100 plus 20 plus three, 147 00:07:11,430 --> 00:07:15,210 and then the number we know mathematically as 123. 148 00:07:15,210 --> 00:07:18,510 But we're all doing this so quickly, you don't really think about this anymore. 149 00:07:18,510 --> 00:07:21,040 Well, computers work fundamentally the same way. 150 00:07:21,040 --> 00:07:22,410 They don't have as many digits-- 151 00:07:22,410 --> 00:07:24,030 0 through 9-- as we do. 152 00:07:24,030 --> 00:07:26,080 They only have zeros and ones. 153 00:07:26,080 --> 00:07:28,290 And so if they were to store values, you're 154 00:07:28,290 --> 00:07:29,790 only going to see zeros and ones on the screen, 155 00:07:29,790 --> 00:07:31,950 but those zeros and ones just mean different things. 156 00:07:31,950 --> 00:07:34,530 Instead of having a ones place, tens, a hundreds, 157 00:07:34,530 --> 00:07:39,210 they're going to have a ones place, a twos place, a fours place, 158 00:07:39,210 --> 00:07:41,410 and then eights and 16 and beyond. 159 00:07:41,410 --> 00:07:42,060 Now, why? 160 00:07:42,060 --> 00:07:46,250 Well, one and 10 and 100, turns out those are powers of 10. 161 00:07:46,250 --> 00:07:48,580 10 to the 0 is technically 1. 162 00:07:48,580 --> 00:07:50,340 10 to the 1 is just 10. 163 00:07:50,340 --> 00:07:51,750 10 to the 2 is 100. 164 00:07:51,750 --> 00:07:55,140 And that's why you have ones, tens hundreds, thousands, and so forth. 165 00:07:55,140 --> 00:07:57,720 Computers are apparently using powers of 2. 166 00:07:57,720 --> 00:07:58,440 Not surprising. 167 00:07:58,440 --> 00:07:59,580 Binary-- two. 168 00:07:59,580 --> 00:08:04,410 So if you only have ones, twos, and fours as your placeholders, 169 00:08:04,410 --> 00:08:08,190 if a computer were storing these digits-- 170 00:08:08,190 --> 00:08:12,990 0, 0, 0-- that computer is presumably storing what number so far as we 171 00:08:12,990 --> 00:08:14,680 humans understand it? 172 00:08:14,680 --> 00:08:16,560 Well, that's how a computer would store zero. 173 00:08:16,560 --> 00:08:19,800 If a computer is storing literally 0, 0, 0, just 174 00:08:19,800 --> 00:08:22,590 like in our human world, that also is 0, but that's technically 175 00:08:22,590 --> 00:08:26,940 because it's 4 times 0 plus 2 times 0 plus 1 times zero, which is obviously 176 00:08:26,940 --> 00:08:27,600 zero. 177 00:08:27,600 --> 00:08:30,870 Meanwhile, if a computer is actually storing not just, 178 00:08:30,870 --> 00:08:35,790 say, 0, 0, 0, but instead is storing this value in binary, 179 00:08:35,790 --> 00:08:38,190 what does that map to in decimal? 180 00:08:38,190 --> 00:08:39,120 So that's one. 181 00:08:39,120 --> 00:08:46,640 And now, why, if we change this 0 and 1 to this value here, is this two? 182 00:08:46,640 --> 00:08:49,030 Well, mathematically, for the exact same reasons. 183 00:08:49,030 --> 00:08:52,290 And so earlier, I had five fingers, but if you consider just my first three, 184 00:08:52,290 --> 00:08:56,310 when I did this holding up one finger, I was representing two. 185 00:08:56,310 --> 00:08:59,550 And if I want to represent three, recall that I put up the second finger. 186 00:08:59,550 --> 00:09:02,760 And so the reason that could nicely represent three 187 00:09:02,760 --> 00:09:07,110 is because all I was doing with my human hand was counting in binary. 188 00:09:07,110 --> 00:09:09,160 And I could keep counting more and more and more. 189 00:09:09,160 --> 00:09:14,110 And so if I have five fingers or five bits, bit meaning binary digits, 190 00:09:14,110 --> 00:09:16,200 I could count up, it turns out, if we do the math, 191 00:09:16,200 --> 00:09:19,200 as high as 31 by starting to zero. 192 00:09:19,200 --> 00:09:22,530 It's going to be hard to physically do that, but we could. 193 00:09:22,530 --> 00:09:24,280 So why is this useful? 194 00:09:24,280 --> 00:09:26,150 At the end of the day, a computer, therefore, 195 00:09:26,150 --> 00:09:30,750 can represent any number of values from 0 to 1 to 2 to 3 to some number much, 196 00:09:30,750 --> 00:09:31,910 much, much higher than that. 197 00:09:31,910 --> 00:09:34,740 All it needs is enough bits, enough zeros and ones. 198 00:09:34,740 --> 00:09:35,940 Well, what are those bits? 199 00:09:35,940 --> 00:09:40,050 Well, all of us have these days in our phones sources of light, for instance. 200 00:09:40,050 --> 00:09:43,050 So I could actually say that this physical device right now-- 201 00:09:43,050 --> 00:09:44,490 might be a little hard to tell-- 202 00:09:44,490 --> 00:09:47,440 it does have a flashlight and it's technically off at the moment. 203 00:09:47,440 --> 00:09:51,200 But if I turn this flashlight on, thereby using some of the electricity, 204 00:09:51,200 --> 00:09:52,860 now, I'm storing a one. 205 00:09:52,860 --> 00:09:53,940 And so the phone is on. 206 00:09:53,940 --> 00:09:54,870 Now, it's off. 207 00:09:54,870 --> 00:09:55,620 Now, it's on. 208 00:09:55,620 --> 00:09:56,600 And if I see-- 209 00:09:56,600 --> 00:09:58,270 can I borrow someone's phone real quick? 210 00:09:58,270 --> 00:09:59,140 May I? 211 00:09:59,140 --> 00:09:59,640 OK. 212 00:09:59,640 --> 00:10:00,700 And flashlight. 213 00:10:00,700 --> 00:10:02,560 How do I turn on the flashlight? 214 00:10:02,560 --> 00:10:03,060 Oh. 215 00:10:03,060 --> 00:10:03,560 Shake it. 216 00:10:06,900 --> 00:10:07,560 That's OK. 217 00:10:07,560 --> 00:10:08,060 OK. 218 00:10:08,060 --> 00:10:09,340 Thank you. 219 00:10:09,340 --> 00:10:09,840 Oh. 220 00:10:09,840 --> 00:10:10,260 Thank you. 221 00:10:10,260 --> 00:10:10,710 OK. 222 00:10:10,710 --> 00:10:11,410 So this is great. 223 00:10:11,410 --> 00:10:13,560 Now, I can count higher. 224 00:10:13,560 --> 00:10:17,790 Now, this represents the number what if I have two light bulbs or two switches 225 00:10:17,790 --> 00:10:19,180 on at the moment? 226 00:10:19,180 --> 00:10:19,680 Yeah. 227 00:10:19,680 --> 00:10:20,170 Three. 228 00:10:20,170 --> 00:10:22,050 Because I have a one, I have a one, and I 229 00:10:22,050 --> 00:10:24,150 have two, which of course is going to end up equaling three. 230 00:10:24,150 --> 00:10:27,090 And if I pick up a third phone somehow, I could count even higher. 231 00:10:27,090 --> 00:10:29,570 Technically, if I had three light bulbs on-- 232 00:10:29,570 --> 00:10:32,350 one, one, one-- what would that value be? 233 00:10:32,350 --> 00:10:32,850 Seven. 234 00:10:32,850 --> 00:10:36,010 Because it's a four plus a two plus a one, and so forth. 235 00:10:36,010 --> 00:10:38,020 Thank you so much for the spontaneity. 236 00:10:38,020 --> 00:10:42,780 So why does this not lead to limitations for us? 237 00:10:42,780 --> 00:10:45,120 I can count in decimal as high as I want. 238 00:10:45,120 --> 00:10:48,540 I can now count in binary as high as I want, so long as I have enough bits. 239 00:10:48,540 --> 00:10:52,310 But how do I actually represent other information? 240 00:10:52,310 --> 00:10:57,250 Well, if I want to represent something like a letter, how do I get there? 241 00:10:57,250 --> 00:11:01,540 If computers only have electricity in them and they use binary to count, 242 00:11:01,540 --> 00:11:04,300 and yet somehow they're much more useful than just doing math-- 243 00:11:04,300 --> 00:11:08,050 they can have text messages and e-mails and websites and videos and more-- 244 00:11:08,050 --> 00:11:11,140 how do we get from zeros and ones to letters? 245 00:11:11,140 --> 00:11:12,400 Well, we-- yeah. 246 00:11:15,120 --> 00:11:15,620 Sorry. 247 00:11:15,620 --> 00:11:18,140 A little louder. 248 00:11:18,140 --> 00:11:18,650 Yeah. 249 00:11:18,650 --> 00:11:20,630 We just need to kind of relate the numbers to letters. 250 00:11:20,630 --> 00:11:23,510 In other words, all the people in this room just need to decide at some point 251 00:11:23,510 --> 00:11:24,410 that, you know what? 252 00:11:24,410 --> 00:11:27,200 If we want to represent something like the capital letter A, 253 00:11:27,200 --> 00:11:29,180 we just need to decide on a pattern of bits, 254 00:11:29,180 --> 00:11:31,730 a pattern of fingers, that's going to represent A. 255 00:11:31,730 --> 00:11:34,670 And it turns out humans years ago just unilaterally 256 00:11:34,670 --> 00:11:39,840 decided 65 shall be the decimal number that represents capital letter A. 257 00:11:39,840 --> 00:11:43,700 And you might guess capital B is represented by what decimal number? 258 00:11:43,700 --> 00:11:44,420 66. 259 00:11:44,420 --> 00:11:46,500 And then C is 67 and so forth. 260 00:11:46,500 --> 00:11:50,630 And there's a mapping of like 128 or even 256 possible values 261 00:11:50,630 --> 00:11:52,880 for all the keys you might see on a typical keyboard 262 00:11:52,880 --> 00:11:54,680 in order to represent letters. 263 00:11:54,680 --> 00:11:58,260 Now, how does a computer distinguish, though, numbers from letters? 264 00:11:58,260 --> 00:11:59,680 Well, just depends on the context. 265 00:11:59,680 --> 00:12:01,880 If you're using like a calculator program on your Mac 266 00:12:01,880 --> 00:12:05,600 or PC or iPhone or Android, well, the computer, the device, 267 00:12:05,600 --> 00:12:09,050 is just going to know contextually, let me interpret this pattern of zeros 268 00:12:09,050 --> 00:12:11,330 and ones as actual numbers to do math. 269 00:12:11,330 --> 00:12:14,880 But if you're using the SMS app or the messages app on your phone, 270 00:12:14,880 --> 00:12:17,060 you're going to actually be in the context of text, 271 00:12:17,060 --> 00:12:20,380 and so your phone is going to interpret that same pattern of zeros and ones, 272 00:12:20,380 --> 00:12:23,780 or light bulbs being off, or, at the end of the day, transistors, 273 00:12:23,780 --> 00:12:27,450 tiny pieces of hardware and computers that are either on or off-- 274 00:12:27,450 --> 00:12:30,470 it's going to interpret those patterns as just representing a letter. 275 00:12:30,470 --> 00:12:34,220 If you're in the context of a text messaging application or Microsoft Word 276 00:12:34,220 --> 00:12:38,180 or Google Docs or the like, it completely depends on context. 277 00:12:38,180 --> 00:12:41,780 The system we humans came up with just called ASCII, American Standard Code 278 00:12:41,780 --> 00:12:43,100 for Information Interchange. 279 00:12:43,100 --> 00:12:46,760 The name isn't interesting, but the fact that we all agreed years ago 280 00:12:46,760 --> 00:12:50,210 that 65 is A and so forth is what's important. 281 00:12:50,210 --> 00:12:52,800 And so for instance, if we look at this mapping here 282 00:12:52,800 --> 00:12:56,340 of just the first few letters, what does this mean? 283 00:12:56,340 --> 00:13:00,920 If I were to now get a text message and I had the ability somehow 284 00:13:00,920 --> 00:13:04,190 to look underneath the hood, so to speak, at the pattern of zeros and ones 285 00:13:04,190 --> 00:13:06,890 that someone had just texted me, and that pattern, 286 00:13:06,890 --> 00:13:12,230 if I convert it to decimal, technically said, let's say, 72 and 73-- 287 00:13:12,230 --> 00:13:13,970 so I get a whole bunch of zeros and ones. 288 00:13:13,970 --> 00:13:17,480 I do some math and I realize, OK, I just received 72 and 73, 289 00:13:17,480 --> 00:13:20,690 but this is texting, and so it's not just numbers my friend is sending me. 290 00:13:20,690 --> 00:13:21,690 It's a message. 291 00:13:21,690 --> 00:13:27,840 What message did my friend likely send me if he or she sent 72 and then 73? 292 00:13:27,840 --> 00:13:28,370 Yeah. 293 00:13:28,370 --> 00:13:28,860 Hi. 294 00:13:28,860 --> 00:13:31,280 H-I. Because if you skim ahead at the right there, 295 00:13:31,280 --> 00:13:36,720 that just happens to be in ASCII the mapping between 72 and 73, H and I. 296 00:13:36,720 --> 00:13:40,240 If technically, the message had a third byte, if you will. 297 00:13:40,240 --> 00:13:43,090 A byte, if you've ever wondered, is just eight bits. 298 00:13:43,090 --> 00:13:45,260 It's convenient to talk not in terms of single bits, 299 00:13:45,260 --> 00:13:48,440 where you can't count very high, but with a byte, or eight bits, 300 00:13:48,440 --> 00:13:49,700 you can count higher. 301 00:13:49,700 --> 00:13:53,600 And so it turns out if I received a third byte, another sequence of eight 302 00:13:53,600 --> 00:13:55,520 zeros and ones together-- 303 00:13:55,520 --> 00:13:56,150 33. 304 00:13:56,150 --> 00:13:59,490 How would we know what this message now is? 305 00:13:59,490 --> 00:13:59,990 Yeah. 306 00:13:59,990 --> 00:14:01,640 So it turns out you would not know this other 307 00:14:01,640 --> 00:14:04,430 than by guessing or Googling or just coming in with this knowledge. 308 00:14:04,430 --> 00:14:05,360 This is now "HI!" 309 00:14:05,360 --> 00:14:09,020 with an exclamation point because 33 just so happens, if you look it up, 310 00:14:09,020 --> 00:14:12,180 to map to an exclamation point, as well. 311 00:14:12,180 --> 00:14:14,360 Now, if we actually looked at the binary of this, 312 00:14:14,360 --> 00:14:16,610 you would actually see this pattern of zeros and ones. 313 00:14:16,610 --> 00:14:18,920 This is how you represent 72 in binary. 314 00:14:18,920 --> 00:14:20,690 This is you represent 73. 315 00:14:20,690 --> 00:14:22,820 And this is how you represent 33. 316 00:14:22,820 --> 00:14:26,810 And notice I've only used one, two, three, four, five, six bits, 317 00:14:26,810 --> 00:14:30,200 even though I technically tend to receive things in units of 8, 318 00:14:30,200 --> 00:14:31,220 units of bytes. 319 00:14:31,220 --> 00:14:37,770 But why did I not bother writing another 00 here and another 0 here? 320 00:14:37,770 --> 00:14:40,660 Does it matter when you write these things out? 321 00:14:40,660 --> 00:14:41,160 No. 322 00:14:41,160 --> 00:14:41,670 Not really. 323 00:14:41,670 --> 00:14:46,010 Like in English, in our human world, if you were to write one, two, three, 324 00:14:46,010 --> 00:14:47,280 that's 123. 325 00:14:47,280 --> 00:14:50,850 If you were to write 0, 1, 2, 3, that's still 123. 326 00:14:50,850 --> 00:14:53,670 So even though we tend to get them in clusters of 8, 327 00:14:53,670 --> 00:14:56,660 we don't necessarily need to write those when just talking about them. 328 00:14:56,660 --> 00:14:57,540 So what have we done? 329 00:14:57,540 --> 00:15:00,160 Well, let me introduce a fancier word now known as abstraction. 330 00:15:00,160 --> 00:15:03,480 Abstraction is just a term generally used in computer science and we'll soon 331 00:15:03,480 --> 00:15:07,530 see in programming for taking some low level-- like literally low level-- 332 00:15:07,530 --> 00:15:11,700 implementation details, like minutiae even, and understanding them 333 00:15:11,700 --> 00:15:15,450 at some point, but then deciding this is not a useful level conceptually 334 00:15:15,450 --> 00:15:16,750 to think about problems. 335 00:15:16,750 --> 00:15:20,280 I really don't want to solve problems in this world of thinking in 0's and 1's. 336 00:15:20,280 --> 00:15:23,910 I'd much rather think about things minimally in decimal, or better yet, 337 00:15:23,910 --> 00:15:26,640 in the context of letters if I'm actually receiving text, or even 338 00:15:26,640 --> 00:15:28,110 some other representation. 339 00:15:28,110 --> 00:15:32,040 So abstraction is about taking fairly low level details 340 00:15:32,040 --> 00:15:35,980 and just simplifying them so that we can have a more useful conversation 341 00:15:35,980 --> 00:15:39,300 and never again worry about where the electricity is coming from. 342 00:15:39,300 --> 00:15:42,730 We can just stipulate my computer can represent zeros and ones. 343 00:15:42,730 --> 00:15:44,230 Therefore, it can represent numbers. 344 00:15:44,230 --> 00:15:47,010 Therefore, it can also represent ASCII or letters. 345 00:15:47,010 --> 00:15:50,850 And we can kind of move on and start solving more interesting problems. 346 00:15:50,850 --> 00:15:56,700 But it would seem that we can't solve all problems because on my keyboard 347 00:15:56,700 --> 00:16:00,300 here, this American keyboard here, there's a whole bunch of symbols, 348 00:16:00,300 --> 00:16:03,810 like a 100 or 200 maybe in total if we actually hit shift and option and all 349 00:16:03,810 --> 00:16:04,560 of that. 350 00:16:04,560 --> 00:16:07,170 But what you don't see are some pretty common characters. 351 00:16:07,170 --> 00:16:09,010 Especially in a very international audience, 352 00:16:09,010 --> 00:16:12,490 what can I apparently not even type on this keyboard? 353 00:16:12,490 --> 00:16:13,410 What kinds of symbols? 354 00:16:13,410 --> 00:16:15,000 Yeah. 355 00:16:15,000 --> 00:16:16,190 Anything with an accent. 356 00:16:16,190 --> 00:16:18,240 If you have accents over vowels or other letters. 357 00:16:18,240 --> 00:16:19,740 What else? 358 00:16:19,740 --> 00:16:20,810 I'm sorry. 359 00:16:20,810 --> 00:16:22,560 Umlauts or other characters above letters. 360 00:16:22,560 --> 00:16:23,060 Yeah. 361 00:16:25,200 --> 00:16:26,170 Like pound symbol? 362 00:16:26,170 --> 00:16:26,670 Oh. 363 00:16:26,670 --> 00:16:27,710 Like the UK pound symbol. 364 00:16:27,710 --> 00:16:28,210 Sure. 365 00:16:28,210 --> 00:16:29,340 And other countries, too. 366 00:16:29,340 --> 00:16:30,790 Any number of Asian languages. 367 00:16:30,790 --> 00:16:33,410 There's so many symbols that are not depicted on this keyboard, 368 00:16:33,410 --> 00:16:36,630 and yet somehow, all of us with international keyboards or phones 369 00:16:36,630 --> 00:16:38,410 can surely express themselves. 370 00:16:38,410 --> 00:16:42,180 But that's because phones and computers these days don't just use ASCII. 371 00:16:42,180 --> 00:16:45,030 ASCII literally used just eight bits total. 372 00:16:45,030 --> 00:16:47,290 Technically seven, but then, ultimately, really eight. 373 00:16:47,290 --> 00:16:49,380 And with eight bits, if you actually do the math-- 374 00:16:49,380 --> 00:16:51,750 if you have eight bits or eight fingers, you 375 00:16:51,750 --> 00:16:56,130 can only permute them in 256 total possible ways, which 376 00:16:56,130 --> 00:17:01,620 is to say that you can only represent 256 characters using ASCII with numbers 377 00:17:01,620 --> 00:17:03,930 underneath the hood, and that's not enough to represent 378 00:17:03,930 --> 00:17:06,220 so many different symbols like those enumerated here. 379 00:17:06,220 --> 00:17:08,850 You can't represent any of the accents that you can nonetheless 380 00:17:08,850 --> 00:17:11,400 type on your Macs and PCs and you certainly 381 00:17:11,400 --> 00:17:13,740 can't type these things, which are very much in vogue. 382 00:17:13,740 --> 00:17:17,520 Which even though they're pictures, they're actually just characters. 383 00:17:17,520 --> 00:17:22,020 Because it turns out some years ago, the world decided eight bits is not enough. 384 00:17:22,020 --> 00:17:25,020 Let's start using something called Unicode, where you actually use 385 00:17:25,020 --> 00:17:28,200 one or two or three or even four bytes. 386 00:17:28,200 --> 00:17:33,450 So eight bits or 16 bits, 24 bits, or even 32 bits to represent characters. 387 00:17:33,450 --> 00:17:35,940 And now, we have the ability to represent thousands or even 388 00:17:35,940 --> 00:17:37,440 millions of characters. 389 00:17:37,440 --> 00:17:42,540 And frankly, daresay, the result of that huge amount of availability 390 00:17:42,540 --> 00:17:45,340 is partly why there are so many of these things these days. 391 00:17:45,340 --> 00:17:48,540 And they just keep making more because there's just so many different numbers 392 00:17:48,540 --> 00:17:49,320 available to us. 393 00:17:49,320 --> 00:17:52,950 So Unicode is often a specific version of it called UTF-8, 394 00:17:52,950 --> 00:17:54,210 which we'll see before long. 395 00:17:54,210 --> 00:17:56,060 But let me ask this question here. 396 00:17:56,060 --> 00:18:01,710 This is a crying face with joy, I think this is called. 397 00:18:01,710 --> 00:18:03,830 So it turns out, according to Apple or iOS, 398 00:18:03,830 --> 00:18:06,420 this is the most popular emoji that at least iPhone people 399 00:18:06,420 --> 00:18:07,710 are sending to each other. 400 00:18:07,710 --> 00:18:10,710 So when you're receiving this, though, if we can really take the fun out 401 00:18:10,710 --> 00:18:14,860 of this, what pattern of bits are you actually receiving from your friend? 402 00:18:14,860 --> 00:18:17,580 He or she is clearly trying to express some emotion, 403 00:18:17,580 --> 00:18:24,030 but really, what your friend is sending you-- the decimal number 128,514. 404 00:18:24,030 --> 00:18:26,670 Or really, if you looked at the zeros and ones coming 405 00:18:26,670 --> 00:18:29,220 to you over the internet or airwaves, you're 406 00:18:29,220 --> 00:18:33,540 getting this pattern of zeros and ones, which is hardly joyful or hardly 407 00:18:33,540 --> 00:18:36,060 descriptive, but all your phone or computer 408 00:18:36,060 --> 00:18:38,040 are doing is seeing this pattern of bits, 409 00:18:38,040 --> 00:18:40,530 looking it up in like a little cheat sheet, and saying, oh. 410 00:18:40,530 --> 00:18:44,220 Whenever I see this pattern of bits in the context of text like texting, 411 00:18:44,220 --> 00:18:47,490 I should actually display it as that picture. 412 00:18:47,490 --> 00:18:50,550 Now, that picture has a lot of yellow and other colors 413 00:18:50,550 --> 00:18:53,050 in it, but how do we even get there? 414 00:18:53,050 --> 00:18:55,410 Well, it turns out that this same pattern of numbers-- 415 00:18:55,410 --> 00:19:00,040 72, 73, 33-- which just to be sure, a moment ago, meant what? 416 00:19:00,040 --> 00:19:00,540 Hi. 417 00:19:00,540 --> 00:19:04,650 In the context of a textual program like Microsoft Word, Google Docs, texting, 418 00:19:04,650 --> 00:19:05,940 this means hi. 419 00:19:05,940 --> 00:19:09,900 But what if you saw this same pattern of bytes-- 420 00:19:09,900 --> 00:19:11,820 and again, we could draw the zeros and ones, 421 00:19:11,820 --> 00:19:13,820 but it's not interesting anymore, so we're going 422 00:19:13,820 --> 00:19:15,540 to abstract away at the decimal level. 423 00:19:15,540 --> 00:19:17,450 If you got this same pattern of zeros and ones 424 00:19:17,450 --> 00:19:21,060 or numbers in the context of like Photoshop or a browser 425 00:19:21,060 --> 00:19:24,900 or some kind of photo program, well, it might make more sense 426 00:19:24,900 --> 00:19:28,660 to interpret it not as text, but as imagery, some kind of colors. 427 00:19:28,660 --> 00:19:31,120 Well, it turns out there's this other system in the world-- 428 00:19:31,120 --> 00:19:32,820 you might have seen this acronym before-- 429 00:19:32,820 --> 00:19:35,560 called RGB-- red, green, blue. 430 00:19:35,560 --> 00:19:38,980 And this is just a way of humans having standardized years 431 00:19:38,980 --> 00:19:39,940 ago that you know what? 432 00:19:39,940 --> 00:19:42,910 If we want to represent a dot on someone's screen, otherwise known 433 00:19:42,910 --> 00:19:44,950 as a pixel a tiny little square on the screen 434 00:19:44,950 --> 00:19:47,880 of your phone, your laptop, or even TV these days, 435 00:19:47,880 --> 00:19:49,900 we're going to use three bytes-- 436 00:19:49,900 --> 00:19:54,010 one byte to specify how much red should be in that specific pixel, 437 00:19:54,010 --> 00:19:57,940 one more byte to specify how much green should be combined with red to form 438 00:19:57,940 --> 00:20:00,400 that pixel, and then one more byte, a third, 439 00:20:00,400 --> 00:20:03,550 to represent how much blue to combine with those other two colors 440 00:20:03,550 --> 00:20:05,620 to make a new color all together. 441 00:20:05,620 --> 00:20:09,230 So it's kind of like combining paints, except in this case, 442 00:20:09,230 --> 00:20:13,810 it's more really waves of light in order to get a specific color using just 443 00:20:13,810 --> 00:20:16,510 red, green, and blue as your palette. 444 00:20:16,510 --> 00:20:20,180 And so if we were to see this red, green, blue pattern and say, 445 00:20:20,180 --> 00:20:20,770 you know what? 446 00:20:20,770 --> 00:20:25,780 Give me 72 red, 73 of green, and 33 of blue. 447 00:20:25,780 --> 00:20:30,490 If the total possible range that I alluded to earlier is like 0 to 256, 448 00:20:30,490 --> 00:20:35,770 or technically 0 to 255 if you start counting in computer science from zero, 449 00:20:35,770 --> 00:20:38,590 this is like a medium amount of red, medium amount of green, 450 00:20:38,590 --> 00:20:42,140 and just a little bit of blue, if the range goes from 0 to 255. 451 00:20:42,140 --> 00:20:44,100 So if you combine these three things together 452 00:20:44,100 --> 00:20:46,790 and you want to know what color you get-- 453 00:20:46,790 --> 00:20:47,770 yeah. 454 00:20:47,770 --> 00:20:50,840 So it's kind of a light yellow that looks like this. 455 00:20:50,840 --> 00:20:54,360 So if a computer is storing a single dot on the screen that 456 00:20:54,360 --> 00:20:57,720 happens to be in yellow, what the computer's actually storing 457 00:20:57,720 --> 00:21:00,640 is not this dot physically, but a pattern of three 458 00:21:00,640 --> 00:21:03,600 bytes-- how much red, how much green, how much blue should the computer 459 00:21:03,600 --> 00:21:05,560 display at this particular point. 460 00:21:05,560 --> 00:21:10,230 So if we look at this crying face of joy and we kind of enhance or zoom in 461 00:21:10,230 --> 00:21:14,320 on it here, you can actually see it start to pixelate, so to speak, 462 00:21:14,320 --> 00:21:15,660 where you start to see the dots. 463 00:21:15,660 --> 00:21:17,970 If I punch in a little more, now you can really 464 00:21:17,970 --> 00:21:20,170 start to see the dots on the screen. 465 00:21:20,170 --> 00:21:22,230 And if I go an even further, you can actually 466 00:21:22,230 --> 00:21:26,250 see the tiny little squares that compose this image, most of which 467 00:21:26,250 --> 00:21:29,430 at the zoom level are yellow, but a bunch of which are black, 468 00:21:29,430 --> 00:21:33,310 a bunch of which are light black or dark yellow. 469 00:21:33,310 --> 00:21:35,790 And that's what composes this image ultimately. 470 00:21:35,790 --> 00:21:39,990 So this is to say if you count up all of the pixels on the screen 471 00:21:39,990 --> 00:21:43,260 and then multiply it by one, two, three bytes, that's 472 00:21:43,260 --> 00:21:46,950 how many bytes or kilobytes or megabytes, if you've heard those terms, 473 00:21:46,950 --> 00:21:51,670 are going to be stored on your computer just to represent an image. 474 00:21:51,670 --> 00:21:56,130 So we've gone from electricity to down here, so to speak, to zeros and ones, 475 00:21:56,130 --> 00:21:58,050 to decimal, now to colors. 476 00:21:58,050 --> 00:22:00,300 Well, with colors, you can get images. 477 00:22:00,300 --> 00:22:01,540 What comes after images? 478 00:22:01,540 --> 00:22:05,280 Well, we've all watched videos or movies certainly digitally these days. 479 00:22:05,280 --> 00:22:07,530 Well, what is a movie or a video file? 480 00:22:07,530 --> 00:22:09,220 How might that be implemented? 481 00:22:11,770 --> 00:22:13,380 Say it a little louder. 482 00:22:13,380 --> 00:22:13,880 Yeah. 483 00:22:13,880 --> 00:22:15,120 It's a collection of images. 484 00:22:15,120 --> 00:22:16,910 If you've ever heard of frames per second-- 485 00:22:16,910 --> 00:22:20,750 like movies tend to be 24 frames per second or 30 frames per second-- 486 00:22:20,750 --> 00:22:23,510 that just means that a typical movie, every second 487 00:22:23,510 --> 00:22:26,810 is showing you 24 or 30 images per second 488 00:22:26,810 --> 00:22:29,240 and they're just flying by so quickly that you actually 489 00:22:29,240 --> 00:22:31,970 don't notice you're just watching a sequence of static images. 490 00:22:31,970 --> 00:22:35,270 It's like as a kid, if you ever had one of those paper flip books where there's 491 00:22:35,270 --> 00:22:38,060 tons of drawings in them, and as you flip through the pages, 492 00:22:38,060 --> 00:22:40,770 you see things moving, but that's just because your eyes are just 493 00:22:40,770 --> 00:22:45,230 seeing little snapshots ever so quickly of something moving on the paper. 494 00:22:45,230 --> 00:22:47,010 That's all a video file actually is. 495 00:22:47,010 --> 00:22:49,850 So if you have an iPhone and you've ever played with these animojis, 496 00:22:49,850 --> 00:22:53,180 so to speak, well, all those are are little video files 497 00:22:53,180 --> 00:22:55,700 composed of lots and lots and lots of images 498 00:22:55,700 --> 00:23:00,060 that you have saved on your phone or texted to someone else. 499 00:23:00,060 --> 00:23:03,150 And if we just think, now, we're at the point of video, but that's OK. 500 00:23:03,150 --> 00:23:04,700 Videos are just bunches of images. 501 00:23:04,700 --> 00:23:06,380 Images are just bunches of colors. 502 00:23:06,380 --> 00:23:07,940 Colors are just patterns of bits. 503 00:23:07,940 --> 00:23:09,650 And bits, at the end of the day, are just 504 00:23:09,650 --> 00:23:12,800 the result of electricity coming into my machine or transistors 505 00:23:12,800 --> 00:23:14,570 turning switches on and off. 506 00:23:14,570 --> 00:23:18,170 Like we've all of a sudden to hold this entire story, but none of us 507 00:23:18,170 --> 00:23:21,710 ever is going to need to really think about binary in the context of videos 508 00:23:21,710 --> 00:23:25,400 because a video is just an abstraction on top of bunches of images, 509 00:23:25,400 --> 00:23:29,430 and images are just an abstraction on top bunches of pixels, and so forth. 510 00:23:29,430 --> 00:23:31,550 So we can keep painting this hierarchy that 511 00:23:31,550 --> 00:23:34,010 just allows us to talk about things at a more useful level, 512 00:23:34,010 --> 00:23:35,930 and the reason we had this conversation is 513 00:23:35,930 --> 00:23:38,480 just because we needed a way to represent 514 00:23:38,480 --> 00:23:41,440 inputs and outputs to problems. 515 00:23:41,440 --> 00:23:46,400 Let me pause there for just a second to see if there's any questions. 516 00:23:46,400 --> 00:23:48,830 Anything at all? 517 00:23:48,830 --> 00:23:49,460 No? 518 00:23:49,460 --> 00:23:49,960 All right. 519 00:23:49,960 --> 00:23:52,480 So what's inside this black box? 520 00:23:52,480 --> 00:23:56,080 Well, it turns out this is where the really interesting work starts 521 00:23:56,080 --> 00:23:58,100 to happen and the thought starts to come in. 522 00:23:58,100 --> 00:24:01,810 This is the proverbial algorithms, step by step instructions 523 00:24:01,810 --> 00:24:03,160 for solving some problem. 524 00:24:03,160 --> 00:24:05,410 And some of you might have solved this problem before, 525 00:24:05,410 --> 00:24:07,480 either digitally or textually, but of course, 526 00:24:07,480 --> 00:24:09,770 if you have contacts in your phone these days 527 00:24:09,770 --> 00:24:11,650 and you've got bunches of friends and family, 528 00:24:11,650 --> 00:24:14,040 odds are they're alphabetized by first name or last name. 529 00:24:14,040 --> 00:24:16,540 And you have auto complete these days, but it really is just 530 00:24:16,540 --> 00:24:18,300 a long list of names and numbers. 531 00:24:18,300 --> 00:24:20,890 That's not all that different from yesteryear's implementation 532 00:24:20,890 --> 00:24:24,640 of the same problem, which was this device here, a phonebook. 533 00:24:24,640 --> 00:24:28,120 Now, this phonebook might have a friend of ours in it, say Mike Smith, 534 00:24:28,120 --> 00:24:30,490 whose last name starts with S. And I could, of course, 535 00:24:30,490 --> 00:24:32,800 if trying to find Mike Smith, start by looking 536 00:24:32,800 --> 00:24:37,780 at the first page, the second page, the third page, the fourth page, 537 00:24:37,780 --> 00:24:39,790 and eventually, just hopefully find Mike Smith. 538 00:24:39,790 --> 00:24:42,640 Indeed, is this algorithm, this step by step process, 539 00:24:42,640 --> 00:24:45,710 correct for finding someone like Mike Smith? 540 00:24:45,710 --> 00:24:46,210 Yeah. 541 00:24:46,210 --> 00:24:46,940 It's correct. 542 00:24:46,940 --> 00:24:48,970 It's stupid and slow perhaps because it's 543 00:24:48,970 --> 00:24:51,430 going to take forever in a phone book of this size, 544 00:24:51,430 --> 00:24:54,390 but it is correct because if Mike's in here, I will, in fact, find him. 545 00:24:54,390 --> 00:24:55,520 But I could do this better. 546 00:24:55,520 --> 00:24:57,140 I could do it sort of two at a time. 547 00:24:57,140 --> 00:25:00,880 So two, four, six, eight, 10-- or imperfectly-- 548 00:25:00,880 --> 00:25:02,560 10, 12, 14. 549 00:25:02,560 --> 00:25:04,450 Is that faster? 550 00:25:04,450 --> 00:25:06,640 Obviously, it's going twice as fast. 551 00:25:06,640 --> 00:25:08,470 Is it correct? 552 00:25:08,470 --> 00:25:08,970 No. 553 00:25:08,970 --> 00:25:10,640 Why is it not correct? 554 00:25:10,640 --> 00:25:11,140 Yeah. 555 00:25:11,140 --> 00:25:12,260 I might miss him, right? 556 00:25:12,260 --> 00:25:15,560 Mike just accidentally might eventually get sandwiched between two pages 557 00:25:15,560 --> 00:25:18,380 and I have the unlucky experience of just missing him. 558 00:25:18,380 --> 00:25:19,430 Now, is it fixable? 559 00:25:19,430 --> 00:25:20,030 Yeah. 560 00:25:20,030 --> 00:25:24,440 I can probably, once I hit like SN or the T section, for instance-- 561 00:25:24,440 --> 00:25:26,690 I can just say, obviously, I've gone too far for Mike. 562 00:25:26,690 --> 00:25:28,830 Let me just double back one or just a few pages. 563 00:25:28,830 --> 00:25:30,030 So it is fixable. 564 00:25:30,030 --> 00:25:33,380 And so long as I've saved time by flying through this twice as fast, 565 00:25:33,380 --> 00:25:37,310 can I at least afford to spend a few more steps at the very end just 566 00:25:37,310 --> 00:25:38,420 to find Mike Smith? 567 00:25:38,420 --> 00:25:39,920 But none of us are going to do that. 568 00:25:39,920 --> 00:25:42,440 And our Apple devices and Android devices certainly 569 00:25:42,440 --> 00:25:44,330 don't do that for efficiency today. 570 00:25:44,330 --> 00:25:48,210 Odds are most of us are going to do what to find someone in any book like this? 571 00:25:48,210 --> 00:25:48,710 Yeah. 572 00:25:48,710 --> 00:25:51,740 Open to roughly the middle or maybe bias ourselves toward the end 573 00:25:51,740 --> 00:25:53,090 because S is after the middle. 574 00:25:53,090 --> 00:25:55,340 But you know, I'm in the middle of the phonebook here. 575 00:25:55,340 --> 00:25:58,730 And now, if I know that Mike is in the S's and therefore over here, 576 00:25:58,730 --> 00:26:01,260 where do I know he's not? 577 00:26:01,260 --> 00:26:03,200 He's not in the beginning and I can literally 578 00:26:03,200 --> 00:26:07,670 tear a problem like this in half, throw figuratively and literally half 579 00:26:07,670 --> 00:26:11,540 of the problem away, and be left with fundamentally the same problem, 580 00:26:11,540 --> 00:26:12,470 but it's half as big. 581 00:26:12,470 --> 00:26:13,670 I went from like-- whatever-- 582 00:26:13,670 --> 00:26:17,300 1,000 pages to 500 pages and I can now repeat this algorithm. 583 00:26:17,300 --> 00:26:18,160 I look down. 584 00:26:18,160 --> 00:26:19,120 I'm a little too far. 585 00:26:19,120 --> 00:26:20,450 I'm in the T section now. 586 00:26:20,450 --> 00:26:20,990 OK. 587 00:26:20,990 --> 00:26:25,010 I can again tear the problem in half, throw that half away, 588 00:26:25,010 --> 00:26:29,180 taking a 500 page byte out, a 250 page byte out, now leaving myself 589 00:26:29,180 --> 00:26:31,040 with just 250 pages more. 590 00:26:31,040 --> 00:26:32,720 And notice just how quickly I got here. 591 00:26:32,720 --> 00:26:39,260 The first two algorithms got me from 1,000 to 999 to 998, or 1,000 to 998 592 00:26:39,260 --> 00:26:40,910 to 996. 593 00:26:40,910 --> 00:26:43,760 But here, I went from 1,000 to 500 to 250. 594 00:26:43,760 --> 00:26:45,660 Feels like we're making up time here. 595 00:26:45,660 --> 00:26:47,540 And indeed, if I keep repeating this process, 596 00:26:47,540 --> 00:26:50,390 hopefully, I'll be left with just one page of the book 597 00:26:50,390 --> 00:26:55,100 that Mike is either on or not, at which point, I will call him. 598 00:26:55,100 --> 00:26:58,850 And so that's an algorithm that honestly leverages probably all the intuition we 599 00:26:58,850 --> 00:27:01,130 have and a lot of what programming is going to be, 600 00:27:01,130 --> 00:27:04,480 is thinking about a problem like this, figuring out how to divide and conquer 601 00:27:04,480 --> 00:27:06,500 it, and then expressing yourself in a way 602 00:27:06,500 --> 00:27:09,030 that the computer can then solve that problem for you. 603 00:27:09,030 --> 00:27:12,530 And just to paint a picture of how much better this algorithm is, well, 604 00:27:12,530 --> 00:27:15,890 if this is just a very abstract chart where we have on the vertical, 605 00:27:15,890 --> 00:27:18,530 or y-axis, how much time it takes to solve a problem, 606 00:27:18,530 --> 00:27:21,260 and on the horizontal axis how big the problem is-- 607 00:27:21,260 --> 00:27:24,330 so the farther out you go this way, the more pages in the problem, 608 00:27:24,330 --> 00:27:25,670 the more pages in the phonebook. 609 00:27:25,670 --> 00:27:28,070 And the higher you go up here, the more seconds or page 610 00:27:28,070 --> 00:27:29,270 turns it's going to take. 611 00:27:29,270 --> 00:27:32,720 That first algorithm is just like a linear slope, 612 00:27:32,720 --> 00:27:35,660 so to speak, because for every additional page in the book, 613 00:27:35,660 --> 00:27:37,190 it might take me one more second. 614 00:27:37,190 --> 00:27:38,670 Right, up. 615 00:27:38,670 --> 00:27:41,700 It's just a one for one relationship with pages. 616 00:27:41,700 --> 00:27:43,820 The second algorithm, if I plot it, where 617 00:27:43,820 --> 00:27:45,530 I'm flying through twice as fast, what is 618 00:27:45,530 --> 00:27:47,070 that line going to look like instead? 619 00:27:51,430 --> 00:27:51,930 Yeah. 620 00:27:51,930 --> 00:27:53,550 It's going to look lower than this one. 621 00:27:53,550 --> 00:27:56,550 It's still going to be a straight line because now, there's a two to one 622 00:27:56,550 --> 00:27:59,580 relationship, but if you've got a phone book that's got this many pages, 623 00:27:59,580 --> 00:28:02,910 and in the first algorithm, it took this long, here, 624 00:28:02,910 --> 00:28:07,410 well, in the second algorithm, it will take half as many steps, plus or minus 625 00:28:07,410 --> 00:28:09,840 or two if you need to actually double back a little bit. 626 00:28:09,840 --> 00:28:12,660 But that third algorithm is what we'll call logarithmic. 627 00:28:12,660 --> 00:28:15,030 If n is the number of pages in the phone book, 628 00:28:15,030 --> 00:28:16,890 the first algorithm, in the very worst case, 629 00:28:16,890 --> 00:28:18,640 might take all n pages to find Mike Smith. 630 00:28:18,640 --> 00:28:21,470 The second algorithm is going to take half as many steps because I'm 631 00:28:21,470 --> 00:28:22,920 flying through it two at a time. 632 00:28:22,920 --> 00:28:26,250 But the third algorithm is going to look and feel like this. 633 00:28:26,250 --> 00:28:30,720 It's going to be curved and ever so slowly rising and rising and rising, 634 00:28:30,720 --> 00:28:33,950 the implication of which is if Verizon or the phone company 635 00:28:33,950 --> 00:28:35,790 doubles the number of pages in the phonebook 636 00:28:35,790 --> 00:28:39,090 next year because Cambridge and Somerville merged together in the phone 637 00:28:39,090 --> 00:28:41,230 book and we now have 2,000 pages. 638 00:28:41,230 --> 00:28:45,150 Well, how many more steps does my third algorithm take? 639 00:28:45,150 --> 00:28:45,690 Just one. 640 00:28:45,690 --> 00:28:48,300 Because I can take a 1,000 page bite out of the problem 641 00:28:48,300 --> 00:28:52,140 with that clever algorithm, whereas my first two algorithms would take it 642 00:28:52,140 --> 00:28:54,150 one or just two pages at a time. 643 00:28:54,150 --> 00:28:58,740 So that is to say we have to hugely increase the size of this problem 644 00:28:58,740 --> 00:29:03,450 just for the number of seconds or page turns to appreciably actually increase. 645 00:29:03,450 --> 00:29:05,490 And so as we start to learn about programming, 646 00:29:05,490 --> 00:29:08,670 it's, again, going to be leveraging of this intuition in order 647 00:29:08,670 --> 00:29:13,260 to actually solve problems and code more effectively than we 648 00:29:13,260 --> 00:29:17,820 might without that intuition alone. 649 00:29:17,820 --> 00:29:19,560 So let's formalize this now. 650 00:29:19,560 --> 00:29:22,920 So that was kind of a very intuitive way of dividing and conquering a problem. 651 00:29:22,920 --> 00:29:24,750 Just kind of made sense to go in the middle, 652 00:29:24,750 --> 00:29:26,940 tear it, then go to the other half or the other half 653 00:29:26,940 --> 00:29:29,010 and tear it again, and so forth. 654 00:29:29,010 --> 00:29:33,990 But a computer, even as cool as Alexa and Google Home and all of this are, 655 00:29:33,990 --> 00:29:36,120 you can't really just talk to them as another human 656 00:29:36,120 --> 00:29:37,800 and have them execute things correctly. 657 00:29:37,800 --> 00:29:41,400 I struggle just to get Siri to set a timer on my phone. 658 00:29:41,400 --> 00:29:43,950 So we're not quite there yet, so we're still at the age 659 00:29:43,950 --> 00:29:46,830 where we have to be ever so precise with computers, 660 00:29:46,830 --> 00:29:50,430 voice activated or otherwise, and so thus enter pseudocode for now. 661 00:29:50,430 --> 00:29:52,560 Pseudocode has no formal definition. 662 00:29:52,560 --> 00:29:57,000 This is just a way of saying use English like syntax or any spoken language 663 00:29:57,000 --> 00:30:00,210 and just express yourself succinctly and correctly 664 00:30:00,210 --> 00:30:03,090 so that a computer or a robot or even another person 665 00:30:03,090 --> 00:30:05,640 can understand what it is you're trying to say. 666 00:30:05,640 --> 00:30:08,340 So here, I propose, is an algorithm written 667 00:30:08,340 --> 00:30:11,650 in pseudocode, English like syntax, that just gets my point across. 668 00:30:11,650 --> 00:30:13,530 And I could write this in any number of ways. 669 00:30:13,530 --> 00:30:16,680 I've numbered the steps from zero on up, just for the sake of discussion, 670 00:30:16,680 --> 00:30:18,900 but this would seem to capture what I did there. 671 00:30:18,900 --> 00:30:20,220 Pick up the phone book. 672 00:30:20,220 --> 00:30:21,840 Open to the middle of the phone. 673 00:30:21,840 --> 00:30:22,710 Look at the names. 674 00:30:22,710 --> 00:30:25,260 If Smith is among the names, call Mike. 675 00:30:25,260 --> 00:30:27,660 Else, if Mike Smith is earlier in the book, 676 00:30:27,660 --> 00:30:31,050 go to the left, specifically the middle of the left half of the book, 677 00:30:31,050 --> 00:30:32,910 and then go back to step two. 678 00:30:32,910 --> 00:30:35,650 Because indeed, I was just doing the same thing again and again, 679 00:30:35,650 --> 00:30:38,350 and the reason I wasn't doing it forever was because every time I 680 00:30:38,350 --> 00:30:42,060 repeated myself by opening and tearing, I was shrinking the problem. 681 00:30:42,060 --> 00:30:44,850 And I can only shrink a problem of some fixed finite size 682 00:30:44,850 --> 00:30:47,220 so many times until I get just one page, and so 683 00:30:47,220 --> 00:30:51,060 if I continue this logic looking to the right or to the left or just quitting, 684 00:30:51,060 --> 00:30:53,460 if I don't find Mike at all on the last page, 685 00:30:53,460 --> 00:30:56,700 this would seem to capture more precisely that code. 686 00:30:56,700 --> 00:30:59,640 Well, let's actually excerpt from this now a few concepts 687 00:30:59,640 --> 00:31:01,980 and then start to apply them to actual code. 688 00:31:01,980 --> 00:31:05,730 Highlighted in yellow here, I dare say, are all of the verbs or actions. 689 00:31:05,730 --> 00:31:08,550 These are the functions, as we're going to start calling them, 690 00:31:08,550 --> 00:31:09,330 in this algorithm. 691 00:31:09,330 --> 00:31:12,480 A function is just a specific step, a specific action 692 00:31:12,480 --> 00:31:15,600 you take in order to do something. 693 00:31:15,600 --> 00:31:19,800 And so in yellow here-- pick up, open to, look at, call, open, quit 694 00:31:19,800 --> 00:31:20,940 are all actions or verbs. 695 00:31:20,940 --> 00:31:23,400 Are henceforth, we'll call them functions. 696 00:31:23,400 --> 00:31:27,450 Meanwhile, highlighted in yellow here-- if, else if, else if, else. 697 00:31:27,450 --> 00:31:29,320 These are kind of starting to ask questions. 698 00:31:29,320 --> 00:31:32,620 What might these be called if you have some familiarity? 699 00:31:32,620 --> 00:31:33,120 Yes. 700 00:31:33,120 --> 00:31:35,490 Turns out many programming languages, if you've seen any before, 701 00:31:35,490 --> 00:31:36,650 would call these conditions. 702 00:31:36,650 --> 00:31:39,060 They're branches, or proverbial forks in the road. 703 00:31:39,060 --> 00:31:40,840 If this is true, go this way. 704 00:31:40,840 --> 00:31:44,160 Else, maybe go this other way, or perhaps a third or fourth direction 705 00:31:44,160 --> 00:31:45,030 altogether. 706 00:31:45,030 --> 00:31:48,630 Meanwhile, if we actually look at these highlighted phrases-- 707 00:31:48,630 --> 00:31:51,420 if Smith is among names or if Smith is earlier in book 708 00:31:51,420 --> 00:31:52,710 or Smith is later in book-- 709 00:31:52,710 --> 00:31:56,440 these are the specific questions we're asking in order to make that decision. 710 00:31:56,440 --> 00:31:59,130 These are known as Boolean expressions, named after a gentleman 711 00:31:59,130 --> 00:32:01,170 by the last name of Boole some years ago. 712 00:32:01,170 --> 00:32:03,120 And so a Boolean expression is just a question 713 00:32:03,120 --> 00:32:07,340 that has a yes or no answer, a true false answer, a one zero 714 00:32:07,340 --> 00:32:08,710 answer, if you will. 715 00:32:08,710 --> 00:32:11,340 And that's a nice mapping to what computers are really good at. 716 00:32:11,340 --> 00:32:13,420 So within conditions, you have Boolean expressions 717 00:32:13,420 --> 00:32:16,680 to decide which fork in the road you want to go down. 718 00:32:16,680 --> 00:32:18,510 And then lastly, highlighted in yellow here 719 00:32:18,510 --> 00:32:21,210 is go back to step 2 in a couple of places. 720 00:32:21,210 --> 00:32:23,940 This is inducing some kind of cycle or loop 721 00:32:23,940 --> 00:32:27,750 that's telling the computer to do something again and again and again. 722 00:32:27,750 --> 00:32:31,620 So in short, we have these building blocks already conceptually. 723 00:32:31,620 --> 00:32:34,260 And it turns out, we can now start to translate these 724 00:32:34,260 --> 00:32:36,220 to an actual programming language. 725 00:32:36,220 --> 00:32:38,310 The first of the languages we'll introduce in CS50 726 00:32:38,310 --> 00:32:39,830 is something called Scratch. 727 00:32:39,830 --> 00:32:41,670 Turns out this is not a text based language, 728 00:32:41,670 --> 00:32:44,130 like in my English pseudocode there, but it's graphical 729 00:32:44,130 --> 00:32:46,680 and things look like puzzle pieces that you can drag and drop 730 00:32:46,680 --> 00:32:48,870 and they interconnect if it makes logical sense to do so. 731 00:32:48,870 --> 00:32:51,390 And in fact, some of you might have played this back in the day as kids 732 00:32:51,390 --> 00:32:54,030 or even more recently because it's actually targeted typically 733 00:32:54,030 --> 00:32:55,910 at students in like after school programs who 734 00:32:55,910 --> 00:33:00,170 just want to learn more methodical, more algorithmic, or computational thinking. 735 00:33:00,170 --> 00:33:02,930 And we're going to use it to explore not only these building 736 00:33:02,930 --> 00:33:04,940 blocks, but a few others, as well. 737 00:33:04,940 --> 00:33:08,600 It turns out in the other languages we'll explore in CS50 and beyond, 738 00:33:08,600 --> 00:33:12,560 are languages like C that we'll actually transition to as quickly as next week, 739 00:33:12,560 --> 00:33:16,580 to then translate what we do this week in Scratch to next week in C. 740 00:33:16,580 --> 00:33:18,590 And in languages like Python and JavaScript 741 00:33:18,590 --> 00:33:21,650 and SQL, which we'll also explore, do we have other capabilities-- 742 00:33:21,650 --> 00:33:24,020 the ability to store data in variables, so to speak, 743 00:33:24,020 --> 00:33:27,830 to use threads, which means get the computer to do multiple things at once, 744 00:33:27,830 --> 00:33:30,920 events, to mean listen for things happening, like a click 745 00:33:30,920 --> 00:33:33,920 on the page or a human typing or even saying something. 746 00:33:33,920 --> 00:33:36,050 We'll be able to do all of the things that you take 747 00:33:36,050 --> 00:33:38,180 for granted in your very own phones. 748 00:33:38,180 --> 00:33:41,040 And we'll do this first by way of this guy. 749 00:33:41,040 --> 00:33:44,060 So this is Scratch, the default cat that comes with this programming 750 00:33:44,060 --> 00:33:45,890 language from MIT's media lab. 751 00:33:45,890 --> 00:33:48,990 And via Scratch can we start programming him to move up, 752 00:33:48,990 --> 00:33:52,550 down, left, right, say something, utter something, and other commands 753 00:33:52,550 --> 00:33:53,580 all together. 754 00:33:53,580 --> 00:33:56,720 In fact, let me go ahead and switch contexts here 755 00:33:56,720 --> 00:33:59,200 to show you the very first thing I ever wrote in Scratch. 756 00:33:59,200 --> 00:34:01,370 It was back in the day when I was in graduate school 757 00:34:01,370 --> 00:34:03,720 and Scratch had just been invented by MIT. 758 00:34:03,720 --> 00:34:05,540 Let me go ahead and open this. 759 00:34:05,540 --> 00:34:08,710 And I called it Oscar Time. 760 00:34:08,710 --> 00:34:11,630 And if we could perhaps have a volunteer come on up for just a moment. 761 00:34:11,630 --> 00:34:14,180 You have to be comfortable being on stage and on the internet. 762 00:34:14,180 --> 00:34:15,590 How about here in the white shirt? 763 00:34:15,590 --> 00:34:16,550 I saw your hand first. 764 00:34:16,550 --> 00:34:18,020 Come on down. 765 00:34:18,020 --> 00:34:19,460 So this is Oscar Time. 766 00:34:19,460 --> 00:34:21,740 It's implemented in a language called Scratch. 767 00:34:21,740 --> 00:34:25,850 And at the end of the day, all that is underneath the hood of this program 768 00:34:25,850 --> 00:34:29,190 is functions and loops and conditions and a few other of these concepts. 769 00:34:29,190 --> 00:34:29,690 Hi. 770 00:34:29,690 --> 00:34:30,080 What's your name? 771 00:34:30,080 --> 00:34:30,870 AVIVA: Aviva. 772 00:34:30,870 --> 00:34:31,280 DAVID MALAN: Aviva. 773 00:34:31,280 --> 00:34:31,710 David. 774 00:34:31,710 --> 00:34:32,410 Nice to meet you. 775 00:34:32,410 --> 00:34:33,980 Come on over here. 776 00:34:33,980 --> 00:34:37,070 And in just a moment, I'm going to go ahead and click 777 00:34:37,070 --> 00:34:39,230 the green flag at the top left hand corner, which 778 00:34:39,230 --> 00:34:40,460 is going to play this game. 779 00:34:40,460 --> 00:34:42,330 And we'll see on the screen the instructions. 780 00:34:42,330 --> 00:34:45,810 [MUSIC PLAYING] 781 00:34:45,810 --> 00:34:50,300 OSCAR: (SINGING) Oh, I love trash. 782 00:34:50,300 --> 00:34:54,500 Anything dirty or dingy or dusty. 783 00:34:54,500 --> 00:34:58,670 Anything ragged or rotten or rusty. 784 00:34:58,670 --> 00:35:02,710 Yes, I love trash. 785 00:35:02,710 --> 00:35:05,300 If you really want to see something trashy, look at this. 786 00:35:05,300 --> 00:35:09,400 I have here a sneaker that's tattered and worn. 787 00:35:09,400 --> 00:35:13,340 It's all full of holes and the laces are torn. 788 00:35:13,340 --> 00:35:17,710 A gift from my mother the day I was born. 789 00:35:17,710 --> 00:35:21,270 I love it because it's trash. 790 00:35:21,270 --> 00:35:25,650 Oh, I love trash. 791 00:35:25,650 --> 00:35:29,880 Anything dirty or dingy or dusty. 792 00:35:29,880 --> 00:35:34,190 Anything ragged or rotten or rusty. 793 00:35:34,190 --> 00:35:37,290 Yes, I love trash. 794 00:35:37,290 --> 00:35:40,610 Here's some more rotten stuff. 795 00:35:40,610 --> 00:35:43,370 I have here some newspaper 13 months old. 796 00:35:43,370 --> 00:35:44,330 DAVID MALAN: All right. 797 00:35:44,330 --> 00:35:46,950 Everybody, give a round of applause for Aviva for coming on up. 798 00:35:46,950 --> 00:35:47,750 Thank you. 799 00:35:47,750 --> 00:35:48,250 Here. 800 00:35:48,250 --> 00:35:48,750 Aviva. 801 00:35:48,750 --> 00:35:51,230 [APPLAUSE] 802 00:35:51,230 --> 00:35:53,180 A little CS50 stress ball. 803 00:35:53,180 --> 00:35:56,150 So suffice it to say, if you're tired of this song, 804 00:35:56,150 --> 00:35:59,900 consider how tired I was eight hours later while debugging and building 805 00:35:59,900 --> 00:36:00,590 this program. 806 00:36:00,590 --> 00:36:02,090 But consider what it is we just saw. 807 00:36:02,090 --> 00:36:05,610 It's this interactive game and stuff is animated and music is playing. 808 00:36:05,610 --> 00:36:09,560 But if you focus on decomposing, so to speak, this program into just 809 00:36:09,560 --> 00:36:13,520 basic building blocks, this is just kind of a big abstraction over some lower 810 00:36:13,520 --> 00:36:15,380 level pieces of functionality. 811 00:36:15,380 --> 00:36:16,580 Like this trash can here. 812 00:36:16,580 --> 00:36:18,830 At the moment, it's just a picture, and on occasion, 813 00:36:18,830 --> 00:36:21,580 as soon as Aviva dropped something into the trash, the lid came up 814 00:36:21,580 --> 00:36:24,680 and Oscar came out, he said something, and then he went back down. 815 00:36:24,680 --> 00:36:26,720 But that animation is super simplistic. 816 00:36:26,720 --> 00:36:31,490 It was just a sequence of 1, 2, 3, or so images displaying and then 817 00:36:31,490 --> 00:36:33,920 going back down to create the illusion of animation. 818 00:36:33,920 --> 00:36:36,290 Meanwhile, every time Oscar said something, 819 00:36:36,290 --> 00:36:39,320 that was keeping track of her score in what's called a variable. 820 00:36:39,320 --> 00:36:42,900 In algebra, you have x and y and z, but in programming, you have the same idea, 821 00:36:42,900 --> 00:36:45,530 but it's generally more useful to call them more descriptively, 822 00:36:45,530 --> 00:36:46,370 like your score. 823 00:36:46,370 --> 00:36:48,620 And so there's probably a variable in this game called 824 00:36:48,620 --> 00:36:51,290 score that was just keeping track of how many times 825 00:36:51,290 --> 00:36:53,240 Aviva had dropped something into the trash. 826 00:36:53,240 --> 00:36:57,650 Meanwhile, the trash itself and the shoe and the newspaper-- and even more 827 00:36:57,650 --> 00:37:00,470 things happen eventually-- were falling from the sky 828 00:37:00,470 --> 00:37:03,680 at sort of random locations, and that's because I programmed the game 829 00:37:03,680 --> 00:37:06,230 to sort of start the trash here or over here, just 830 00:37:06,230 --> 00:37:08,950 to make it a little more challenging as the game picked up. 831 00:37:08,950 --> 00:37:11,540 And in fact, things start falling faster and faster over time, 832 00:37:11,540 --> 00:37:14,550 like a typical game, getting more and more difficult. 833 00:37:14,550 --> 00:37:16,830 So how do we get to something like that? 834 00:37:16,830 --> 00:37:19,130 Well, let me go ahead and open up Scratch itself 835 00:37:19,130 --> 00:37:21,240 and introduce the environment. 836 00:37:21,240 --> 00:37:24,200 So in Scratch, you essentially have three general areas. 837 00:37:24,200 --> 00:37:26,660 And it's web based, and so you can do this on any computer. 838 00:37:26,660 --> 00:37:29,510 And in the left hand side here, you have those puzzle pieces 839 00:37:29,510 --> 00:37:30,920 to which I referred earlier. 840 00:37:30,920 --> 00:37:36,140 These puzzle pieces are all mapping to functions or loops or conditions 841 00:37:36,140 --> 00:37:38,140 or variables, things that we saw before, and I'm 842 00:37:38,140 --> 00:37:41,180 going to able to drag and drop them into the middle in order 843 00:37:41,180 --> 00:37:43,040 to interconnect them and write my program, 844 00:37:43,040 --> 00:37:44,430 which we'll do in just a moment. 845 00:37:44,430 --> 00:37:48,330 Meanwhile, Scratch lives in this stage, this world, where he can move up, 846 00:37:48,330 --> 00:37:49,250 down, left, right. 847 00:37:49,250 --> 00:37:50,870 You can change what Scratch looks like. 848 00:37:50,870 --> 00:37:53,300 You can add other characters, otherwise known as sprites, 849 00:37:53,300 --> 00:37:55,140 in order to have multiple things happening at once. 850 00:37:55,140 --> 00:37:56,680 And of course, you can fullscreen it. 851 00:37:56,680 --> 00:38:00,410 And so the Oscar Time game a moment ago was actually a whole bunch of sprites. 852 00:38:00,410 --> 00:38:02,250 Oscar's trash can was one. 853 00:38:02,250 --> 00:38:04,640 Each piece of trash was another sprite. 854 00:38:04,640 --> 00:38:07,050 The newspaper was a sprite, and so forth. 855 00:38:07,050 --> 00:38:11,250 So each of them were separate programs running in parallel at the same time. 856 00:38:11,250 --> 00:38:13,110 So let's actually make him do something. 857 00:38:13,110 --> 00:38:16,770 It turns out that if I jump down to, say, events, 858 00:38:16,770 --> 00:38:18,770 I'm going to see one of the most powerful blocks 859 00:38:18,770 --> 00:38:22,000 from the get go, which is this when green flag clicked. 860 00:38:22,000 --> 00:38:24,000 That's indeed how I started the game with Aviva, 861 00:38:24,000 --> 00:38:27,260 by clicking just above Scratch's world this green flag. 862 00:38:27,260 --> 00:38:29,030 And if I wanted to stop it, as I did, you 863 00:38:29,030 --> 00:38:31,130 can click the red stop sign to say stop. 864 00:38:31,130 --> 00:38:34,730 Meanwhile, the green flag, I can constantly 865 00:38:34,730 --> 00:38:37,220 listen for by dragging and dropping this puzzle piece. 866 00:38:37,220 --> 00:38:39,710 When the green flag is clicked, what do I want to do? 867 00:38:39,710 --> 00:38:41,470 Well, let me go up to looks. 868 00:38:41,470 --> 00:38:43,140 And these are just different categories. 869 00:38:43,140 --> 00:38:45,180 And we can scroll through all the different colorful blocks, 870 00:38:45,180 --> 00:38:47,240 but they pretty much just do what they say. 871 00:38:47,240 --> 00:38:50,370 I'm going to go under looks, where I know there to be a block that's called 872 00:38:50,370 --> 00:38:53,990 say, and I'm going to go ahead and type the most canonical computer science 873 00:38:53,990 --> 00:38:55,500 thing-- hello world-- 874 00:38:55,500 --> 00:38:56,160 in this box. 875 00:38:56,160 --> 00:38:58,550 So notice that functions themselves can actually 876 00:38:58,550 --> 00:39:01,610 take inputs and the input to this function, say, 877 00:39:01,610 --> 00:39:03,080 is going to be hello world. 878 00:39:03,080 --> 00:39:06,530 If I now go over to the green flag and click it-- 879 00:39:06,530 --> 00:39:07,530 hello world. 880 00:39:07,530 --> 00:39:08,030 All right. 881 00:39:08,030 --> 00:39:11,000 So not all that difficult. Not all that interesting. 882 00:39:11,000 --> 00:39:14,810 But it actually got the job done, and so my program is indeed just this. 883 00:39:14,810 --> 00:39:17,060 Well, how might I make this a little more interesting? 884 00:39:17,060 --> 00:39:19,730 Just saying, hello world all the time isn't all that compelling. 885 00:39:19,730 --> 00:39:20,620 Well, you know what? 886 00:39:20,620 --> 00:39:21,170 Let me think. 887 00:39:21,170 --> 00:39:22,550 Let me undo this. 888 00:39:22,550 --> 00:39:24,380 Let me scroll down to sensing. 889 00:39:24,380 --> 00:39:25,880 And notice this. 890 00:39:25,880 --> 00:39:28,490 Functions can also take input from a human 891 00:39:28,490 --> 00:39:32,270 and functions can hand you back a value, a so-called return value. 892 00:39:32,270 --> 00:39:35,090 So this block here, ask something-- by default, it says, 893 00:39:35,090 --> 00:39:36,500 what's your name and weight-- 894 00:39:36,500 --> 00:39:39,350 is another function built into Scratch that allows me to do this. 895 00:39:39,350 --> 00:39:41,420 So I'm going to go ahead and drag this here 896 00:39:41,420 --> 00:39:43,950 and I'm going to let it say, what's your name? 897 00:39:43,950 --> 00:39:46,990 Notice now that below this block is a special block, 898 00:39:46,990 --> 00:39:48,650 whatever it is the block returns. 899 00:39:48,650 --> 00:39:51,560 So answer is whatever the human is going to type in. 900 00:39:51,560 --> 00:39:56,060 And if I want to now say what the human typed in, let me go again to looks. 901 00:39:56,060 --> 00:39:57,060 Go to say. 902 00:39:57,060 --> 00:39:59,150 And notice that these blocks are kind of magnetic. 903 00:39:59,150 --> 00:40:00,340 They want to snap together. 904 00:40:00,340 --> 00:40:02,130 So I'm going to go ahead and let go there. 905 00:40:02,130 --> 00:40:05,160 And if I go back to sensing and grab answer, 906 00:40:05,160 --> 00:40:07,730 notice that even though it's not quite the same size, 907 00:40:07,730 --> 00:40:09,740 it's going to grow to fill, and now, I can 908 00:40:09,740 --> 00:40:12,890 have my program ask the user what his or her name is and then 909 00:40:12,890 --> 00:40:14,930 say whatever that answer is. 910 00:40:14,930 --> 00:40:18,020 So let me go ahead and stop and click play again. 911 00:40:18,020 --> 00:40:21,020 Notice it's asking me for my name, so let me go ahead and type in David. 912 00:40:21,020 --> 00:40:22,020 Enter. 913 00:40:22,020 --> 00:40:22,520 OK. 914 00:40:22,520 --> 00:40:24,770 It's a little weird way to greet someone. 915 00:40:24,770 --> 00:40:25,790 David. 916 00:40:25,790 --> 00:40:28,490 So it'd be nice to clean that up a bit. 917 00:40:28,490 --> 00:40:29,330 So you know what? 918 00:40:29,330 --> 00:40:31,380 I know this only from having poked around before. 919 00:40:31,380 --> 00:40:33,230 Not all of this is obvious at first glance. 920 00:40:33,230 --> 00:40:36,320 But it turns out that under operators, the category, 921 00:40:36,320 --> 00:40:39,070 there's this thing here-- join apple and banana. 922 00:40:39,070 --> 00:40:40,320 Which are just default values. 923 00:40:40,320 --> 00:40:41,460 You can change them. 924 00:40:41,460 --> 00:40:42,710 Because what do I want to do? 925 00:40:42,710 --> 00:40:47,720 I want to say hello, David, or whoever, so I kind of want to say hello, comma, 926 00:40:47,720 --> 00:40:49,710 and then, David-- whatever the human typed in. 927 00:40:49,710 --> 00:40:51,090 And that's what join lets you do. 928 00:40:51,090 --> 00:40:54,880 It lets you join or concatenate two phrases that are somehow 929 00:40:54,880 --> 00:40:56,140 provided by you or the user. 930 00:40:56,140 --> 00:40:58,690 So let me pull this out, the answer. 931 00:40:58,690 --> 00:41:00,750 Let me go ahead and grab the join block. 932 00:41:00,750 --> 00:41:02,650 Notice it, too, is going to grow to fill. 933 00:41:02,650 --> 00:41:09,010 Let me go ahead and say, hello, comma, space, and now, drag answer into there. 934 00:41:09,010 --> 00:41:11,770 And notice this nesting. 935 00:41:11,770 --> 00:41:12,520 Just like in math. 936 00:41:12,520 --> 00:41:14,360 This nesting of functions. 937 00:41:14,360 --> 00:41:18,700 I can first join hello and answer by taking those two things as input 938 00:41:18,700 --> 00:41:20,710 and then pass them to say as another input 939 00:41:20,710 --> 00:41:22,480 because these things are layered on top. 940 00:41:22,480 --> 00:41:26,420 And so now, if I stop this and play it again and say, David-- 941 00:41:26,420 --> 00:41:27,250 hello, David. 942 00:41:27,250 --> 00:41:30,700 Now, we have the makings of a more interesting interactive program 943 00:41:30,700 --> 00:41:32,290 that isn't just hardcoded. 944 00:41:32,290 --> 00:41:38,080 Of course, it's not nearly as audible as something like Oscar Time a moment ago. 945 00:41:38,080 --> 00:41:39,380 So let me go ahead and do this. 946 00:41:39,380 --> 00:41:43,660 Let me start over altogether and treat Scratch like the cat he is 947 00:41:43,660 --> 00:41:45,570 and just start the sound called meow. 948 00:41:45,570 --> 00:41:48,130 So it turns out there's a category of blocks called sound, 949 00:41:48,130 --> 00:41:50,740 and within sound, there is play some default sounds. 950 00:41:50,740 --> 00:41:52,400 So start sound meow. 951 00:41:52,400 --> 00:41:54,070 And now, things will get a little cuter. 952 00:41:54,070 --> 00:41:55,480 [MEOW] 953 00:41:55,980 --> 00:41:56,940 Aw. 954 00:41:56,940 --> 00:41:58,010 And now, again. 955 00:41:58,010 --> 00:41:59,010 [MEOW] 956 00:41:59,510 --> 00:42:03,200 And I can kind of simulate a cat by [MEOW] standing here for a while 957 00:42:03,200 --> 00:42:04,610 and keeping to click this button. 958 00:42:04,610 --> 00:42:05,360 But you know what? 959 00:42:05,360 --> 00:42:07,890 Let me make him meow few times because that's more realistic. 960 00:42:07,890 --> 00:42:09,580 So let me grab a second one and a third one. 961 00:42:09,580 --> 00:42:11,570 And you can get this infinite supply of blocks. 962 00:42:11,570 --> 00:42:12,230 Let me hit play. 963 00:42:12,230 --> 00:42:13,620 [MEOW] 964 00:42:16,490 --> 00:42:17,320 Seems like a bug. 965 00:42:17,320 --> 00:42:17,990 Let's try again. 966 00:42:17,990 --> 00:42:18,780 Play. 967 00:42:18,780 --> 00:42:20,140 [MEOW] 968 00:42:21,050 --> 00:42:23,570 This is my first bug, or mistake. 969 00:42:23,570 --> 00:42:24,710 This looks correct. 970 00:42:24,710 --> 00:42:28,700 It says when green flag clicked, start sound meow, start sound meow, 971 00:42:28,700 --> 00:42:30,020 start sound meow. 972 00:42:30,020 --> 00:42:33,490 Why am I only hearing one meow? 973 00:42:33,490 --> 00:42:34,980 Yeah. 974 00:42:34,980 --> 00:42:37,960 They're kind of at the same time or so close to the same time 975 00:42:37,960 --> 00:42:41,050 that the sounds are kind of tripping over each other and just overlapping, 976 00:42:41,050 --> 00:42:41,550 right? 977 00:42:41,550 --> 00:42:43,720 The block literally says, start sound meow. 978 00:42:43,720 --> 00:42:45,120 But computers are really fast. 979 00:42:45,120 --> 00:42:48,030 If you've heard of the expression gigahertz, that's a unit of measure. 980 00:42:48,030 --> 00:42:52,300 And if your computer has a one gigahertz CPU, central processing unit, or brain, 981 00:42:52,300 --> 00:42:55,660 that means it can literally do like a billion things per second. 982 00:42:55,660 --> 00:42:59,200 It can certainly start three sounds super fast. 983 00:42:59,200 --> 00:43:02,200 And if they're effectively all happening one after the other 984 00:43:02,200 --> 00:43:05,290 before the sound even finishes, you're just hearing one net effect. 985 00:43:05,290 --> 00:43:06,680 So how can we fix this? 986 00:43:06,680 --> 00:43:09,880 Well, I can actually go and fix this with this block here-- 987 00:43:09,880 --> 00:43:12,190 play sound meow until done. 988 00:43:12,190 --> 00:43:15,550 Play sound meow until done. 989 00:43:15,550 --> 00:43:16,240 And now. 990 00:43:16,240 --> 00:43:18,510 [MEOWING] 991 00:43:18,510 --> 00:43:19,010 OK. 992 00:43:19,010 --> 00:43:20,890 It's a little unhappy, this particular cat, 993 00:43:20,890 --> 00:43:22,730 but at least it's now more correct. 994 00:43:22,730 --> 00:43:25,420 And, as it turns out, if I go to control-- 995 00:43:25,420 --> 00:43:26,100 you know what? 996 00:43:26,100 --> 00:43:28,770 There's this block here-- wait some number of seconds. 997 00:43:28,770 --> 00:43:30,940 I can go ahead and insert this here. 998 00:43:30,940 --> 00:43:32,560 Let me do another one here. 999 00:43:32,560 --> 00:43:33,510 And now, hit play. 1000 00:43:33,510 --> 00:43:36,910 [MEOWING] 1001 00:43:38,730 --> 00:43:40,170 You know, it's not bad. 1002 00:43:40,170 --> 00:43:43,230 It now sounds a little more realistic. 1003 00:43:43,230 --> 00:43:45,510 But honestly, if I keep doing this-- or you know what? 1004 00:43:45,510 --> 00:43:47,840 You can actually right click or control click on blocks, 1005 00:43:47,840 --> 00:43:51,020 duplicate them, and just copy and paste even more if you want them. 1006 00:43:51,020 --> 00:43:55,440 So if I were to do this, now, it's just going to go six times. 1007 00:43:55,440 --> 00:43:57,390 And then I could copy it again and go 12 times. 1008 00:43:57,390 --> 00:43:59,140 But there's got to be a better way, right? 1009 00:43:59,140 --> 00:44:00,480 This is now bad programming. 1010 00:44:00,480 --> 00:44:01,770 This is bad design. 1011 00:44:01,770 --> 00:44:04,620 Because I'm literally copying and pasting, albeit, graphically. 1012 00:44:04,620 --> 00:44:07,170 But we've already seen a building block with which 1013 00:44:07,170 --> 00:44:09,390 we can design this program better. 1014 00:44:09,390 --> 00:44:11,190 It's correct, but it's not well designed. 1015 00:44:11,190 --> 00:44:15,820 What would the building block be that I need to make this a little cleaner? 1016 00:44:15,820 --> 00:44:16,320 OK. 1017 00:44:16,320 --> 00:44:16,860 A four loop. 1018 00:44:16,860 --> 00:44:18,150 Doesn't quite exist in Scratch. 1019 00:44:18,150 --> 00:44:21,300 But a loop fundamentally does something cyclically. 1020 00:44:21,300 --> 00:44:23,980 And indeed, if I go under control and start poking around, 1021 00:44:23,980 --> 00:44:26,910 you'll notice that there's a few blocks that might apply here. 1022 00:44:26,910 --> 00:44:29,010 There's the repeat block some number of times 1023 00:44:29,010 --> 00:44:32,400 or the forever block, both of which sound like loops, or cycles. 1024 00:44:32,400 --> 00:44:35,460 So sure enough, let me go ahead here and I can throw away blocks 1025 00:44:35,460 --> 00:44:37,110 by just dragging them to the left. 1026 00:44:37,110 --> 00:44:40,080 Let me pull this out for a second. 1027 00:44:40,080 --> 00:44:44,460 And then just say forever play this sound, and then wait one second. 1028 00:44:44,460 --> 00:44:47,070 So now, my program looks like this. 1029 00:44:47,070 --> 00:44:50,360 [MEOWING] 1030 00:44:53,770 --> 00:44:56,770 You know, we'll never know if it's technically correct because it's just 1031 00:44:56,770 --> 00:45:00,130 going to go, we think, forever, but it looks like this is correct. 1032 00:45:00,130 --> 00:45:02,320 And it was a lot less code and it's a lot easier 1033 00:45:02,320 --> 00:45:05,260 to maintain because if I want him to kind of get sleepy, 1034 00:45:05,260 --> 00:45:09,150 I can then maybe say two seconds instead. 1035 00:45:09,150 --> 00:45:10,560 [MEOW] 1036 00:45:11,060 --> 00:45:13,730 You know, and we can adjust this on the fly as we go. 1037 00:45:13,730 --> 00:45:15,710 But let's start to combine some of these ideas 1038 00:45:15,710 --> 00:45:19,460 now and change what it is the ultimate effect is. 1039 00:45:19,460 --> 00:45:22,310 Let me go ahead and open an example I made in advance. 1040 00:45:22,310 --> 00:45:24,260 This one's called Count Zero. 1041 00:45:24,260 --> 00:45:27,510 And we'll put this on the website later so that you can play with if you like. 1042 00:45:27,510 --> 00:45:29,630 And this is kind of the opposite of counting sheep. 1043 00:45:29,630 --> 00:45:32,180 Rather than me or the person sleeping counting sheep, 1044 00:45:32,180 --> 00:45:34,440 this sheep will count itself. 1045 00:45:34,440 --> 00:45:37,130 So let me go ahead and just play. 1046 00:45:37,130 --> 00:45:41,720 And adorably, he seems to just be counting 1, 2, 3. 1047 00:45:41,720 --> 00:45:42,800 But why is that? 1048 00:45:42,800 --> 00:45:44,420 He's just going to count forever. 1049 00:45:44,420 --> 00:45:47,330 But let's look at the blocks with which he is counting forever. 1050 00:45:47,330 --> 00:45:49,420 When green flag clicked, set counter. 1051 00:45:49,420 --> 00:45:51,720 Turns out this orange box is what we called a variable. 1052 00:45:51,720 --> 00:45:53,570 So in algebra, it would be like x or y or z. 1053 00:45:53,570 --> 00:45:54,780 Those are not descriptive. 1054 00:45:54,780 --> 00:45:58,490 I called this one counter instead, but I could have called it x or y or z. 1055 00:45:58,490 --> 00:46:03,080 And then I forever say the counter for one second, then wait one second, 1056 00:46:03,080 --> 00:46:04,910 and then change the counter by one, which 1057 00:46:04,910 --> 00:46:06,410 technically means just increment it. 1058 00:46:06,410 --> 00:46:07,430 Add 1 to it. 1059 00:46:07,430 --> 00:46:10,190 And the sheep is just going to therefore count up and up and up. 1060 00:46:10,190 --> 00:46:11,900 Now, this is a little tedious, but that's 1061 00:46:11,900 --> 00:46:15,120 kind of the point of counting sheep, of course, to fall asleep. 1062 00:46:15,120 --> 00:46:18,960 But what if the sheep actually kind of liked counting a little faster? 1063 00:46:18,960 --> 00:46:21,500 Well, let me go under operators here. 1064 00:46:21,500 --> 00:46:24,860 Multiplication sounds like it could get us places quicker. 1065 00:46:24,860 --> 00:46:27,260 And let me go ahead and go to variables. 1066 00:46:27,260 --> 00:46:29,830 And instead of changing the counter by one, 1067 00:46:29,830 --> 00:46:32,290 let me go ahead and just keep setting it to something else. 1068 00:46:32,290 --> 00:46:33,530 So let me drag and drop this. 1069 00:46:33,530 --> 00:46:38,360 Set the counter equal to something times something, specifically 1070 00:46:38,360 --> 00:46:44,810 the counter times two, thereby doubling, doubling, doubling, doubling. 1071 00:46:44,810 --> 00:46:47,250 That would seem to grow, so to speak, a lot faster. 1072 00:46:47,250 --> 00:46:49,400 Let's see. 1073 00:46:49,400 --> 00:46:54,210 1, 2, 4. 1074 00:46:54,210 --> 00:46:56,780 So he's counting faster, but it's still kind of tedious. 1075 00:46:56,780 --> 00:46:58,640 What if we instead do this? 1076 00:46:58,640 --> 00:47:02,660 Let's stop waiting and let's go ahead and, with the looks, 1077 00:47:02,660 --> 00:47:06,960 not say counter for one second, but let's just quickly say counter. 1078 00:47:06,960 --> 00:47:08,300 So I'm going to say the counter. 1079 00:47:08,300 --> 00:47:09,290 Whoops. 1080 00:47:09,290 --> 00:47:13,460 I'm going to say the counter and then I'm going to set it to itself times 2. 1081 00:47:13,460 --> 00:47:14,560 So here's where we're at. 1082 00:47:14,560 --> 00:47:18,950 Initialize, or set the counter to 1 initially, say it, then double it, 1083 00:47:18,950 --> 00:47:21,930 then double it, then double it, saying it along the way. 1084 00:47:21,930 --> 00:47:23,690 So here we go. 1085 00:47:23,690 --> 00:47:26,720 That's impressive. 1086 00:47:26,720 --> 00:47:32,320 So now the sheep has counted up to 10 to the 60th so far. 1087 00:47:32,320 --> 00:47:34,160 10 to the 100th. 1088 00:47:34,160 --> 00:47:34,660 OK. 1089 00:47:34,660 --> 00:47:39,370 Now, it doesn't even fit in the speech bubble, but he's still going. 1090 00:47:39,370 --> 00:47:41,380 How high can he go? 1091 00:47:41,380 --> 00:47:45,970 What's the biggest number you can count to in a computer? 1092 00:47:45,970 --> 00:47:49,410 Anyone want to guess? 1093 00:47:49,410 --> 00:47:51,400 Could be here a while. 1094 00:47:51,400 --> 00:47:54,130 10 to the 270th now. 1095 00:47:54,130 --> 00:47:57,530 How high can you count, or rather-- 1096 00:47:57,530 --> 00:47:58,030 OK. 1097 00:47:58,030 --> 00:48:01,010 So we gave up and just called it infinity. 1098 00:48:01,010 --> 00:48:03,340 So it turns out infinity does have a precise value-- 1099 00:48:03,340 --> 00:48:05,680 10 to the 250th or so. 1100 00:48:05,680 --> 00:48:07,150 But what happens here? 1101 00:48:07,150 --> 00:48:09,490 Well, because computers, at the end of the day, 1102 00:48:09,490 --> 00:48:12,700 are just storing information digitally-- but that information digitally has 1103 00:48:12,700 --> 00:48:15,640 to be physically stored using electricity, using these lower level 1104 00:48:15,640 --> 00:48:16,990 switches called transistors. 1105 00:48:16,990 --> 00:48:20,200 At the end of the day, my phone, my laptop, whatever device in question 1106 00:48:20,200 --> 00:48:22,510 only has a finite amount of those things. 1107 00:48:22,510 --> 00:48:24,380 I only have a finite number of fingers. 1108 00:48:24,380 --> 00:48:26,800 Using unary, my old school hashmark approach, 1109 00:48:26,800 --> 00:48:28,360 I can count to five on this hand. 1110 00:48:28,360 --> 00:48:32,380 Using binary, I claimed I could count to 31 on this hand. 1111 00:48:32,380 --> 00:48:33,610 But it's still finite. 1112 00:48:33,610 --> 00:48:37,270 I cannot count to infinity on this hand because I only have five fingers. 1113 00:48:37,270 --> 00:48:40,570 Similarly does a computer only have so many transistors or so 1114 00:48:40,570 --> 00:48:43,240 many bytes or bits of memory, and at some point, 1115 00:48:43,240 --> 00:48:45,790 the programmer has to think about what is he 1116 00:48:45,790 --> 00:48:48,580 or she going to do when the user wants to count so high that you 1117 00:48:48,580 --> 00:48:51,080 can't physically fit it anymore. 1118 00:48:51,080 --> 00:48:54,310 You have to give up like this and say something semi accurately 1119 00:48:54,310 --> 00:48:57,980 or you have to handle that issue in some other way. 1120 00:48:57,980 --> 00:49:01,180 And we'll see when we get to C that how you handle this problem is not 1121 00:49:01,180 --> 00:49:03,730 necessarily straightforward, and indeed a lot of software 1122 00:49:03,730 --> 00:49:05,610 out there does not handle this problem. 1123 00:49:05,610 --> 00:49:07,660 And odds are, all of us have programs that if you 1124 00:49:07,660 --> 00:49:10,180 type big enough words or big enough numbers into them, 1125 00:49:10,180 --> 00:49:12,970 they might very well break or crash or freeze 1126 00:49:12,970 --> 00:49:16,600 because the humans, unlike MIT, did not anticipate that that might actually 1127 00:49:16,600 --> 00:49:17,890 happen and handle it. 1128 00:49:17,890 --> 00:49:19,310 Well, let me go ahead and do this. 1129 00:49:19,310 --> 00:49:22,780 Let me open up this program and see if we can't read the code now. 1130 00:49:22,780 --> 00:49:28,330 This is called Pet Zero and this is a program that simulates petting. 1131 00:49:28,330 --> 00:49:31,630 So if I click play and don't touch the keyboard, 1132 00:49:31,630 --> 00:49:35,350 nothing seems to be happening, but if I now move my cursor over to the cat-- 1133 00:49:35,350 --> 00:49:36,800 [MEOW] 1134 00:49:36,800 --> 00:49:37,770 Aw. 1135 00:49:37,770 --> 00:49:38,590 It's kind of cute. 1136 00:49:38,590 --> 00:49:39,280 [MEOW] 1137 00:49:39,280 --> 00:49:42,790 Right now, it's more only meowing on demand when you pet the cat. 1138 00:49:42,790 --> 00:49:43,480 Why? 1139 00:49:43,480 --> 00:49:45,640 Well, notice I've added some other building blocks. 1140 00:49:45,640 --> 00:49:48,130 We haven't used this one before, but it intuitively 1141 00:49:48,130 --> 00:49:49,630 probably makes pretty clear sense. 1142 00:49:49,630 --> 00:49:52,660 When the green flag is clicked, forever do the following. 1143 00:49:52,660 --> 00:49:56,070 If the cat is touching the mouse pointer-- this thing in blue 1144 00:49:56,070 --> 00:49:58,030 is what we called earlier a Boolean expression. 1145 00:49:58,030 --> 00:50:00,760 It has a yes/no, a true/false, a one/zero answer. 1146 00:50:00,760 --> 00:50:03,560 And touching mouse pointer is one of the options in the little drop 1147 00:50:03,560 --> 00:50:05,150 down here if you tinker with it. 1148 00:50:05,150 --> 00:50:08,290 So if the cat is touching the mouse pointer, then and only then, 1149 00:50:08,290 --> 00:50:10,450 play sound meow until done. 1150 00:50:10,450 --> 00:50:14,200 So we've combined now functions with loops with a condition. 1151 00:50:14,200 --> 00:50:16,330 But why the loop? 1152 00:50:16,330 --> 00:50:19,390 The cat's only meowing once when I pet him. 1153 00:50:19,390 --> 00:50:21,580 Why am I doing anything forever here? 1154 00:50:24,490 --> 00:50:25,530 Someone-- yeah. 1155 00:50:28,600 --> 00:50:29,100 Yeah. 1156 00:50:29,100 --> 00:50:32,910 I might want to pet it again, so I want the program to anticipate that. 1157 00:50:32,910 --> 00:50:36,480 And honestly, if I omitted this forever block and my program 1158 00:50:36,480 --> 00:50:39,130 instead looked just like this-- 1159 00:50:39,130 --> 00:50:40,890 so let me get rid of that and this-- 1160 00:50:40,890 --> 00:50:45,780 and then I clicked play, and now, I hover over him, 1161 00:50:45,780 --> 00:50:47,550 why is it not working even once? 1162 00:50:51,540 --> 00:50:52,090 Say it again. 1163 00:50:56,700 --> 00:50:57,200 Yeah. 1164 00:50:57,200 --> 00:51:00,740 So I mean, at this point, if I can summarize, 1165 00:51:00,740 --> 00:51:03,080 the computer is so damn fast that this already 1166 00:51:03,080 --> 00:51:07,190 happened by the time I moved my cursor over to the cat, and at the moment 1167 00:51:07,190 --> 00:51:10,220 I clicked play, I was not touching the cat. 1168 00:51:10,220 --> 00:51:12,780 Those blocks executed, so to speak, top to bottom. 1169 00:51:12,780 --> 00:51:13,970 That's it for the program. 1170 00:51:13,970 --> 00:51:16,970 So by the time I move the cursor over to the cat, the program is over. 1171 00:51:16,970 --> 00:51:17,760 It's not listening. 1172 00:51:17,760 --> 00:51:21,960 And so forever, this way I can actually listen in perpetuity for something 1173 00:51:21,960 --> 00:51:23,430 to actually happen. 1174 00:51:23,430 --> 00:51:26,000 What if I want to do something not just if something is true, 1175 00:51:26,000 --> 00:51:27,300 but handle two cases? 1176 00:51:27,300 --> 00:51:28,340 If or else. 1177 00:51:28,340 --> 00:51:31,130 Well, let me go ahead and open up Pet One. 1178 00:51:31,130 --> 00:51:32,870 And this is another example. 1179 00:51:32,870 --> 00:51:37,310 And could someone perhaps describe, after reading this code, what 1180 00:51:37,310 --> 00:51:38,810 this program is going to do instead? 1181 00:51:42,290 --> 00:51:42,790 Yeah. 1182 00:51:53,790 --> 00:51:54,300 Exactly. 1183 00:51:54,300 --> 00:51:55,750 And let me summarize more verbally. 1184 00:51:55,750 --> 00:51:59,590 So if this time, you're touching the cat, it's going to roar instead. 1185 00:51:59,590 --> 00:52:01,950 Else, it's just going to meow sweetly. 1186 00:52:01,950 --> 00:52:05,830 So this time, it is meowing perpetually once every second, 1187 00:52:05,830 --> 00:52:08,850 but if you touch this particular cat, it doesn't like it. 1188 00:52:08,850 --> 00:52:09,660 So play. 1189 00:52:09,660 --> 00:52:10,570 [MEOW] 1190 00:52:10,570 --> 00:52:11,070 Meow. 1191 00:52:11,070 --> 00:52:12,310 [MEOW] 1192 00:52:12,310 --> 00:52:13,130 Meow. 1193 00:52:13,130 --> 00:52:14,350 [MEOW] 1194 00:52:14,350 --> 00:52:15,360 And now. 1195 00:52:15,360 --> 00:52:16,320 [ROAR] 1196 00:52:16,320 --> 00:52:17,640 Don't touch the cat. 1197 00:52:17,640 --> 00:52:20,160 So now, we might interact in two different ways 1198 00:52:20,160 --> 00:52:22,970 by having two different roads that you can go down. 1199 00:52:22,970 --> 00:52:25,560 Well, let's actually make something a little more interactive. 1200 00:52:25,560 --> 00:52:27,730 Let me go ahead and open another example. 1201 00:52:27,730 --> 00:52:29,850 This one's called Bounce Zero because now, we 1202 00:52:29,850 --> 00:52:33,690 can start to see some design elements from what Oscar Time was. 1203 00:52:33,690 --> 00:52:36,360 Like this now it's getting a little interesting. 1204 00:52:36,360 --> 00:52:38,350 What is actually going on here? 1205 00:52:38,350 --> 00:52:40,470 So let me zoom in on the blocks here. 1206 00:52:40,470 --> 00:52:43,980 This block is just saying forever move 10 steps, which 1207 00:52:43,980 --> 00:52:45,360 is another block we haven't seen. 1208 00:52:45,360 --> 00:52:46,920 But 10 steps is like 10 pixels. 1209 00:52:46,920 --> 00:52:48,930 So move 10 pixels on the screen. 1210 00:52:48,930 --> 00:52:52,330 But if you're touching the edge, then turn around 180 degrees. 1211 00:52:52,330 --> 00:52:54,200 And you can see exactly that happening. 1212 00:52:54,200 --> 00:52:57,540 Scratch is turning around 180 degrees and this rotation style just 1213 00:52:57,540 --> 00:52:58,320 means double back. 1214 00:52:58,320 --> 00:53:00,810 Don't like loop around 180 degrees. 1215 00:53:00,810 --> 00:53:02,020 So that's kind of cool. 1216 00:53:02,020 --> 00:53:05,100 But this is not how humans or cats walk. 1217 00:53:05,100 --> 00:53:10,340 Like what is obviously unnatural about this? 1218 00:53:10,340 --> 00:53:10,840 Yeah. 1219 00:53:10,840 --> 00:53:12,840 I mean, I mean, I can't even simulate it, right? 1220 00:53:12,840 --> 00:53:15,580 Like his feet are in static position, yet sliding back and forth 1221 00:53:15,580 --> 00:53:16,540 on the screen. 1222 00:53:16,540 --> 00:53:18,160 And yet, that is not what walking is. 1223 00:53:18,160 --> 00:53:20,950 Like walking presumably has some kind of movement and what? 1224 00:53:20,950 --> 00:53:22,870 Well, we could just kind of simulate it like-- 1225 00:53:22,870 --> 00:53:24,190 OK, I could just walk-- 1226 00:53:24,190 --> 00:53:28,420 walking and you can imagine taking like really quick photographs of my legs 1227 00:53:28,420 --> 00:53:31,450 or the cat's leg moving and then just deciding this photo 1228 00:53:31,450 --> 00:53:33,400 will be representative of one step. 1229 00:53:33,400 --> 00:53:35,530 This photo will be representative another. 1230 00:53:35,530 --> 00:53:37,810 And you know, with just two of those steps, 1231 00:53:37,810 --> 00:53:41,320 I'd wager we could actually do a pretty good job of simulating 1232 00:53:41,320 --> 00:53:42,620 what walking looks like. 1233 00:53:42,620 --> 00:53:46,210 In fact, if I go back to where we began, this picture of Scratch, 1234 00:53:46,210 --> 00:53:51,040 what if I just move his legs ever so slightly, then go back, 1235 00:53:51,040 --> 00:53:52,280 then go forward? 1236 00:53:52,280 --> 00:53:56,050 And even just in my PDF, I can simulate animation by hitting up arrow, 1237 00:53:56,050 --> 00:53:58,670 down arrow, up arrow, down arrow because it kind of 1238 00:53:58,670 --> 00:54:01,720 looks like he's walking now, when really, your human eyes are just seeing 1239 00:54:01,720 --> 00:54:04,250 two different pictures again and again. 1240 00:54:04,250 --> 00:54:05,340 So how can I do this? 1241 00:54:05,340 --> 00:54:08,350 Well, if I go back to Scratch, he's still walking. 1242 00:54:08,350 --> 00:54:15,500 Let me go ahead and open up Bounce One, the second version of this, and now, 1243 00:54:15,500 --> 00:54:16,300 do this. 1244 00:54:16,300 --> 00:54:18,630 OK. 1245 00:54:18,630 --> 00:54:19,570 So how did I add this? 1246 00:54:19,570 --> 00:54:21,870 There's a little purple block that we haven't seen yet, 1247 00:54:21,870 --> 00:54:23,740 but if you poke around the categories, you'll 1248 00:54:23,740 --> 00:54:26,070 see other blocks like this next costume that just keeps 1249 00:54:26,070 --> 00:54:28,230 changing the costume that he's wearing. 1250 00:54:28,230 --> 00:54:31,860 It turns out Scratch exists as a picture and his default picture 1251 00:54:31,860 --> 00:54:35,760 is him not moving, but if I go up here to top left and click costumes, 1252 00:54:35,760 --> 00:54:38,220 you can actually see that here's his one costume. 1253 00:54:38,220 --> 00:54:39,960 Here's his second costume. 1254 00:54:39,960 --> 00:54:43,140 And so that purple block that says next costume, because it's 1255 00:54:43,140 --> 00:54:47,460 in the forever loop, it just keeps doing next, next, next, next, next, just 1256 00:54:47,460 --> 00:54:49,240 showing one costume or the other. 1257 00:54:49,240 --> 00:54:52,210 They're clearly mimicking walking. 1258 00:54:52,210 --> 00:54:53,640 Now, this is not very unnatural. 1259 00:54:53,640 --> 00:54:59,040 Why don't we slow him down to, say, five steps at a time and have him go again? 1260 00:54:59,040 --> 00:55:00,660 Now, this is still going pretty fast. 1261 00:55:00,660 --> 00:55:03,240 Let me go ahead and say-- 1262 00:55:03,240 --> 00:55:05,010 we could have control. 1263 00:55:05,010 --> 00:55:11,100 We could have him wait a second after moving very dramatically. 1264 00:55:11,100 --> 00:55:12,550 We could probably speed this up. 1265 00:55:12,550 --> 00:55:15,540 So let's wait 1/10 of a second, 0.1. 1266 00:55:15,540 --> 00:55:18,650 Or maybe let's do 0.01, 1/100 of a second. 1267 00:55:18,650 --> 00:55:20,400 Now, it's getting a little more realistic. 1268 00:55:20,400 --> 00:55:21,810 But this is what animation is. 1269 00:55:21,810 --> 00:55:25,510 If you've ever watched a cartoon or a movie based on pictures like this, 1270 00:55:25,510 --> 00:55:28,530 you're just tinkering with some of these parameters, these inputs, 1271 00:55:28,530 --> 00:55:30,780 in order to produce this output by understanding 1272 00:55:30,780 --> 00:55:34,260 what the fundamental representation of these things is, which in this case 1273 00:55:34,260 --> 00:55:37,200 are just pictures, again and again and again in order 1274 00:55:37,200 --> 00:55:38,700 to create that animation. 1275 00:55:38,700 --> 00:55:40,400 But what about interactivity? 1276 00:55:40,400 --> 00:55:41,490 Let me do this one myself. 1277 00:55:41,490 --> 00:55:45,960 Let me go ahead and get rid of this, go back to events, and say, 1278 00:55:45,960 --> 00:55:47,670 when green flag clicked. 1279 00:55:47,670 --> 00:55:50,250 Then, let me go ahead and grab a forever block 1280 00:55:50,250 --> 00:55:52,230 so that this keeps going again and again. 1281 00:55:52,230 --> 00:55:54,150 And then, let me go ahead to go to motion. 1282 00:55:54,150 --> 00:55:57,270 It turns out that under motion, there's this block we haven't seen-- 1283 00:55:57,270 --> 00:55:59,340 point towards the mouse pointer. 1284 00:55:59,340 --> 00:56:01,650 And let me go ahead and pull this in here. 1285 00:56:01,650 --> 00:56:06,480 And then, let me have it move just like one step at a time, instead of 10. 1286 00:56:06,480 --> 00:56:09,560 What is this going to do? 1287 00:56:09,560 --> 00:56:12,490 What's this program do? 1288 00:56:12,490 --> 00:56:12,990 Yeah. 1289 00:56:12,990 --> 00:56:14,370 Say it again. 1290 00:56:14,370 --> 00:56:15,280 Follow the mouse. 1291 00:56:15,280 --> 00:56:15,780 Yeah. 1292 00:56:15,780 --> 00:56:20,580 This is kind of like a way of taking your cat for a walk. 1293 00:56:20,580 --> 00:56:24,930 Perhaps not quite the animal we intended, but he'll follow the cursor. 1294 00:56:24,930 --> 00:56:27,170 And I can actually speed this up a little bit. 1295 00:56:27,170 --> 00:56:28,780 So let's have him move 10 steps. 1296 00:56:28,780 --> 00:56:29,280 OK. 1297 00:56:29,280 --> 00:56:32,020 Now, there we go. 1298 00:56:32,020 --> 00:56:34,960 So now, he's moving up and down, and so now, it's interactive. 1299 00:56:34,960 --> 00:56:38,650 So you might recall that when we were playing Oscar Time earlier and picking 1300 00:56:38,650 --> 00:56:39,150 up-- 1301 00:56:39,150 --> 00:56:40,530 OK. 1302 00:56:40,530 --> 00:56:41,110 Don't do that. 1303 00:56:41,110 --> 00:56:42,870 See, that's a bug. 1304 00:56:42,870 --> 00:56:43,710 He's just confused. 1305 00:56:43,710 --> 00:56:45,660 He's constantly moving toward it, but you're already-- 1306 00:56:45,660 --> 00:56:45,940 OK. 1307 00:56:45,940 --> 00:56:46,930 So we're going to stop. 1308 00:56:46,930 --> 00:56:47,430 OK. 1309 00:56:47,430 --> 00:56:50,610 So now, he's following, but that's how we might now create, 1310 00:56:50,610 --> 00:56:52,980 for instance, the ability to move those pieces of trash 1311 00:56:52,980 --> 00:56:54,950 around and have them follow the mouse cursor. 1312 00:56:54,950 --> 00:56:58,070 If you think back to Oscar Time, every time you picked up a piece of trash, 1313 00:56:58,070 --> 00:57:01,320 he'd follow the cursor because there was a forever loop and a block like this 1314 00:57:01,320 --> 00:57:03,180 pointing toward the mouse pointer. 1315 00:57:03,180 --> 00:57:05,760 Well, now, let's integrate multiple ideas 1316 00:57:05,760 --> 00:57:07,590 and actually have multiple scripts. 1317 00:57:07,590 --> 00:57:12,240 I proposed earlier that programs can actually have multiple threads. 1318 00:57:12,240 --> 00:57:15,780 A thread is just a fancy way of saying, in our context, multiple scripts. 1319 00:57:15,780 --> 00:57:21,120 Multiple scripts in one program that are happening essentially in parallel. 1320 00:57:21,120 --> 00:57:24,570 A computer can effectively do multiple things at a time thanks to threading, 1321 00:57:24,570 --> 00:57:26,010 and more on that down the road. 1322 00:57:26,010 --> 00:57:27,720 So these are more involved, but let's see 1323 00:57:27,720 --> 00:57:30,120 if we can-- let's understand first what this program does. 1324 00:57:30,120 --> 00:57:31,320 Let me go ahead and hit play. 1325 00:57:31,320 --> 00:57:33,950 And this one tends to be a little loud. 1326 00:57:33,950 --> 00:57:37,180 [SEA LION BARKING] 1327 00:57:38,120 --> 00:57:42,800 So the sea lion is just barking endlessly, annoyingly. 1328 00:57:42,800 --> 00:57:48,650 So by reading the code, how can I stop him from barking? 1329 00:57:48,650 --> 00:57:49,890 Hit the spacebar. 1330 00:57:49,890 --> 00:57:50,390 All right. 1331 00:57:50,390 --> 00:57:52,770 So hit the spacebar. 1332 00:57:52,770 --> 00:57:53,750 OK. 1333 00:57:53,750 --> 00:57:55,700 I could just stop the program, obviously, 1334 00:57:55,700 --> 00:57:57,800 but this program is still running, technically. 1335 00:57:57,800 --> 00:57:59,270 But why did that work? 1336 00:57:59,270 --> 00:58:02,300 Well, notice this on the left hand side is the first script. 1337 00:58:02,300 --> 00:58:06,370 When the green flag is clicked, set this variable that I called muted to false. 1338 00:58:06,370 --> 00:58:08,960 Could have called it x or y or z or counter, but none of those 1339 00:58:08,960 --> 00:58:10,620 really make sense, so I called it muted. 1340 00:58:10,620 --> 00:58:13,400 And I set it equal to false, which is, again, a Boolean value. 1341 00:58:13,400 --> 00:58:16,170 True or false just mean yes or no. 1342 00:58:16,170 --> 00:58:19,920 Forever, if the keyspace is pressed, then do this. 1343 00:58:19,920 --> 00:58:24,200 If muted is currently false, then change muted to true. 1344 00:58:24,200 --> 00:58:26,670 Else, change muted to false. 1345 00:58:26,670 --> 00:58:29,510 So if muted is false, change it to true. 1346 00:58:29,510 --> 00:58:31,840 If muted is true, change it to false. 1347 00:58:31,840 --> 00:58:35,910 Any time the human hits the spacebar, update that variable. 1348 00:58:35,910 --> 00:58:39,500 Now, if we look at the other script, which is also driving the sea lion, 1349 00:58:39,500 --> 00:58:40,530 what is he doing? 1350 00:58:40,530 --> 00:58:43,580 Forever, if muted is false. 1351 00:58:43,580 --> 00:58:44,750 So if he's not muted. 1352 00:58:44,750 --> 00:58:46,820 If muted is false means not muted. 1353 00:58:46,820 --> 00:58:50,360 Start the sound sea lion and then think hi, hi, hi for two seconds, 1354 00:58:50,360 --> 00:58:52,160 and then wait for one more second. 1355 00:58:52,160 --> 00:58:53,900 And then just repeat, repeat, repeat. 1356 00:58:53,900 --> 00:58:56,750 But if I change with the spacebar muted to true, 1357 00:58:56,750 --> 00:59:00,230 he's going to say if muted equals false, that's not so. 1358 00:59:00,230 --> 00:59:01,950 I'm not going to play a sound this time. 1359 00:59:01,950 --> 00:59:05,360 And so now, we have the ability to integrate multiple scripts together 1360 00:59:05,360 --> 00:59:08,390 in order to achieve a more interactive result. 1361 00:59:08,390 --> 00:59:09,670 And what about this? 1362 00:59:09,670 --> 00:59:15,560 Back when I was a kid, might have played in the summers Marco Polo. 1363 00:59:15,560 --> 00:59:16,650 Super simple game. 1364 00:59:16,650 --> 00:59:18,650 We played it in the pool, for some reason, where 1365 00:59:18,650 --> 00:59:20,840 one person in the pool very safely is blindfolded, 1366 00:59:20,840 --> 00:59:22,550 and then he or she yells Marco. 1367 00:59:22,550 --> 00:59:25,550 And then, everyone around him or her is supposed to yell polo. 1368 00:59:25,550 --> 00:59:27,260 And then, the person who's blindfolded is 1369 00:59:27,260 --> 00:59:29,540 supposed to go chase the other kids in the pool and tag them, 1370 00:59:29,540 --> 00:59:30,650 and then they become it. 1371 00:59:30,650 --> 00:59:33,020 But in other words, it's this like signaling mechanism. 1372 00:59:33,020 --> 00:59:35,750 Someone yells, Marco and everyone else responds 1373 00:59:35,750 --> 00:59:38,180 to that broadcast of the word Marco. 1374 00:59:38,180 --> 00:59:40,910 Well, it turns out we can simulate this with these two puppets. 1375 00:59:40,910 --> 00:59:43,550 This guy here-- notice that I've highlighted the orange puppet 1376 00:59:43,550 --> 00:59:45,350 because there's a second blue puppet there. 1377 00:59:45,350 --> 00:59:46,220 Separate sprites. 1378 00:59:46,220 --> 00:59:49,490 And these are just photographs we uploaded to the game. 1379 00:59:49,490 --> 00:59:53,780 Forever, if the key space is pressed, so if the spacebar is pressed, 1380 00:59:53,780 --> 00:59:57,290 say Marco for two seconds and then broadcast an event. 1381 00:59:57,290 --> 01:00:02,350 Meanwhile, the blue puppet here has a super simple block, 1382 01:00:02,350 --> 01:00:04,770 but it's fundamentally different from the ones we've seen. 1383 01:00:04,770 --> 01:00:07,580 He's not starting when the green flag is clicked. 1384 01:00:07,580 --> 01:00:10,730 He is starting only when he receives an event. 1385 01:00:10,730 --> 01:00:13,470 So it turns out that sprites and Scratch can't hear or see 1386 01:00:13,470 --> 01:00:15,680 what the other one is saying in those speech bubbles. 1387 01:00:15,680 --> 01:00:17,420 You have to use a fancier technique, which 1388 01:00:17,420 --> 01:00:19,430 is the special block called broadcast, which 1389 01:00:19,430 --> 01:00:23,390 is like passing a note digitally from one sprite to another 1390 01:00:23,390 --> 01:00:26,210 that the other one can read or receive, so to speak. 1391 01:00:26,210 --> 01:00:28,430 So only when he receives this event, so to speak, 1392 01:00:28,430 --> 01:00:30,800 does he say polo for two seconds. 1393 01:00:30,800 --> 01:00:34,350 And again, the orange puppet sends that secret message 1394 01:00:34,350 --> 01:00:35,810 just using this other puzzle piece. 1395 01:00:35,810 --> 01:00:40,530 Broadcast an event, like passing a note that the human doesn't actually see. 1396 01:00:40,530 --> 01:00:45,410 So if I now hit the green flag and hit the spacebar, orange yells Marco. 1397 01:00:45,410 --> 01:00:48,380 Blue guy yells polo in response. 1398 01:00:48,380 --> 01:00:50,270 But those aren't timed together. 1399 01:00:50,270 --> 01:00:53,690 Rather, the blue guy is hearing with the orange one has said, 1400 01:00:53,690 --> 01:00:58,740 thereby allowing multiple sprites to actually intercommunicate. 1401 01:00:58,740 --> 01:00:59,780 So how did we get here? 1402 01:00:59,780 --> 01:01:02,930 Well, recall that we had all of these building blocks a moment ago. 1403 01:01:02,930 --> 01:01:05,270 First, we started out with just functions and conditions 1404 01:01:05,270 --> 01:01:06,710 and Boolean expressions and loops. 1405 01:01:06,710 --> 01:01:09,040 We've now added to that the ability to store information 1406 01:01:09,040 --> 01:01:12,530 in variables and threads to do multiple things at once, and then, if you do 1407 01:01:12,530 --> 01:01:14,840 have multiple things happening, events, where 1408 01:01:14,840 --> 01:01:17,180 they can enter communicate somehow. 1409 01:01:17,180 --> 01:01:18,530 Yet another building block. 1410 01:01:18,530 --> 01:01:21,620 So if we now take a step back and consider 1411 01:01:21,620 --> 01:01:26,360 how we can make functions of our own, we have the final piece 1412 01:01:26,360 --> 01:01:27,890 of the puzzle, so to speak. 1413 01:01:27,890 --> 01:01:29,090 Let me go ahead and do this. 1414 01:01:29,090 --> 01:01:33,500 Let me go ahead and create a simple program with, when green flag clipped, 1415 01:01:33,500 --> 01:01:35,570 that simply simulates coughing for a cat. 1416 01:01:35,570 --> 01:01:40,760 So this cat is going to say not hello, but cough for one second. 1417 01:01:40,760 --> 01:01:44,540 And then he's going to go ahead and wait for one second. 1418 01:01:44,540 --> 01:01:48,200 And then I'm going to go ahead and copy paste, as I did before-- 1419 01:01:48,200 --> 01:01:52,040 this is one of those do as I say, not as I do-- to implement this program here, 1420 01:01:52,040 --> 01:01:54,170 where he coughs three times. 1421 01:01:54,170 --> 01:01:57,020 We already know, though, from earlier that this is not good design. 1422 01:01:57,020 --> 01:01:59,430 Why? 1423 01:01:59,430 --> 01:02:00,520 You're repeating yourself. 1424 01:02:00,520 --> 01:02:02,080 Don't repeat yourself. 1425 01:02:02,080 --> 01:02:03,570 DRY is an acronym, actually. 1426 01:02:03,570 --> 01:02:05,320 Don't repeat yourself because you're doing 1427 01:02:05,320 --> 01:02:09,340 three times as many times something that you only really need to do once. 1428 01:02:09,340 --> 01:02:12,680 The solution before, of course, was just use a loop of some sort. 1429 01:02:12,680 --> 01:02:14,230 So let me actually take that out. 1430 01:02:14,230 --> 01:02:18,130 Let me use a repeat block, change 10 to three, 1431 01:02:18,130 --> 01:02:19,810 and then just use two of these blocks. 1432 01:02:19,810 --> 01:02:22,150 And notice already, the program is so much more compact. 1433 01:02:22,150 --> 01:02:27,040 And now, if I want to change the three to a 30 or to a 10 or any number, 1434 01:02:27,040 --> 01:02:28,550 I just change one simple value. 1435 01:02:28,550 --> 01:02:31,840 I don't have to rewrite or copy paste or delete things. 1436 01:02:31,840 --> 01:02:33,820 I can update the program much more readily, 1437 01:02:33,820 --> 01:02:39,370 and now, the same thing is going to happen with just cough, cough, cough. 1438 01:02:39,370 --> 01:02:43,010 But it turns out that it would be nice to henceforth abstract away from this, 1439 01:02:43,010 --> 01:02:43,510 right? 1440 01:02:43,510 --> 01:02:46,960 I just want any program I write to know how to cough. 1441 01:02:46,960 --> 01:02:49,210 And coughing is really just saying something, perhaps 1442 01:02:49,210 --> 01:02:50,540 some number of times. 1443 01:02:50,540 --> 01:02:53,110 But it turns out we can abstract this away in code. 1444 01:02:53,110 --> 01:02:55,930 Let me go down to my blocks here and this allows 1445 01:02:55,930 --> 01:02:57,710 me to click this button-- make a block. 1446 01:02:57,710 --> 01:02:59,440 It allows me to make my own function. 1447 01:02:59,440 --> 01:03:01,120 I get this dialog window here. 1448 01:03:01,120 --> 01:03:03,850 And I'm just going to call this block cough. 1449 01:03:03,850 --> 01:03:05,870 I'm going to go ahead and click OK. 1450 01:03:05,870 --> 01:03:09,420 And now, I have this new pink block that itself can have blocks underneath it. 1451 01:03:09,420 --> 01:03:10,840 And you know what I'm going to do? 1452 01:03:10,840 --> 01:03:12,410 I'm going to go ahead and do this. 1453 01:03:12,410 --> 01:03:16,570 I'm going to go ahead and say cough under there. 1454 01:03:16,570 --> 01:03:20,260 And now, notice on the left, I now have access to this new pink piece. 1455 01:03:20,260 --> 01:03:22,220 I can now put this in here. 1456 01:03:22,220 --> 01:03:26,470 So now, notice even though, yes, this is how coughing is implemented 1457 01:03:26,470 --> 01:03:30,320 on the left hand side here, next time, when I write a program, 1458 01:03:30,320 --> 01:03:31,960 I just want to call cough. 1459 01:03:31,960 --> 01:03:34,630 And I don't care about those lower level implementation details. 1460 01:03:34,630 --> 01:03:36,460 I don't care about the party or any of that. 1461 01:03:36,460 --> 01:03:38,230 I just want this to be an abstraction. 1462 01:03:38,230 --> 01:03:40,510 But I could do better than this wouldn't it 1463 01:03:40,510 --> 01:03:43,730 be nice if instead of just repeating cough three times, 1464 01:03:43,730 --> 01:03:45,670 what if I made that a feature of cough? 1465 01:03:45,670 --> 01:03:46,760 So let me do this. 1466 01:03:46,760 --> 01:03:51,010 I can go ahead and right click on this pink piece and I can edit it. 1467 01:03:51,010 --> 01:03:52,990 That brings up that same window from before. 1468 01:03:52,990 --> 01:03:53,710 And notice this. 1469 01:03:53,710 --> 01:03:55,070 Add an input. 1470 01:03:55,070 --> 01:03:58,210 So when I make a custom block, I can actually make pretty fancy blocks 1471 01:03:58,210 --> 01:04:01,570 just like the ones MIT gives us with the software, 1472 01:04:01,570 --> 01:04:03,700 and now, I can type in something like n. 1473 01:04:03,700 --> 01:04:07,060 And if I add a label just to make it more descriptive, I can just say times. 1474 01:04:07,060 --> 01:04:09,040 So now, I've made a special custom puzzle 1475 01:04:09,040 --> 01:04:12,880 piece that says cough some number of times, where n for number 1476 01:04:12,880 --> 01:04:16,340 is just the go to variable that programmers tend to use. 1477 01:04:16,340 --> 01:04:24,260 So now, I can actually move this repeat block into cough itself, 1478 01:04:24,260 --> 01:04:26,290 but rather than hard code 3, notice this. 1479 01:04:26,290 --> 01:04:31,360 I can steal that variable and now say cough this many times by repeating 1480 01:04:31,360 --> 01:04:34,630 saying this again and again and again. 1481 01:04:34,630 --> 01:04:39,880 And now, when I cough in my actual program, I just type in three here. 1482 01:04:39,880 --> 01:04:42,350 So I have this beautiful abstraction now, so to speak. 1483 01:04:42,350 --> 01:04:45,490 Cough this many times and I and no one else in the world 1484 01:04:45,490 --> 01:04:48,730 never again needs to care about what it means to cough because we've already 1485 01:04:48,730 --> 01:04:50,330 implemented that before. 1486 01:04:50,330 --> 01:04:52,810 And so just as MIT has given us so much functionality 1487 01:04:52,810 --> 01:04:54,940 that we ourselves don't have to think about, so can 1488 01:04:54,940 --> 01:04:57,560 I now make functionality that I don't have to think about. 1489 01:04:57,560 --> 01:05:00,340 And as we progress to higher level languages like C and JavaScript 1490 01:05:00,340 --> 01:05:02,800 and Python, we're going to continue this process, sometimes 1491 01:05:02,800 --> 01:05:06,460 solving problems ourselves by making our own custom puzzle pieces, 1492 01:05:06,460 --> 01:05:08,620 but very often using things called libraries, 1493 01:05:08,620 --> 01:05:13,060 code that other humans wrote before us that's just useful to get the job done, 1494 01:05:13,060 --> 01:05:16,970 just as Scratch has done here in part for us. 1495 01:05:16,970 --> 01:05:19,360 Let me go ahead, then, and bring all of this 1496 01:05:19,360 --> 01:05:23,750 together by opening this other example here. 1497 01:05:23,750 --> 01:05:27,470 Let me go ahead and open up this one, which isn't something we've seen, 1498 01:05:27,470 --> 01:05:31,330 but it's kind of an interactive game like this made by a former student. 1499 01:05:31,330 --> 01:05:35,540 [MUSIC PLAYING] 1500 01:05:35,540 --> 01:05:36,790 Should we have an apple? 1501 01:05:36,790 --> 01:05:38,840 Yes. 1502 01:05:38,840 --> 01:05:40,240 A little animation. 1503 01:05:44,420 --> 01:05:44,920 OK. 1504 01:05:44,920 --> 01:05:46,720 That didn't end well. 1505 01:05:46,720 --> 01:05:47,770 Let's try again. 1506 01:05:47,770 --> 01:05:49,660 Play again. 1507 01:05:49,660 --> 01:05:51,960 And notice the say block is happening. 1508 01:05:51,960 --> 01:05:54,250 There's some kind of ask block. 1509 01:05:54,250 --> 01:05:56,730 The student was checking if the human typed in yes or no. 1510 01:05:56,730 --> 01:05:57,730 Let's type no this time. 1511 01:05:57,730 --> 01:05:58,600 No apple. 1512 01:05:58,600 --> 01:05:59,100 Ooh. 1513 01:05:59,100 --> 01:05:59,740 Cupcake. 1514 01:05:59,740 --> 01:06:00,240 OK. 1515 01:06:00,240 --> 01:06:00,890 Yes. 1516 01:06:00,890 --> 01:06:01,390 Enter. 1517 01:06:06,790 --> 01:06:08,030 OK. 1518 01:06:08,030 --> 01:06:09,660 Don't do that. 1519 01:06:09,660 --> 01:06:10,380 One more life. 1520 01:06:10,380 --> 01:06:10,880 Here we go. 1521 01:06:10,880 --> 01:06:13,200 [MUSIC PLAYING] 1522 01:06:13,200 --> 01:06:13,700 OK. 1523 01:06:13,700 --> 01:06:15,590 No apple. 1524 01:06:15,590 --> 01:06:16,160 No cupcake. 1525 01:06:18,950 --> 01:06:21,660 Little variable. 1526 01:06:21,660 --> 01:06:24,590 [SCREAMING] 1527 01:06:26,050 --> 01:06:28,970 [LAUGHTER] 1528 01:06:28,970 --> 01:06:29,470 OK. 1529 01:06:29,470 --> 01:06:32,620 So I won the game. 1530 01:06:32,620 --> 01:06:37,780 In our final moments here, let me go ahead and open one final example. 1531 01:06:37,780 --> 01:06:41,760 As you know, CS50 is offered not only at Harvard, but at Yale, as well, 1532 01:06:41,760 --> 01:06:43,810 so it seems fitting to perhaps end on a note that 1533 01:06:43,810 --> 01:06:47,320 pits one campus perhaps against the other by way of another game 1534 01:06:47,320 --> 01:06:50,110 that a former student wrote called Ivy's Hardest Game. 1535 01:06:50,110 --> 01:06:53,450 But for this, I think we need one final volunteer who's company coming up. 1536 01:06:53,450 --> 01:06:53,950 OK. 1537 01:06:53,950 --> 01:06:54,590 First hand. 1538 01:06:54,590 --> 01:06:55,090 Right there. 1539 01:06:55,090 --> 01:06:56,920 Come on down. 1540 01:06:56,920 --> 01:07:00,140 So in Ivy's Hardest Game, it's a game played with the keyboard. 1541 01:07:00,140 --> 01:07:02,980 And even though it might look a little overwhelming at first glance, 1542 01:07:02,980 --> 01:07:06,310 just like Oscar Time did and just like the gingerbread animation might, 1543 01:07:06,310 --> 01:07:09,520 realize that if you decompose it in just your mind's eye thinking about what 1544 01:07:09,520 --> 01:07:11,770 those individual building blocks are, you can probably 1545 01:07:11,770 --> 01:07:13,190 guess what the puzzle pieces are. 1546 01:07:13,190 --> 01:07:13,450 Hi. 1547 01:07:13,450 --> 01:07:13,780 What's your name? 1548 01:07:13,780 --> 01:07:14,050 ANDREA: Hi. 1549 01:07:14,050 --> 01:07:14,610 I'm Andrea. 1550 01:07:14,610 --> 01:07:14,950 DAVID MALAN: Andrea. 1551 01:07:14,950 --> 01:07:15,440 David. 1552 01:07:15,440 --> 01:07:16,270 Nice to meet you. 1553 01:07:16,270 --> 01:07:20,010 Here is Ivy's Hardest Game. 1554 01:07:20,010 --> 01:07:22,500 Pits you against all of the Ivies here. 1555 01:07:22,500 --> 01:07:26,300 And then right after this will we adjourn for cupcakes in the transept. 1556 01:07:26,300 --> 01:07:28,770 Ready? 1557 01:07:28,770 --> 01:07:31,480 [MUSIC PLAYING] 1558 01:07:31,480 --> 01:07:33,230 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1559 01:07:33,230 --> 01:07:36,690 MC HAMMER: (SINGING) You can't touch this. 1560 01:07:36,690 --> 01:07:42,940 You can't touch this. 1561 01:07:42,940 --> 01:07:43,690 DAVID MALAN: Nice. 1562 01:07:43,690 --> 01:07:44,130 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1563 01:07:44,130 --> 01:07:46,440 MC HAMMER: (SINGING) You can't touch this. 1564 01:07:46,440 --> 01:07:48,460 My, my, my, my. 1565 01:07:48,460 --> 01:07:50,250 Music hits you so hard. 1566 01:07:50,250 --> 01:07:52,250 Makes me say, oh my lord. 1567 01:07:52,250 --> 01:07:55,740 Thank you for blessing me with a mind to rhyme and two hype feet. 1568 01:07:55,740 --> 01:07:57,650 It feels good when you know you're down. 1569 01:07:57,650 --> 01:07:59,240 A super dope homeboy from the Oaktown. 1570 01:07:59,240 --> 01:08:01,230 And I'm known as such. 1571 01:08:01,230 --> 01:08:04,230 And this is the beat you can't touch. 1572 01:08:04,230 --> 01:08:07,470 I told you, homeboy, you can't touch this. 1573 01:08:07,470 --> 01:08:07,970 Yeah. 1574 01:08:07,970 --> 01:08:11,210 That's how we're living and you know you can't touch this. 1575 01:08:11,210 --> 01:08:12,210 Look in my eyes. 1576 01:08:12,210 --> 01:08:14,710 Man, you can't touch this. 1577 01:08:14,710 --> 01:08:15,620 [APPLAUSE] 1578 01:08:15,620 --> 01:08:17,370 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1579 01:08:17,370 --> 01:08:19,200 (SINGING) Fresh new kicks and pants. 1580 01:08:19,200 --> 01:08:21,530 You got it like that and now you know you want to dance. 1581 01:08:21,530 --> 01:08:23,520 So move out of your seat and get a fly girl 1582 01:08:23,520 --> 01:08:25,680 and catch this beat while it's rolling. 1583 01:08:25,680 --> 01:08:26,180 Hold on. 1584 01:08:26,180 --> 01:08:29,180 Pump a little bit and let me know it's going on like that. 1585 01:08:29,180 --> 01:08:30,170 Like that. 1586 01:08:30,170 --> 01:08:31,670 Cold on a mission so pull on back. 1587 01:08:31,670 --> 01:08:36,660 Let them know that you're too much and this is the beat you can't touch. 1588 01:08:36,660 --> 01:08:40,650 Yo, I told you, you can't touch this. 1589 01:08:40,650 --> 01:08:42,150 Why you standing there, man? 1590 01:08:42,150 --> 01:08:43,150 You can't touch this. 1591 01:08:43,150 --> 01:08:44,650 Yo, sound the bells. 1592 01:08:44,650 --> 01:08:45,640 School is in, sucker. 1593 01:08:45,640 --> 01:08:46,520 You can't touch this. 1594 01:08:46,520 --> 01:08:48,140 Give me a song or rhythm. 1595 01:08:48,140 --> 01:08:48,890 Making them sweat. 1596 01:08:48,890 --> 01:08:50,130 That's what I'm giving them. 1597 01:08:50,130 --> 01:08:52,630 Now they know when you talk about the Hammer, 1598 01:08:52,630 --> 01:08:55,120 you talking about a show that's hyped and tight. 1599 01:08:55,120 --> 01:08:58,120 Singers are sweating so pass them a wipe or a tape 1600 01:08:58,120 --> 01:09:01,390 to learn what it's going to take in the 90s to burn the charts. 1601 01:09:01,390 --> 01:09:02,810 DAVID MALAN: Second to last level. 1602 01:09:02,810 --> 01:09:03,310 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1603 01:09:03,310 --> 01:09:05,970 MC HAMMER: (SINGING) Either work hard or you might as well quit. 1604 01:09:05,970 --> 01:09:10,810 That's word because you know you can't touch this. 1605 01:09:10,810 --> 01:09:14,310 You can't touch this. 1606 01:09:14,310 --> 01:09:15,310 Break it down. 1607 01:09:28,310 --> 01:09:28,810 Stop. 1608 01:09:28,810 --> 01:09:29,810 Hammer time. 1609 01:09:29,810 --> 01:09:31,010 Go with the flow, it is said. 1610 01:09:31,010 --> 01:09:33,350 If you can't groove to this, then you probably are dead. 1611 01:09:33,350 --> 01:09:34,810 So wave your hands in the air. 1612 01:09:34,810 --> 01:09:35,810 Bust a few moves. 1613 01:09:35,810 --> 01:09:37,260 Run your fingers through your hair. 1614 01:09:37,260 --> 01:09:38,390 This is it for a winner. 1615 01:09:38,390 --> 01:09:40,310 Dance to this and you're going to get thinner. 1616 01:09:40,310 --> 01:09:41,310 Move. 1617 01:09:41,310 --> 01:09:42,310 Slide your rump. 1618 01:09:42,310 --> 01:09:45,810 Just for a minute, let's all do the bump, bump, bump. 1619 01:09:45,810 --> 01:09:46,810 Yeah. 1620 01:09:46,810 --> 01:09:49,310 You can't touch this. 1621 01:09:49,310 --> 01:09:50,310 Look, man. 1622 01:09:50,310 --> 01:09:52,310 You can't touch this. 1623 01:09:52,310 --> 01:09:54,310 You better get hype, boy, because you know-- 1624 01:09:54,310 --> 01:09:56,180 [CROWD YELLING] 1625 01:09:56,180 --> 01:09:57,930 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1626 01:09:57,930 --> 01:10:00,310 (SINGING) Break it down. 1627 01:10:00,310 --> 01:10:04,310 DAVID MALAN: [INAUDIBLE] 1628 01:10:04,310 --> 01:10:05,100 ANDREA: [INAUDIBLE] 1629 01:10:05,100 --> 01:10:05,760 DAVID MALAN: No. 1630 01:10:05,760 --> 01:10:06,310 It's OK. 1631 01:10:06,310 --> 01:10:09,810 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1632 01:10:09,810 --> 01:10:10,810 One more life. 1633 01:10:10,810 --> 01:10:11,310 [MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"] 1634 01:10:11,310 --> 01:10:12,390 MC HAMMER: (SINGING) Stop. 1635 01:10:12,390 --> 01:10:12,920 Hammer time. 1636 01:10:18,420 --> 01:10:19,380 DAVID MALAN: All right. 1637 01:10:19,380 --> 01:10:21,210 A round of applause for Andrea, if we could. 1638 01:10:21,210 --> 01:10:23,950 [APPLAUSE] 1639 01:10:23,950 --> 01:10:25,830 OK. 1640 01:10:25,830 --> 01:10:27,600 That's it for CS50. 1641 01:10:27,600 --> 01:10:28,950 See the website for details. 1642 01:10:28,950 --> 01:10:31,090 We'll see you for cake in the transept. 1643 01:10:31,090 --> 01:10:32,940 Welcome aboard.