1 00:00:00,000 --> 00:00:11,375 2 00:00:11,375 --> 00:00:14,430 >> DAVID MALAN: This is CS50, and this is the start of week one. 3 00:00:14,430 --> 00:00:17,930 And what we thought we'd do is pull back the curtain of a device that many 4 00:00:17,930 --> 00:00:20,970 of you have probably walked past now for years, but perhaps never quite 5 00:00:20,970 --> 00:00:22,280 known what it is. 6 00:00:22,280 --> 00:00:26,000 So this device will probably look familiar to most of you by now. 7 00:00:26,000 --> 00:00:32,972 >> [VIDEO PLAYBACK] 8 00:00:32,972 --> 00:00:36,150 >> -A triumph of mathematical and mechanical skill is this great new 9 00:00:36,150 --> 00:00:39,120 automatic calculator at Harvard University. 10 00:00:39,120 --> 00:00:43,220 Intricate problems in mathematics put through the machine in coded form on 11 00:00:43,220 --> 00:00:47,840 tape are accurately solved in a minute fraction of the time required for 12 00:00:47,840 --> 00:00:49,090 human calculation. 13 00:00:49,090 --> 00:01:00,090 14 00:01:00,090 --> 00:01:04,080 >> Designed to expedite all forms of mathematical and scientific research, 15 00:01:04,080 --> 00:01:07,860 the giant mechanical brain will work for the United States 16 00:01:07,860 --> 00:01:09,180 Navy until war's end. 17 00:01:09,180 --> 00:01:18,180 18 00:01:18,180 --> 00:01:18,600 >> [END VIDEO PLAYBACK] 19 00:01:18,600 --> 00:01:21,980 >> DAVID MALAN: So that's what you see in the Science Center, which is where 20 00:01:21,980 --> 00:01:23,540 that machine is now housed. 21 00:01:23,540 --> 00:01:25,910 You actually only see part of it. 22 00:01:25,910 --> 00:01:28,800 Only a portion is currently on display, and that's a device called 23 00:01:28,800 --> 00:01:31,970 the Mark I, and it was essentially a really huge calculator. 24 00:01:31,970 --> 00:01:36,250 It was succeeded by the Mark II, and it's from the Mark II that we actually 25 00:01:36,250 --> 00:01:39,210 get a bit of lexicon. 26 00:01:39,210 --> 00:01:43,780 >> This is the first recorded instance of something you're about to become all 27 00:01:43,780 --> 00:01:45,860 too familiar with over the course of the semester-- 28 00:01:45,860 --> 00:01:48,460 namely, a bug. 29 00:01:48,460 --> 00:01:52,030 So it turned out at one point the Mark II device was not functioning 30 00:01:52,030 --> 00:01:55,930 properly, and upon closer inspection, there was indeed a bug-- a moth-- 31 00:01:55,930 --> 00:01:56,910 that moth-- 32 00:01:56,910 --> 00:01:57,610 inside. 33 00:01:57,610 --> 00:02:03,130 And ever since, we have referred to mistakes in programs as bugs. 34 00:02:03,130 --> 00:02:05,330 So more on that to come. 35 00:02:05,330 --> 00:02:08,919 >> Speaking of video as well, if you haven't already, know that CS50 is 36 00:02:08,919 --> 00:02:09,669 obviously filmed. 37 00:02:09,669 --> 00:02:13,330 The lectures typically go up right after class in sort of a pre-release 38 00:02:13,330 --> 00:02:17,240 unedited format, and then a day or two later, we put up a higher resolution 39 00:02:17,240 --> 00:02:18,490 and higher quality version. 40 00:02:18,490 --> 00:02:21,800 You may recall Vanessa, who had the fortune of playing with Google Glass, 41 00:02:21,800 --> 00:02:23,540 which I remembered to charge this time. 42 00:02:23,540 --> 00:02:26,450 So if you want to come up after class and play, they should work today. 43 00:02:26,450 --> 00:02:30,816 And if you'd like to see what Vanessa saw the other day, what you'll see 44 00:02:30,816 --> 00:02:35,260 here-- let me raise the volume here on my laptop. 45 00:02:35,260 --> 00:02:35,690 >> [VIDEO PLAYBACK] 46 00:02:35,690 --> 00:02:37,620 >> -So let me queue this is up. 47 00:02:37,620 --> 00:02:38,070 All right. 48 00:02:38,070 --> 00:02:40,930 So touch this. 49 00:02:40,930 --> 00:02:41,720 All right. 50 00:02:41,720 --> 00:02:43,470 OK, Glass. 51 00:02:43,470 --> 00:02:44,720 Record a video. 52 00:02:44,720 --> 00:02:47,240 53 00:02:47,240 --> 00:02:48,640 >> All right. 54 00:02:48,640 --> 00:02:50,446 Vanessa? 55 00:02:50,446 --> 00:02:50,870 >> [END VIDEO PLAYBACK] 56 00:02:50,870 --> 00:02:53,485 >> DAVID MALAN: All right, so the reason that it didn't work for most of you at 57 00:02:53,485 --> 00:02:56,540 the end of class was because I proceeded to record an hour of that 58 00:02:56,540 --> 00:02:59,505 footage pointing at myself after I put the glasses down. 59 00:02:59,505 --> 00:03:04,010 So today, I'll leave them off, but thank you to Vanessa for being such a 60 00:03:04,010 --> 00:03:05,420 good sport. 61 00:03:05,420 --> 00:03:08,330 >> Meanwhile, a couple of announcements before we forge ahead today. 62 00:03:08,330 --> 00:03:11,120 So one, CS50 has a tradition of doing what we can to try to make a very 63 00:03:11,120 --> 00:03:12,630 large class feel smaller. 64 00:03:12,630 --> 00:03:16,640 And toward that end, most every Friday, we gather at a restaurant in 65 00:03:16,640 --> 00:03:20,160 the square, Fire and Ice, with 30 or 40 of your classmates, myself, some of 66 00:03:20,160 --> 00:03:22,520 the teaching staff, and we just have a very casual lunch. 67 00:03:22,520 --> 00:03:26,280 We often invite friends from industry, alumni of the class, alumni of the 68 00:03:26,280 --> 00:03:30,340 college, really just to chat each other up, talk about life after 69 00:03:30,340 --> 00:03:32,260 college, life in college, and the like. 70 00:03:32,260 --> 00:03:35,570 So if you would like to partake in this first such lunch this Friday at 71 00:03:35,570 --> 00:03:38,640 1:15 PM, head to that URL at some point. 72 00:03:38,640 --> 00:03:40,830 >> Space is limited, so we'll do first come first serve. 73 00:03:40,830 --> 00:03:43,880 But we'll do this again on a recurring basis, so not to worry if you don't 74 00:03:43,880 --> 00:03:45,800 make it into this first batch. 75 00:03:45,800 --> 00:03:48,960 >> Now, in terms of the resources provided curricularly by the course, 76 00:03:48,960 --> 00:03:51,500 there's a whole slew, and if you haven't pulled up the course's website 77 00:03:51,500 --> 00:03:54,310 already, in particular, under lectures, you'll find that everything 78 00:03:54,310 --> 00:03:57,070 we did last week is there, both in video and some sort 79 00:03:57,070 --> 00:03:58,435 of electronic form. 80 00:03:58,435 --> 00:04:01,210 But beyond the videos, you'll find that there are now full text 81 00:04:01,210 --> 00:04:03,290 transcripts, for instance, of every lecture. 82 00:04:03,290 --> 00:04:08,260 >> So if we actually go back to this screen here with Vanessa, and navigate 83 00:04:08,260 --> 00:04:12,010 to the bottom corner of the screen, you'll find that actually, not for 84 00:04:12,010 --> 00:04:15,060 Friday's lecture, but if we go back to Wednesday, since this feature takes a 85 00:04:15,060 --> 00:04:17,870 few days, you'll find that you can actually pull up, for better or for 86 00:04:17,870 --> 00:04:22,260 worse, every word that I or a volunteer on stage said. 87 00:04:22,260 --> 00:04:23,270 And more than just that. 88 00:04:23,270 --> 00:04:25,700 You can actually search it, you can click on any of those sentences, jump 89 00:04:25,700 --> 00:04:29,250 to that point in the video, all toward and end of making the material all the 90 00:04:29,250 --> 00:04:30,620 more navigable. 91 00:04:30,620 --> 00:04:34,550 >> But if you'd rather not follow it in such detail, you'll find, for 92 00:04:34,550 --> 00:04:39,710 instance, this little menu here for 1.5x speed, 2x speed, or i f I already 93 00:04:39,710 --> 00:04:42,100 speak too fast for you, 0.75x speed. 94 00:04:42,100 --> 00:04:45,190 So realize all of that is available there for you. 95 00:04:45,190 --> 00:04:49,120 >> But beyond that, for lectures, realize that we also make these resources 96 00:04:49,120 --> 00:04:50,530 available as well. 97 00:04:50,530 --> 00:04:55,240 Slides, example code, anything that I do on stage or in advance of class, 98 00:04:55,240 --> 00:04:58,160 we'll post there so that you can play along at home or in Sanders. 99 00:04:58,160 --> 00:05:01,280 But what we also thought we'd do this year for the first time is also 100 00:05:01,280 --> 00:05:04,240 provide you with walkthroughs of these examples. 101 00:05:04,240 --> 00:05:08,120 Increasingly, as I've reflected on the value of CS50's lectures, I've begun 102 00:05:08,120 --> 00:05:11,450 to question just how useful it is for you guys to sit there, for me to stand 103 00:05:11,450 --> 00:05:15,020 here, and for me to talk at you, particularly as the material, the 104 00:05:15,020 --> 00:05:17,020 examples, get more complex. 105 00:05:17,020 --> 00:05:20,790 Because invariably, after some number of minutes, someone will zone out, and 106 00:05:20,790 --> 00:05:24,310 then you miss some key insight of some example, and then you're essentially 107 00:05:24,310 --> 00:05:27,720 gone for the remaining portion of class, which is not the best use of 108 00:05:27,720 --> 00:05:31,330 your time, or, really, ours collectively as a class. 109 00:05:31,330 --> 00:05:33,920 >> And so what we thought we'd try to do, particularly for some of the more 110 00:05:33,920 --> 00:05:37,700 complex examples that I might not do justice to in class, that we might not 111 00:05:37,700 --> 00:05:41,560 have time for in class, or you might just zone out during, is we'll produce 112 00:05:41,560 --> 00:05:45,500 a series of walkthroughs of these examples so that if you go to, for 113 00:05:45,500 --> 00:05:51,410 instance, last Friday's video page here under examples, you'll see that 114 00:05:51,410 --> 00:05:54,050 for Friday, there's now this link to walkthroughs. 115 00:05:54,050 --> 00:05:55,780 And the format of these will change over time. 116 00:05:55,780 --> 00:05:58,080 For now, we're using a simple YouTube playlist. 117 00:05:58,080 --> 00:06:00,980 >> But what I did with our production team afterwards was walk through each 118 00:06:00,980 --> 00:06:04,480 and every one of those examples again, hopefully pointing out in much greater 119 00:06:04,480 --> 00:06:07,550 clarity exactly what the takeaways could be or should be from each. 120 00:06:07,550 --> 00:06:10,560 So you'll find, for instance, from last week, all of the scratch examples 121 00:06:10,560 --> 00:06:12,950 we went through are done linearly in that fashion. 122 00:06:12,950 --> 00:06:15,840 So feel free to engage or not engage in this material. 123 00:06:15,840 --> 00:06:19,140 Really a theme of this particular course is that there's probably more 124 00:06:19,140 --> 00:06:21,870 in the way of resources than you could possibly absorb over 125 00:06:21,870 --> 00:06:23,260 the course of a semester. 126 00:06:23,260 --> 00:06:23,980 >> But that's deliberate. 127 00:06:23,980 --> 00:06:26,990 It's meant to be so that you as individual students can self-select 128 00:06:26,990 --> 00:06:28,070 based on your learning style. 129 00:06:28,070 --> 00:06:29,790 So if lectures work for you, great. 130 00:06:29,790 --> 00:06:31,090 If sections work for you, great. 131 00:06:31,090 --> 00:06:32,930 If walkthroughs work for you better, great. 132 00:06:32,930 --> 00:06:36,980 It really will be up to you to choose what resources work best for you. 133 00:06:36,980 --> 00:06:40,380 >> Finally, thanks to an alumnus of the course, we also provide a canonical 134 00:06:40,380 --> 00:06:41,890 set of scribe notes. 135 00:06:41,890 --> 00:06:46,270 So rather than have you guys heads down in lectures scribbling down 136 00:06:46,270 --> 00:06:49,880 things that come up in class, we, the staff, will provide you with what we 137 00:06:49,880 --> 00:06:53,410 feel are a canonical set of notes to free you of that distraction, so that 138 00:06:53,410 --> 00:06:57,150 if you are here and engaged, you are truly engaged with what's going on, 139 00:06:57,150 --> 00:07:00,110 with your classmates are doing up on stage, with what's up on the screen, 140 00:07:00,110 --> 00:07:03,570 and not simply writing down verbatim what happened to have been said. 141 00:07:03,570 --> 00:07:06,730 So realize all of those resources are available to you. 142 00:07:06,730 --> 00:07:09,210 >> Sectioning, in answer to an FAQ, will begin this Wednesday. 143 00:07:09,210 --> 00:07:12,050 We essentially wait until after most other classes are done so that we can 144 00:07:12,050 --> 00:07:14,420 minimize the number of changes that we need to do. 145 00:07:14,420 --> 00:07:20,010 But coming up this weekend, starting Sunday, will be a one time instance of 146 00:07:20,010 --> 00:07:21,360 what we call super sections. 147 00:07:21,360 --> 00:07:23,990 These will be filmed for those who can't attend, and essentially, the 148 00:07:23,990 --> 00:07:28,880 course's heads will walk us through some of the C portion of the course, 149 00:07:28,880 --> 00:07:32,330 toward an end of problem set one, which is going to be our first C based 150 00:07:32,330 --> 00:07:33,140 problem set. 151 00:07:33,140 --> 00:07:36,240 And these will just be optional sections in anticipation of what will 152 00:07:36,240 --> 00:07:40,010 become recurring sections led by the course's 50 plus 153 00:07:40,010 --> 00:07:41,600 teaching fellows weekly. 154 00:07:41,600 --> 00:07:45,130 On Sundays or Mondays or Tuesdays, we have 90 minute sections in a very 155 00:07:45,130 --> 00:07:48,020 traditional sense, which will be opportunities for hands on and more 156 00:07:48,020 --> 00:07:50,800 intimate review of the course's material. 157 00:07:50,800 --> 00:07:53,820 >> Without further ado, let me to introduce the course's heads, who are 158 00:07:53,820 --> 00:07:57,380 behind the scenes with me, making everything happen. 159 00:07:57,380 --> 00:08:02,670 If those here today could join me, Rob and Lauren and Joseph and Lucas, all 160 00:08:02,670 --> 00:08:05,770 of whom have been with the course for some time. 161 00:08:05,770 --> 00:08:08,450 RJ is the third such member of our team. 162 00:08:08,450 --> 00:08:11,720 He couldn't be here today, but he asked me to show a photo of him. 163 00:08:11,720 --> 00:08:14,600 I'm not sure he wanted this one, but there he is. 164 00:08:14,600 --> 00:08:18,350 >> And let me just allow the team to say hello and introduce themselves so that 165 00:08:18,350 --> 00:08:22,260 you get to know them as well as your own teaching fellow this term. 166 00:08:22,260 --> 00:08:23,490 >> LAUREN CARVALHO: Hi. 167 00:08:23,490 --> 00:08:24,740 My name is Lauren Carvalho. 168 00:08:24,740 --> 00:08:26,900 I'm a resident tutor in Leverett House. 169 00:08:26,900 --> 00:08:31,120 I'm also super excited to be a head TF for CS50 this year. 170 00:08:31,120 --> 00:08:35,830 >> CS50 is a very challenging class as well as a very large class, so if you 171 00:08:35,830 --> 00:08:38,780 have any concerns, if you feel like you're falling behind, feel free to 172 00:08:38,780 --> 00:08:43,840 reach out to any of us at heads@cs50.het, or to me, if you'd 173 00:08:43,840 --> 00:08:46,330 like to play with my golden retriever puppy. 174 00:08:46,330 --> 00:08:48,060 Here's Lucas. 175 00:08:48,060 --> 00:08:48,560 >> LUCAS FREITAS: Hey, guys. 176 00:08:48,560 --> 00:08:49,330 My name is Lucas Freitas. 177 00:08:49,330 --> 00:08:51,880 I'm a junior [INAUDIBLE] 178 00:08:51,880 --> 00:08:53,630 computer science and linguistics. 179 00:08:53,630 --> 00:08:58,030 I'm actually from Brazil, and I also realize east Asian studies. 180 00:08:58,030 --> 00:09:00,740 So if you have any questions about computer science or language or 181 00:09:00,740 --> 00:09:01,870 anything, just let me know. 182 00:09:01,870 --> 00:09:05,630 Or CS50, especially. 183 00:09:05,630 --> 00:09:05,880 >> JOSEPH ONG: Hi. 184 00:09:05,880 --> 00:09:06,265 I'm Joseph. 185 00:09:06,265 --> 00:09:11,360 I'm a senior studying computer science in Elliott House. 186 00:09:11,360 --> 00:09:13,500 So, let's see. 187 00:09:13,500 --> 00:09:16,530 You'll see me around carrying one of these huge cameras. 188 00:09:16,530 --> 00:09:21,110 I'm the resident staff photographer for 50 as well, and students often 189 00:09:21,110 --> 00:09:25,140 sometimes mistake me for an Asian tourist. 190 00:09:25,140 --> 00:09:29,220 >> So if you see me with a camera, don't run away. 191 00:09:29,220 --> 00:09:32,960 Just smile and don't be shy. 192 00:09:32,960 --> 00:09:34,870 And I hope you enjoy 50. 193 00:09:34,870 --> 00:09:38,690 I enjoyed it very much when I took it, and that's why I've been TFing for 194 00:09:38,690 --> 00:09:41,164 these three years. 195 00:09:41,164 --> 00:09:41,570 >> ROB BOWDEN: Hi. 196 00:09:41,570 --> 00:09:42,550 I'm Rob. 197 00:09:42,550 --> 00:09:46,240 This is my fourth semester with CS50. 198 00:09:46,240 --> 00:09:47,430 I just graduated in May. 199 00:09:47,430 --> 00:09:49,650 I was in Kirkland. 200 00:09:49,650 --> 00:09:53,670 I'm excited for this semester, and I hope you are too. 201 00:09:53,670 --> 00:09:56,650 >> DAVID MALAN: Thank you to this year's heads, and to RJ as well. 202 00:09:56,650 --> 00:09:59,910 203 00:09:59,910 --> 00:10:02,310 >> So a few final notes on resources. 204 00:10:02,310 --> 00:10:04,090 One, p set 0 is already up. 205 00:10:04,090 --> 00:10:06,620 This one exists only in standard edition, which is meant for the 206 00:10:06,620 --> 00:10:07,610 entirety of the class. 207 00:10:07,610 --> 00:10:10,940 Starting with p set 1, there will be hacker editions of most problem sets, 208 00:10:10,940 --> 00:10:14,660 which, again, will cover the material with a bit more of a challenge, while 209 00:10:14,660 --> 00:10:17,400 still touching on some of the same topics. 210 00:10:17,400 --> 00:10:21,400 Do take a look at the course's website for that specification for p set 0. 211 00:10:21,400 --> 00:10:25,140 >> Office hours, too, will begin this week, tonight, tomorrow, Wednesday, 212 00:10:25,140 --> 00:10:27,670 and Thursday in various dining halls on campus. 213 00:10:27,670 --> 00:10:32,140 Check out cs50.net/ohs for the office hours for the course, and realize this 214 00:10:32,140 --> 00:10:34,430 will be very casual opportunities, certainly, at the start of the 215 00:10:34,430 --> 00:10:37,190 semester, to come by with your laptop during brain break. 216 00:10:37,190 --> 00:10:40,500 Bring some friends and chat up the course's TFs and CAs with any 217 00:10:40,500 --> 00:10:41,750 questions that you might have. 218 00:10:41,750 --> 00:10:45,320 >> And I can't emphasize enough, even for scratch and problem set 0, there 219 00:10:45,320 --> 00:10:47,110 really is no dumb question. 220 00:10:47,110 --> 00:10:51,200 There is no student too uncomfortable to ask questions. 221 00:10:51,200 --> 00:10:53,940 Please feel free to take advantage of this resource. 222 00:10:53,940 --> 00:10:56,430 It will be there recurringly throughout the semester, 223 00:10:56,430 --> 00:10:57,840 as will CS50 Discuss. 224 00:10:57,840 --> 00:11:00,900 This is the course's online discussion forums at that address there. 225 00:11:00,900 --> 00:11:04,490 >> What we'll also do during lectures is also monitor this, thanks to the 226 00:11:04,490 --> 00:11:05,580 course's teaching team. 227 00:11:05,580 --> 00:11:08,390 And so if you have a question during lecture, because I said something 228 00:11:08,390 --> 00:11:11,690 poorly or not at all, by all means, ask that in real time if you have your 229 00:11:11,690 --> 00:11:15,700 laptop or phone on the website, and we'll do our best in near real time to 230 00:11:15,700 --> 00:11:17,410 respond to that. 231 00:11:17,410 --> 00:11:21,500 >> Are there any questions about CS 50? 232 00:11:21,500 --> 00:11:23,930 Then one last notes of mine. 233 00:11:23,930 --> 00:11:25,530 That issue of SAT UNSAT. 234 00:11:25,530 --> 00:11:28,550 As you finalize your study cards, if you haven't already, realize that the 235 00:11:28,550 --> 00:11:32,360 overarching vision of SAT UNSAT, available for all students, is really 236 00:11:32,360 --> 00:11:36,350 to help take the edge off of a course like this, so to speak, so that if you 237 00:11:36,350 --> 00:11:40,510 find yourself late at night, 2AM, 3AM, really banging your head up against 238 00:11:40,510 --> 00:11:43,960 the wall where you know you're 99% of the way there to completion, but you 239 00:11:43,960 --> 00:11:47,510 have so many other things to do, I think a better use, often, of 240 00:11:47,510 --> 00:11:51,260 students' time is to indeed move on to those other things so that you can 241 00:11:51,260 --> 00:11:52,780 optimize your time all around. 242 00:11:52,780 --> 00:11:56,950 And SAT UNSAT is one mechanism via which you can take comfort in the fact 243 00:11:56,950 --> 00:12:00,530 that 99% of the way there is still pretty darn good. 244 00:12:00,530 --> 00:12:03,790 >> Similarly, too, if you've come into the course with no prior background or 245 00:12:03,790 --> 00:12:07,960 not nearly as much background as you think your classmates might have, true 246 00:12:07,960 --> 00:12:13,210 or false, realize that this is an opportunity to get your hands dirty 247 00:12:13,210 --> 00:12:16,590 with the course, put your toes in the water, so to speak, much like I myself 248 00:12:16,590 --> 00:12:18,500 did years ago with pass/fail. 249 00:12:18,500 --> 00:12:21,850 As I said last week, had this course, like others, not been available 250 00:12:21,850 --> 00:12:25,090 pass/fail at the time, I probably would not have ever stepped foot in 251 00:12:25,090 --> 00:12:28,310 the class, even though I did, five weeks later, change my mind, which you 252 00:12:28,310 --> 00:12:32,400 may as well, and switch over to or from letter graded status. 253 00:12:32,400 --> 00:12:32,990 >> All right. 254 00:12:32,990 --> 00:12:37,380 So we looked at things like this on Friday, and we're very quickly going 255 00:12:37,380 --> 00:12:39,890 to transition to something that looks more cryptic today. 256 00:12:39,890 --> 00:12:42,300 But it's nonetheless the same fundamental idea. 257 00:12:42,300 --> 00:12:45,540 What was the general term that we used to describe a puzzle piece 258 00:12:45,540 --> 00:12:47,690 that looked like this? 259 00:12:47,690 --> 00:12:48,510 >> So, a statement. 260 00:12:48,510 --> 00:12:50,020 And you can call this any number of things. 261 00:12:50,020 --> 00:12:51,870 But we'll just call it a statement, and a statement 262 00:12:51,870 --> 00:12:53,200 just tells the program-- 263 00:12:53,200 --> 00:12:55,290 or in the case of Scratch, the sprite-- 264 00:12:55,290 --> 00:12:56,520 to do something. 265 00:12:56,520 --> 00:12:58,430 Say hello in this case. 266 00:12:58,430 --> 00:13:01,450 >> Something like this we instead called what? 267 00:13:01,450 --> 00:13:03,560 Yeah, so a Boolean expression. 268 00:13:03,560 --> 00:13:07,380 A Boolean expression is just something that is true or false, and so Scratch 269 00:13:07,380 --> 00:13:10,530 draws them with this shape, but also with a question mark to convey the 270 00:13:10,530 --> 00:13:14,510 idea that the answer to this question is either true or false, yes or no 271 00:13:14,510 --> 00:13:15,910 one, 1 or 0. 272 00:13:15,910 --> 00:13:19,290 >> And we use these Boolean expressions inside of constructs like these, which 273 00:13:19,290 --> 00:13:21,010 we called what? 274 00:13:21,010 --> 00:13:22,460 So, condition or branch. 275 00:13:22,460 --> 00:13:25,600 And the condition you see here in Scratch has a little placeholder for a 276 00:13:25,600 --> 00:13:28,260 puzzle piece of that shape, the purpose of which is so that you can 277 00:13:28,260 --> 00:13:32,670 drag it and drop it on top and then dictate to the program if this Boolean 278 00:13:32,670 --> 00:13:36,410 expression is true, do this set of instructions, this set of statements, 279 00:13:36,410 --> 00:13:39,320 else do this other set of instructions. 280 00:13:39,320 --> 00:13:41,270 >> And recall that you can nest these things. 281 00:13:41,270 --> 00:13:44,170 Even though there's not much room visually in that puzzle piece, Scratch 282 00:13:44,170 --> 00:13:47,820 will grow and shrink to fit whatever puzzle pieces you drag and drop in 283 00:13:47,820 --> 00:13:51,410 there so you can actually nest this and have a three way fork in the road. 284 00:13:51,410 --> 00:13:53,870 If, else if, else. 285 00:13:53,870 --> 00:13:58,100 And you can even go beyond that by nesting further and further as needed. 286 00:13:58,100 --> 00:14:02,520 >> So lastly, we saw constructs like this, otherwise known as a loop. 287 00:14:02,520 --> 00:14:05,690 And this is just something that does something again and again and again. 288 00:14:05,690 --> 00:14:09,250 In this case, it happens to be a finite number of times, but we also 289 00:14:09,250 --> 00:14:12,890 saw an example where the number of times came from a variable, so it 290 00:14:12,890 --> 00:14:13,690 could change. 291 00:14:13,690 --> 00:14:16,910 And we also saw another block altogether called a forever block that 292 00:14:16,910 --> 00:14:19,590 allowed us to loop infinitely long. 293 00:14:19,590 --> 00:14:23,300 >> So one other construct that Scratch 2.0 has-- and those of you more 294 00:14:23,300 --> 00:14:26,330 comfortable with prior background are already well familiar with this 295 00:14:26,330 --> 00:14:27,080 construct-- 296 00:14:27,080 --> 00:14:29,910 these things we called what? 297 00:14:29,910 --> 00:14:32,540 So we called these functions, otherwise known as procedures. 298 00:14:32,540 --> 00:14:35,030 There's a slight semantic difference, but we'll call them just functions. 299 00:14:35,030 --> 00:14:39,950 >> And a function, in the case of Scratch, is a custom puzzle piece that 300 00:14:39,950 --> 00:14:43,670 you yourself can create so that you have a new puzzle piece that, in this 301 00:14:43,670 --> 00:14:47,190 case, would be called cough, that appears among all of the other 302 00:14:47,190 --> 00:14:48,730 available puzzle pieces in Scratch. 303 00:14:48,730 --> 00:14:52,010 Even though MIT didn't invent this puzzle piece, you did. 304 00:14:52,010 --> 00:14:57,600 >> So a function allows you to create new behavior, give it a function name, and 305 00:14:57,600 --> 00:14:59,200 then call it, so to speak. 306 00:14:59,200 --> 00:15:04,690 Really use it in programs again and again and again without having to wait 307 00:15:04,690 --> 00:15:08,600 for someone like MIT down the road to invent new functionality from Scratch. 308 00:15:08,600 --> 00:15:12,460 You yourself can build these puzzle pieces yourself, and then reuse them 309 00:15:12,460 --> 00:15:13,600 in your own programs. 310 00:15:13,600 --> 00:15:17,520 And we'll see a recurring example of that today and onward. 311 00:15:17,520 --> 00:15:20,960 >> So today, we transition now to something more arcane. 312 00:15:20,960 --> 00:15:24,350 But at the end of the day, something more powerful and expressive and more 313 00:15:24,350 --> 00:15:28,110 representative of the path that we'll be on all semester throughout a number 314 00:15:28,110 --> 00:15:28,800 of languages-- 315 00:15:28,800 --> 00:15:30,800 that of code or source code. 316 00:15:30,800 --> 00:15:34,720 And source code, it turns out, is not what a computer 317 00:15:34,720 --> 00:15:37,400 typically actually runs. 318 00:15:37,400 --> 00:15:39,860 Source code looks something like this. 319 00:15:39,860 --> 00:15:43,010 >> So this is, again, perhaps the simplest program we can write in a 320 00:15:43,010 --> 00:15:46,670 language called C. We will start to tease this apart before long, and if 321 00:15:46,670 --> 00:15:49,260 this really does look like Greek to you right now, trust me. 322 00:15:49,260 --> 00:15:54,040 Within just a week or two's time, this will be all too familiar and actually 323 00:15:54,040 --> 00:15:57,780 quite representative of increasingly complex examples with which you'll 324 00:15:57,780 --> 00:15:59,870 also get more comfortable. 325 00:15:59,870 --> 00:16:03,100 >> But you can't just run this kind of code usually. 326 00:16:03,100 --> 00:16:06,750 You have to turn it into something that the computer itself understands. 327 00:16:06,750 --> 00:16:09,840 And so for that, we need something we're going to call a compiler. 328 00:16:09,840 --> 00:16:13,910 A compiler is a program that takes source code, like you just saw, as 329 00:16:13,910 --> 00:16:17,680 input, and it produces zeroes and ones as output, otherwise 330 00:16:17,680 --> 00:16:19,210 known as object code. 331 00:16:19,210 --> 00:16:23,190 And it's those zeroes and ones that might look quite like this that 332 00:16:23,190 --> 00:16:26,310 ultimately are understood by your Mac or your PC. 333 00:16:26,310 --> 00:16:30,050 >> If you've ever heard the marketing expression Intel Inside, that just 334 00:16:30,050 --> 00:16:32,800 means that a company called Intel has manufactured the 335 00:16:32,800 --> 00:16:34,130 brains of your computer-- 336 00:16:34,130 --> 00:16:37,270 otherwise known as the CPU, central processing unit-- 337 00:16:37,270 --> 00:16:39,640 and that's just the thing that understands patterns 338 00:16:39,640 --> 00:16:40,880 of zeroes and ones. 339 00:16:40,880 --> 00:16:46,330 And so by converting source code into object code through this process here 340 00:16:46,330 --> 00:16:49,130 that we'll do with a couple of commands in just a bit, you are 341 00:16:49,130 --> 00:16:53,070 creating patterns of zeroes and ones that the employees at Intel have 342 00:16:53,070 --> 00:16:56,000 decided represent certain statements. 343 00:16:56,000 --> 00:16:58,810 >> Now, I don't quite know which is which by just glancing at 344 00:16:58,810 --> 00:16:59,890 these zeroes and ones. 345 00:16:59,890 --> 00:17:01,420 Most humans these days don't. 346 00:17:01,420 --> 00:17:04,900 But somewhere in there is a pattern of zeroes and ones that represents the 347 00:17:04,900 --> 00:17:06,349 statement print. 348 00:17:06,349 --> 00:17:10,460 Somewhere in there could be a different set of zeroes and ones that 349 00:17:10,460 --> 00:17:16,940 represent the notion of forever or repeat 10 times or even meow, if it's 350 00:17:16,940 --> 00:17:20,000 actually a program that can include some sounds. 351 00:17:20,000 --> 00:17:24,390 >> So in short, humans have just decided, much like we did for ASCII-- 352 00:17:24,390 --> 00:17:27,480 for letters of the alphabet last week-- humans have decided that even 353 00:17:27,480 --> 00:17:32,200 more complex patterns of zeroes and ones represent more complex behavior 354 00:17:32,200 --> 00:17:35,650 like printing or saying or meowing. 355 00:17:35,650 --> 00:17:38,640 And so for today, and largely onward in the semester, we'll take for 356 00:17:38,640 --> 00:17:41,820 granted that someone has figured out how to do that mapping. 357 00:17:41,820 --> 00:17:45,560 But we, consistent with this idea of layering on top of the work of people 358 00:17:45,560 --> 00:17:49,340 who've come before us, will take for granted the fact that this is going on 359 00:17:49,340 --> 00:17:50,300 underneath the hood. 360 00:17:50,300 --> 00:17:53,840 But we're much more interested in building things that are more 361 00:17:53,840 --> 00:17:55,880 interesting on top of all this. 362 00:17:55,880 --> 00:17:58,900 >> And so indeed, the first program we wrote in Scratch was this super simple 363 00:17:58,900 --> 00:18:00,600 one, Hello World. 364 00:18:00,600 --> 00:18:05,100 And you can think of this as being the main program that governs Scratch's 365 00:18:05,100 --> 00:18:07,280 behavior as of last Friday. 366 00:18:07,280 --> 00:18:11,510 >> Today, we're going to start to translate Scratch puzzle pieces into 367 00:18:11,510 --> 00:18:16,040 source code in this language called C so that what looked like this on 368 00:18:16,040 --> 00:18:20,230 Friday, starting today onward, is now going to look like this. 369 00:18:20,230 --> 00:18:21,860 Admittedly more cryptic. 370 00:18:21,860 --> 00:18:25,010 It's pretty distracting and sort of mind numbing to see all of the quotes 371 00:18:25,010 --> 00:18:27,760 and the semicolons and the parentheses and so forth. 372 00:18:27,760 --> 00:18:32,350 >> But if you start to ignore anything you don't understand, and really look 373 00:18:32,350 --> 00:18:35,960 at the essence of the white text on the screen, surely you see, like I do, 374 00:18:35,960 --> 00:18:40,360 "hello, world." That lines up with the purple statement up top, and it turns 375 00:18:40,360 --> 00:18:44,790 out that the keyword main is going to line up conceptually with when green 376 00:18:44,790 --> 00:18:46,350 flag clicked. 377 00:18:46,350 --> 00:18:49,820 >> So in other words, whereas in Scratch, we implement this Hello World program 378 00:18:49,820 --> 00:18:53,360 with those two puzzle pieces, in C, we're going to implement it with these 379 00:18:53,360 --> 00:18:55,510 four somewhat cryptic lines. 380 00:18:55,510 --> 00:18:58,130 But as we dive deeper into this, you'll see that each one of those 381 00:18:58,130 --> 00:19:02,730 white characters on the screen actually has some significant meaning, 382 00:19:02,730 --> 00:19:05,880 and most of our programs are going to look structurally quite the same. 383 00:19:05,880 --> 00:19:08,980 So a statement, to be clear, what looked like "say hello world" on 384 00:19:08,980 --> 00:19:12,020 Friday is going to start looking like this on Monday. 385 00:19:12,020 --> 00:19:15,250 printf ("hello, world"). 386 00:19:15,250 --> 00:19:17,700 And there's some weird backslash n, there's parentheses, there's 387 00:19:17,700 --> 00:19:18,490 semicolons. 388 00:19:18,490 --> 00:19:21,950 >> But at the end of the day, the key takeaway today is that all we're doing 389 00:19:21,950 --> 00:19:25,710 is translating things from picture form to text form. 390 00:19:25,710 --> 00:19:28,820 So a loop that looked like this last week is going to now look 391 00:19:28,820 --> 00:19:30,140 something like this. 392 00:19:30,140 --> 00:19:33,020 >> And let me disclaim, just like in Scratch, there's different ways to 393 00:19:33,020 --> 00:19:34,110 achieve the same goal. 394 00:19:34,110 --> 00:19:38,860 Same in C. I'm not showing you the only way to do things, but one way to 395 00:19:38,860 --> 00:19:43,230 translate this forever block to a loop in C. 396 00:19:43,230 --> 00:19:44,980 >> This one here, repeat 10 times. 397 00:19:44,980 --> 00:19:47,580 This one really kind of bends the mind the first time you see it. 398 00:19:47,580 --> 00:19:49,860 But to implement that henceforth, we're going to use a 399 00:19:49,860 --> 00:19:51,990 for loop, so to speak. 400 00:19:51,990 --> 00:19:56,150 And clearly there's some syntax that's a little complex there with I and 401 00:19:56,150 --> 00:19:59,740 equal and 0, but once we dive into that today onward, you're just going 402 00:19:59,740 --> 00:20:04,260 to see that that's all simply doing some basic arithmetic to get us from 0 403 00:20:04,260 --> 00:20:07,720 on up to 9 or 10 in this case. 404 00:20:07,720 --> 00:20:11,570 >> Lastly, with variables, you'll recall that a variable is a piece of storage. 405 00:20:11,570 --> 00:20:14,290 Well, what we had on the left there last week is going to look a little 406 00:20:14,290 --> 00:20:15,920 something like this this week. 407 00:20:15,920 --> 00:20:17,870 But again, more on that to come. 408 00:20:17,870 --> 00:20:20,700 >> We had Boolean expressions, which last week, looked like this. 409 00:20:20,700 --> 00:20:23,370 This week onward, they're going to start to look a little like that. 410 00:20:23,370 --> 00:20:24,890 >> And this one's actually a little reassuring. 411 00:20:24,890 --> 00:20:27,680 It's actually almost as simple as the green blocks. 412 00:20:27,680 --> 00:20:31,115 But here we have, again, some new syntax with ampersands, but you can 413 00:20:31,115 --> 00:20:35,390 perhaps guess whereas in Scratch, you say A-N-D, apparently in C, we're 414 00:20:35,390 --> 00:20:37,070 going to say ampersand ampersand. 415 00:20:37,070 --> 00:20:39,650 But all of this will become natural before long. 416 00:20:39,650 --> 00:20:40,680 >> Conditions. 417 00:20:40,680 --> 00:20:42,610 Last week, it looked like this on the left. 418 00:20:42,610 --> 00:20:44,800 This week, on the right, it's going to look like that. 419 00:20:44,800 --> 00:20:48,560 But again, the key takeaway here is that as you see things like that on 420 00:20:48,560 --> 00:20:51,950 the right, and especially if less comfortable, it's sort of over your 421 00:20:51,950 --> 00:20:54,950 head, you get a little overwhelmed that I really have no idea how I could 422 00:20:54,950 --> 00:20:58,420 possibly come up with that from scratch-- 423 00:20:58,420 --> 00:20:59,500 no pun intended-- 424 00:20:59,500 --> 00:21:05,300 just realize that it's really the same idea that probably felt so much more 425 00:21:05,300 --> 00:21:06,760 natural the previous week. 426 00:21:06,760 --> 00:21:10,960 And even if you haven't dived into problem set 0, I daresay you'll find 427 00:21:10,960 --> 00:21:13,790 that the puzzle pieces are fairly intuitive, or at least 428 00:21:13,790 --> 00:21:16,050 become so over time. 429 00:21:16,050 --> 00:21:16,990 >> So let's dive in. 430 00:21:16,990 --> 00:21:20,760 Let's get our hands dirty here with this first simple program. 431 00:21:20,760 --> 00:21:22,410 And simple is, of course, relative. 432 00:21:22,410 --> 00:21:25,190 To do this, I'm going to change screens to that device I called the 433 00:21:25,190 --> 00:21:26,150 CS50 appliance. 434 00:21:26,150 --> 00:21:29,700 The CS50 appliance is a piece of software that's running inside a 435 00:21:29,700 --> 00:21:33,920 window on my Mac, and you can also run it in a window inside of your PC, that 436 00:21:33,920 --> 00:21:38,720 allows me to have the same exact computer environment as every TF, as 437 00:21:38,720 --> 00:21:40,940 every CA, as every student in the class. 438 00:21:40,940 --> 00:21:44,590 Because the CS50 appliance is literally another operating system 439 00:21:44,590 --> 00:21:45,410 called Linux-- 440 00:21:45,410 --> 00:21:47,160 specifically Fedora Linux-- 441 00:21:47,160 --> 00:21:51,320 and we, the staff, have configured the installation process for this 442 00:21:51,320 --> 00:21:55,200 operating system in such a way that one, there's a little CS50 logo in the 443 00:21:55,200 --> 00:21:59,240 middle, two, there's a custom menu at the bottom, and three, pedagogically, 444 00:21:59,240 --> 00:22:02,570 we've simplified the user interface as much as possible so that when you 445 00:22:02,570 --> 00:22:05,540 first boot this thing up on your own Mac or PC, you have the same 446 00:22:05,540 --> 00:22:08,280 environment as everyone else, and you have all of the tools that you need 447 00:22:08,280 --> 00:22:11,390 for the course without having to figure out how to install each and 448 00:22:11,390 --> 00:22:13,280 every tool individually. 449 00:22:13,280 --> 00:22:15,840 >> So this looks relatively like Mac OS or Windows. 450 00:22:15,840 --> 00:22:18,760 In the bottom left hand corner, you essentially have a Start menu of 451 00:22:18,760 --> 00:22:20,280 sorts, or an Apple menu. 452 00:22:20,280 --> 00:22:23,700 And then you have what we suspect will be your three most common icons. 453 00:22:23,700 --> 00:22:24,460 One is Chrome. 454 00:22:24,460 --> 00:22:26,460 There's a browser inside of the appliance. 455 00:22:26,460 --> 00:22:29,970 >> Two is a program called gedit, which we saw briefly last week, which is 456 00:22:29,970 --> 00:22:31,310 just a graphical editor. 457 00:22:31,310 --> 00:22:35,350 And three is a Terminal program, which is a black and white window from 458 00:22:35,350 --> 00:22:39,860 yesteryear that allows us to type more arcane but more powerful commands at 459 00:22:39,860 --> 00:22:40,660 our keyboard. 460 00:22:40,660 --> 00:22:44,220 So I'm going to go ahead and click on gedit, and rest assured that problem 461 00:22:44,220 --> 00:22:48,520 set 1, to be released on Friday, will walk you through all these same steps. 462 00:22:48,520 --> 00:22:51,730 >> Notice that I have a pretty simple user interface with three parts. 463 00:22:51,730 --> 00:22:54,990 On the left, I have a big empty window called source code. 464 00:22:54,990 --> 00:22:58,930 That's where I'm going to see a summary of any code that I write. 465 00:22:58,930 --> 00:23:01,990 On the right hand side, I have a big tab, currently unsaved, and that's 466 00:23:01,990 --> 00:23:03,510 where my code's going to go. 467 00:23:03,510 --> 00:23:06,780 And on the bottom, I have an embedded Terminal window, a black and white 468 00:23:06,780 --> 00:23:12,060 prompt at which I can type commands to compile and to run my programs-- that 469 00:23:12,060 --> 00:23:14,110 is, to make and to run my programs. 470 00:23:14,110 --> 00:23:15,170 >> So let's start simple. 471 00:23:15,170 --> 00:23:17,700 Let me go up to File, Save. 472 00:23:17,700 --> 00:23:20,530 I'm going to go ahead and go into jharvard. 473 00:23:20,530 --> 00:23:24,320 So the appliance, irrespective of what your name is, belongs to a guy named 474 00:23:24,320 --> 00:23:28,250 John Harvard, who has a jharvard home directory inside of which all of your 475 00:23:28,250 --> 00:23:29,100 files will go. 476 00:23:29,100 --> 00:23:31,640 It's your own copy of his account, so we're not all sharing 477 00:23:31,640 --> 00:23:33,530 the same hard drive. 478 00:23:33,530 --> 00:23:35,580 You just have the illusion of John Harvard's own. 479 00:23:35,580 --> 00:23:38,350 >> In advance, I actually installed Dropbox. 480 00:23:38,350 --> 00:23:42,600 The appliance comes preprepared with Dropbox so that if anything goes wrong 481 00:23:42,600 --> 00:23:45,950 during the semester, if you've configured your appliance with a free 482 00:23:45,950 --> 00:23:48,950 Dropbox account, all of your files will be automatically backed up so you 483 00:23:48,950 --> 00:23:50,430 can very easily recover them. 484 00:23:50,430 --> 00:23:53,980 And I'm going to go into my Dropbox directory, and there's already going 485 00:23:53,980 --> 00:23:55,530 to be a couple other things there. 486 00:23:55,530 --> 00:24:02,090 >> But I'm going to go ahead and go to File, Save. 487 00:24:02,090 --> 00:24:05,930 I'm going to go into jharvard, Dropbox, and up here, I'm going to 488 00:24:05,930 --> 00:24:10,590 give my program a super simple name, hello.c, and then save. 489 00:24:10,590 --> 00:24:11,810 So I have the same interface now. 490 00:24:11,810 --> 00:24:14,190 The only thing that's changed is the tab up top. 491 00:24:14,190 --> 00:24:17,170 So I'm very quickly going to recreate that program from the 492 00:24:17,170 --> 00:24:19,060 slides a moment ago. 493 00:24:19,060 --> 00:24:25,690 >> So into main(void), and then printf, "hello, world," a somewhat cryptic 494 00:24:25,690 --> 00:24:29,920 backslash n, close quote, close parenthesis, semicolon, and 495 00:24:29,920 --> 00:24:32,030 then Ctrl S to save. 496 00:24:32,030 --> 00:24:34,860 And notice as an aside what just appeared here on the top left. 497 00:24:34,860 --> 00:24:37,490 And this is not useful today, but will be useful over time. 498 00:24:37,490 --> 00:24:40,160 You just see, again, a summary of the code that you've written. 499 00:24:40,160 --> 00:24:43,320 And because I wrote something called main, that's why the word main popped 500 00:24:43,320 --> 00:24:44,030 up over there. 501 00:24:44,030 --> 00:24:46,610 But for the most part, we don't need that today. 502 00:24:46,610 --> 00:24:51,130 >> All right, so I claim that this is my first program written in a programming 503 00:24:51,130 --> 00:24:54,620 language called C. We know from Friday that what this thing is going to do 504 00:24:54,620 --> 00:24:58,450 very uninterestingly when I run it is just display-- 505 00:24:58,450 --> 00:25:01,270 in other words, print, as a computer scientist would say-- 506 00:25:01,270 --> 00:25:03,310 hello world on the screen. 507 00:25:03,310 --> 00:25:06,800 So to do this, you'll notice that I have to go down here to my terminal 508 00:25:06,800 --> 00:25:09,250 window, and now I have a blinking prompt. 509 00:25:09,250 --> 00:25:15,480 But this is now a keyboard version of my John Harvard home directory. 510 00:25:15,480 --> 00:25:19,610 It's a keyboard way via which to navigate my hard drive. 511 00:25:19,610 --> 00:25:25,380 >> So I can't actually type like I did last week make hello, because it's 512 00:25:25,380 --> 00:25:27,230 going to say no rule to make target-- 513 00:25:27,230 --> 00:25:27,950 something's wrong. 514 00:25:27,950 --> 00:25:31,690 I don't quite know what that message means, but that's because by default, 515 00:25:31,690 --> 00:25:35,350 when this black and white window is opened, I'm inside of John Harvard's 516 00:25:35,350 --> 00:25:36,160 home directory. 517 00:25:36,160 --> 00:25:39,180 But where did I save that program? 518 00:25:39,180 --> 00:25:40,440 In my Dropbox folder. 519 00:25:40,440 --> 00:25:43,110 >> Now, all of us have grown up with computers where you just double click 520 00:25:43,110 --> 00:25:45,610 on a folder's icon, it opens up, and voila. 521 00:25:45,610 --> 00:25:48,280 That's where the file called hello.c would be. 522 00:25:48,280 --> 00:25:49,470 So in fact, let's do that. 523 00:25:49,470 --> 00:25:51,610 >> Let me go ahead and minimize gedit just like you would on 524 00:25:51,610 --> 00:25:52,990 Windows or Mac OS. 525 00:25:52,990 --> 00:25:56,160 Let me go into the Home folder at top left. 526 00:25:56,160 --> 00:25:57,620 I'll see here a bunch of folders. 527 00:25:57,620 --> 00:25:58,870 Here's my Dropbox folder. 528 00:25:58,870 --> 00:26:00,770 The checkmarks means it's been synced. 529 00:26:00,770 --> 00:26:04,040 And there's a few things in here, but notice that hello.c is 530 00:26:04,040 --> 00:26:04,960 indeed right there. 531 00:26:04,960 --> 00:26:08,050 >> And so in Mac OS or Windows, normally, I'd double click that, it opens up in 532 00:26:08,050 --> 00:26:10,220 whatever program, and I'm good to go. 533 00:26:10,220 --> 00:26:13,370 But now, at least starting today, we need to take a step backwards just to 534 00:26:13,370 --> 00:26:16,960 give you some basic tools for your tool kit with which to do more 535 00:26:16,960 --> 00:26:18,980 powerful things before long. 536 00:26:18,980 --> 00:26:22,280 >> So at this prompt, I have to do the equivalent of double clicking the 537 00:26:22,280 --> 00:26:23,560 Dropbox folder. 538 00:26:23,560 --> 00:26:29,900 And to do that, I'm going to type CD for change directory, Dropbox, enter. 539 00:26:29,900 --> 00:26:36,100 And now notice the parentheses at my so-called prompt now says that I'm in 540 00:26:36,100 --> 00:26:38,470 tilde, the little squiggly symbol at the top left of 541 00:26:38,470 --> 00:26:40,860 most keyboards, /Dropbox. 542 00:26:40,860 --> 00:26:45,200 This is just shorthand notation for saying human, you are now inside of 543 00:26:45,200 --> 00:26:48,650 the Dropbox folder, just as though you had double clicked on it. 544 00:26:48,650 --> 00:26:54,610 >> So now if I go ahead and type make hello, enter, I see a very cryptic 545 00:26:54,610 --> 00:26:57,450 command, but not an error message, it seems, and then 546 00:26:57,450 --> 00:26:59,020 another blinking prompt. 547 00:26:59,020 --> 00:27:04,470 And if you recall, ever so quickly on Friday, we did this. ./hello. 548 00:27:04,470 --> 00:27:10,000 >> What you're about to see is the result of my running this program, or, in Mac 549 00:27:10,000 --> 00:27:14,310 OS and PC terms, if I'd normally double click on a program-- a .app 550 00:27:14,310 --> 00:27:17,620 program or a .exe, it opens a window and runs. 551 00:27:17,620 --> 00:27:21,920 Typing ./hello is just like double clicking a program to run it, but 552 00:27:21,920 --> 00:27:23,280 using my keyboard. 553 00:27:23,280 --> 00:27:25,410 >> Enter, hello world. 554 00:27:25,410 --> 00:27:26,150 And that's it. 555 00:27:26,150 --> 00:27:29,150 So let's tease this apart to so it doesn't feel too much like Greek. 556 00:27:29,150 --> 00:27:31,370 >> So ./ means what? 557 00:27:31,370 --> 00:27:34,120 Anyone who's ever navigated a keyboard environment like this, what's dot 558 00:27:34,120 --> 00:27:36,690 referring to? 559 00:27:36,690 --> 00:27:37,800 The current directory. 560 00:27:37,800 --> 00:27:41,320 >> So the current directory is just a synonym for a folder. 561 00:27:41,320 --> 00:27:46,230 So by saying dot slash, I am saying, look for a program called hello in the 562 00:27:46,230 --> 00:27:47,250 current directory-- 563 00:27:47,250 --> 00:27:48,540 in the current folder. 564 00:27:48,540 --> 00:27:50,480 In other words, my Dropbox folder. 565 00:27:50,480 --> 00:27:54,220 And that's why hitting Enter thereafter actually runs that program. 566 00:27:54,220 --> 00:27:58,790 >> If I now type LS, notice that I see everything in my Dropbox folder. 567 00:27:58,790 --> 00:28:02,000 Getting Started.pdf, which Dropbox gives you for free, my photos 568 00:28:02,000 --> 00:28:05,450 directory, a public directory, Unsaved Document 1 which I made by mistake a 569 00:28:05,450 --> 00:28:09,730 moment ago, foo.c, which I made earlier today, and then two lines-- 570 00:28:09,730 --> 00:28:12,090 hello.c and hello. 571 00:28:12,090 --> 00:28:17,280 >> Hello.c is the program I wrote with gedit, and what is hello in green? 572 00:28:17,280 --> 00:28:20,920 573 00:28:20,920 --> 00:28:22,940 The hello in green is the executable. 574 00:28:22,940 --> 00:28:27,020 In other words, when I ran this command a moment ago-- let me roll 575 00:28:27,020 --> 00:28:28,040 back in time-- 576 00:28:28,040 --> 00:28:32,400 when I ran make hello, that's the middle step here. 577 00:28:32,400 --> 00:28:34,410 And it took hello.c as input-- 578 00:28:34,410 --> 00:28:39,910 AKA source code-- and it produced a file called hello as output, which 579 00:28:39,910 --> 00:28:42,510 contains all of these zeros and ones. 580 00:28:42,510 --> 00:28:47,640 And that's why, in the end, I can actually run a command like ./hello. 581 00:28:47,640 --> 00:28:53,310 Because of Intel Inside, my operating system, Linux, opens up those zeros 582 00:28:53,310 --> 00:28:55,820 and ones, feeds them into the brains of my computer-- 583 00:28:55,820 --> 00:28:56,580 the CPU-- 584 00:28:56,580 --> 00:29:00,460 and that CPU knows, oh, here's a pattern of zeros and ones that says 585 00:29:00,460 --> 00:29:02,800 print the following thing on the screen. 586 00:29:02,800 --> 00:29:04,450 >> What's the following thing? 587 00:29:04,450 --> 00:29:07,650 Quote unquote, "hello world." 588 00:29:07,650 --> 00:29:12,100 >> All right, so a quick summary of some of these commands so that we can start 589 00:29:12,100 --> 00:29:13,350 taking them for granted. 590 00:29:13,350 --> 00:29:15,550 Let me skip ahead to where we left off. 591 00:29:15,550 --> 00:29:22,230 And you'll recall that we had CD at our back end a moment ago. 592 00:29:22,230 --> 00:29:24,880 These are, I dare say, for today's purposes-- 593 00:29:24,880 --> 00:29:29,010 maybe this week-- the only commands we might need to type at the command 594 00:29:29,010 --> 00:29:30,580 prompt besides make. 595 00:29:30,580 --> 00:29:34,510 >> So CD stood for what? 596 00:29:34,510 --> 00:29:35,180 Change directory. 597 00:29:35,180 --> 00:29:38,010 It's just the equivalent of double clicking on a folder. 598 00:29:38,010 --> 00:29:41,320 And as an aside, if you get lost ever inside of your 599 00:29:41,320 --> 00:29:43,430 appliance, such that Dropbox-- 600 00:29:43,430 --> 00:29:45,420 I want to get back to my jharvard directory-- 601 00:29:45,420 --> 00:29:50,490 when in doubt, CD, enter, with no other words, and it just zips you back 602 00:29:50,490 --> 00:29:53,110 to where you started, which is a nice little shortcut. 603 00:29:53,110 --> 00:29:54,840 >> All right, so now other commands. 604 00:29:54,840 --> 00:29:55,660 LS. 605 00:29:55,660 --> 00:29:56,840 I typed that a moment ago. 606 00:29:56,840 --> 00:29:59,500 What do you think LS stands for? 607 00:29:59,500 --> 00:30:00,470 >> So, list, right? 608 00:30:00,470 --> 00:30:03,300 Back in the day, when humans were coming up with these commands, they 609 00:30:03,300 --> 00:30:07,180 were trying to be efficient, and rather type out L-I-S-T enter, they 610 00:30:07,180 --> 00:30:08,470 decided LS. 611 00:30:08,470 --> 00:30:09,080 That's fine. 612 00:30:09,080 --> 00:30:10,420 It sounds enough like list. 613 00:30:10,420 --> 00:30:13,870 Let's just type LS enter, and that lists the files or folders in my 614 00:30:13,870 --> 00:30:15,000 current folder. 615 00:30:15,000 --> 00:30:17,530 >> mkdir, you can probably guess. 616 00:30:17,530 --> 00:30:18,440 Make directory. 617 00:30:18,440 --> 00:30:22,010 So you don't go to some file menu and choose New Folder in this environment. 618 00:30:22,010 --> 00:30:27,410 Rather, you type the command mkdir space and then the name of the folder 619 00:30:27,410 --> 00:30:29,850 that you want to create, and it will be done. 620 00:30:29,850 --> 00:30:32,380 >> rm, you can probably guess. 621 00:30:32,380 --> 00:30:33,480 Remove or delete. 622 00:30:33,480 --> 00:30:36,740 So if you want to delete a file, you'll see in the problem set how you 623 00:30:36,740 --> 00:30:37,240 can do this. 624 00:30:37,240 --> 00:30:39,510 And rmdir, remove directory. 625 00:30:39,510 --> 00:30:42,320 >> So, again, I'll tend to fly through some of these details, one, because 626 00:30:42,320 --> 00:30:46,010 they're not all that much fun to spend time on, but two, we'll provide you in 627 00:30:46,010 --> 00:30:48,720 much greater detail in any of the course's problem sets, particularly 628 00:30:48,720 --> 00:30:52,370 the standard editions, any of the steps that you might need to type so 629 00:30:52,370 --> 00:30:54,830 that you get more and more comfy with these things. 630 00:30:54,830 --> 00:30:57,430 >> But now let me tease apart one other thing. 631 00:30:57,430 --> 00:31:02,580 When we typed make hello a moment ago, we saw a cryptic sequence of commands. 632 00:31:02,580 --> 00:31:04,805 We saw, again, make hello. 633 00:31:04,805 --> 00:31:07,640 634 00:31:07,640 --> 00:31:08,520 OK, let's do this. 635 00:31:08,520 --> 00:31:11,840 I was about to make a mistake, but [INAUDIBLE] now admit I was about to 636 00:31:11,840 --> 00:31:14,400 and do it deliberately. 637 00:31:14,400 --> 00:31:15,810 >> What did I do wrong just now? 638 00:31:15,810 --> 00:31:18,680 639 00:31:18,680 --> 00:31:21,060 Yeah, so I'm not in the Dropbox directory, because a moment ago I 640 00:31:21,060 --> 00:31:22,720 typed CD and just hit enter. 641 00:31:22,720 --> 00:31:25,480 That zoomed me back to where I started, which is not where my file 642 00:31:25,480 --> 00:31:31,720 is, so I really need to do CD Dropbox enter, and now I can do rm hello 643 00:31:31,720 --> 00:31:34,060 enter, remove regular file hello. 644 00:31:34,060 --> 00:31:35,200 I don't know why it's so cryptic. 645 00:31:35,200 --> 00:31:37,600 That just means are you sure you want to delete hello? 646 00:31:37,600 --> 00:31:41,510 You can type y for yes, enter, and now it's gone. 647 00:31:41,510 --> 00:31:46,010 >> And now if I type make hello again, it's going to recreate my object code, 648 00:31:46,010 --> 00:31:47,460 recreate those zeros and ones. 649 00:31:47,460 --> 00:31:51,070 But what is the deal with this very long command? 650 00:31:51,070 --> 00:31:53,960 Well, we'll tease this apart before long, but make-- 651 00:31:53,960 --> 00:31:55,500 I told a bit of a white lie before-- 652 00:31:55,500 --> 00:31:56,880 is not a compiler. 653 00:31:56,880 --> 00:32:00,350 Make is a program that automatically figures out how to run a 654 00:32:00,350 --> 00:32:02,060 compiler for you. 655 00:32:02,060 --> 00:32:07,490 >> So the command that you really should be running in order to convert hello.c 656 00:32:07,490 --> 00:32:11,240 into hello actually looks a little more like this. 657 00:32:11,240 --> 00:32:15,900 And God forbid we have to actually remember to ever type that whole 658 00:32:15,900 --> 00:32:17,280 sequence of commands out. 659 00:32:17,280 --> 00:32:21,360 The purpose of make in life is to remember that degree of complexity for 660 00:32:21,360 --> 00:32:24,470 us and automate what would otherwise be very tedious steps. 661 00:32:24,470 --> 00:32:26,040 >> But I can approximate that. 662 00:32:26,040 --> 00:32:29,960 So per this little cheat sheet here, I can approximate what that command is 663 00:32:29,960 --> 00:32:33,960 doing by simply typing clang, which is the name of the compiler we'll be 664 00:32:33,960 --> 00:32:35,170 using this semester. 665 00:32:35,170 --> 00:32:38,500 If any of you have ever done iOS development for the iPhone or iPad, 666 00:32:38,500 --> 00:32:42,930 Clang is the program you've used within xcode to make your apps. 667 00:32:42,930 --> 00:32:46,360 Clang, though, is just another program whose name we can type at the command 668 00:32:46,360 --> 00:32:49,210 prompt, and by introducing this now, I can introduce something that's a 669 00:32:49,210 --> 00:32:52,410 little representative of commands to come. 670 00:32:52,410 --> 00:32:53,910 So let me do this manually. 671 00:32:53,910 --> 00:32:59,240 >> So rm hello, and now notice in case I, just as a sanity check-- 672 00:32:59,240 --> 00:33:03,180 ./hello, what do you think I should see if I do this? 673 00:33:03,180 --> 00:33:04,060 Some kind of error. 674 00:33:04,060 --> 00:33:05,110 No such file directory. 675 00:33:05,110 --> 00:33:06,740 So that one's actually pretty direct. 676 00:33:06,740 --> 00:33:08,110 >> So now let me clear the screen. 677 00:33:08,110 --> 00:33:11,370 Control S just clears the screen to remove some distractions. 678 00:33:11,370 --> 00:33:17,160 And now I'm going to manually type clang -o hello hello.c. 679 00:33:17,160 --> 00:33:18,890 So what is this about to do? 680 00:33:18,890 --> 00:33:23,820 Exactly the same thing that make did for us, but I did throw away some of 681 00:33:23,820 --> 00:33:25,430 the superfluous words. 682 00:33:25,430 --> 00:33:28,570 >> Clang is the program that's going to take a .c file as input and produce 683 00:33:28,570 --> 00:33:30,110 zeros and ones as output. 684 00:33:30,110 --> 00:33:35,120 But -o henceforth will call a switch or a flag. 685 00:33:35,120 --> 00:33:39,080 This is just techie jargon for saying this is a word you type at a prompt 686 00:33:39,080 --> 00:33:42,590 that somehow influences the behavior of the program you're running. 687 00:33:42,590 --> 00:33:47,180 So does anyone want to guess what -o conveys? 688 00:33:47,180 --> 00:33:47,840 >> Output. 689 00:33:47,840 --> 00:33:52,670 It's just someone decided -o means output a file called hello and take as 690 00:33:52,670 --> 00:33:56,440 input whatever the last word on the prompt is, hello.c, so that if I hit 691 00:33:56,440 --> 00:33:58,930 Enter now, nothing seems to happen. 692 00:33:58,930 --> 00:34:02,010 And oddly enough, in this world of programming, if nothing happens, 693 00:34:02,010 --> 00:34:03,980 that's probably a good thing because there's no error 694 00:34:03,980 --> 00:34:05,350 message on the screen. 695 00:34:05,350 --> 00:34:07,190 Hello, Enter. 696 00:34:07,190 --> 00:34:08,670 My program has now run. 697 00:34:08,670 --> 00:34:14,517 >> But I can do stupid little things like this. clang -o, hihihi, hello.c, 698 00:34:14,517 --> 00:34:18,590 enter, and now I have an identical program named hihihi. 699 00:34:18,590 --> 00:34:20,739 It behaves the same, but the name is different. 700 00:34:20,739 --> 00:34:23,360 >> So this is only to say that if you want to influence the behavior of a 701 00:34:23,360 --> 00:34:25,909 program, we're going to increasingly see things called 702 00:34:25,909 --> 00:34:27,739 command line arguments. 703 00:34:27,739 --> 00:34:28,639 This is a line. 704 00:34:28,639 --> 00:34:29,580 You're typing commands. 705 00:34:29,580 --> 00:34:30,969 Ergo, command line. 706 00:34:30,969 --> 00:34:35,690 And command line arguments are just words or little pieces of syntax that 707 00:34:35,690 --> 00:34:40,840 somehow influence the behavior of programs that we are running. 708 00:34:40,840 --> 00:34:44,639 >> All right, that is perhaps the most boring program we can write and see. 709 00:34:44,639 --> 00:34:46,670 Let's now do something more interesting. 710 00:34:46,670 --> 00:34:49,420 Let me go ahead and create a new file. 711 00:34:49,420 --> 00:34:53,139 I'm going to go ahead and save this in John Harvard's home directory as, 712 00:34:53,139 --> 00:34:58,670 let's call it custom.c, to be a little custom program. 713 00:34:58,670 --> 00:35:01,920 I'm going to zoom in at my top of the tabs here and start again. 714 00:35:01,920 --> 00:35:03,730 >> Include standard stdio.h. 715 00:35:03,730 --> 00:35:04,990 More on that in a moment. 716 00:35:04,990 --> 00:35:08,240 int main(void), more on that in a moment. 717 00:35:08,240 --> 00:35:09,490 printf ("hello, David"). 718 00:35:09,490 --> 00:35:12,810 719 00:35:12,810 --> 00:35:14,560 Much more interesting, right? 720 00:35:14,560 --> 00:35:20,690 >> So now if-- not because of my name, just because it's different-- 721 00:35:20,690 --> 00:35:24,950 make custom enter. 722 00:35:24,950 --> 00:35:26,780 Custom, hello, David. 723 00:35:26,780 --> 00:35:28,020 But even that, too, is a white lie. 724 00:35:28,020 --> 00:35:30,850 This program might as well be called david.c, because it's not custom 725 00:35:30,850 --> 00:35:31,880 behavior at all. 726 00:35:31,880 --> 00:35:35,080 Really, I want a program that asks the user for their name. 727 00:35:35,080 --> 00:35:36,320 >> So how do I do that? 728 00:35:36,320 --> 00:35:38,520 Well, let me scroll up over here. 729 00:35:38,520 --> 00:35:42,960 And before I use a statement in Scratch, before I use printf, 730 00:35:42,960 --> 00:35:47,600 otherwise known as a function, let me first ask the user for some input. 731 00:35:47,600 --> 00:35:49,690 >> So to do this, I need to do a couple of things. 732 00:35:49,690 --> 00:35:53,360 One, I need a place to store the user's name after I've asked him or 733 00:35:53,360 --> 00:35:54,510 her for it. 734 00:35:54,510 --> 00:36:00,120 What construct do I need to use probably to store a value like that? 735 00:36:00,120 --> 00:36:00,910 A variable. 736 00:36:00,910 --> 00:36:05,130 >> So unlike Scratch, where you just get a nice puzzle piece, here, we have to 737 00:36:05,130 --> 00:36:07,620 use, obviously, words to express ourselves. 738 00:36:07,620 --> 00:36:11,510 And henceforth, anytime you want a word or a phrase, in programming, 739 00:36:11,510 --> 00:36:13,090 we're going to call that a string. 740 00:36:13,090 --> 00:36:14,990 So a string is a word or a phrase. 741 00:36:14,990 --> 00:36:18,170 It's a sequence of zero or more characters, more formally. 742 00:36:18,170 --> 00:36:22,520 >> And if you want such a variable to exist, you have to say give me one. 743 00:36:22,520 --> 00:36:27,490 And the way by which you do that is to say string s semicolon, where s can be 744 00:36:27,490 --> 00:36:28,740 anything you want. 745 00:36:28,740 --> 00:36:30,510 In fact, s is a little underwhelming. 746 00:36:30,510 --> 00:36:33,250 Let me go ahead and more clearly call it name. 747 00:36:33,250 --> 00:36:36,570 Give me a string called name. 748 00:36:36,570 --> 00:36:40,180 >> But now, what is the person's name by default? 749 00:36:40,180 --> 00:36:42,930 Well, if the human doesn't type anything in, obviously, there's going 750 00:36:42,930 --> 00:36:44,310 to be no value in name. 751 00:36:44,310 --> 00:36:48,120 And it turns out in C, if you don't give a variable a value, it's going to 752 00:36:48,120 --> 00:36:49,180 have a garbage value. 753 00:36:49,180 --> 00:36:51,650 It might have some random zeros and ones, so you're just going to see 754 00:36:51,650 --> 00:36:54,570 garbage, truly, on the screen if you try to display it. 755 00:36:54,570 --> 00:36:59,800 >> But I can fix that by saying, actually, store inside of name the 756 00:36:59,800 --> 00:37:01,400 following value. 757 00:37:01,400 --> 00:37:09,240 The way you store values in variables is if you have a variable name on the 758 00:37:09,240 --> 00:37:13,370 left, you have to somehow put what you want to store in it on the right, and 759 00:37:13,370 --> 00:37:17,650 the syntax for that's going to be as follows. 760 00:37:17,650 --> 00:37:22,120 >> We've not seen this before, but take on faith for a moment that there 761 00:37:22,120 --> 00:37:26,270 exists another statement in the world that I'm going to again call function, 762 00:37:26,270 --> 00:37:28,310 and this function is called get string. 763 00:37:28,310 --> 00:37:29,670 And it does literally that. 764 00:37:29,670 --> 00:37:34,680 It somehow gets a sequence of characters from the human and then 765 00:37:34,680 --> 00:37:36,380 does something with it. 766 00:37:36,380 --> 00:37:38,410 >> And this does look like cryptic syntax. 767 00:37:38,410 --> 00:37:40,880 Normally, if you're using equal signs-- you're in the world of, say, 768 00:37:40,880 --> 00:37:43,740 algebra, and you're actually saying x equals 1. 769 00:37:43,740 --> 00:37:46,570 Here we're not saying that name equals get string. 770 00:37:46,570 --> 00:37:51,270 We're saying store in name what's on the right hand side 771 00:37:51,270 --> 00:37:52,400 of that equals sign. 772 00:37:52,400 --> 00:37:55,030 The equals sign represents the assignment operator. 773 00:37:55,030 --> 00:37:58,240 It's the syntax that says put what's on the right hand side inside what's 774 00:37:58,240 --> 00:37:59,090 on the left. 775 00:37:59,090 --> 00:38:03,040 So get string is a new piece of functionality that not only does 776 00:38:03,040 --> 00:38:03,670 something-- 777 00:38:03,670 --> 00:38:05,000 asks the user for a string-- 778 00:38:05,000 --> 00:38:08,500 it also returns it, so to speak. 779 00:38:08,500 --> 00:38:10,690 >> So let's see what's going to happen here. 780 00:38:10,690 --> 00:38:14,300 I'm going to go ahead now and zoom out, and I'm going to get a couple of 781 00:38:14,300 --> 00:38:15,780 mistakes at my prompt. 782 00:38:15,780 --> 00:38:18,750 I'm going to type make custom, enter. 783 00:38:18,750 --> 00:38:19,230 Wow. 784 00:38:19,230 --> 00:38:20,500 A whole bunch of mistakes. 785 00:38:20,500 --> 00:38:23,120 >> All right, so this is kind of overwhelming, certainly at first, but 786 00:38:23,120 --> 00:38:27,220 it turns out all of these very admittedly cryptic error messages are 787 00:38:27,220 --> 00:38:30,700 referring to some patterns that we'll start to see over time. 788 00:38:30,700 --> 00:38:32,600 So this one I'll pluck off, because it's first. 789 00:38:32,600 --> 00:38:35,700 Use of undeclared identifier string. 790 00:38:35,700 --> 00:38:39,400 >> That just means that gedit, or Clang, in this case, doesn't know 791 00:38:39,400 --> 00:38:40,780 what I mean by string. 792 00:38:40,780 --> 00:38:43,880 And that's because in C, the programming language, there actually 793 00:38:43,880 --> 00:38:47,750 is no variable of type string. 794 00:38:47,750 --> 00:38:49,810 C does not know that strings exist. 795 00:38:49,810 --> 00:38:52,260 >> But we, CS50 staff, do. 796 00:38:52,260 --> 00:38:57,080 And so in advance of class, we created a file called CS50.h. 797 00:38:57,080 --> 00:39:00,060 And inside of this file-- and more on the syntax to come-- 798 00:39:00,060 --> 00:39:03,730 inside of this file is a definition of what I just defined 799 00:39:03,730 --> 00:39:05,340 verbally as a string. 800 00:39:05,340 --> 00:39:09,100 So simply by concluding this one line of code in my program, I am now 801 00:39:09,100 --> 00:39:14,620 teaching gedit and make and Clang all in turn what a string is. 802 00:39:14,620 --> 00:39:15,475 >> I don't know how yet. 803 00:39:15,475 --> 00:39:17,110 In a few weeks, we'll peel back that layer. 804 00:39:17,110 --> 00:39:20,340 But for now, know that this teaches the compiler what a 805 00:39:20,340 --> 00:39:22,020 string actually is. 806 00:39:22,020 --> 00:39:24,230 >> So now let me clear my terminal down here. 807 00:39:24,230 --> 00:39:28,530 Let me re-run make custom, and I solved all of those problems. 808 00:39:28,530 --> 00:39:30,210 So this, too, is going to be a recurring theme. 809 00:39:30,210 --> 00:39:34,190 I only wrote a 9 line program, and yet I had 18 lines of errors. 810 00:39:34,190 --> 00:39:35,650 That's kind of intimidating. 811 00:39:35,650 --> 00:39:39,260 But realize that they often cascade such that one little mistake triggers 812 00:39:39,260 --> 00:39:43,070 the confusion for the compiler, and it looks much worse than it is. 813 00:39:43,070 --> 00:39:47,460 >> So now an astute observer will know that I'm getting a string from the 814 00:39:47,460 --> 00:39:51,460 user, but I'm not actually doing what with it? 815 00:39:51,460 --> 00:39:53,570 I'm not actually doing anything with it. 816 00:39:53,570 --> 00:40:01,240 So if I actually run custom again, it's going to just sit there. 817 00:40:01,240 --> 00:40:03,380 What's going on? 818 00:40:03,380 --> 00:40:07,990 My program seems to be stuck on what line, would you say? 819 00:40:07,990 --> 00:40:09,860 >> So it's actually stuck on line 7. 820 00:40:09,860 --> 00:40:13,600 And we've not seen this before, but on line 7, there's, again, this 821 00:40:13,600 --> 00:40:14,650 statement, get string. 822 00:40:14,650 --> 00:40:15,930 And it does what it says. 823 00:40:15,930 --> 00:40:18,480 It's waiting now for the human to take the string. 824 00:40:18,480 --> 00:40:22,230 This is completely unintuitive to me, the human, because I haven't been told 825 00:40:22,230 --> 00:40:23,040 what to do. 826 00:40:23,040 --> 00:40:27,320 >> But let me go ahead on faith and let me pretend to be Rob. 827 00:40:27,320 --> 00:40:28,500 Enter. 828 00:40:28,500 --> 00:40:30,930 That's not the program I intended. 829 00:40:30,930 --> 00:40:32,350 So we have our first blog. 830 00:40:32,350 --> 00:40:34,390 >> But let's try to reason through why this is the case. 831 00:40:34,390 --> 00:40:36,940 First, let me fix this first problem. 832 00:40:36,940 --> 00:40:44,690 Let me add a line here that says printf, what is your name? 833 00:40:44,690 --> 00:40:47,700 Let's just do close quote, close parenthesis. 834 00:40:47,700 --> 00:40:51,350 >> Now let me re-run the program. 835 00:40:51,350 --> 00:40:51,760 Problem. 836 00:40:51,760 --> 00:40:54,690 I'm not seeing that. 837 00:40:54,690 --> 00:40:54,930 >> Right. 838 00:40:54,930 --> 00:40:55,990 I didn't recompile it. 839 00:40:55,990 --> 00:40:59,800 So in a language like C, because you change the source code does not mean 840 00:40:59,800 --> 00:41:03,170 that you have changed your object code to generate new object code, 841 00:41:03,170 --> 00:41:04,330 new zeros and ones. 842 00:41:04,330 --> 00:41:05,590 You have to recompile. 843 00:41:05,590 --> 00:41:10,300 If I now re-run custom, enter, ah, a little more clear. 844 00:41:10,300 --> 00:41:11,890 My name is Rob. 845 00:41:11,890 --> 00:41:12,760 Still buggy. 846 00:41:12,760 --> 00:41:14,170 >> And I'd argue there's a couple bugs now. 847 00:41:14,170 --> 00:41:15,450 This just looks hideous, frankly. 848 00:41:15,450 --> 00:41:18,980 Like, minimally, I should have a space there, or move the 849 00:41:18,980 --> 00:41:20,330 cursor to the new line. 850 00:41:20,330 --> 00:41:21,370 >> And actually, new line. 851 00:41:21,370 --> 00:41:24,660 That's kind of a key phrase here that we've seen but not defined. 852 00:41:24,660 --> 00:41:28,560 The way in which you get a text to move onto the next line, 853 00:41:28,560 --> 00:41:29,480 you don't do this. 854 00:41:29,480 --> 00:41:31,070 This is confusing to the computer. 855 00:41:31,070 --> 00:41:33,830 This looks like two separate lines of code, neither of which 856 00:41:33,830 --> 00:41:35,010 is perfectly correct. 857 00:41:35,010 --> 00:41:39,380 You instead have to be more explicit and say, give me a new line, which we 858 00:41:39,380 --> 00:41:41,810 represent in c with backslash n. 859 00:41:41,810 --> 00:41:43,230 >> So there's a few such characters. 860 00:41:43,230 --> 00:41:45,080 Backslash n will fix that aesthetic. 861 00:41:45,080 --> 00:41:49,640 862 00:41:49,640 --> 00:41:50,870 Let me recompile my program. 863 00:41:50,870 --> 00:41:52,300 Let me re-run my program. 864 00:41:52,300 --> 00:41:53,520 >> This looks better. 865 00:41:53,520 --> 00:41:56,960 It's still pretty primitive, but it's also still buggy. 866 00:41:56,960 --> 00:41:58,090 So there's one-- 867 00:41:58,090 --> 00:42:01,680 even if you've never programmed before, you can probably take a guess 868 00:42:01,680 --> 00:42:04,960 at why this program is still flawed. 869 00:42:04,960 --> 00:42:08,540 What do I need to do that I haven't done yet? 870 00:42:08,540 --> 00:42:10,450 >> So I have to actually do something with name. 871 00:42:10,450 --> 00:42:14,190 Just like in scratch, you might actually assign a variable like n or 872 00:42:14,190 --> 00:42:16,150 counter like we did last week-- a value. 873 00:42:16,150 --> 00:42:18,930 But if you don't do anything with it, nothing's going to happen. 874 00:42:18,930 --> 00:42:20,210 >> All right, so let fix. 875 00:42:20,210 --> 00:42:23,670 Let me type my variable there, and let me go down here. 876 00:42:23,670 --> 00:42:29,120 Let me recompile, let me re-run, Rob. 877 00:42:29,120 --> 00:42:30,540 Now I'm just an idiot, right? 878 00:42:30,540 --> 00:42:33,280 >> So this is not really getting me any further. 879 00:42:33,280 --> 00:42:35,850 But that was a perfectly natural instinct, I'd argue, right? 880 00:42:35,850 --> 00:42:39,340 If the variable is called name, and I want it to go there, why don't I just 881 00:42:39,340 --> 00:42:40,200 type it there? 882 00:42:40,200 --> 00:42:42,210 >> Well, of course, we have to distinguish now 883 00:42:42,210 --> 00:42:44,070 between what is a string? 884 00:42:44,070 --> 00:42:47,060 Notice that quote unquote here, hello name? 885 00:42:47,060 --> 00:42:48,960 This actually itself is a string. 886 00:42:48,960 --> 00:42:49,820 It's hard coded. 887 00:42:49,820 --> 00:42:50,690 It's not from the human. 888 00:42:50,690 --> 00:42:54,020 It's from me, the original programmer, but it's still a string. 889 00:42:54,020 --> 00:42:58,300 >> So if you literally write N-A-M-E inside of a string, what's going to 890 00:42:58,300 --> 00:42:59,120 get printed? 891 00:42:59,120 --> 00:43:03,660 Well, N-A-M-E. We instead need to tell printf, don't print 892 00:43:03,660 --> 00:43:04,870 out literally name. 893 00:43:04,870 --> 00:43:06,680 Print out the value of name. 894 00:43:06,680 --> 00:43:08,410 >> And to do that, we do the following. 895 00:43:08,410 --> 00:43:09,800 And this is just a human convention. 896 00:43:09,800 --> 00:43:14,990 We instead say %s, and that stands for string. 897 00:43:14,990 --> 00:43:19,810 And then at the end of my close quotes, I'm going to put a comma and 898 00:43:19,810 --> 00:43:23,790 pass a second argument into this statement. 899 00:43:23,790 --> 00:43:25,970 I'm going to type name there. 900 00:43:25,970 --> 00:43:27,960 >> So now notice we have a slightly new syntax. 901 00:43:27,960 --> 00:43:32,320 We still have two parentheses, and henceforth let me say that the stuff 902 00:43:32,320 --> 00:43:36,830 between parentheses represents arguments into a function-- 903 00:43:36,830 --> 00:43:39,730 inputs that are somehow going to influence its behavior. 904 00:43:39,730 --> 00:43:43,010 So by that logic, how many arguments does printf seem to be 905 00:43:43,010 --> 00:43:45,450 taking at this moment? 906 00:43:45,450 --> 00:43:48,370 >> So it seems to be taking two, and that's indeed implied 907 00:43:48,370 --> 00:43:49,570 by the comma here. 908 00:43:49,570 --> 00:43:51,640 So this is one argument in between quotes. 909 00:43:51,640 --> 00:43:54,540 Even though it has a comma in it, everything's in quotes, which means 910 00:43:54,540 --> 00:43:56,230 it's one long string. 911 00:43:56,230 --> 00:43:58,280 Comma, variable name. 912 00:43:58,280 --> 00:44:00,680 >> So now let me zoom out. 913 00:44:00,680 --> 00:44:02,240 Let me re-run it. 914 00:44:02,240 --> 00:44:03,490 Rob. 915 00:44:03,490 --> 00:44:06,520 Damn it, what did I do wrong? 916 00:44:06,520 --> 00:44:07,920 >> All right, so you have to recompile. 917 00:44:07,920 --> 00:44:11,030 So again, easy mistakes to make early on again and again. 918 00:44:11,030 --> 00:44:15,280 So now re-run custom, Rob, enter, and voila. 919 00:44:15,280 --> 00:44:17,110 We now have a custom program. 920 00:44:17,110 --> 00:44:21,660 >> So I now have a program that using a few characteristics 921 00:44:21,660 --> 00:44:22,520 that are worth noting. 922 00:44:22,520 --> 00:44:27,850 One, I'm using CS50.h, otherwise known as the CS50 library. 923 00:44:27,850 --> 00:44:33,050 And inside of the CS50 library are functions that other people wrote-- 924 00:44:33,050 --> 00:44:33,980 namely the staff-- 925 00:44:33,980 --> 00:44:36,620 for you to use, and get string is one of them. 926 00:44:36,620 --> 00:44:41,310 >> Inside of line 2 is stdio.h, and why is this there? 927 00:44:41,310 --> 00:44:42,070 Take a guess. 928 00:44:42,070 --> 00:44:46,200 What exists inside of standard IO parent? 929 00:44:46,200 --> 00:44:46,940 Printf. 930 00:44:46,940 --> 00:44:49,600 So printf, you don't see it anywhere else on the screen. 931 00:44:49,600 --> 00:44:51,870 It must come with the computer somehow. 932 00:44:51,870 --> 00:44:54,870 Where does it come from? 933 00:44:54,870 --> 00:44:57,660 It's inside of-- it's declared, so to speak, inside of 934 00:44:57,660 --> 00:44:59,890 a file called stdio.h. 935 00:44:59,890 --> 00:45:02,670 >> Now, for this stuff, we'll come back to eventually, because it's a bit 936 00:45:02,670 --> 00:45:03,930 distracting for now. 937 00:45:03,930 --> 00:45:05,490 This refers to our return type. 938 00:45:05,490 --> 00:45:07,660 This refers to an argument. 939 00:45:07,660 --> 00:45:10,230 But for today, we're focused just here on these lines. 940 00:45:10,230 --> 00:45:13,280 >> So string name, just to recap, what did this do for me? 941 00:45:13,280 --> 00:45:16,940 942 00:45:16,940 --> 00:45:19,560 So it declared a variable of type string. 943 00:45:19,560 --> 00:45:22,630 So it's meant to be for words, not for numbers. 944 00:45:22,630 --> 00:45:25,530 Just as an aside, if I did want it to be a number, I would say 945 00:45:25,530 --> 00:45:26,560 something like int. 946 00:45:26,560 --> 00:45:29,740 If I wanted to be a floating point value, something with a decimal point, 947 00:45:29,740 --> 00:45:30,600 I would say float. 948 00:45:30,600 --> 00:45:32,640 But for today, I've just said string. 949 00:45:32,640 --> 00:45:35,940 >> And I've given this variable a name of name, but I could 950 00:45:35,940 --> 00:45:37,240 have called it anything. 951 00:45:37,240 --> 00:45:39,340 In fact, originally, I called it S. 952 00:45:39,340 --> 00:45:41,050 >> This here is just another statement. 953 00:45:41,050 --> 00:45:44,170 How many arguments is this statement taking? 954 00:45:44,170 --> 00:45:47,890 So it's just taking one, and that is simply an aesthetic detail of 955 00:45:47,890 --> 00:45:49,610 displaying something on the screen to me. 956 00:45:49,610 --> 00:45:54,750 What is this line 8 doing in layman's terms? 957 00:45:54,750 --> 00:45:57,320 >> It's getting a value from the user-- namely, a string-- 958 00:45:57,320 --> 00:45:58,770 and what's it doing with it? 959 00:45:58,770 --> 00:46:02,450 It's essentially handing it over to the guy on the left hand side of the 960 00:46:02,450 --> 00:46:07,100 equals sign so that in this case, the name variable can actually store it. 961 00:46:07,100 --> 00:46:10,020 >> And then printf is another instance of a function. 962 00:46:10,020 --> 00:46:11,870 And we say to call a function. 963 00:46:11,870 --> 00:46:13,870 To use a function is to call a function. 964 00:46:13,870 --> 00:46:15,640 This thing apparently takes two arguments. 965 00:46:15,640 --> 00:46:18,100 This one, comma, this one. 966 00:46:18,100 --> 00:46:20,910 >> This one itself is just a string that contains a placeholder. 967 00:46:20,910 --> 00:46:23,860 %s means put another string here. 968 00:46:23,860 --> 00:46:27,360 And so by putting name after the comma, that's inserting 969 00:46:27,360 --> 00:46:29,420 that thing for me. 970 00:46:29,420 --> 00:46:31,300 >> So I can do other things still. 971 00:46:31,300 --> 00:46:36,820 Let me go ahead now and do a bit of a silly program, but let me delete this, 972 00:46:36,820 --> 00:46:38,880 and let me create a loop. 973 00:46:38,880 --> 00:46:41,390 While true. 974 00:46:41,390 --> 00:46:45,210 The symbol true is obviously always going to be true. 975 00:46:45,210 --> 00:46:49,830 So what kind of loop am I inducing by writing a line like this? 976 00:46:49,830 --> 00:46:51,070 >> So an infinite loop, right? 977 00:46:51,070 --> 00:46:52,090 Just while true. 978 00:46:52,090 --> 00:46:56,210 There's no way I can change the word true to be anything else, so this is 979 00:46:56,210 --> 00:46:57,310 just going to run forever. 980 00:46:57,310 --> 00:46:59,960 So if you've ever actually had a program in your Mac or PC that seems 981 00:46:59,960 --> 00:47:02,480 to have locked up, or it's doing something and something, and you just 982 00:47:02,480 --> 00:47:05,500 can't get the thing to quit unless you reboot your computer or pull up the 983 00:47:05,500 --> 00:47:08,360 Task Manager or the like, here's such an example. 984 00:47:08,360 --> 00:47:14,340 >> I can say, "I am a buggy program." Close quote, close 985 00:47:14,340 --> 00:47:15,780 parenthesis, semicolon. 986 00:47:15,780 --> 00:47:17,010 So again, notice the patterns. 987 00:47:17,010 --> 00:47:21,440 Even though some of our syntax is new, the keyword while, the keyword true, 988 00:47:21,440 --> 00:47:23,590 notice I have the same kind of curly braces. 989 00:47:23,590 --> 00:47:25,890 I've got a semicolon and parentheses. 990 00:47:25,890 --> 00:47:28,000 >> So now let's go ahead and compile this. 991 00:47:28,000 --> 00:47:29,630 Make custom, custom. 992 00:47:29,630 --> 00:47:32,690 993 00:47:32,690 --> 00:47:36,660 So at this point, you do not need to restart your appliance, all right? 994 00:47:36,660 --> 00:47:39,925 So the easy lesson here is with two fingers, Control C, 995 00:47:39,925 --> 00:47:41,230 we'll quit that program. 996 00:47:41,230 --> 00:47:45,430 But you can see exactly why this thing was repeating itself again and again. 997 00:47:45,430 --> 00:47:48,080 >> I can do something a little more complex as a teaser. 998 00:47:48,080 --> 00:47:49,600 I'm going to go ahead and say the following. 999 00:47:49,600 --> 00:47:55,210 For int, which is an integer, i is just a generic name we give to most 1000 00:47:55,210 --> 00:47:59,500 variables in programming when you're just counting, equals 0. 1001 00:47:59,500 --> 00:48:08,640 Let me go ahead and say while i is, let's say, less than 100, i plus plus. 1002 00:48:08,640 --> 00:48:10,240 We'll back to this syntax, but this just means 1003 00:48:10,240 --> 00:48:12,270 increment i again and again. 1004 00:48:12,270 --> 00:48:14,200 >> And what do I want to do here? 1005 00:48:14,200 --> 00:48:21,130 Printf, "I can count to i." This is a buggy program. 1006 00:48:21,130 --> 00:48:22,450 What did I actually intend here? 1007 00:48:22,450 --> 00:48:25,220 1008 00:48:25,220 --> 00:48:30,940 >> Percent not s, but you wouldn't guess this. %d is a decimal integer. 1009 00:48:30,940 --> 00:48:32,720 Comma i. 1010 00:48:32,720 --> 00:48:34,340 Let me roll back. 1011 00:48:34,340 --> 00:48:37,170 >> Let me now recompile with make. 1012 00:48:37,170 --> 00:48:39,280 Let me now rerun with custom. 1013 00:48:39,280 --> 00:48:42,410 I can count really damn fast when I write a program like this. 1014 00:48:42,410 --> 00:48:45,480 >> Now let's do something a little unintentional. 1015 00:48:45,480 --> 00:48:51,220 How about let's do this while i is greater than or equal to 0. 1016 00:48:51,220 --> 00:48:53,630 What's this going to induce? 1017 00:48:53,630 --> 00:49:01,030 So logically, this is not so wise, because now if I rerun this, recount, 1018 00:49:01,030 --> 00:49:05,050 I can now count really, really high. 1019 00:49:05,050 --> 00:49:08,850 Unfortunately, about as high as I can count is 4 billion, so this is going 1020 00:49:08,850 --> 00:49:10,140 to take a while. 1021 00:49:10,140 --> 00:49:13,440 >> So why don't we leave this as our cliffhanger, promising that on 1022 00:49:13,440 --> 00:49:15,650 Wednesday, we'll see if this program is done. 1023 00:49:15,650 --> 00:49:19,260 Two, we'll introduce how you yourself write your own function so that very 1024 00:49:19,260 --> 00:49:22,930 quickly, by Wednesday, we'll, as we transition from Scratch into C, start 1025 00:49:22,930 --> 00:49:25,880 writing ever increasingly complex programs that do much, 1026 00:49:25,880 --> 00:49:27,070 much more than this. 1027 00:49:27,070 --> 00:49:28,766 We will see you then. 1028 00:49:28,766 --> 00:49:31,580 >> [APPLAUSE] 1029 00:49:31,580 --> 00:49:34,690 >> SPEAKER 1: At the next CS50, Puzzle Day was a success. 1030 00:49:34,690 --> 00:49:39,690