1 00:00:11,156 --> 00:00:13,346 >> All right, welcome back. 2 00:00:13,386 --> 00:00:16,216 This is CS50 and this is the end of week one. 3 00:00:16,216 --> 00:00:17,756 We are so pleased that so many 4 00:00:17,756 --> 00:00:19,786 of you have returned to us indeed. 5 00:00:19,786 --> 00:00:22,876 There are a total of 646 of you who have returned 6 00:00:22,876 --> 00:00:26,786 to us this year, roughly 246 of which are still sleeping 7 00:00:26,786 --> 00:00:28,116 or at home it seems now. 8 00:00:28,146 --> 00:00:31,276 But that's okay, everything will remain online. 9 00:00:31,476 --> 00:00:32,996 The lectures go up quite quickly 10 00:00:33,266 --> 00:00:35,416 and we will transcribe them into notes. 11 00:00:35,566 --> 00:00:38,266 The transcripts thereof and yet other resource will be 12 00:00:38,266 --> 00:00:39,616 on the course's website. 13 00:00:39,836 --> 00:00:43,086 But a couple of mentions now because this is, of course, 14 00:00:43,086 --> 00:00:44,046 the beginning of the semester 15 00:00:44,046 --> 00:00:45,506 and you certainly have the discretion 16 00:00:45,506 --> 00:00:48,056 over the next several weeks to remain with or leave us. 17 00:00:48,406 --> 00:00:51,566 Let me make a personal request that if you do decide 18 00:00:51,796 --> 00:00:54,366 that life's getting in the way or you are thinking 19 00:00:54,366 --> 00:00:56,596 to yourself you are the least comfortable person 20 00:00:56,596 --> 00:00:58,766 in the room do reach out whether during office hours, 21 00:00:58,766 --> 00:01:00,686 off to the side, or just reach out to me 22 00:01:00,686 --> 00:01:02,036 so we can have a conversation. 23 00:01:02,306 --> 00:01:04,576 We have a team of over 80 TFs and CAs, 24 00:01:04,576 --> 00:01:07,406 all of whom are very eager to make sure that every single one 25 00:01:07,406 --> 00:01:10,676 of the 646 in this room make it through to term's end, 26 00:01:10,676 --> 00:01:13,906 and you can and you will but do engage us in a conversation 27 00:01:13,906 --> 00:01:14,996 if you are feeling more 28 00:01:14,996 --> 00:01:17,026 than just a little bit stressed at any point. 29 00:01:17,496 --> 00:01:21,696 With that said, we would like to, and do take advantage too 30 00:01:21,826 --> 00:01:24,236 of the option to do pass-fail which we can do 31 00:01:24,236 --> 00:01:27,226 up until the 5th Monday, and more on that in the syllabus. 32 00:01:27,606 --> 00:01:31,956 So with that said I cordially invite all 646 to lunch, 33 00:01:32,176 --> 00:01:33,646 most any Friday during the term. 34 00:01:33,646 --> 00:01:35,226 Because it's a large course we 35 00:01:35,226 --> 00:01:38,546 like to make sure there are more opportunities to chat with me, 36 00:01:38,546 --> 00:01:41,386 with the TFs, with the CAs, and often with industry guests 37 00:01:41,386 --> 00:01:43,676 and recruiter friends of ours so that you have the chance 38 00:01:43,676 --> 00:01:45,126 to make the course feel much more intimate. 39 00:01:45,126 --> 00:01:47,996 So typically what we will do every Friday or so is open 40 00:01:47,996 --> 00:01:52,226 up an RSVP, starting next week, where we will take 20 41 00:01:52,226 --> 00:01:55,036 or 30 folks in the class out to lunch somewhere in the square 42 00:01:55,036 --> 00:01:57,656 or a house and really just have a casual conversation. 43 00:01:57,656 --> 00:02:00,056 Because we will no longer after today have lectures 44 00:02:00,056 --> 00:02:04,006 on Fridays we will try to use Fridays for that lunch slot, 45 00:02:04,006 --> 00:02:06,076 but for those for whom it's impossible we will try 46 00:02:06,076 --> 00:02:08,446 to make other opportunities available. 47 00:02:08,446 --> 00:02:11,396 So more on that next Monday when we set up the RSVPs. 48 00:02:12,076 --> 00:02:15,886 So with regard to our team of some 40 TFs and 40 CAs, 49 00:02:15,886 --> 00:02:22,296 all of whom are here to help you throughout the semester -- 50 00:02:23,446 --> 00:02:29,646 oh, very nice indeed. 51 00:02:29,646 --> 00:02:30,936 So that's our staff. 52 00:02:31,216 --> 00:02:33,726 You can take a look at it online, but you should know 53 00:02:33,726 --> 00:02:35,626 that office hours have commenced. 54 00:02:35,626 --> 00:02:38,676 Indeed, our high-tech, fancy, iPad-based approach 55 00:02:38,676 --> 00:02:41,046 to office hours did begin in [inaudible] 56 00:02:41,046 --> 00:02:43,926 and we thought we would share a few statistics. 57 00:02:43,986 --> 00:02:45,776 So, over the past few nights alone, 58 00:02:46,056 --> 00:02:49,616 though this is just term start, we had over 25 questions asked 59 00:02:49,616 --> 00:02:54,046 on Monday, almost 100 questions asked on Tuesday and Wednesday, 60 00:02:54,046 --> 00:02:56,086 and 2 questions asked last night. 61 00:02:56,086 --> 00:02:58,106 Which is great because no one is taking late days 62 00:02:58,106 --> 00:02:59,786 yet really, so that's terrific. 63 00:02:59,916 --> 00:03:03,836 But in terms of the wait times, which has been the target 64 00:03:03,926 --> 00:03:07,456 of this whole initiative, on Monday the average wait time 65 00:03:07,456 --> 00:03:11,756 between raising your hand and getting help from a TF or a CA, 66 00:03:11,756 --> 00:03:14,836 and even on Tuesday though this bar looks big...that's seconds 67 00:03:14,836 --> 00:03:17,326 on the left-hand side, so the wait time was just 68 00:03:17,326 --> 00:03:20,176 over 2 minutes, and then that decreased again on Wednesday 69 00:03:20,176 --> 00:03:21,986 when we threw more man power at it 70 00:03:22,166 --> 00:03:24,126 and our response time last night was really good, 71 00:03:24,336 --> 00:03:29,606 it was 0 seconds, so Thursdays are clearly the sweet spot. 72 00:03:29,606 --> 00:03:31,526 But in fact, quite the opposite. 73 00:03:31,526 --> 00:03:33,686 So do realize that one of the best things that you can do 74 00:03:33,686 --> 00:03:35,976 in tackling the problems sets is to start early, 75 00:03:35,976 --> 00:03:37,996 no because they are going to take all week necessarily 76 00:03:38,246 --> 00:03:39,896 but honestly so that you have that option 77 00:03:39,896 --> 00:03:41,676 if you are stressed out, you hit some barrier, 78 00:03:41,676 --> 00:03:44,096 or you hit some stupid bug and you just don't see it, 79 00:03:44,326 --> 00:03:45,986 you can call it a night and you can take a break. 80 00:03:45,986 --> 00:03:47,696 And honestly to this day any time I work 81 00:03:47,696 --> 00:03:49,616 on coding projects the moment I start 82 00:03:49,616 --> 00:03:51,936 to feel myself getting stressed you just get stupid, 83 00:03:51,936 --> 00:03:53,136 or I do at that point. 84 00:03:53,136 --> 00:03:55,266 And it is just not worth trying to force through it, 85 00:03:55,416 --> 00:03:58,206 better to take a break and come back a little more refreshed 86 00:03:58,506 --> 00:03:59,776 and a little more excited. 87 00:03:59,936 --> 00:04:01,256 So what's on the horizon? 88 00:04:01,256 --> 00:04:02,496 So sectioning has commenced. 89 00:04:02,796 --> 00:04:04,936 You will use the FAS sectioning tool, which is linked 90 00:04:04,936 --> 00:04:07,806 on the course's home page between now and Monday 91 00:04:07,806 --> 00:04:10,566 so that we don't have to rush putting you into sections 92 00:04:10,566 --> 00:04:12,396 that might conflict with your other classes. 93 00:04:12,796 --> 00:04:14,866 The sections themselves won't officially start 94 00:04:15,026 --> 00:04:17,846 for another week out so what we will have this coming Sunday, 95 00:04:18,116 --> 00:04:21,726 Monday, and Tuesday is one super section per day 96 00:04:21,726 --> 00:04:22,966 to which anyone can come. 97 00:04:22,966 --> 00:04:24,066 They are not based on track. 98 00:04:24,276 --> 00:04:27,816 It's simply an opportunity to get together with just a couple 99 00:04:27,816 --> 00:04:30,466 of TFs and generally more students this first week 100 00:04:30,736 --> 00:04:32,696 for an opportunity to discuss the problem sets 101 00:04:32,696 --> 00:04:34,206 or lecture material or the light. 102 00:04:34,466 --> 00:04:36,976 The schedule for those will be posted 103 00:04:37,276 --> 00:04:39,786 on the course's website home page. 104 00:04:40,156 --> 00:04:41,276 So what lies ahead? 105 00:04:41,306 --> 00:04:43,796 So believe it or not starting this weekend 106 00:04:43,796 --> 00:04:46,936 or this coming week, the first C-based problem set you will 107 00:04:47,066 --> 00:04:49,146 tackle will be, we hope, quite a bit of fun. 108 00:04:49,146 --> 00:04:51,596 Not only will you get this thing called the CS50 appliance 109 00:04:51,596 --> 00:04:52,716 installed on your computer, 110 00:04:53,006 --> 00:04:54,856 you will also implement a few programs. 111 00:04:54,856 --> 00:04:56,956 One, the simplest of them, 112 00:04:56,956 --> 00:04:59,186 is what is generally called Hello World. 113 00:04:59,186 --> 00:05:01,396 And we have seen that as recently as Wednesday 114 00:05:01,396 --> 00:05:03,176 where you just make a program do something silly, 115 00:05:03,176 --> 00:05:05,206 like say hi, or the like. 116 00:05:05,466 --> 00:05:07,456 But then we will wrap up to things more interesting 117 00:05:07,456 --> 00:05:09,946 and reminiscent, perhaps, of the types of problems you might want 118 00:05:09,946 --> 00:05:14,106 to have solved in scratch where you will be challenged 119 00:05:14,176 --> 00:05:18,186 to implement a simulation of sorts and that interaction 120 00:05:18,186 --> 00:05:19,836 that I had up here in the first day 121 00:05:20,056 --> 00:05:22,946 where I offered either $10 million up front 122 00:05:23,186 --> 00:05:25,866 or just one penny a day, and you will get an opportunity 123 00:05:25,866 --> 00:05:29,226 to just get comfortable with the syntax and implement 124 00:05:29,226 --> 00:05:31,156 that sort of comparison. 125 00:05:31,156 --> 00:05:32,996 You will also be challenged 126 00:05:32,996 --> 00:05:36,776 to solve what is generally called a greedy problem, 127 00:05:36,776 --> 00:05:38,086 but more on that in the spec, 128 00:05:38,086 --> 00:05:41,046 and we also drew inspiration this year and last from one 129 00:05:41,046 --> 00:05:43,126 of CS50's own students, [inaudible], 130 00:05:43,126 --> 00:05:45,286 who graduated in 2010. 131 00:05:45,286 --> 00:05:46,756 Her final project, if familiar, 132 00:05:46,996 --> 00:05:49,836 was this site called Isawyouharvard.com. 133 00:05:50,236 --> 00:05:52,816 If you click on this page here are statistics. 134 00:05:52,816 --> 00:05:55,846 You will see that she is using a free library, 135 00:05:55,846 --> 00:05:57,956 or API from Google, that allows you 136 00:05:57,956 --> 00:06:00,676 to create visualizations quite easily by plugging 137 00:06:00,676 --> 00:06:03,636 in data she has in her database and generate charts as to 138 00:06:03,636 --> 00:06:05,776 who is spotting whom or where sightings occurred. 139 00:06:05,946 --> 00:06:09,766 We will implement this and it will be a little less sexy 140 00:06:10,006 --> 00:06:13,866 in the context of the appliance using C implement charts 141 00:06:13,906 --> 00:06:14,566 like these. 142 00:06:14,566 --> 00:06:17,316 Then fast forward [inaudible] then these fairly primitive 143 00:06:17,316 --> 00:06:19,366 charts will fairly quickly evolve 144 00:06:19,406 --> 00:06:21,016 into something more sophisticated. 145 00:06:21,206 --> 00:06:23,176 Now those of you tackling the hacker edition 146 00:06:23,176 --> 00:06:25,696 of this P set will tackle some similar problems 147 00:06:25,696 --> 00:06:27,746 but there will be a twist to each of them 148 00:06:27,746 --> 00:06:29,746 where the bar will be raised and you will have to think 149 00:06:29,746 --> 00:06:32,316 of it harder and again the hacker editions assume 150 00:06:32,316 --> 00:06:34,796 that you have some prior programming experience 151 00:06:34,796 --> 00:06:37,776 or you simply want to push yourself as hard as you might, 152 00:06:38,046 --> 00:06:41,776 but again we do expect and we do encourage a super majority 153 00:06:41,776 --> 00:06:44,856 of the class to tackle the standard editions by default. 154 00:06:45,166 --> 00:06:48,296 The walkthrough now for problem set one will be this Sunday 155 00:06:48,296 --> 00:06:49,076 at 7 pm. 156 00:06:49,076 --> 00:06:50,996 We are going to exit Maxwell [inaudible] because it's hard 157 00:06:50,996 --> 00:06:54,236 to get in on weekends without swiping and so we will relocate 158 00:06:54,236 --> 00:06:56,506 to another building but the location will be posted 159 00:06:56,816 --> 00:06:58,746 on the course's home page. 160 00:06:59,016 --> 00:07:01,356 And now the last, perhaps most compelling announcement, 161 00:07:01,356 --> 00:07:04,236 is that there is an event in Saunders around 2 pm today 162 00:07:04,236 --> 00:07:05,376 so we will adjourn early. 163 00:07:05,426 --> 00:07:08,246 So that is our parting Friday gift for you. 164 00:07:09,326 --> 00:07:15,346 Thank you. 165 00:07:16,246 --> 00:07:19,496 So let's dive right in. 166 00:07:19,806 --> 00:07:22,656 So we looked Wednesday for the very first time at how 167 00:07:22,656 --> 00:07:26,426 to write a real program by using real programming language, 168 00:07:26,426 --> 00:07:28,356 and by real I just mean a little more traditional. 169 00:07:28,356 --> 00:07:30,016 A little less user friendly than scratch 170 00:07:30,316 --> 00:07:33,086 but ultimately much more powerful because with a language 171 00:07:33,086 --> 00:07:35,946 like C, and later PHP and JavaScript, 172 00:07:35,946 --> 00:07:37,046 can you command a computer 173 00:07:37,046 --> 00:07:39,996 to really do some sophisticated things without some 174 00:07:39,996 --> 00:07:43,116 of the limitations or challenges that you might have run 175 00:07:43,116 --> 00:07:45,126 into in scratch where you might have just found it hard 176 00:07:45,126 --> 00:07:46,486 to express yourself sometimes 177 00:07:46,676 --> 00:07:48,366 because a puzzle piece just didn't exist 178 00:07:48,366 --> 00:07:49,976 or you didn't know where to find it. 179 00:07:50,246 --> 00:07:53,296 So we introduced C and we introduced the CS50 appliance 180 00:07:53,296 --> 00:07:56,856 and just to kind of get things flowing, what, in sort of easy 181 00:07:56,856 --> 00:07:59,836 to understand terms, is the CS50 appliance. 182 00:08:00,116 --> 00:08:00,516 Someone? 183 00:08:01,206 --> 00:08:04,336 >> An operating system inside of a window. 184 00:08:04,566 --> 00:08:06,916 >> Good. So it's an operating system inside of a window 185 00:08:06,916 --> 00:08:11,296 on your computer so it allows us to create the illusion that all 186 00:08:11,296 --> 00:08:13,156 of you are running the same operating system. 187 00:08:13,156 --> 00:08:15,656 It happens to be called Fedora, which is a version of Linux, 188 00:08:15,916 --> 00:08:18,726 but what that means is that you will all have a uniform desktop 189 00:08:18,726 --> 00:08:20,906 open in a window on your computer no matter 190 00:08:20,906 --> 00:08:24,396 if you have a Mac or Windows or any other type of machine. 191 00:08:24,396 --> 00:08:26,516 But once you get this thing installed and the PDFs 192 00:08:26,516 --> 00:08:28,886 from problem set one, which will be posted later today 193 00:08:29,196 --> 00:08:30,506 on the course's website will walk you 194 00:08:30,506 --> 00:08:31,846 through the process of installing that. 195 00:08:32,096 --> 00:08:34,506 Once you have your environment on the computer, 196 00:08:34,746 --> 00:08:37,786 we opened up a very simple text editor last time, 197 00:08:37,786 --> 00:08:41,136 akin to notepad or text edit or even Microsoft word, 198 00:08:41,136 --> 00:08:43,086 but much simpler than that. 199 00:08:43,086 --> 00:08:46,616 So if I go to menu in the CS50 appliance and I go 200 00:08:46,616 --> 00:08:50,286 to programming and I go to G edit here or, 201 00:08:50,286 --> 00:08:52,046 notice at the bottom just like in Mac OS 202 00:08:52,046 --> 00:08:54,256 and Windows I can click an icon that is just right there, 203 00:08:54,306 --> 00:08:57,496 always at the bottom...I get this text editor like this. 204 00:08:57,496 --> 00:08:59,756 And in the top here I have a blinking prompt 205 00:08:59,756 --> 00:09:02,216 and a big white space where I can start writing code. 206 00:09:02,436 --> 00:09:05,206 And at the bottom I have what we call a terminal window, 207 00:09:05,406 --> 00:09:07,656 and this is just another program that happens 208 00:09:07,656 --> 00:09:09,656 to be embedded inside of G edit 209 00:09:09,656 --> 00:09:13,306 and it just facilitates the process of doing what? 210 00:09:14,536 --> 00:09:17,946 Running your program, and before that compiling your program. 211 00:09:17,946 --> 00:09:21,956 Because in Linux and also in Mac OS and even in Windows, 212 00:09:21,956 --> 00:09:25,466 if you have the right tools, the first programs will write 213 00:09:25,466 --> 00:09:28,316 and assume a command line environment where you don't 214 00:09:28,316 --> 00:09:30,816 so much double click icons and chose things from menus, 215 00:09:31,046 --> 00:09:33,396 rather when you want to write a program you type it's name 216 00:09:33,396 --> 00:09:33,896 at a prompt. 217 00:09:34,036 --> 00:09:36,516 That program might be GCC, which was the name 218 00:09:36,516 --> 00:09:37,576 of the compiler we used. 219 00:09:37,786 --> 00:09:40,666 That program might be called hello, which you yourself wrote, 220 00:09:40,796 --> 00:09:42,756 but ultimately you can run it in this window. 221 00:09:42,756 --> 00:09:45,026 Now as you acclimate and the problem set will mention this 222 00:09:45,026 --> 00:09:46,736 as well, but this gets very limiting. 223 00:09:46,736 --> 00:09:50,026 You have got 90% of the screen for your code and 10% 224 00:09:50,236 --> 00:09:51,296 for this terminal window. 225 00:09:51,436 --> 00:09:55,536 Well realize also under the CS50 menu you can go to menu, 226 00:09:55,616 --> 00:09:57,426 programming, and then terminal, 227 00:09:57,546 --> 00:09:59,826 and then you can actually get a full screen version 228 00:09:59,966 --> 00:10:01,286 of this very same tool. 229 00:10:01,286 --> 00:10:03,226 It's black and white instead of white and black 230 00:10:03,226 --> 00:10:06,096 but the program is exactly the same and then just like a Mac 231 00:10:06,096 --> 00:10:09,116 or a Windows computer you can just alternate with these icons 232 00:10:09,116 --> 00:10:11,756 at the bottom between whatever program you want to use. 233 00:10:11,756 --> 00:10:13,496 So just realize you have options, 234 00:10:13,496 --> 00:10:15,646 especially if you find this interface limited. 235 00:10:15,646 --> 00:10:17,176 Now later in the term we will transfer 236 00:10:17,176 --> 00:10:18,576 to more sophisticated tools 237 00:10:18,826 --> 00:10:21,236 but for now the intention very deliberately is 238 00:10:21,236 --> 00:10:24,066 to keep things simple so we can focus on the ideas and the code 239 00:10:24,246 --> 00:10:25,966 and not so much on the intricacies 240 00:10:25,966 --> 00:10:27,266 of stupid new programs. 241 00:10:27,266 --> 00:10:29,086 We will get to more sophisticated things 242 00:10:29,086 --> 00:10:29,806 before long. 243 00:10:30,106 --> 00:10:32,656 So how do you actually go about writing a program? 244 00:10:32,656 --> 00:10:35,696 Well, you open up G edit, you type in some code 245 00:10:35,696 --> 00:10:38,106 and we will come back to that in a bit, and then you save it. 246 00:10:38,106 --> 00:10:39,526 And then you save it with what? 247 00:10:39,526 --> 00:10:41,686 Easy question from file extension last time. 248 00:10:42,526 --> 00:10:44,746 So .c. So .c is just a convention 249 00:10:44,746 --> 00:10:47,046 where if you are writing a C program call it .c 250 00:10:47,186 --> 00:10:49,616 and in the world of scratch your files happen to be called .sv, 251 00:10:49,616 --> 00:10:52,526 but your operating system may have even hid 252 00:10:52,526 --> 00:10:53,816 that file extension from you. 253 00:10:53,816 --> 00:10:54,296 So that's it. 254 00:10:54,296 --> 00:10:55,246 It's just a convention. 255 00:10:55,496 --> 00:10:58,006 Then once you have a file called Hello.C, 256 00:10:58,346 --> 00:11:00,986 recall that we can compile it in a couple of ways 257 00:11:01,216 --> 00:11:02,996 but the simplest that we focused 258 00:11:02,996 --> 00:11:05,806 on ultimately is just to type 'make hello.' 259 00:11:05,996 --> 00:11:07,276 Now, make is not a compiler. 260 00:11:07,276 --> 00:11:09,226 It is just one of these automating tools 261 00:11:09,426 --> 00:11:11,006 that just makes it easy to figure 262 00:11:11,006 --> 00:11:14,236 out what compiler commands to type so you don't have to think 263 00:11:14,236 --> 00:11:17,606 about it, or type out something very long and tedious. 264 00:11:17,746 --> 00:11:20,596 So 'make hello' will look in your current folder 265 00:11:20,846 --> 00:11:22,626 for a file called Hello.C, 266 00:11:22,626 --> 00:11:24,546 and if it is there it will translate it 267 00:11:24,546 --> 00:11:25,826 into those 0s and 1s. 268 00:11:26,186 --> 00:11:29,126 So how did you proceed to run a program called hello? 269 00:11:31,106 --> 00:11:37,336 Good, .slash hello, so generally .slash 270 00:11:37,476 --> 00:11:39,846 and .this way would be forward slash 271 00:11:39,926 --> 00:11:41,166 and the other way would be backslash. 272 00:11:41,166 --> 00:11:42,196 Windows is back slashes 273 00:11:42,196 --> 00:11:44,756 and usually other OS's are forward slash, or just slash. 274 00:11:44,796 --> 00:11:48,216 But exactly, dot slash and then the name of the program, hello, 275 00:11:48,216 --> 00:11:50,706 and the dot slash is just a nuisance that specifies 276 00:11:50,846 --> 00:11:53,106 that it is in this folder because I put it there, 277 00:11:53,106 --> 00:11:54,896 it is not some program I installed 278 00:11:54,896 --> 00:11:56,666 in some special default location 279 00:11:56,666 --> 00:11:59,116 as you would typically do on your own computer. 280 00:11:59,356 --> 00:12:02,676 So that's how we might make it and that's how we might run it, 281 00:12:02,936 --> 00:12:05,636 now let's do some more interesting things. 282 00:12:05,926 --> 00:12:08,406 So to start writing programs in scratch you had 283 00:12:08,606 --> 00:12:11,766 when green flag clicked, or maybe when I received, 284 00:12:11,766 --> 00:12:14,136 if you were using event, and that block indicated 285 00:12:14,136 --> 00:12:16,116 that this is the guts of this program. 286 00:12:16,116 --> 00:12:16,656 It's a script. 287 00:12:16,996 --> 00:12:19,366 Well in C, as in a lot of programming language, 288 00:12:19,366 --> 00:12:21,686 made is the equivalent of that. 289 00:12:21,686 --> 00:12:22,986 This is the name of the function, 290 00:12:23,046 --> 00:12:25,176 the sort of miniature program you need to write 291 00:12:25,176 --> 00:12:28,746 with a simple text editor that automatically gets executed, 292 00:12:28,826 --> 00:12:32,216 gets run, whenever you type dot slash hello, 293 00:12:32,216 --> 00:12:33,726 or whatever your program is called. 294 00:12:34,036 --> 00:12:37,596 And so main is really the guts of our program, but main itself, 295 00:12:37,856 --> 00:12:40,226 though a function, can call other functions. 296 00:12:40,486 --> 00:12:43,846 Recall that on Wednesday we used several times a function called 297 00:12:43,906 --> 00:12:48,136 Print F and the F stood for...just formatting. 298 00:12:48,136 --> 00:12:51,196 So with Print F you don't just need to print 299 00:12:51,196 --> 00:12:52,726 out verbatim letters and numbers, 300 00:12:52,996 --> 00:12:55,036 you can format them in interesting ways. 301 00:12:55,036 --> 00:12:57,406 And indeed problem set one will have you leverage Print F 302 00:12:57,656 --> 00:13:00,016 to print things out like money and the like so 303 00:13:00,016 --> 00:13:01,086 that you don't have to figure out how 304 00:13:01,086 --> 00:13:03,106 to insert a decimal point in the output, 305 00:13:03,286 --> 00:13:05,016 you can just tell Print F to make this look 306 00:13:05,016 --> 00:13:08,186 like a decimal point, make it look like a piece of currency. 307 00:13:08,486 --> 00:13:12,036 So Print F takes we call them arguments or parameters, 308 00:13:12,036 --> 00:13:13,566 or more generally inputs. 309 00:13:13,676 --> 00:13:15,796 And those were things in between the parentheses 310 00:13:15,956 --> 00:13:19,136 and it could take 0 arguments or 1 argument or 2 arguments, 311 00:13:19,136 --> 00:13:20,986 you just separated them by commas. 312 00:13:21,186 --> 00:13:23,606 But by far the most common thing we saw was just 313 00:13:23,606 --> 00:13:25,416 to have a very simple function. 314 00:13:26,336 --> 00:13:29,446 They used Print F and it looks a little something like this. 315 00:13:29,446 --> 00:13:32,866 So I am going to go ahead and preemptively go to file, save, 316 00:13:32,866 --> 00:13:35,066 and I'm just going to put this on my desktop for now. 317 00:13:35,066 --> 00:13:38,046 I'm going to call this Hello.C, and hit enter. 318 00:13:38,046 --> 00:13:39,946 And again the problem set will walk you through this 319 00:13:39,996 --> 00:13:41,666 but even though it looks a little different from Windows 320 00:13:41,666 --> 00:13:43,966 and Mac OS, it's the same idea as poking 321 00:13:43,966 --> 00:13:45,516 around on your own hard drive. 322 00:13:45,516 --> 00:13:48,256 So I'm going to click save, and notice the top 323 00:13:48,256 --> 00:13:49,446 of the window has changed, 324 00:13:49,496 --> 00:13:52,086 indicating that this is indeed Hello.C. 325 00:13:52,716 --> 00:13:55,046 And silly though this is to dwell on file save, 326 00:13:55,376 --> 00:13:59,636 the advantage of this in G edit is because the files ends in .c, 327 00:13:59,896 --> 00:14:01,296 G edit is going to do something fancy 328 00:14:01,296 --> 00:14:03,336 for us called syntax highlighting, 329 00:14:03,366 --> 00:14:05,866 where it will colorize everything I type in a way 330 00:14:05,866 --> 00:14:08,036 that hopefully makes interesting things pop out, 331 00:14:08,306 --> 00:14:10,356 rather than leave my code just black and white, 332 00:14:10,356 --> 00:14:13,086 so it's not saving the colors in the file it's just showing them 333 00:14:13,086 --> 00:14:15,886 to me as I go, but recall I began this program 334 00:14:15,886 --> 00:14:19,456 with includes standard IO.H, then I had [inaudible]. 335 00:14:19,536 --> 00:14:23,446 Now notice what G edit is doing to, which is both helpful 336 00:14:23,446 --> 00:14:25,286 and annoying depending on your view. 337 00:14:25,286 --> 00:14:29,246 It has started added parenthesis for me, so I typed main 338 00:14:29,246 --> 00:14:31,926 and then did open parenthesis, it preemptively 339 00:14:31,976 --> 00:14:33,586 because it knows I'm writing C and figured 340 00:14:33,586 --> 00:14:36,126 if you open a parenthesis and you probably want to close it, 341 00:14:36,126 --> 00:14:37,086 I will do that for you. 342 00:14:37,326 --> 00:14:40,336 So realize that tools like this try to save you keystrokes 343 00:14:40,586 --> 00:14:42,526 but it can get annoying if you are not expecting it 344 00:14:42,736 --> 00:14:45,186 so just know you can disable this by going to edit 345 00:14:45,186 --> 00:14:46,826 and preferences and poking around. 346 00:14:47,186 --> 00:14:49,026 So after this I had some curly braces 347 00:14:49,026 --> 00:14:51,796 and those two were automatically inserted partly for me 348 00:14:51,976 --> 00:14:56,576 and then I just said something like oh hi world. 349 00:14:57,046 --> 00:15:02,876 So let's go ahead and now compile this code 350 00:15:03,146 --> 00:15:08,316 and we have now the following end results. 351 00:15:08,316 --> 00:15:12,906 Let me go into my desktop 352 00:15:12,906 --> 00:15:16,286 and notice what I am doing down here. 353 00:15:16,286 --> 00:15:20,016 At the [inaudible] prompt we have, by default, 354 00:15:20,496 --> 00:15:21,796 my so-called home directory. 355 00:15:21,796 --> 00:15:25,026 This is where all of my files get stored and notice 356 00:15:25,066 --> 00:15:27,336 that the thing in parentheses that just says tilde, 357 00:15:27,666 --> 00:15:29,816 this is just shorthand notation for exactly that. 358 00:15:29,816 --> 00:15:31,346 You are in your home directory. 359 00:15:31,586 --> 00:15:34,386 But I said a moment ago that I saved this file on my desktop, 360 00:15:34,656 --> 00:15:36,426 so recall from Wednesday that the command 361 00:15:36,426 --> 00:15:39,426 to change directory is quite succinctly CD. 362 00:15:39,686 --> 00:15:43,146 So by typing CD desktop and hitting enter notice 363 00:15:43,146 --> 00:15:45,676 that my prompt changes, reminding me parenthetically 364 00:15:46,236 --> 00:15:50,866 where I am, and if I now type LS for list, I will see everything 365 00:15:50,866 --> 00:15:52,326 that is on my desktop. 366 00:15:52,326 --> 00:15:55,966 And indeed if I minimize this window temporarily and go back 367 00:15:55,966 --> 00:15:59,266 to my desktop there is Hello.C. It wasn't there before 368 00:15:59,416 --> 00:16:00,936 but I created it with G edit. 369 00:16:01,076 --> 00:16:03,406 So let me maximize this window again and I will zoom in so 370 00:16:03,406 --> 00:16:04,336 that it is more legible. 371 00:16:04,336 --> 00:16:07,086 And if I now want to compile my very first program 372 00:16:07,246 --> 00:16:10,846 for the second time, I type make hello. 373 00:16:11,066 --> 00:16:13,336 Quite simple as that, so make hello, enter. 374 00:16:13,686 --> 00:16:17,246 The fact that no error messages appear, just this one line 375 00:16:17,246 --> 00:16:21,346 of output, means make indeed automated the compilation 376 00:16:21,346 --> 00:16:22,236 of my program. 377 00:16:22,496 --> 00:16:25,066 Notice that if we weren't using make we would probably have 378 00:16:25,106 --> 00:16:27,806 to type a fairly long and fairly annoying cryptic command 379 00:16:27,806 --> 00:16:31,816 like this, GCC [inaudible] and so forth, 380 00:16:31,816 --> 00:16:35,806 and we will explain what those mean, 381 00:16:35,996 --> 00:16:38,786 but for now make just simplifies this process. 382 00:16:39,046 --> 00:16:43,106 So to now run this program as we proposed before, I can do hello, 383 00:16:43,666 --> 00:16:47,766 but not quite hello because it is indeed 384 00:16:47,766 --> 00:16:48,866 in my current directory. 385 00:16:49,116 --> 00:16:51,986 So just to make this more clear, this gets frankly even 386 00:16:51,986 --> 00:16:54,176 in lecture a little annoying using such a small window. 387 00:16:54,176 --> 00:16:56,996 We can do exactly the same thing with this terminal window. 388 00:16:56,996 --> 00:17:00,296 I click terminal, I do CD desktop, I type LS, 389 00:17:00,376 --> 00:17:02,556 and I see the same file plus another one. 390 00:17:02,786 --> 00:17:05,136 What's nice about the black and white version of terminal is 391 00:17:05,136 --> 00:17:07,806 that it also colorizes files based 392 00:17:07,806 --> 00:17:11,296 on whether they are executable, or directories, or files, 393 00:17:11,596 --> 00:17:12,816 so it is a little more user friendly. 394 00:17:12,976 --> 00:17:16,376 So know that it exists and similarly I can now run hello. 395 00:17:16,596 --> 00:17:18,296 And if I sort of accidentally tried 396 00:17:18,296 --> 00:17:21,296 to recompile it then it realizes you haven't changed your code 397 00:17:21,296 --> 00:17:23,816 and it is not going to waste time regenerating the same 0s 398 00:17:23,816 --> 00:17:24,276 and 1s. 399 00:17:24,516 --> 00:17:27,206 Bam, we are now done with hello. 400 00:17:27,386 --> 00:17:30,146 So let's tease this apart and start adding new features. 401 00:17:30,146 --> 00:17:34,396 So at the very top here we have sharp include standard IO.H, 402 00:17:34,396 --> 00:17:39,196 and just in easy to understand terms what was 403 00:17:39,786 --> 00:17:47,346 that doing for us on Wednesday? 404 00:17:47,346 --> 00:17:47,413 >> [ Inaudible ] 405 00:17:47,413 --> 00:17:47,886 >> Exactly. 406 00:17:47,886 --> 00:17:50,956 That directive includes standard IO.H, specifically 407 00:17:50,956 --> 00:17:54,146 in angled braces, and it is telling the compiler, 408 00:17:54,146 --> 00:17:56,576 GCC specifically and in turn make, 409 00:17:56,876 --> 00:18:00,176 and for any functions I might call and see if they exist 410 00:18:00,176 --> 00:18:03,526 in a file somewhere on the hard drive called standard IO.H, 411 00:18:03,766 --> 00:18:07,616 because if I myself didn't write them odds are someone else did 412 00:18:07,786 --> 00:18:10,756 but I have to tell the compiler in what file they are, 413 00:18:10,756 --> 00:18:11,996 and we call them declare. 414 00:18:11,996 --> 00:18:13,916 So we have not used ourselves a .h file, 415 00:18:13,916 --> 00:18:15,516 we will start using those before long, 416 00:18:15,726 --> 00:18:20,086 but for now a .h file just contains the names of functions 417 00:18:20,176 --> 00:18:22,846 that other people have written that you yourself can use. 418 00:18:23,116 --> 00:18:25,276 Now fast forward down here and there is this mention 419 00:18:25,276 --> 00:18:27,916 of [inaudible] and I mentioned briefly on Wednesday the notion 420 00:18:27,916 --> 00:18:30,486 of error codes and the like, so for now just trust 421 00:18:30,586 --> 00:18:33,486 that every program returns a number and the best number 422 00:18:33,486 --> 00:18:36,406 to return is 0 and anything else is usually a problem, 423 00:18:36,586 --> 00:18:39,716 so for now we are just going to take on faith it main, 424 00:18:39,786 --> 00:18:42,726 and then in parenthesis void, and this is a new word, 425 00:18:42,726 --> 00:18:44,666 but take a guess what it means if you say void 426 00:18:44,666 --> 00:18:45,736 in between parenthesis. 427 00:18:47,206 --> 00:18:48,826 Just nothing, it takes no arguments. 428 00:18:48,826 --> 00:18:51,136 It takes no inputs and that is kind of sensible here 429 00:18:51,136 --> 00:18:53,136 because all this program does, quite foolishly, 430 00:18:53,136 --> 00:18:54,686 is just say oh hi world. 431 00:18:54,686 --> 00:18:55,386 Oh hi world. 432 00:18:55,386 --> 00:18:56,726 There is no notion of input 433 00:18:56,976 --> 00:19:00,646 so I can explicitly tell GCC don't even expect any input 434 00:19:00,646 --> 00:19:01,456 into this program. 435 00:19:01,456 --> 00:19:02,856 Just do the same thing again and again. 436 00:19:03,366 --> 00:19:05,306 So now the curly braces are just a convention. 437 00:19:05,526 --> 00:19:07,666 Print F is the name of a function that happens 438 00:19:07,666 --> 00:19:08,896 to exist in that file. 439 00:19:09,076 --> 00:19:09,776 How do you know? 440 00:19:10,036 --> 00:19:11,906 Well, you kind of learn these things in lecture 441 00:19:11,906 --> 00:19:13,686 or you read them in documentation 442 00:19:13,686 --> 00:19:15,786 and you just know what files to include. 443 00:19:16,116 --> 00:19:18,546 Now between quotes we have what we call an argument 444 00:19:18,736 --> 00:19:21,346 or a parameter and you just have to worry about the minutia 445 00:19:21,346 --> 00:19:24,196 of things like the closed parenthesis, the semicolon, 446 00:19:24,316 --> 00:19:25,826 and more interestingly this thing, 447 00:19:26,096 --> 00:19:27,926 and what did backslash n represent? 448 00:19:28,886 --> 00:19:29,476 Okay, good. 449 00:19:29,606 --> 00:19:33,156 We will move quickly then, so new line. 450 00:19:33,266 --> 00:19:36,516 And that is telling the computer explicitly to put a line break 451 00:19:36,516 --> 00:19:38,856 after these words because otherwise it is not going 452 00:19:38,856 --> 00:19:39,256 to appear. 453 00:19:39,526 --> 00:19:40,906 All right, well we can do things 454 00:19:40,906 --> 00:19:42,496 that are a little more interesting now, 455 00:19:42,526 --> 00:19:43,886 like not just say oh hi world. 456 00:19:43,886 --> 00:19:47,606 Let's say oh hi David, or some arbitrary name that I type in. 457 00:19:47,606 --> 00:19:49,786 We need the ability now to get user input 458 00:19:50,086 --> 00:19:53,526 so we can actually do this with the following syntax. 459 00:19:53,886 --> 00:19:55,826 A word, recall from Wednesday, 460 00:19:55,896 --> 00:19:57,966 is generally called a string in programming. 461 00:19:57,966 --> 00:19:59,416 A word or a sentence, any sequence 462 00:19:59,416 --> 00:20:00,936 of characters, is a string. 463 00:20:01,216 --> 00:20:04,166 I'm going to call it S, only because I only have one string. 464 00:20:04,166 --> 00:20:05,216 I know it's S and I don't need 465 00:20:05,216 --> 00:20:06,776 to give it something more descriptive. 466 00:20:06,936 --> 00:20:09,176 Then it turns out there is a function I can use called 467 00:20:09,246 --> 00:20:09,906 get string. 468 00:20:10,226 --> 00:20:13,936 It too takes no arguments, but just to distinguish the two 469 00:20:13,936 --> 00:20:15,066 when you call the function 470 00:20:15,186 --> 00:20:17,516 that takes no arguments you don't have to say void. 471 00:20:17,836 --> 00:20:19,266 You only say void in something 472 00:20:19,266 --> 00:20:21,786 like if you are saying preemptively don't pass 473 00:20:21,786 --> 00:20:22,316 me anything. 474 00:20:22,786 --> 00:20:24,306 So we will tease these apart before long, 475 00:20:24,306 --> 00:20:25,766 but for now, get string. 476 00:20:25,766 --> 00:20:29,296 Open string and close string is going to do exactly that. 477 00:20:29,296 --> 00:20:31,656 It is going to get a string from the keyboard and it is going 478 00:20:31,656 --> 00:20:35,776 to hand it to me in a variable called S. But unfortunately I am 479 00:20:35,776 --> 00:20:37,106 about to run into a problem. 480 00:20:37,106 --> 00:20:39,686 Let me go down here and I am going to change 481 00:20:39,686 --> 00:20:42,676 that to percent S and then I am going to put a comma S 482 00:20:43,226 --> 00:20:43,936 because if you recall 483 00:20:43,936 --> 00:20:47,376 from Wednesday briefly this is how I dynamically inserted the 484 00:20:47,376 --> 00:20:49,646 value of a variable into a string 485 00:20:49,946 --> 00:20:51,906 that I don't necessarily know in advance. 486 00:20:52,056 --> 00:20:55,306 I know I want it to say oh hi, something, 487 00:20:55,626 --> 00:20:58,876 but to dynamically insert that something you use a format code. 488 00:20:58,916 --> 00:21:01,226 A format code is just something that starts with percent, 489 00:21:01,486 --> 00:21:03,846 percent S means string, put a string here. 490 00:21:04,166 --> 00:21:07,656 So comma S means take that value, whether it is David 491 00:21:07,656 --> 00:21:08,846 or whatever the user may type, 492 00:21:09,176 --> 00:21:12,066 and put it inside of the string itself. 493 00:21:12,606 --> 00:21:14,846 So unfortunately this isn't quite perfect. 494 00:21:14,846 --> 00:21:16,336 Let me go down to my terminal window, 495 00:21:16,666 --> 00:21:17,896 this is still called hello. 496 00:21:17,896 --> 00:21:19,866 I am going to go ahead and make hello, 497 00:21:19,966 --> 00:21:21,766 and wow, lots of issues here. 498 00:21:22,166 --> 00:21:24,936 So when in doubt, and honestly, if a whole lot 499 00:21:24,936 --> 00:21:27,196 of errors scroll it doesn't mean you made an infinite number 500 00:21:27,196 --> 00:21:27,666 of errors. 501 00:21:27,666 --> 00:21:28,896 It might mean you made one 502 00:21:29,036 --> 00:21:30,706 and confused the heck out of the compiler. 503 00:21:30,916 --> 00:21:33,576 So never start fixing errors from the bottom up, 504 00:21:33,576 --> 00:21:36,856 always scroll back up and try to fix the first one first. 505 00:21:37,186 --> 00:21:42,066 And there is the first one, error: unknown type name string. 506 00:21:42,636 --> 00:21:45,326 Well it turns out we are kind of lying to you here. 507 00:21:45,326 --> 00:21:49,136 It turns out in C there is no data type called string. 508 00:21:49,136 --> 00:21:52,036 There is it, there is float, there is char 509 00:21:52,116 --> 00:21:54,946 and a few others we mentioned before, but string is kind 510 00:21:54,946 --> 00:21:57,716 of a CS50 simplification just for the first week or two. 511 00:21:58,046 --> 00:22:00,956 So if you actually want to use this data type you actually need 512 00:22:00,956 --> 00:22:04,226 to use another library, so I am just going 513 00:22:04,226 --> 00:22:06,506 to do some quick copy paste because it turns 514 00:22:06,506 --> 00:22:10,356 out in the appliance is a file called CS50.h. And in 515 00:22:10,356 --> 00:22:13,296 that so called header file is not Print F this time, 516 00:22:13,506 --> 00:22:16,136 but functions like get string and get it, 517 00:22:16,366 --> 00:22:20,026 and some other user friendly functions that just make it easy 518 00:22:20,026 --> 00:22:23,276 in these first weeks to do simple, reasonable things. 519 00:22:23,596 --> 00:22:25,156 So now I am going to save this file. 520 00:22:25,196 --> 00:22:28,036 I am going to go back to my terminal window and I am going 521 00:22:28,036 --> 00:22:30,746 to go ahead and rerun make hello, enter, 522 00:22:31,316 --> 00:22:32,736 and now all is better. 523 00:22:32,736 --> 00:22:34,016 I still see this cryptic command 524 00:22:34,016 --> 00:22:36,686 but I don't see the word error so now is better. 525 00:22:36,876 --> 00:22:40,236 So let me go ahead and run dot slash hello, and nothing seems 526 00:22:40,236 --> 00:22:41,286 to be happening but that is 527 00:22:41,286 --> 00:22:42,836 because it is waiting for some input. 528 00:22:42,836 --> 00:22:44,716 So let me try typing my name, enter, 529 00:22:44,756 --> 00:22:47,016 and now it says oh hi David. 530 00:22:47,016 --> 00:22:49,316 Well we can make this marginally more user friendly. 531 00:22:49,316 --> 00:22:51,976 Let me scroll to the top, and rather than just kind 532 00:22:51,976 --> 00:22:53,946 of blindly getting a string from the user, 533 00:22:54,116 --> 00:23:00,926 let me first say Print F, say your name, colon, space, 534 00:23:00,926 --> 00:23:03,896 semicolon, just to kind of format it in an interesting way. 535 00:23:04,276 --> 00:23:05,846 Now let me go back to the bottom here. 536 00:23:05,846 --> 00:23:11,526 Let me rerun hello and hit enter, and bug. 537 00:23:12,816 --> 00:23:15,596 Man, not really a bug, just a stupid mistake on my part 538 00:23:15,596 --> 00:23:17,986 because I didn't recompile the program. 539 00:23:18,126 --> 00:23:19,836 And as an aside if you kind of get stuck 540 00:23:19,836 --> 00:23:21,986 in those awkward moments where your program is running 541 00:23:21,986 --> 00:23:23,086 and you didn't want it to run, 542 00:23:23,266 --> 00:23:26,566 almost always control C is your friend and that will quit 543 00:23:26,566 --> 00:23:29,246 out of whatever program you are running at the command line. 544 00:23:29,246 --> 00:23:31,426 Control C and it just looks like carrot C 545 00:23:31,766 --> 00:23:32,776 when it actually appears. 546 00:23:32,946 --> 00:23:34,746 So let me rerun make hello. 547 00:23:35,076 --> 00:23:35,876 That looks good. 548 00:23:35,876 --> 00:23:38,426 Let me now rerun hello, enter, 549 00:23:38,426 --> 00:23:41,026 and now it just looks a little prettier. 550 00:23:41,026 --> 00:23:42,936 It's still underwhelming, the end result, 551 00:23:42,936 --> 00:23:46,366 but at least now it is becoming a little more user friendly. 552 00:23:46,626 --> 00:23:47,646 Yeah? 553 00:23:47,646 --> 00:23:48,466 >> [ Inaudible ] 554 00:23:48,466 --> 00:23:59,236 >> So to be clear for all pieces that you should start using, 555 00:24:00,246 --> 00:24:03,056 in general you could download something like the CS50 556 00:24:03,146 --> 00:24:06,226 or the CS50 library on your own computer 557 00:24:06,226 --> 00:24:07,986 if you were programming outside of the appliance, 558 00:24:08,236 --> 00:24:10,536 or more generally there are other functions altogether 559 00:24:10,536 --> 00:24:12,196 called Scan F, or S Scan F, 560 00:24:12,446 --> 00:24:13,846 which are generally used instead. 561 00:24:13,846 --> 00:24:15,246 But more on those in the future, 562 00:24:15,246 --> 00:24:18,446 for now the CS50 library simplifies these details. 563 00:24:19,406 --> 00:24:20,196 Any other questions? 564 00:24:20,196 --> 00:24:21,686 >> [ Inaudible ] 565 00:24:21,686 --> 00:24:24,226 >> An excellent point. 566 00:24:25,196 --> 00:24:43,826 So I seem to have contradicted myself whereby I said main does 567 00:24:43,826 --> 00:24:47,366 not take any input and yet here we are clearly taking input 568 00:24:47,366 --> 00:24:49,866 from the user, so long story short for today, 569 00:24:49,866 --> 00:24:51,206 there are two types of input. 570 00:24:51,546 --> 00:24:54,896 When you run a program at the so called terminal window, 571 00:24:55,176 --> 00:24:57,926 recall that we are typing things like make hello. 572 00:24:58,106 --> 00:25:00,406 Well, the program in this case is called make 573 00:25:00,406 --> 00:25:03,876 and I am passing it input the name of the program I want it 574 00:25:03,926 --> 00:25:05,596 to make, so this is what we will call 575 00:25:05,596 --> 00:25:07,766 in a future week a command line argument. 576 00:25:07,766 --> 00:25:09,846 It's input you provide when the program is run 577 00:25:10,006 --> 00:25:11,696 and you can't really do this in a GUI, 578 00:25:11,696 --> 00:25:12,966 a graphical user interface, 579 00:25:12,996 --> 00:25:14,366 because you either double click an icon 580 00:25:14,366 --> 00:25:15,906 to run something or you don't. 581 00:25:16,086 --> 00:25:18,466 And in command line you can type words after the name 582 00:25:18,466 --> 00:25:20,416 of the program to provide it with input 583 00:25:20,756 --> 00:25:23,156 but notice what I've been doing is just been saying hello, 584 00:25:23,286 --> 00:25:26,996 so in this sense does hello not take any command line input? 585 00:25:27,326 --> 00:25:28,886 It does take input later 586 00:25:29,146 --> 00:25:32,186 but it takes it via a different channel, a different mechanism. 587 00:25:32,376 --> 00:25:34,846 And that mechanism in this case is called get string. 588 00:25:35,226 --> 00:25:37,486 So we will come back to command line arguments before long 589 00:25:37,486 --> 00:25:39,906 but for now our programs will remain relatively simple 590 00:25:40,096 --> 00:25:43,286 so that we only get input inside of the program itself 591 00:25:43,576 --> 00:25:48,756 with functions like get string and the like. 592 00:25:51,686 --> 00:25:56,346 Any other questions? 593 00:25:56,811 --> 00:25:58,811 >> [ Inaudible ] 594 00:25:59,276 --> 00:26:02,276 >> A good question. 595 00:26:02,276 --> 00:26:03,746 A word on spacing. 596 00:26:03,746 --> 00:26:06,356 So what I have done now is what we will call a matter 597 00:26:06,356 --> 00:26:07,346 of good style. 598 00:26:07,346 --> 00:26:09,056 The fact that I hit the space bar 599 00:26:09,556 --> 00:26:12,106 after this comma before the S is not strictly necessary 600 00:26:12,396 --> 00:26:14,436 but it makes ultimately the code more readable. 601 00:26:14,436 --> 00:26:17,036 Not necessarily if your program is only 5 lines long, 602 00:26:17,186 --> 00:26:19,356 but when your programs are, as they will be before long, 603 00:26:19,356 --> 00:26:22,416 100 lines long or even more it just makes it easier 604 00:26:22,416 --> 00:26:25,246 for the human, you, to read it or even colleagues of yours 605 00:26:25,246 --> 00:26:27,006 with whom you are working. 606 00:26:27,306 --> 00:26:29,146 Similarly here is this a convention. 607 00:26:29,146 --> 00:26:31,716 I am indenting just as I did with Rob 608 00:26:31,716 --> 00:26:33,556 on the stage when we did PBJ. 609 00:26:33,556 --> 00:26:35,926 He invented the pseudo code as we typed. 610 00:26:36,276 --> 00:26:39,316 Similarly here am I hitting tab or really 4 spaces 611 00:26:39,316 --> 00:26:42,866 to actually indent my code to just make it more readable 612 00:26:42,866 --> 00:26:45,826 and more clear what the control flow, so to speak, is. 613 00:26:46,076 --> 00:26:47,946 So you will see in problem set one that we ask you 614 00:26:47,946 --> 00:26:50,396 to read a short style guide written by CS50 615 00:26:50,446 --> 00:26:53,156 that essentially gives you some quick dos and don'ts 616 00:26:53,156 --> 00:26:56,486 as you figure out exactly what kind of style you yourself 617 00:26:56,486 --> 00:27:02,206 like with which to write code. 618 00:27:04,256 --> 00:27:04,656 Yeah? 619 00:27:04,731 --> 00:27:06,731 >> [ Inaudible ] 620 00:27:06,806 --> 00:27:10,526 >> A good point. 621 00:27:10,566 --> 00:27:13,036 So you may see if you read ahead and if you look 622 00:27:13,036 --> 00:27:15,706 at other examples online that a lot of time people, 623 00:27:15,706 --> 00:27:17,976 myself included, will write at the bottom 624 00:27:17,976 --> 00:27:20,876 of main a statement like return zero. 625 00:27:21,136 --> 00:27:24,586 Thereby, be ever so explicit what the exit code is 626 00:27:24,586 --> 00:27:25,396 for this program. 627 00:27:25,396 --> 00:27:28,086 I am actually leaving that off now as a matter 628 00:27:28,316 --> 00:27:29,596 of pedagogical simplicity. 629 00:27:29,596 --> 00:27:31,286 It is actually not strictly required. 630 00:27:31,456 --> 00:27:34,596 If you leave it out zero is automatically returned for you. 631 00:27:34,596 --> 00:27:38,596 So yeah, it's correct, but just a distraction so I tend 632 00:27:38,596 --> 00:27:40,696 to leave it out until our programs get longer. 633 00:27:41,356 --> 00:27:44,236 Let's come back in just a moment. 634 00:27:44,236 --> 00:27:46,576 What else is there besides just get string? 635 00:27:46,576 --> 00:27:48,506 Well in the CS50 library there are a whole bunch 636 00:27:48,506 --> 00:27:49,436 of useful functions. 637 00:27:49,636 --> 00:27:52,286 Capitalization does matter with C, 638 00:27:52,546 --> 00:27:54,616 so the fact that we have capitalized get char, 639 00:27:54,616 --> 00:27:59,376 get double, [inaudible] and get string is deliberate so that 640 00:27:59,376 --> 00:28:00,956 because they are all capitalized 641 00:28:00,956 --> 00:28:02,996 in this way it just makes it more clear to you 642 00:28:02,996 --> 00:28:05,266 in these first weeks that this is a CS50 function 643 00:28:05,496 --> 00:28:07,416 and not something built in to C. 644 00:28:07,416 --> 00:28:09,456 Because eventually we will take these training wheels off 645 00:28:09,456 --> 00:28:11,726 when you can get user input in other ways, 646 00:28:11,726 --> 00:28:14,066 including from the web, but for now these are all 647 00:28:14,246 --> 00:28:15,296 CS50 functions. 648 00:28:15,296 --> 00:28:16,686 So what's the difference between these? 649 00:28:17,016 --> 00:28:19,696 So get char gets a char. 650 00:28:19,696 --> 00:28:20,806 A char? What is a char? 651 00:28:20,806 --> 00:28:21,376 A character. 652 00:28:21,456 --> 00:28:25,706 It's just a single letter or a single number 653 00:28:25,706 --> 00:28:26,876 that was typed at the prompt. 654 00:28:26,876 --> 00:28:28,126 And how was that represented? 655 00:28:28,126 --> 00:28:30,116 Well, with 8 bits or 1 byte, 656 00:28:30,116 --> 00:28:32,906 and recall when we had our human volunteers the other day they 657 00:28:32,906 --> 00:28:35,356 represented a byte worth of bits 658 00:28:35,356 --> 00:28:39,416 and they were representing ask characters, so long story short 659 00:28:39,416 --> 00:28:41,706 if you want to ask the user just for the letter B 660 00:28:41,776 --> 00:28:45,466 and then the letter O and then the letter W, you can ask him 661 00:28:45,466 --> 00:28:48,396 or her for 3 chars one after the other and each 662 00:28:48,396 --> 00:28:50,496 of those chars will be put by the computer 663 00:28:50,636 --> 00:28:54,936 into a different byte, 8 bits, in order to store that value. 664 00:28:54,936 --> 00:28:56,716 But we will see examples of that before long. 665 00:28:56,946 --> 00:28:57,886 Now there's get float. 666 00:28:57,886 --> 00:28:59,286 What's a float? 667 00:29:00,326 --> 00:29:03,606 It's a floating point number or a real number. 668 00:29:03,606 --> 00:29:05,306 It is something that has got a decimal point in it 669 00:29:05,446 --> 00:29:08,466 and it is 32 bits typically, but this is somewhat dependant 670 00:29:08,466 --> 00:29:09,646 on the machine that you are using. 671 00:29:09,896 --> 00:29:13,116 And sometimes just 32 bits is too small so we instead want 672 00:29:13,116 --> 00:29:15,166 to double it, so there is a double. 673 00:29:15,166 --> 00:29:18,016 So the get double function, meanwhile, gets you a double, 674 00:29:18,016 --> 00:29:21,346 but it uses 64 bits, which just gives you more precision. 675 00:29:21,406 --> 00:29:24,006 One of the goals of showing you the video on Wednesday 676 00:29:24,006 --> 00:29:27,356 of the various missile mistakes was to demonstrate 677 00:29:27,356 --> 00:29:29,976 that actually taking into account or failing to take 678 00:29:29,976 --> 00:29:33,246 into account how many bits, how many 0s and 1s you were using 679 00:29:33,246 --> 00:29:34,206 to store your values, 680 00:29:34,426 --> 00:29:36,766 can absolutely have significant effects. 681 00:29:36,886 --> 00:29:38,856 And we saw this again 10 years ago in Y2K. 682 00:29:39,066 --> 00:29:41,756 It wasn't quite an issue of bits per say, it was a matter 683 00:29:41,756 --> 00:29:43,856 of digits then, but the idea was the same 684 00:29:43,856 --> 00:29:46,896 if you only use 2 digits you are going to run into a problem 685 00:29:46,896 --> 00:29:48,526 when the century rolls over 686 00:29:48,636 --> 00:29:51,936 and you now need a third digit potentially to represent that, 687 00:29:52,336 --> 00:29:54,956 as was the case between 1999 and 2000. 688 00:29:55,256 --> 00:29:57,436 So what else is there in the way of type? 689 00:29:57,476 --> 00:30:00,016 So there is char, there's double, there's float, 690 00:30:00,136 --> 00:30:00,756 there's INT [assumed spelling] 691 00:30:00,756 --> 00:30:03,076 and there's the goofily named long-long 692 00:30:03,356 --> 00:30:05,596 and we'll revisit these before long in the problems 693 00:30:05,596 --> 00:30:07,756 that will introduce you in more detail to some of these. 694 00:30:07,756 --> 00:30:10,446 But again a char is one byte or eight bits. 695 00:30:10,856 --> 00:30:13,976 A double is sixty-four bits or eight bytes 696 00:30:14,086 --> 00:30:15,106 if you just divide by eight. 697 00:30:15,446 --> 00:30:17,166 A float is half that, thirty-two bits. 698 00:30:17,166 --> 00:30:20,266 An INT is also 32 bits and a long-long, 699 00:30:20,266 --> 00:30:24,226 just to round this out, is generally 64 bits. 700 00:30:24,466 --> 00:30:27,156 And so part of the problem set will actually have you playing 701 00:30:27,156 --> 00:30:29,396 with money, the notion of ten million dollars 702 00:30:29,396 --> 00:30:32,286 and potentially more for which you know you probably don't want 703 00:30:32,286 --> 00:30:34,786 to run out of bits if the downside is you don't get 704 00:30:34,786 --> 00:30:36,256 as much money as you're due. 705 00:30:36,516 --> 00:30:39,206 So you'll see in this case PSet1 706 00:30:39,496 --> 00:30:41,976 that these data types become applicable. 707 00:30:42,776 --> 00:30:45,006 So one other detail here, 708 00:30:45,006 --> 00:30:48,306 so in the CS50 library there's not only the notion of a string, 709 00:30:48,596 --> 00:30:50,796 which we'll tease apart before long, that just represents 710 00:30:50,796 --> 00:30:52,916 and sequence of characters, multiple chars, 711 00:30:53,246 --> 00:30:54,986 there's also what's called a bull. 712 00:30:55,076 --> 00:30:58,096 And we actually had bulls or the notion thereof in Scratch. 713 00:30:58,246 --> 00:31:01,166 It turns out in C there isn't a bully and data type true 714 00:31:01,166 --> 00:31:03,696 or false so we've created one in the library 715 00:31:03,866 --> 00:31:06,856 so that you yourselves can represent the idea of true 716 00:31:06,986 --> 00:31:09,406 or false and we'll see some examples thereof. 717 00:31:09,406 --> 00:31:11,206 And then there's simple stuff, to be honest. 718 00:31:11,286 --> 00:31:14,216 Computers at the end of the day are indeed calculators 719 00:31:14,786 --> 00:31:18,046 and so there are mathematical operators built 720 00:31:18,046 --> 00:31:21,416 into C. You can do plus and minus and divide and subtract 721 00:31:21,416 --> 00:31:23,876 and so forth and just know, as an aside, 722 00:31:24,106 --> 00:31:25,486 that just like in grade school 723 00:31:25,486 --> 00:31:29,556 where there was a well defined definition of the order 724 00:31:29,556 --> 00:31:33,076 of operations - multiplication comes before plus and so forth - 725 00:31:33,346 --> 00:31:35,816 most of those same grade school rules still apply. 726 00:31:35,816 --> 00:31:37,616 And this chart is not meant to overwhelm but just 727 00:31:37,616 --> 00:31:40,086 to be a reference down the road if you're ever wondering 728 00:31:40,156 --> 00:31:42,256 if does this symbol get executed 729 00:31:42,366 --> 00:31:44,996 by the computer before this other one know that charts 730 00:31:45,036 --> 00:31:47,536 like this exist and you can refer back to the lecture slide 731 00:31:47,536 --> 00:31:48,996 when the problem arises. 732 00:31:49,026 --> 00:31:50,826 But the simplest rules of thumb still apply. 733 00:31:50,826 --> 00:31:54,476 Multiplication binds with higher priority than plus 734 00:31:54,476 --> 00:31:58,146 so realize you might sometimes in code need some parenthesis. 735 00:31:58,496 --> 00:31:59,856 Now let's finish up print death. 736 00:31:59,956 --> 00:32:02,746 So print death we've used percent S thus far 737 00:32:02,996 --> 00:32:04,336 but there's other format codes. 738 00:32:04,396 --> 00:32:06,796 If you want to spit out a character, 739 00:32:07,056 --> 00:32:09,396 you don't say percent S you say percent C 740 00:32:09,396 --> 00:32:10,796 if it's just a single character. 741 00:32:11,076 --> 00:32:14,826 If you want to print out a decimal digit you say percent D, 742 00:32:14,956 --> 00:32:16,506 just a human friendly number. 743 00:32:16,716 --> 00:32:19,466 If you want to print out a floating print value you print 744 00:32:19,466 --> 00:32:22,246 out percent F, whether it's a float or a double, 745 00:32:22,336 --> 00:32:24,476 and this just means that print F will take care 746 00:32:24,476 --> 00:32:27,416 of putting the decimal point aesthetically wherever 747 00:32:27,416 --> 00:32:28,186 it belongs. 748 00:32:28,186 --> 00:32:30,926 And as we saw the other day it will also decide for you 749 00:32:30,926 --> 00:32:33,926 if you don't specify how many decimal or how many numbers 750 00:32:33,926 --> 00:32:35,956 to show after the decimal point. 751 00:32:36,256 --> 00:32:37,926 And then lastly, and just as an aside, 752 00:32:38,506 --> 00:32:42,656 there's also this crazy one LLD, long-long-decimal. 753 00:32:42,906 --> 00:32:45,256 So this is actually important because if 754 00:32:45,256 --> 00:32:48,406 over the next few weeks you use for instance a long-long 755 00:32:48,406 --> 00:32:50,966 because you really want lots of bits to store a big number 756 00:32:51,256 --> 00:32:53,976 but you try to print that number using percent D it's going 757 00:32:54,016 --> 00:32:55,416 to look completely wrong. 758 00:32:55,416 --> 00:32:57,886 It's going to be negative, it's going to be smaller and that's 759 00:32:57,926 --> 00:33:00,306 because print F is going to be misinterpreting the bits 760 00:33:00,306 --> 00:33:02,296 and essentially only looking at thirty-two. 761 00:33:02,636 --> 00:33:03,736 So if you want to tell something 762 00:33:03,736 --> 00:33:07,086 like print F I'm using sixty-four bits just realize 763 00:33:07,086 --> 00:33:11,166 and refer back to things like these other format codes. 764 00:33:12,066 --> 00:33:13,636 So those are just some building blocks. 765 00:33:13,636 --> 00:33:15,986 Let's actually do something with them. 766 00:33:15,986 --> 00:33:18,836 So just as in Scratch we had conditions so do we have them 767 00:33:18,836 --> 00:33:21,196 in C and we saw a glimpse of this on Wednesday 768 00:33:21,446 --> 00:33:23,586 and so the next several examples are really meant 769 00:33:23,586 --> 00:33:26,726 to just translate what was more user friendly and puzzle piece 770 00:33:26,726 --> 00:33:29,006 like to something more deliberate, 771 00:33:29,006 --> 00:33:32,356 something more cryptic and will be increasingly familiar. 772 00:33:32,636 --> 00:33:34,986 If you want to express the notion of a condition, 773 00:33:35,086 --> 00:33:39,066 if this is true do this, well the syntax in C is to say if -- 774 00:33:39,416 --> 00:33:42,256 space -- open parenthesis -- and then something -- 775 00:33:42,256 --> 00:33:43,806 we'll fill in the something in a moment -- 776 00:33:43,806 --> 00:33:46,986 and then open curly brace and then do this. 777 00:33:47,556 --> 00:33:50,936 Now in C this slash slash actually represents 778 00:33:50,936 --> 00:33:51,666 something special. 779 00:33:51,666 --> 00:33:53,426 For those already familiar it's a ... 780 00:33:53,586 --> 00:33:54,086 >> Comment 781 00:33:54,086 --> 00:33:56,696 >> It's a comment so a comment just means this is not actually 782 00:33:56,696 --> 00:33:58,006 a code, it's just English 783 00:33:58,006 --> 00:34:00,226 or whatever language I personally speak that's -- 784 00:34:00,356 --> 00:34:03,056 that's a notation to myself of what's actually going on. 785 00:34:03,056 --> 00:34:04,706 You might not have noticed these in Scratch 786 00:34:04,706 --> 00:34:05,686 but if you right clicked 787 00:34:05,866 --> 00:34:09,766 on the scripts area you could actually paste 788 00:34:09,766 --> 00:34:11,966 in a little yellow sticky, like a Post-It note, 789 00:34:12,176 --> 00:34:13,936 and that was the equivalent in Scratch 790 00:34:13,936 --> 00:34:15,756 of just notes, comments to yourself. 791 00:34:16,186 --> 00:34:17,176 C also has this. 792 00:34:17,646 --> 00:34:20,206 C also has another syntax that you might see in books 793 00:34:20,206 --> 00:34:25,346 and my own examples whereby you can do slash-star-star-slash. 794 00:34:25,606 --> 00:34:29,856 The advantage here is that this syntax can actually span lines 795 00:34:29,856 --> 00:34:31,506 and you can say something like 'and this' 796 00:34:31,866 --> 00:34:35,406 and that's all comment, whereas the slash-slash only applies 797 00:34:35,406 --> 00:34:36,046 to one line. 798 00:34:36,046 --> 00:34:38,646 So just realize they're two different styles and it doesn't 799 00:34:38,646 --> 00:34:40,546 so much matter which one you use so long 800 00:34:40,546 --> 00:34:42,196 as you're ultimately consistent. 801 00:34:42,786 --> 00:34:45,266 Now this only lets you do something or not at all. 802 00:34:45,556 --> 00:34:46,386 Can we do more? 803 00:34:46,786 --> 00:34:48,356 >> What about pound or sharp? 804 00:34:48,356 --> 00:34:52,626 >> Pound or sharp do not work in C. So the only two options 805 00:34:52,626 --> 00:34:55,176 in C are slash-slash and slash-star. 806 00:34:55,466 --> 00:34:57,896 So what if we want two options, two forks in the road? 807 00:34:58,016 --> 00:35:01,816 We can do if this is true do this else do that and just 808 00:35:01,816 --> 00:35:04,386 like in Scratch if we start embedding these things we can 809 00:35:04,386 --> 00:35:05,746 have three forks in the road. 810 00:35:05,746 --> 00:35:09,436 We can say if this do this, else if this condition do that, 811 00:35:09,656 --> 00:35:11,446 else do this other thing. 812 00:35:11,446 --> 00:35:14,426 So again, same ideas in Scratch, we just need to express them now 813 00:35:14,426 --> 00:35:16,406 with a keyboard but the ideas are the same. 814 00:35:16,406 --> 00:35:16,506 Yeah? 815 00:35:16,736 --> 00:35:20,356 >> Is there a way to do automatic formatting in C? 816 00:35:20,356 --> 00:35:22,096 >> Is there a way to do automatic formatting? 817 00:35:22,706 --> 00:35:28,126 >> Yeah, like start [inaudible] 818 00:35:28,686 --> 00:35:29,406 >> Good question. 819 00:35:29,406 --> 00:35:31,696 So those of you with prior backgrounds might have used 820 00:35:31,696 --> 00:35:35,486 tools that actually kind of fix any [inaudible], any sloppy code 821 00:35:35,486 --> 00:35:36,426 that you write for you. 822 00:35:36,616 --> 00:35:37,926 G edit does not do that. 823 00:35:37,926 --> 00:35:40,516 A program called Eclipse that we will eventually use does that 824 00:35:40,516 --> 00:35:43,026 and for those more comfortable are welcome to use other tools 825 00:35:43,026 --> 00:35:45,166 if they'd like, but for now the goal is 826 00:35:45,166 --> 00:35:47,626 to actually get you yourself into the habit 827 00:35:47,626 --> 00:35:49,196 of writing good clean code 828 00:35:49,356 --> 00:35:51,966 until the tools will actually start making this easier for us. 829 00:35:51,966 --> 00:35:53,616 So in short, in G edit no, 830 00:35:53,676 --> 00:35:56,146 you need to be hitting the tab key or space bar. 831 00:35:56,606 --> 00:35:57,906 So bullion [assumed spelling] expressions, 832 00:35:57,906 --> 00:36:00,376 just as in Scratch you can say if this is true 833 00:36:00,406 --> 00:36:03,946 and this is true do something or you can say if this is true 834 00:36:03,996 --> 00:36:06,036 or this is true do something. 835 00:36:06,036 --> 00:36:08,626 And as I recall those box are generally green in shape 836 00:36:08,626 --> 00:36:10,496 and they've got little diamonds, if you used any 837 00:36:10,496 --> 00:36:11,406 of these in your program. 838 00:36:11,696 --> 00:36:14,776 To express that same idea in C you can say 839 00:36:14,776 --> 00:36:16,416 if this condition is true 840 00:36:16,836 --> 00:36:20,556 or some other condition is true then do this 841 00:36:20,716 --> 00:36:22,986 with whatever's inside the curly braces. 842 00:36:22,986 --> 00:36:26,446 Now the vertical bar, vertical bar is just again C syntax 843 00:36:26,446 --> 00:36:27,416 for saying or. 844 00:36:27,416 --> 00:36:30,066 A single vertical bar in a few weeks we'll see means something 845 00:36:30,066 --> 00:36:32,366 else so this is just the equivalent of or. 846 00:36:32,586 --> 00:36:35,776 The equivalent of and meanwhile is this, and-and. 847 00:36:36,076 --> 00:36:40,156 And so now with this syntax we can express doing something if, 848 00:36:40,156 --> 00:36:42,686 only if, two things are true. 849 00:36:42,686 --> 00:36:46,146 So let's actually try to use a program that leverages these. 850 00:36:46,146 --> 00:36:49,346 Let me go ahead and close Hello Dot C. I'm going to go into one 851 00:36:49,346 --> 00:36:51,806 of the prefab examples for today, which is available 852 00:36:51,806 --> 00:36:54,316 as a pdf and also as downloadable code 853 00:36:54,316 --> 00:36:57,786 on the courses website under lectures, and I'm going 854 00:36:57,786 --> 00:37:00,996 to open a file called Conditions 1.C 855 00:37:01,466 --> 00:37:03,036 and it looks a little something like this. 856 00:37:03,476 --> 00:37:06,026 Now at the top of the file there's actually a whole lot 857 00:37:06,026 --> 00:37:08,096 of text but this is just a comment and the fact 858 00:37:08,096 --> 00:37:10,126 that I've hit the asterisk this many times, 859 00:37:10,376 --> 00:37:12,716 this is just a human convention to kind of make super clear, 860 00:37:12,716 --> 00:37:16,136 this is not only a comment, this is a pretty huge comment. 861 00:37:16,136 --> 00:37:18,786 You'd better read this but this is just a matter of style. 862 00:37:18,786 --> 00:37:20,986 You don't necessarily need to adopt the same convention. 863 00:37:21,226 --> 00:37:23,466 What's important though is that the first character 864 00:37:23,466 --> 00:37:25,076 at top left is slash-star 865 00:37:25,076 --> 00:37:29,186 and the very last one involved is star-slash. 866 00:37:29,186 --> 00:37:32,606 All the other stars are just an aesthetic curiosity. 867 00:37:32,786 --> 00:37:35,536 But here I just have some comments so let's scroll 868 00:37:35,536 --> 00:37:36,916 down to the more interesting part. 869 00:37:37,186 --> 00:37:40,356 Notice that I'm using apparently the CS5 library. 870 00:37:40,726 --> 00:37:43,366 I'm using Standard IO so I can preemptively guess 871 00:37:43,526 --> 00:37:45,436 that I'm probably calling something 872 00:37:45,436 --> 00:37:47,206 like get string or get ints. 873 00:37:47,206 --> 00:37:48,846 I'm using something from the library 874 00:37:49,146 --> 00:37:51,236 and I'm probably also using what other function? 875 00:37:51,826 --> 00:37:54,286 I kind of infer the answer. 876 00:37:54,286 --> 00:37:54,696 It's right there. 877 00:37:54,696 --> 00:37:55,126 >> [ Inaudible ] 878 00:37:55,126 --> 00:37:56,086 >> Right, print death, right? 879 00:37:56,116 --> 00:37:59,866 So I included standard IO.H because I'm calling print death. 880 00:38:00,006 --> 00:38:03,036 But otherwise this is familiar except for a few new features. 881 00:38:03,346 --> 00:38:04,606 So let me scroll down here 882 00:38:04,746 --> 00:38:07,066 and here's now a more compelling role of comments. 883 00:38:07,066 --> 00:38:09,316 Thus far just saying do this is kind of useless, 884 00:38:09,386 --> 00:38:10,966 it's meant just to be a placeholder. 885 00:38:11,356 --> 00:38:14,166 But now another matter of "good style" - 886 00:38:14,166 --> 00:38:16,156 and we'll reinforce this in section 887 00:38:16,156 --> 00:38:18,876 over the next several weeks - is to not just write your code 888 00:38:18,876 --> 00:38:21,166 and leave it to the reader, whether it's CF, a colleague 889 00:38:21,166 --> 00:38:22,336 or you one month hence, 890 00:38:22,676 --> 00:38:24,876 to figure out what the heck you meant by your code. 891 00:38:25,116 --> 00:38:27,176 The best habit to get into from the start is 892 00:38:27,176 --> 00:38:30,186 to actually make a note to yourself in one line or two 893 00:38:30,186 --> 00:38:32,536 or three or more what's going on so 894 00:38:32,536 --> 00:38:35,376 that you yourself honestly the next morning your code 895 00:38:35,376 --> 00:38:37,456 and realize oh that's what I was doing and why. 896 00:38:37,716 --> 00:38:38,766 Moreover someone else 897 00:38:38,766 --> 00:38:41,246 in the real world can also understand what you're doing. 898 00:38:41,246 --> 00:38:42,186 So what am I doing here? 899 00:38:42,466 --> 00:38:44,106 I'm asking the user for an integer. 900 00:38:44,106 --> 00:38:44,796 How do I do that? 901 00:38:45,146 --> 00:38:47,806 Well as before I just have this aesthetic of I'd 902 00:38:47,806 --> 00:38:50,176 like an integer please, colon, space. 903 00:38:50,536 --> 00:38:51,856 But I could have said anything there. 904 00:38:52,446 --> 00:38:53,536 The interesting part is this. 905 00:38:54,266 --> 00:38:55,696 So what's going on here? 906 00:38:55,836 --> 00:38:57,846 Well before I used get string 907 00:38:58,276 --> 00:39:01,276 and get string returns, so to speak, a value. 908 00:39:01,276 --> 00:39:03,486 It figures out how to get all the user's keystrokes, 909 00:39:03,646 --> 00:39:06,596 it then bundles them up together and then hands me a value. 910 00:39:06,596 --> 00:39:07,946 But if I want to keep that around I need 911 00:39:07,946 --> 00:39:09,246 to put it in a variable. 912 00:39:09,246 --> 00:39:12,096 Well in Scratch to create a variable you click that button, 913 00:39:12,096 --> 00:39:14,096 like create variable, and you give it a name. 914 00:39:14,666 --> 00:39:16,876 Same thing in C but you have to be more explicit. 915 00:39:16,876 --> 00:39:19,476 You have to give the variable a type and you have 916 00:39:19,506 --> 00:39:23,336 to say is it a string, is it an int, is it a float. 917 00:39:23,336 --> 00:39:25,716 So the key difference here in C is 918 00:39:25,716 --> 00:39:27,196 that you literally have to say that. 919 00:39:27,376 --> 00:39:30,126 I want a variable called N, its data type -- 920 00:39:30,326 --> 00:39:32,546 per this keyword to the left -- is an int. 921 00:39:32,916 --> 00:39:35,026 And what do I want to put into that variable? 922 00:39:35,026 --> 00:39:37,926 I want to assign it, so the equal sign means assignment, 923 00:39:38,246 --> 00:39:41,896 whatever it is that get INT has gotten from the user. 924 00:39:42,346 --> 00:39:46,126 Now just as an aside because you'll see other syntax still, 925 00:39:46,446 --> 00:39:49,926 these two things -- getting in the INT and saving the INT -- 926 00:39:50,106 --> 00:39:52,346 do not all need to be done in the same line. 927 00:39:52,346 --> 00:39:54,236 It is not uncommon to see this instead. 928 00:39:54,236 --> 00:39:57,866 I'm going to put a semicolon there and then on the next line 929 00:39:57,906 --> 00:40:00,436 or somewhere else in my program I can do this. 930 00:40:00,676 --> 00:40:04,206 So the takeaway here is that you always have to declare, 931 00:40:04,206 --> 00:40:05,366 so to speak, a variable. 932 00:40:05,366 --> 00:40:07,786 And to declare a variable just means tell me its type, 933 00:40:08,056 --> 00:40:09,926 tell me its name, semicolon, that's it. 934 00:40:10,556 --> 00:40:12,886 But to assign a variable a value you have 935 00:40:12,886 --> 00:40:15,716 to eventually use the assignment operator, the equal sign, 936 00:40:15,906 --> 00:40:18,526 but notice that you don't re-declare what? 937 00:40:19,276 --> 00:40:19,366 >> Int. 938 00:40:19,366 --> 00:40:20,356 >> You don't say INT again. 939 00:40:20,356 --> 00:40:22,166 You already told the compiler what it is. 940 00:40:22,166 --> 00:40:24,126 It would be redundant to say what it is again 941 00:40:24,326 --> 00:40:26,036 and so you don't need to say it again. 942 00:40:26,036 --> 00:40:28,636 Now why use Version 2 versus Version 1? 943 00:40:29,066 --> 00:40:30,916 You know frankly again as a matter of style, 944 00:40:30,916 --> 00:40:33,426 and this is something that you kind of pick up on your own. 945 00:40:33,716 --> 00:40:35,766 It just kind of looks a little ugly, right? 946 00:40:35,766 --> 00:40:38,346 I'm a little anal and I just kind of like the elegance 947 00:40:38,476 --> 00:40:39,866 of this one line of code. 948 00:40:40,136 --> 00:40:42,076 And so when we say code is elegant 949 00:40:42,076 --> 00:40:45,166 or clean it's these little details where your code starts 950 00:40:45,166 --> 00:40:47,306 to get more compact and just more readable. 951 00:40:47,306 --> 00:40:49,616 And this too is a style that we won't enforce on you 952 00:40:49,816 --> 00:40:52,926 but rather encourage exactly what conventions you adopt. 953 00:40:52,926 --> 00:40:54,236 Now what about here? 954 00:40:54,236 --> 00:40:55,536 This is the meat of the program. 955 00:40:55,536 --> 00:40:58,036 Analyze the user's input somewhat inaccurately. 956 00:40:58,996 --> 00:41:00,756 Now here's the syntax for this. 957 00:41:00,756 --> 00:41:03,566 If N, which again is the INT that the user just typed, 958 00:41:03,816 --> 00:41:07,206 is greater than zero print out, you pick the positive number, 959 00:41:07,476 --> 00:41:11,216 backslash N, else you picked a negative number. 960 00:41:11,356 --> 00:41:14,576 Now I disclaimed in parenthesis somewhat inaccurately. 961 00:41:14,576 --> 00:41:16,146 Why? What's the bug in this program? 962 00:41:16,316 --> 00:41:16,836 >> Zero. 963 00:41:16,836 --> 00:41:17,726 >> Okay, good. 964 00:41:18,036 --> 00:41:20,076 So zero is obviously not considered. 965 00:41:20,226 --> 00:41:21,976 So if I type zero what's it going to say? 966 00:41:21,976 --> 00:41:22,566 >> Negative. 967 00:41:23,216 --> 00:41:24,826 >> You picked a negative number and that's not true. 968 00:41:24,826 --> 00:41:27,816 Zero is neither negative nor positive so we can fix this 969 00:41:28,166 --> 00:41:32,146 so I can actually do something like else if N is less 970 00:41:32,276 --> 00:41:36,006 than zero say this and then otherwise - can we go ahead 971 00:41:36,006 --> 00:41:38,396 and just copy and paste and then get rid 972 00:41:38,396 --> 00:41:40,126 of the redundant condition there -- 973 00:41:40,426 --> 00:41:44,566 you picked, let's say you picked zero. 974 00:41:44,696 --> 00:41:46,416 And now I have three branches, 975 00:41:46,416 --> 00:41:48,966 the last of which is the catch-all but the only case left 976 00:41:48,966 --> 00:41:51,966 to catch is zero so this would now seem to work. 977 00:41:52,136 --> 00:41:54,976 But also I seem to be violating the previous slide. 978 00:41:54,976 --> 00:41:57,156 What piece of syntax have I omitted here? 979 00:41:58,326 --> 00:41:58,686 >> Curly braces. 980 00:41:58,686 --> 00:41:59,906 >> Yeah, those curly braces. 981 00:41:59,906 --> 00:42:02,716 Remember in every prior example in black and white I had if 982 00:42:02,716 --> 00:42:05,716 and then a condition in parenths, open curly brace, 983 00:42:05,996 --> 00:42:08,516 then some comment like do this, close curly brace. 984 00:42:08,716 --> 00:42:12,916 Well it turns out this is okay if and only if all 985 00:42:12,916 --> 00:42:15,766 that you're doing inside of the fork in the road is one thing. 986 00:42:15,976 --> 00:42:19,206 If you only have one line of code frankly it just feels kind 987 00:42:19,206 --> 00:42:22,896 of lame to add three lines in total just to say one thing. 988 00:42:23,086 --> 00:42:24,936 So this is correct and frankly 989 00:42:24,936 --> 00:42:27,756 when your code gets more fancy this is actually more readable 990 00:42:27,756 --> 00:42:30,876 because it makes it even more clear what's inside this fork 991 00:42:30,876 --> 00:42:31,406 in the road. 992 00:42:31,686 --> 00:42:34,996 But as it is it like triples the size of this block of code 993 00:42:35,126 --> 00:42:36,426 and it's just not necessary. 994 00:42:36,426 --> 00:42:41,306 So this too is a matter of style but notice this would be wrong: 995 00:42:41,486 --> 00:42:46,876 print F, oh by the way hi or something like that. 996 00:42:47,076 --> 00:42:50,216 This is now wrong and the program will not even compile 997 00:42:50,216 --> 00:42:52,906 in this case because even though I've indented it, that's great. 998 00:42:52,956 --> 00:42:54,206 You can indent things all you want. 999 00:42:54,446 --> 00:42:56,166 If you've used more than one line of code 1000 00:42:56,166 --> 00:42:59,056 and no curly braces the compiler's actually going 1001 00:42:59,056 --> 00:42:59,706 to complain. 1002 00:42:59,966 --> 00:43:01,596 So let's go back to the correct version. 1003 00:43:01,596 --> 00:43:03,196 Let me get rid of this and I'll leave 1004 00:43:03,196 --> 00:43:04,706 in the third fork in the road. 1005 00:43:04,926 --> 00:43:06,506 Let me go down now to this file. 1006 00:43:07,206 --> 00:43:10,766 Now this happens to be a file I created in advance and I put it 1007 00:43:10,906 --> 00:43:15,026 in a directory called Lectures and sub directory called One 1008 00:43:15,176 --> 00:43:18,426 for week one and then in a sub directory called SRC and now 1009 00:43:18,586 --> 00:43:21,126 if I type L-S here's a whole bunch of code 1010 00:43:21,126 --> 00:43:22,766 that we'll be playing with this week and next. 1011 00:43:23,016 --> 00:43:25,356 Let me go ahead now and say make -- 1012 00:43:25,746 --> 00:43:26,926 what was the name of this program? 1013 00:43:27,436 --> 00:43:29,486 Conditions One, enter. 1014 00:43:29,906 --> 00:43:30,656 That looks good. 1015 00:43:30,656 --> 00:43:32,386 Let me go ahead and run conditions one. 1016 00:43:32,466 --> 00:43:33,596 I'd like an integer please. 1017 00:43:33,646 --> 00:43:35,776 Let's say one hundred twenty-three enter. 1018 00:43:36,196 --> 00:43:37,406 You picked a positive number. 1019 00:43:37,406 --> 00:43:38,646 How about negative three? 1020 00:43:39,146 --> 00:43:40,196 You picked a negative number. 1021 00:43:40,196 --> 00:43:41,216 And how about zero? 1022 00:43:41,766 --> 00:43:42,876 So it seems to have caught that. 1023 00:43:43,146 --> 00:43:44,536 So we now have conditions. 1024 00:43:44,536 --> 00:43:44,646 Yeah? 1025 00:43:44,646 --> 00:43:44,736 >> [Inaudible] 1026 00:43:44,736 --> 00:43:47,046 >> Sorry, a little louder. 1027 00:43:47,756 --> 00:43:52,306 >> What if you try and feed the program a float? 1028 00:43:52,306 --> 00:43:52,896 >> Good question. 1029 00:43:52,896 --> 00:43:55,656 So what if I try to feed the program a float, 1030 00:43:55,906 --> 00:43:57,556 which is clearly not an INT, 1031 00:43:57,726 --> 00:43:59,406 a float being something with a decimal point. 1032 00:43:59,406 --> 00:44:01,896 And again recall that I'm using get N, well let's see. 1033 00:44:01,896 --> 00:44:04,006 And again this too should be a recurring theme like when 1034 00:44:04,006 --> 00:44:05,256 in doubt -- and obviously it's fine now 1035 00:44:05,256 --> 00:44:06,516 if you don't have your own computer up -- 1036 00:44:06,686 --> 00:44:07,826 but when in doubt just try it. 1037 00:44:07,826 --> 00:44:10,226 The worst thing that can happen is it will break something 1038 00:44:10,436 --> 00:44:13,906 and that will in itself reveal the particular answer. 1039 00:44:13,946 --> 00:44:15,496 I'd like an integer please. 1040 00:44:15,496 --> 00:44:18,566 How about 1.99 enter. 1041 00:44:18,996 --> 00:44:20,056 So it says retry. 1042 00:44:20,266 --> 00:44:23,626 So one of the things the C50 library does too for you is 1043 00:44:23,626 --> 00:44:26,466 that if you're calling get INT it's only going 1044 00:44:26,466 --> 00:44:27,576 to let you get an int. 1045 00:44:27,576 --> 00:44:30,136 If the user is difficult and types in something stupid 1046 00:44:30,136 --> 00:44:33,186 like monkey it's also just going to reject it 1047 00:44:33,306 --> 00:44:35,636 and automatically say retry. 1048 00:44:35,936 --> 00:44:37,976 So it does these kind of checks for you. 1049 00:44:38,176 --> 00:44:40,016 If I wanted to actually get a float I would have 1050 00:44:40,016 --> 00:44:41,276 to actually use get a float. 1051 00:44:41,466 --> 00:44:43,906 Now just a quick tip, how do I get rid of this program? 1052 00:44:43,906 --> 00:44:46,026 I'm kind of done executing it. 1053 00:44:46,026 --> 00:44:47,296 Control C will abort. 1054 00:44:47,606 --> 00:44:47,966 Yeah? 1055 00:44:47,966 --> 00:44:51,296 >> If you were to take input in several lines 1056 00:44:51,296 --> 00:44:54,316 of code how would get INT react to that? 1057 00:44:54,506 --> 00:44:54,976 >> Good question. 1058 00:44:54,976 --> 00:44:58,246 If you were to use multiple lines of input as in the case 1059 00:44:58,246 --> 00:45:02,946 of let me go back to Hello where I say something like 'Hello, 1060 00:45:02,946 --> 00:45:06,146 my name is David,' enter. 1061 00:45:06,426 --> 00:45:08,636 So get string can only get one line of strings. 1062 00:45:08,736 --> 00:45:09,876 So to get multiple lines 1063 00:45:09,876 --> 00:45:12,246 of input we'll actually need more sophisticated techniques. 1064 00:45:12,496 --> 00:45:14,616 We cannot use get string but that's fine because at 1065 00:45:14,616 --> 00:45:15,976 that point these training wheels will be off. 1066 00:45:17,206 --> 00:45:19,566 All right, so we now have conditions, 1067 00:45:19,906 --> 00:45:22,706 we now have bullion expressions. 1068 00:45:22,706 --> 00:45:25,936 And again a bullion expression, to be clear, is just something 1069 00:45:25,936 --> 00:45:30,596 in parenthesis that evaluates or is true or false, one or zero. 1070 00:45:30,846 --> 00:45:32,796 But there's a few other pieces -- 1071 00:45:32,796 --> 00:45:36,516 there's a few other features here among them called switches. 1072 00:45:36,826 --> 00:45:38,626 So it turns out if you have a bunch 1073 00:45:38,626 --> 00:45:42,566 of conditions you don't have to say if, else if, else if, 1074 00:45:42,566 --> 00:45:44,146 else if, else if, else, 1075 00:45:44,426 --> 00:45:46,976 just know that there's this other construct, 1076 00:45:46,976 --> 00:45:49,376 this other piece of syntax altogether called switch 1077 00:45:49,666 --> 00:45:52,996 where all you need to do is actually enumerate cases one 1078 00:45:52,996 --> 00:45:53,586 at a time. 1079 00:45:53,746 --> 00:45:55,556 And we won't dive into detail on this one 1080 00:45:55,796 --> 00:45:57,566 but the takeaway here is that just 1081 00:45:57,566 --> 00:46:00,966 as in Scratch there's rarely if ever going to be one right 1082 00:46:00,966 --> 00:46:04,246 or even one correct way of doing something rather there can be 1083 00:46:04,246 --> 00:46:06,916 any number of ways and so also as a matter of style 1084 00:46:07,096 --> 00:46:09,866 when you start to have if, else if, else if, else if, else if, 1085 00:46:09,866 --> 00:46:12,016 else if that's just a lot of redundant words. 1086 00:46:12,086 --> 00:46:15,516 So switch is just another way, as we'll eventually see, 1087 00:46:15,516 --> 00:46:17,006 that you can simplify syntax 1088 00:46:17,366 --> 00:46:20,376 by just listing them case by case by case. 1089 00:46:20,826 --> 00:46:23,906 But the most powerful thing that we'll need ultimately are loops 1090 00:46:23,976 --> 00:46:26,176 because otherwise our programs can only do one thing 1091 00:46:26,236 --> 00:46:27,626 and once and that's it. 1092 00:46:27,866 --> 00:46:29,756 So with loops, recall from Wednesday, 1093 00:46:29,756 --> 00:46:32,166 we translated some sample Scratch blocks 1094 00:46:32,406 --> 00:46:34,856 into the corresponding looping constructs. 1095 00:46:35,156 --> 00:46:38,206 Now this is one of the scariest ones to look at first 1096 00:46:38,206 --> 00:46:39,916 but it's actually quite easy to use. 1097 00:46:39,966 --> 00:46:42,086 In a four loop you declare a variable, 1098 00:46:42,256 --> 00:46:44,046 you generally call it I by habit, 1099 00:46:44,416 --> 00:46:47,246 then you say how many times do you want to increment I, plus, 1100 00:46:47,246 --> 00:46:49,966 plus, plus, plus, plus, plus, at which point you want to stop 1101 00:46:50,086 --> 00:46:52,056 and inside of this loop you'll do something. 1102 00:46:52,366 --> 00:46:54,196 So we can actually do something a little silly. 1103 00:46:54,196 --> 00:46:56,976 Let me go over to G edit here. 1104 00:46:56,976 --> 00:46:58,976 I'm going to go ahead and just create a new file. 1105 00:46:58,976 --> 00:47:03,816 I'm going to call this on my desktop let's say Loop.C 1106 00:47:04,426 --> 00:47:06,546 and again I'm just going to bang out some familiar lines. 1107 00:47:06,576 --> 00:47:09,926 So include Stand IO.H. I'm not even going 1108 00:47:09,926 --> 00:47:11,326 to use the CS50 library. 1109 00:47:12,006 --> 00:47:17,596 INT, main, void, now I'm going to do print F. How about this? 1110 00:47:17,886 --> 00:47:22,066 So let's do INT N, get int. 1111 00:47:22,256 --> 00:47:26,396 Okay I just lied so I need the CS50 library so not problem. 1112 00:47:26,396 --> 00:47:28,226 So CS50.H bugs off. 1113 00:47:28,416 --> 00:47:30,036 So I'm getting an INT from the user 1114 00:47:30,356 --> 00:47:32,786 and now what do I want to actually do? 1115 00:47:32,786 --> 00:47:37,216 Well I want to look like this, for INT I gets zero. 1116 00:47:37,216 --> 00:47:40,486 So give me an INT, call it I, initialize it to zero. 1117 00:47:40,966 --> 00:47:44,546 Go ahead and keep doing this so long as I is less than N 1118 00:47:45,136 --> 00:47:48,656 and then keep incrementing I on ever iteration of this loop. 1119 00:47:48,656 --> 00:47:51,336 So Scratch kind of automated some of this for us. 1120 00:47:51,336 --> 00:47:55,196 If you use the block like change variable by one, 1121 00:47:55,196 --> 00:47:57,286 it's a little simpler. 1122 00:47:57,526 --> 00:48:00,526 But now let me do this, let me indent here and just as with 1123 00:48:00,526 --> 00:48:03,996 if if you're just executing one thing I don't need curly braces 1124 00:48:03,996 --> 00:48:04,886 around the four loop. 1125 00:48:05,046 --> 00:48:08,076 Let me go ahead and print out the decimal number followed 1126 00:48:08,076 --> 00:48:10,986 by a new line that's currently inside I. 1127 00:48:11,496 --> 00:48:12,656 So very simple program. 1128 00:48:12,656 --> 00:48:15,376 Ask the user for an INT, print it out, then increment it, 1129 00:48:15,376 --> 00:48:17,116 increment it, increment it. 1130 00:48:17,356 --> 00:48:18,446 So let's see what happens. 1131 00:48:18,476 --> 00:48:21,946 This is called Loop.C. Let me go ahead and type make -- 1132 00:48:21,946 --> 00:48:27,856 it's on my desktop -- let me do make loop. 1133 00:48:27,996 --> 00:48:29,366 Okay that's good, no errors. 1134 00:48:29,426 --> 00:48:31,996 Let me go ahead now and run loop. 1135 00:48:32,196 --> 00:48:34,106 Okay I need to type in a number, ninety-nine. 1136 00:48:34,106 --> 00:48:36,336 Okay that's a pretty fast program, right? 1137 00:48:36,336 --> 00:48:38,446 I can now scroll way back here. 1138 00:48:38,636 --> 00:48:41,846 Now of course you can do some perhaps silly things. 1139 00:48:42,256 --> 00:48:46,086 What might be a bug now in this program? 1140 00:48:46,086 --> 00:48:46,153 >> [Inaudible] 1141 00:48:46,153 --> 00:48:48,486 >> Yeah, what if I'm kind of a difficult user 1142 00:48:48,486 --> 00:48:49,936 and I say how about negative one? 1143 00:48:50,026 --> 00:48:52,146 See what happens. 1144 00:48:52,226 --> 00:48:52,736 What happened? 1145 00:48:52,736 --> 00:48:54,506 >> [Inaudible] 1146 00:48:54,506 --> 00:48:55,366 >> Yeah it's already less 1147 00:48:55,506 --> 00:48:58,726 than N. It's already less than negative one. 1148 00:48:58,996 --> 00:49:01,546 So what if I kind of goof and do something like this, right? 1149 00:49:01,546 --> 00:49:02,536 This is a reasonable bug. 1150 00:49:02,566 --> 00:49:07,466 Let me recompile and do make loop and now let me go ahead 1151 00:49:07,466 --> 00:49:10,746 and run loop and now if I type something 1152 00:49:10,746 --> 00:49:13,096 like one okay that works. 1153 00:49:13,156 --> 00:49:14,646 Negative one, uh oh. 1154 00:49:14,996 --> 00:49:18,516 So now we sort of get 1155 00:49:18,516 --> 00:49:21,376 to empirically test what the capacity is of an [inaudible]. 1156 00:49:21,816 --> 00:49:24,286 And just to think back to Wednesday, 1157 00:49:24,286 --> 00:49:26,736 what is the largest INT that we can actually represent? 1158 00:49:27,556 --> 00:49:28,696 >> 21478. 1159 00:49:28,696 --> 00:49:30,386 >> Yeah so it's like two billion give or take. 1160 00:49:30,386 --> 00:49:32,466 It's four billion total if you have thirty-two bits 1161 00:49:32,666 --> 00:49:33,946 but if you want to use some negative, 1162 00:49:33,946 --> 00:49:36,316 some positive you actually have to sacrifice half. 1163 00:49:36,316 --> 00:49:38,526 So it's roughly negative two billion to positive two billion. 1164 00:49:38,756 --> 00:49:41,136 Frankly this is going to take all day if we keep counting 1165 00:49:41,136 --> 00:49:43,916 up this way so let's actually do something a little fancier. 1166 00:49:44,116 --> 00:49:48,006 In my program rather than an increment one let me go ahead 1167 00:49:48,006 --> 00:49:50,016 and say multiply by two. 1168 00:49:50,326 --> 00:49:51,736 So this is just fancy syntax 1169 00:49:51,736 --> 00:49:52,796 that we'll come back to over time. 1170 00:49:52,796 --> 00:49:54,566 But this is just shorthand notation for saying -- 1171 00:49:54,566 --> 00:49:56,086 actually let me do it more simply. 1172 00:49:56,666 --> 00:50:00,216 I on each iteration is going to get the value of I times two. 1173 00:50:00,376 --> 00:50:02,106 So keep doubling it, doubling it, doubling it. 1174 00:50:02,336 --> 00:50:03,716 That should get us out of here quicker. 1175 00:50:03,716 --> 00:50:05,256 So let's try this: make loop, 1176 00:50:05,256 --> 00:50:07,736 let me go ahead now and run loop. 1177 00:50:08,286 --> 00:50:11,826 Negative one, huh, what happened? 1178 00:50:12,086 --> 00:50:14,926 Let' scroll back up here. 1179 00:50:15,136 --> 00:50:15,826 What's going on? 1180 00:50:16,286 --> 00:50:16,916 >> Started at zero. 1181 00:50:17,216 --> 00:50:18,486 >> Yes, I'm kind of an idiot right? 1182 00:50:18,486 --> 00:50:19,836 I shouldn't have started counting at zero 1183 00:50:19,836 --> 00:50:21,836 and then multiply it by two an infinite number of times. 1184 00:50:22,336 --> 00:50:23,106 That was not intended. 1185 00:50:23,276 --> 00:50:25,266 So let's go ahead and do make loop, 1186 00:50:25,596 --> 00:50:27,436 rerun loop with negative one. 1187 00:50:27,986 --> 00:50:29,556 Interesting, it stopped. 1188 00:50:29,966 --> 00:50:30,916 It didn't keep going. 1189 00:50:32,136 --> 00:50:33,446 It should have kept going, right? 1190 00:50:33,446 --> 00:50:35,766 It should have kept going again and again because so long 1191 00:50:35,766 --> 00:50:39,146 as I is greater than N it just go on up to infinity. 1192 00:50:39,146 --> 00:50:40,026 So what might have happened? 1193 00:50:40,026 --> 00:50:40,093 Yeah? 1194 00:50:40,176 --> 00:50:45,356 >> It went past the highest number it could display. 1195 00:50:45,626 --> 00:50:47,426 >> Yeah it went past the highest number it can display 1196 00:50:47,426 --> 00:50:48,296 and more specifically. 1197 00:50:48,296 --> 00:50:50,406 >> It became a negative number because it overflowed. 1198 00:50:50,406 --> 00:50:51,376 >> It became negative. 1199 00:50:51,376 --> 00:50:52,626 So here's the curious thing. 1200 00:50:52,626 --> 00:50:55,006 Just as with our example with humans we had eight bits. 1201 00:50:55,396 --> 00:50:57,916 Just because I want to count beyond 1202 00:50:57,916 --> 00:51:00,146 that eighth bit it doesn't mean you get a free bit all 1203 00:51:00,146 --> 00:51:02,416 of a sudden and now you have nine bits and ten bits, right? 1204 00:51:02,416 --> 00:51:03,756 You are limited in capacity. 1205 00:51:04,036 --> 00:51:06,646 So if we've made all these humans raise their hand 1206 00:51:06,836 --> 00:51:09,736 and then we said okay add one more number to your total value, 1207 00:51:10,026 --> 00:51:11,366 what would they do reasonably? 1208 00:51:11,436 --> 00:51:12,936 Maybe one of them puts their hand down, 1209 00:51:13,086 --> 00:51:14,626 maybe all of them put their hands down. 1210 00:51:14,626 --> 00:51:17,376 In other words, if you keep changing zeros to ones again 1211 00:51:17,376 --> 00:51:19,556 and again and you run out of permutations you're going 1212 00:51:19,556 --> 00:51:20,546 to have to start over. 1213 00:51:20,716 --> 00:51:24,756 But unfortunately once you start over and overflow well, 1214 00:51:24,756 --> 00:51:27,896 what comes after roughly two billion when you overflow? 1215 00:51:27,896 --> 00:51:30,126 How do you start over? 1216 00:51:30,126 --> 00:51:31,576 It becomes negative two billion. 1217 00:51:31,756 --> 00:51:32,686 So even though we didn't see it 1218 00:51:32,686 --> 00:51:36,586 on the screen here what happened is the zeros and ones changed 1219 00:51:36,586 --> 00:51:40,106 into some pattern whereby it actually was representing a 1220 00:51:40,106 --> 00:51:43,166 negative number, because again we only have finitely many zeros 1221 00:51:43,166 --> 00:51:45,956 and ones in thirty-two bits or even sixty-four bits, 1222 00:51:46,316 --> 00:51:49,006 and so now we had the illusion of some negative number 1223 00:51:49,236 --> 00:51:50,726 and this badness happened. 1224 00:51:50,796 --> 00:51:51,626 It just stopped. 1225 00:51:52,006 --> 00:51:54,736 But the loop, a four loop is not the only option. 1226 00:51:55,016 --> 00:51:56,936 We can actually use something called a while loop 1227 00:51:56,936 --> 00:51:59,086 and this shouldn't be too scary too because even 1228 00:51:59,086 --> 00:52:01,996 in Scratch you had repeat blocks, you had forever blocks. 1229 00:52:02,246 --> 00:52:04,426 Same idea and we'll see different syntax. 1230 00:52:04,676 --> 00:52:06,526 And then there's also this one too, 1231 00:52:06,736 --> 00:52:09,846 a do while loop whereby it does something one time 1232 00:52:10,086 --> 00:52:12,986 and then again and again and again. 1233 00:52:13,506 --> 00:52:15,546 And so you'll see in Problem Set 1 1234 00:52:15,546 --> 00:52:17,726 in particular what something like this can do. 1235 00:52:18,066 --> 00:52:20,626 Now let me just go back to the terminal window here 1236 00:52:20,946 --> 00:52:24,066 and demonstrate just two things to peak curiosity 1237 00:52:24,066 --> 00:52:25,186 as to what lies ahead. 1238 00:52:25,496 --> 00:52:28,676 If I go into the pre-fabbed code here you'll see a couple 1239 00:52:28,676 --> 00:52:32,456 of things, one of which is called Thad Gavin [assumed 1240 00:52:32,936 --> 00:52:33,806 spelling], named after the guy. 1241 00:52:33,806 --> 00:52:35,586 So this is when you really want to be anal 1242 00:52:35,586 --> 00:52:37,196 and pretty print to your code. 1243 00:52:37,196 --> 00:52:38,596 You can actually make it look pretty 1244 00:52:38,936 --> 00:52:40,116 by formatting it in this way. 1245 00:52:40,116 --> 00:52:42,116 This is not what we mean when we say good style. 1246 00:52:42,206 --> 00:52:45,296 This is a submission for a special contest where you try 1247 00:52:45,296 --> 00:52:46,636 to outdo all of the other geeks. 1248 00:52:46,636 --> 00:52:48,966 But what you can do here, which is pretty cool, 1249 00:52:49,236 --> 00:52:53,966 is if we run Thad's program you can do some crazy things just 1250 00:52:53,966 --> 00:52:54,876 using these loops. 1251 00:52:55,386 --> 00:52:56,726 So this is a C program. 1252 00:52:56,726 --> 00:52:59,506 It was only a few dozen lines of code - you saw the whole thing - 1253 00:52:59,646 --> 00:53:01,996 but with loops and with C we can do things like this 1254 00:53:01,996 --> 00:53:04,066 and this is just the beginning. 1255 00:53:04,556 --> 00:53:06,156 So with that said, let's call it a day. 1256 00:53:06,156 --> 00:53:07,976 PSet1 will be posted tonight and we'll see you on Monday. 1257 00:53:08,516 --> 00:53:12,500 [ Applause ]