1 00:00:00,000 --> 00:00:12,410 >> [MUSIC PLAYING] 2 00:00:12,410 --> 00:00:12,830 >> DAVID MALAN: All right. 3 00:00:12,830 --> 00:00:13,370 Welcome back. 4 00:00:13,370 --> 00:00:16,510 This is CS50, and this is Week One, Continued. 5 00:00:16,510 --> 00:00:20,050 So one of the reasons we've been passing around technologies like 6 00:00:20,050 --> 00:00:24,120 Google Glass is that they come with something known as an API, an 7 00:00:24,120 --> 00:00:25,980 Application Programming Interface. 8 00:00:25,980 --> 00:00:28,830 And what this means is that with the right files and with the right 9 00:00:28,830 --> 00:00:32,850 documentation, you can actually write software for even devices like these. 10 00:00:32,850 --> 00:00:36,200 >> And so what we have in mind toward semesters end is, if a project 11 00:00:36,200 --> 00:00:39,310 involving, say, Google Glass might appeal, we'll see what we can do in 12 00:00:39,310 --> 00:00:42,220 terms of hooking you up with some loaner hardware as well as the 13 00:00:42,220 --> 00:00:46,500 publicly-accessible API so that you can actually start creating software 14 00:00:46,500 --> 00:00:48,630 that runs on that same Google Glass. 15 00:00:48,630 --> 00:00:51,710 >> Another device that we've been playing with of late that we thought might be 16 00:00:51,710 --> 00:00:53,910 fun at semester's end is this little thing here. 17 00:00:53,910 --> 00:00:56,860 It's called Leap Motion, and what you're about to see is technically an 18 00:00:56,860 --> 00:01:00,280 advertisement for the device, but it does speak to what this particular 19 00:01:00,280 --> 00:01:01,240 device is capable of. 20 00:01:01,240 --> 00:01:05,550 And it, too, comes with an API via which you can write software to 21 00:01:05,550 --> 00:01:10,545 control your own computer in a manner quite like this one-minute film here. 22 00:01:10,545 --> 00:01:12,070 >> [VIDEO PLAYBACK] 23 00:01:12,070 --> 00:02:08,002 24 00:02:08,002 --> 00:02:08,590 >> [END VIDEO PLAYBACK] 25 00:02:08,590 --> 00:02:11,190 >> So right now, this device happens to be implemented as a little USB 26 00:02:11,190 --> 00:02:14,290 connector that you can plug into your computer, but I wouldn't be surprised 27 00:02:14,290 --> 00:02:17,930 if before long we have these kinds of technologies in the next batch of PCs 28 00:02:17,930 --> 00:02:20,510 and Macs so that you can, indeed, interact with it in 29 00:02:20,510 --> 00:02:21,650 a manner like that. 30 00:02:21,650 --> 00:02:24,250 In fact, what I thought I'd do, too, is I pulled up a little demonstration 31 00:02:24,250 --> 00:02:25,860 program that comes with this software. 32 00:02:25,860 --> 00:02:28,700 So I've put the little sensor in front of my laptop. 33 00:02:28,700 --> 00:02:32,420 >> And as you can see, it already realizes that my hand is there 34 00:02:32,420 --> 00:02:33,400 controlling it. 35 00:02:33,400 --> 00:02:37,900 And all this application does is swimmingly little things like this. 36 00:02:37,900 --> 00:02:41,080 But you can see that, indeed, I have five fingers there. 37 00:02:41,080 --> 00:02:44,270 If we can get it to do all 10, you can imagine a neat piano 38 00:02:44,270 --> 00:02:45,390 application or the like. 39 00:02:45,390 --> 00:02:47,820 So if you'd like to come up after class today and play with that as 40 00:02:47,820 --> 00:02:49,720 well, you are welcome to. 41 00:02:49,720 --> 00:02:52,930 So more on that toward semester's end. 42 00:02:52,930 --> 00:02:54,420 >> So a couple of administrative announcements. 43 00:02:54,420 --> 00:02:58,690 One, do section by this Friday at noon at cs60.net/section. 44 00:02:58,690 --> 00:03:01,260 That URL is on the course's homepage as well. 45 00:03:01,260 --> 00:03:04,010 In the meantime, though, supersections will debut this Sunday. 46 00:03:04,010 --> 00:03:07,800 Supersections are a one-time thing, because it takes us some time to work 47 00:03:07,800 --> 00:03:09,470 out who's in which section and where. 48 00:03:09,470 --> 00:03:12,280 >> So this coming Sunday, there'll be one section for those less comfortable, 49 00:03:12,280 --> 00:03:14,040 one section for those more comfortable. 50 00:03:14,040 --> 00:03:16,110 And those of you somewhere in between are welcome to go to 51 00:03:16,110 --> 00:03:17,850 either or both or neither. 52 00:03:17,850 --> 00:03:19,150 Both of these will be filmed. 53 00:03:19,150 --> 00:03:22,740 They'll be in a larger auditorium than a section normally would be in. 54 00:03:22,740 --> 00:03:26,110 But the goal here is to equip you with some comfort with problem set one, 55 00:03:26,110 --> 00:03:29,910 with C, with the CS50 appliance, this weekend, even before you find out your 56 00:03:29,910 --> 00:03:31,050 formal section assignment. 57 00:03:31,050 --> 00:03:34,700 So you'll meet your TF and your smaller group of sectionmates in about 58 00:03:34,700 --> 00:03:36,350 a week's time. 59 00:03:36,350 --> 00:03:38,200 >> Office hours, meanwhile, have been going on. 60 00:03:38,200 --> 00:03:41,020 Do take advantage of them tonight or tomorrow night if you would like. 61 00:03:41,020 --> 00:03:43,460 Problem set 0 is due on Friday. 62 00:03:43,460 --> 00:03:46,680 This is a day later than the regularly-scheduled Thursday. 63 00:03:46,680 --> 00:03:50,410 But with problem set 1, you'll see that it includes some warm-up 64 00:03:50,410 --> 00:03:52,770 exercises with which you yourself can extend your 65 00:03:52,770 --> 00:03:54,660 Thursday deadline to Friday. 66 00:03:54,660 --> 00:03:58,410 Problem set 1, meanwhile, will debut on the course's website this coming 67 00:03:58,410 --> 00:04:02,000 Friday, if you'd like to curl up with it as I did once upon a time late 68 00:04:02,000 --> 00:04:04,370 Friday night in your room. 69 00:04:04,370 --> 00:04:07,660 >> So cs50.net/appliance is the place where you can get the CS50 appliance 70 00:04:07,660 --> 00:04:09,040 that we started using on Monday. 71 00:04:09,040 --> 00:04:10,140 We'll use it a bit more today. 72 00:04:10,140 --> 00:04:14,040 But rest assured that the problem set 1 specification will walk you through 73 00:04:14,040 --> 00:04:16,490 precisely the steps you need to get that up and running. 74 00:04:16,490 --> 00:04:22,120 So don't worry about doing that before Pset 1 if you'd rather not. 75 00:04:22,120 --> 00:04:22,660 >> All right. 76 00:04:22,660 --> 00:04:27,180 So we looked on Monday at source code, but in the context of a new 77 00:04:27,180 --> 00:04:30,895 programming language, namely C. And C, unlike Scratch, is not graphical. 78 00:04:30,895 --> 00:04:32,270 It's not puzzle pieces. 79 00:04:32,270 --> 00:04:34,450 It's more English-like syntax. 80 00:04:34,450 --> 00:04:37,140 And there's a couple of steps involved in actually writing and running a 81 00:04:37,140 --> 00:04:41,650 program in C, because besides source code like this, you need something 82 00:04:41,650 --> 00:04:42,890 called a compiler. 83 00:04:42,890 --> 00:04:46,682 And in layman's terms, what does a compiler do for us? 84 00:04:46,682 --> 00:04:47,650 Yeah. 85 00:04:47,650 --> 00:04:51,040 >> STUDENT: It converts the code that you write to 0s and 1s. 86 00:04:51,040 --> 00:04:51,151 >> DAVID MALAN: Good. 87 00:04:51,151 --> 00:04:53,580 So it converts the code that we write to 0s and 1s. 88 00:04:53,580 --> 00:04:57,730 It converts so-called source code to object code, the latter of which looks 89 00:04:57,730 --> 00:04:59,140 a little something like this. 90 00:04:59,140 --> 00:05:02,860 And it's your CPU, the brains inside of your computer, that because of the 91 00:05:02,860 --> 00:05:06,280 people who created those computers, know what those sequences 92 00:05:06,280 --> 00:05:07,460 of 0s and 1s mean. 93 00:05:07,460 --> 00:05:08,640 >> Maybe it means print. 94 00:05:08,640 --> 00:05:10,265 Maybe it means addition. 95 00:05:10,265 --> 00:05:11,610 Maybe it means subtraction. 96 00:05:11,610 --> 00:05:13,350 Maybe it means display a graphic. 97 00:05:13,350 --> 00:05:16,870 There are predefined patterns of bits that the world has decided mean 98 00:05:16,870 --> 00:05:17,700 certain things. 99 00:05:17,700 --> 00:05:20,760 But for the most part in this course, we'll work at a higher level, and 100 00:05:20,760 --> 00:05:24,180 we'll take for granted that there exist things like compilers that can 101 00:05:24,180 --> 00:05:27,670 make our source code function in the way we intend. 102 00:05:27,670 --> 00:05:31,660 >> So the program we wrote first last week and then began to port, so to 103 00:05:31,660 --> 00:05:33,920 speak, to C this past Monday, was this. 104 00:05:33,920 --> 00:05:35,700 When green flag clicked, say hello. 105 00:05:35,700 --> 00:05:37,430 This was, of course, written in Scratch. 106 00:05:37,430 --> 00:05:40,710 And I claimed that the equivalent program in C looked a little 107 00:05:40,710 --> 00:05:41,520 something like this. 108 00:05:41,520 --> 00:05:45,050 >> So what I thought we'd do at first is, let's tease apart what looks, at first 109 00:05:45,050 --> 00:05:48,790 glance, admittedly, as fairly arcane syntax, but you'll start to notice 110 00:05:48,790 --> 00:05:50,160 patterns quite quickly. 111 00:05:50,160 --> 00:05:53,770 What we'll also do today is provide you with a mental model for some 112 00:05:53,770 --> 00:05:57,280 canonical, some standard constructs in programming. 113 00:05:57,280 --> 00:06:00,420 And then we'll actually get our hands dirty with some examples as well. 114 00:06:00,420 --> 00:06:04,140 >> So for those of you more comfortable, realize that this week and then next 115 00:06:04,140 --> 00:06:05,940 week may very well be a bit of review. 116 00:06:05,940 --> 00:06:08,810 But especially when the hacker edition of problem set 1 goes out on the 117 00:06:08,810 --> 00:06:12,330 course's website on Friday, I do think you'll find that even as you fill in 118 00:06:12,330 --> 00:06:15,390 some blanks over the next week or two, you'll begin to get ever more 119 00:06:15,390 --> 00:06:18,410 challenged and encounter new and newer things. 120 00:06:18,410 --> 00:06:21,310 >> So let's tease this program apart a few lines at a time. 121 00:06:21,310 --> 00:06:24,140 At the very top, we have what's technically called a 122 00:06:24,140 --> 00:06:25,950 preprocessor directive. 123 00:06:25,950 --> 00:06:30,510 This is just a line of code that says to include the contents of a file, 124 00:06:30,510 --> 00:06:35,080 called standard I/N, standard input-- or, sorry, standard I/O, standard 125 00:06:35,080 --> 00:06:38,670 input/output, .h, inside of my own program. 126 00:06:38,670 --> 00:06:41,670 >> So in other words, if I wrote this program with a simple text editor like 127 00:06:41,670 --> 00:06:45,260 gedit, the simpler equivalent of something like Microsoft Word, that 128 00:06:45,260 --> 00:06:50,970 instruction, #include stdio.h, is just going to tell the compiler, grab the 129 00:06:50,970 --> 00:06:56,080 contents of that other file, stdio.h, and paste them right here. 130 00:06:56,080 --> 00:06:57,090 >> Now, why do I care? 131 00:06:57,090 --> 00:07:01,850 What did we claim is inside of this file called stdio.h, which is stored 132 00:07:01,850 --> 00:07:05,490 somewhere on my hard drive, or in this case, somewhere in the CS50 appliance? 133 00:07:05,490 --> 00:07:06,635 Someone else put it there for me. 134 00:07:06,635 --> 00:07:09,910 But what's in it? 135 00:07:09,910 --> 00:07:13,020 >> Yeah, so it's a declaration of a function called printf. 136 00:07:13,020 --> 00:07:17,400 So printf recalls the function that displays words and characters and 137 00:07:17,400 --> 00:07:20,160 numbers on the screen at my bidding. 138 00:07:20,160 --> 00:07:21,220 But I didn't write it. 139 00:07:21,220 --> 00:07:22,230 CS50 didn't write it. 140 00:07:22,230 --> 00:07:26,990 Someone years ago wrote it, and they essentially gave us the recipe for it 141 00:07:26,990 --> 00:07:29,110 in a file called stdio.h. 142 00:07:29,110 --> 00:07:33,240 So that first line just gives me access to functions that someone else 143 00:07:33,240 --> 00:07:36,500 wrote years ago, among which are printf. 144 00:07:36,500 --> 00:07:39,450 >> Now, the next line I'm going to wave my hand at, at least until next week. 145 00:07:39,450 --> 00:07:43,620 But for now, know that int main(void) is essentially the equivalent of this 146 00:07:43,620 --> 00:07:46,310 yellow puzzle piece when green flag clicked. 147 00:07:46,310 --> 00:07:49,510 The world, years ago, decided that if you're writing a program in the 148 00:07:49,510 --> 00:07:53,150 language called C, you start your program with a line 149 00:07:53,150 --> 00:07:54,410 that looks like that. 150 00:07:54,410 --> 00:07:57,650 In just about a week's time, it'll make sense what int is, what void is. 151 00:07:57,650 --> 00:08:00,840 But for now, just think of it as this yellow puzzle piece. 152 00:08:00,840 --> 00:08:04,550 >> Now next, we have a curly brace followed by another close curly brace, 153 00:08:04,550 --> 00:08:05,240 so to speak. 154 00:08:05,240 --> 00:08:07,430 And we'll see these throughout programs in C, as well as 155 00:08:07,430 --> 00:08:08,930 JavaScript and PHP. 156 00:08:08,930 --> 00:08:11,345 And these just encapsulate related lines of code. 157 00:08:11,345 --> 00:08:14,600 The open curly brace essentially says, here comes some code. 158 00:08:14,600 --> 00:08:17,590 And the closed curly brace, which is angled in the other direction, just 159 00:08:17,590 --> 00:08:19,920 means, that's it for the relevant code. 160 00:08:19,920 --> 00:08:22,970 >> So the juicy line in this first program is the last, printf, 161 00:08:22,970 --> 00:08:25,080 quote-unquote, hello world. 162 00:08:25,080 --> 00:08:30,670 And I called the thing between quotes what last time? 163 00:08:30,670 --> 00:08:31,285 So it's a string. 164 00:08:31,285 --> 00:08:35,309 And a string is just the techie term for a sequence of 165 00:08:35,309 --> 00:08:37,169 characters, a word, a phrase. 166 00:08:37,169 --> 00:08:39,380 Even a single letter could be double quoted. 167 00:08:39,380 --> 00:08:41,840 But it's a sequence of zero or more characters. 168 00:08:41,840 --> 00:08:43,650 >> Backslash-n, though, looks a bit strange. 169 00:08:43,650 --> 00:08:46,290 But it simply meant something simple to the computer. 170 00:08:46,290 --> 00:08:48,060 What's backslash-n? 171 00:08:48,060 --> 00:08:48,570 New line. 172 00:08:48,570 --> 00:08:52,490 So that's just the special sequence of characters that the world has decided 173 00:08:52,490 --> 00:08:56,880 means put a line break right there, because the compiler will actually get 174 00:08:56,880 --> 00:09:02,280 confused, usually, if you, very understandably, but incorrectly, just 175 00:09:02,280 --> 00:09:04,570 start hitting Enter to put new lines in your code. 176 00:09:04,570 --> 00:09:07,150 You need to be a little more explicit with backslash-n. 177 00:09:07,150 --> 00:09:10,540 >> And we'll see there's a few other such patterns for special characters. 178 00:09:10,540 --> 00:09:14,340 For instance, if you wanted to let your mind wander to a corner case, 179 00:09:14,340 --> 00:09:18,080 suppose that I wanted to print a double quote on the screen for 180 00:09:18,080 --> 00:09:21,840 whatever reason, that would seem to be a little problematic, right? 181 00:09:21,840 --> 00:09:24,990 Because if I put a double quote in the middle of "hello, world," for whatever 182 00:09:24,990 --> 00:09:30,120 reason, why does that seem potentially problematic? 183 00:09:30,120 --> 00:09:32,180 It breaks the string. 184 00:09:32,180 --> 00:09:34,700 >> The compiler is just a program. 185 00:09:34,700 --> 00:09:37,210 A program like a compiler is just going to read your code top to bottom, 186 00:09:37,210 --> 00:09:37,990 left to right. 187 00:09:37,990 --> 00:09:41,850 And if sees three double quotes instead of two, it's not going to know 188 00:09:41,850 --> 00:09:45,210 if you mean that the thing to the left should be the string, or the thing to 189 00:09:45,210 --> 00:09:46,570 the right, or the whole thing. 190 00:09:46,570 --> 00:09:50,560 It's ambiguous, and so what a typical compiler would do is just freak out, 191 00:09:50,560 --> 00:09:53,710 and give you some error message, and make you deal with the problem. 192 00:09:53,710 --> 00:09:58,120 >> So given that backslash-n means a new line, what would your instincts now 193 00:09:58,120 --> 00:10:02,610 start to be if you wanted to include something anomalous like a double 194 00:10:02,610 --> 00:10:06,210 quote inside of this otherwise-quoted string? 195 00:10:06,210 --> 00:10:07,640 Backslash-double quote. 196 00:10:07,640 --> 00:10:09,630 And so we'll see this kind of pattern too. 197 00:10:09,630 --> 00:10:12,490 >> If you want to do something a little strange, you'll find that the 198 00:10:12,490 --> 00:10:15,060 solutions often follow a pattern, and the backslash 199 00:10:15,060 --> 00:10:17,150 denotes an escape sequence. 200 00:10:17,150 --> 00:10:20,320 And it just means something special that we need to represent in a 201 00:10:20,320 --> 00:10:21,060 different way. 202 00:10:21,060 --> 00:10:23,830 As for how do you represent a backslash, well, we'll 203 00:10:23,830 --> 00:10:24,550 come back to that. 204 00:10:24,550 --> 00:10:26,930 But the answer, too, is actually a bit obvious. 205 00:10:26,930 --> 00:10:31,080 >> So let's now introduce some other programming constructs 206 00:10:31,080 --> 00:10:31,915 that you saw in Scratch. 207 00:10:31,915 --> 00:10:34,790 You've probably taken for granted already if you've already dived into 208 00:10:34,790 --> 00:10:36,060 problem set 0. 209 00:10:36,060 --> 00:10:40,950 But let's now introduce the syntax in C for these otherwise fairly intuitive 210 00:10:40,950 --> 00:10:42,300 ideas, at least some of them. 211 00:10:42,300 --> 00:10:45,570 >> So this notion of a condition or a branch, whereas previously we 212 00:10:45,570 --> 00:10:49,330 represented it with code that looked like this on the left in Scratch-- 213 00:10:49,330 --> 00:10:52,200 if x is less than y, then say so-- 214 00:10:52,200 --> 00:10:56,760 instead now in C-- let me hone in on the simplest case-- 215 00:10:56,760 --> 00:11:01,235 we're simply going to say, if, open parenthesis, something is true-- a 216 00:11:01,235 --> 00:11:04,190 Boolean expression is going to go where I have said condition-- 217 00:11:04,190 --> 00:11:08,170 close parenthesis, then do what's inside of those curly braces. 218 00:11:08,170 --> 00:11:11,020 >> So here, too, the curly braces are kind of like Scratch's 219 00:11:11,020 --> 00:11:12,460 U-shaped puzzle pieces. 220 00:11:12,460 --> 00:11:14,890 Do what's inside these curly braces. 221 00:11:14,890 --> 00:11:16,790 In this case, slash slash. 222 00:11:16,790 --> 00:11:19,720 And notice this is a forward slash, not a backslash. 223 00:11:19,720 --> 00:11:21,440 Forward slash, forward slash, for those who've programmed 224 00:11:21,440 --> 00:11:23,370 before, just means-- 225 00:11:23,370 --> 00:11:24,190 it's just a comment. 226 00:11:24,190 --> 00:11:26,630 A comment is not a line of code, per se. 227 00:11:26,630 --> 00:11:30,200 It's a line of English that you, the human, have written to yourself to 228 00:11:30,200 --> 00:11:33,540 perhaps remind you to do something, to explain to yourself, to explain to 229 00:11:33,540 --> 00:11:35,260 someone else, what your code is doing. 230 00:11:35,260 --> 00:11:37,050 It's just a descriptive comment. 231 00:11:37,050 --> 00:11:40,880 >> Now, of course, we can have a two-way fork in the road just by doing this, a 232 00:11:40,880 --> 00:11:43,930 three-way fork in the road by doing this, and if you continue this 233 00:11:43,930 --> 00:11:47,570 pattern, you can have four-way, five-way, six-way forks in the road if 234 00:11:47,570 --> 00:11:50,150 you need to handle, indeed, that many conditions. 235 00:11:50,150 --> 00:11:52,010 And then notice the parallelism here-- 236 00:11:52,010 --> 00:11:55,070 if (condition), else if (condition), and the only one that's a little 237 00:11:55,070 --> 00:11:58,010 strange is the last, which is just that else. 238 00:11:58,010 --> 00:12:01,170 But again, this is identical, conceptually, to what we've done 239 00:12:01,170 --> 00:12:04,690 already, even though the syntax will take a little bit getting used to. 240 00:12:04,690 --> 00:12:07,730 >> Now, in this example, there are some other strange syntax. 241 00:12:07,730 --> 00:12:11,220 We see printf again, open parenthesis, and close parenthesis. 242 00:12:11,220 --> 00:12:13,190 And inside of there is a quoted string. 243 00:12:13,190 --> 00:12:17,060 The parentheses, in the context of printf, go on the left 244 00:12:17,060 --> 00:12:18,160 and right of what? 245 00:12:18,160 --> 00:12:22,190 What do we describe that quoted string as more generally? 246 00:12:22,190 --> 00:12:23,320 >> So it's an argument. 247 00:12:23,320 --> 00:12:26,620 Any time you have a function, like printf, and then an open paren and a 248 00:12:26,620 --> 00:12:30,330 close paren and something inside of it, whether it's a string, or an 249 00:12:30,330 --> 00:12:34,420 integer, or anything else, the thing in between those parentheses is called 250 00:12:34,420 --> 00:12:35,110 an argument. 251 00:12:35,110 --> 00:12:37,910 >> And an argument just influences the function's behavior. 252 00:12:37,910 --> 00:12:39,990 In this case, it's pretty clear how. 253 00:12:39,990 --> 00:12:44,480 x < y is what's being passed in, so to speak, as an argument. 254 00:12:44,480 --> 00:12:47,720 And printf is going to print exactly that. 255 00:12:47,720 --> 00:12:51,590 Because surely, the person years ago who implemented printf had no idea 256 00:12:51,590 --> 00:12:55,000 what we human successors were going to use printf for. 257 00:12:55,000 --> 00:12:58,610 So that's why arguments exist, to influence behavior after a function's 258 00:12:58,610 --> 00:12:59,450 been written. 259 00:12:59,450 --> 00:13:00,780 >> Now, Boolean expression. 260 00:13:00,780 --> 00:13:02,470 We've seen these before in Scratch. 261 00:13:02,470 --> 00:13:06,680 It turns out in C you can also "or" them together, so to speak. 262 00:13:06,680 --> 00:13:10,930 Two vertical bars means that this block of code, this do this, will 263 00:13:10,930 --> 00:13:15,350 execute if the first condition is true or the second condition is true. 264 00:13:15,350 --> 00:13:17,710 And even though you might not have done this in Scratch, you could indeed 265 00:13:17,710 --> 00:13:20,580 do this in Scratch, And you can also express yourself differently-- 266 00:13:20,580 --> 00:13:25,300 if the first condition and the second condition are true, then do what's 267 00:13:25,300 --> 00:13:27,160 inside of the curly braces. 268 00:13:27,160 --> 00:13:31,210 >> And just as an aside, the reason it's a double ampersand and a double 269 00:13:31,210 --> 00:13:34,610 vertical bar, we'll see eventually that a single vertical bar and a 270 00:13:34,610 --> 00:13:38,710 single ampersand actually have different meaning in C. So for now, 271 00:13:38,710 --> 00:13:41,840 that duplication of symbols is intentional. 272 00:13:41,840 --> 00:13:45,070 >> So let's introduce, briefly, this other bit of syntax. 273 00:13:45,070 --> 00:13:48,940 This doesn't quite carry over to Scratch, but I claim that I can 274 00:13:48,940 --> 00:13:55,190 implement the notion of an if else, if else, if else, if else statement using 275 00:13:55,190 --> 00:13:56,760 something called a switch statement. 276 00:13:56,760 --> 00:14:00,820 And the only reason this really exists is to just give you, the programmer, a 277 00:14:00,820 --> 00:14:05,470 slightly different way of solving some problem, even though logically it 278 00:14:05,470 --> 00:14:07,340 doesn't give you any new capabilities. 279 00:14:07,340 --> 00:14:08,530 >> By that I mean this. 280 00:14:08,530 --> 00:14:13,330 When you say switch, and then a space, and then a pair of parentheses, inside 281 00:14:13,330 --> 00:14:14,570 of which goes an expression-- 282 00:14:14,570 --> 00:14:18,010 and this might be a little non-obvious at first, but this is not an argument, 283 00:14:18,010 --> 00:14:20,680 technically, because switch is not a function. 284 00:14:20,680 --> 00:14:24,230 So for now, just assume that we're using parentheses in different places 285 00:14:24,230 --> 00:14:25,250 for different reasons. 286 00:14:25,250 --> 00:14:29,310 >> So switch on expression means I can put a variable inside of those 287 00:14:29,310 --> 00:14:31,350 parentheses where it says expression. 288 00:14:31,350 --> 00:14:33,090 And then if that variable-- 289 00:14:33,090 --> 00:14:35,400 suppose it's called x, and it's just an integer-- 290 00:14:35,400 --> 00:14:38,900 I can then enumerate in my slide here something like this. 291 00:14:38,900 --> 00:14:41,690 If x is my variable, and I want to do something if x 292 00:14:41,690 --> 00:14:43,480 equals 1, I can do that. 293 00:14:43,480 --> 00:14:46,660 >> If I instead want to do something if x is 2, I can do that. 294 00:14:46,660 --> 00:14:50,390 Else, if I want to do something else entirely, I can have a default case in 295 00:14:50,390 --> 00:14:52,750 which case I do this other thing. 296 00:14:52,750 --> 00:14:58,730 So in that sense, it's equivalent to an if x equals equals 1, else if else, 297 00:14:58,730 --> 00:15:01,150 but I mention this now just because we'll see it again. 298 00:15:01,150 --> 00:15:03,720 But for now, just know that it exists. 299 00:15:03,720 --> 00:15:04,220 >> All right. 300 00:15:04,220 --> 00:15:07,660 So these last couple are a little more complex at first glance, but they do 301 00:15:07,660 --> 00:15:09,385 something fairly straightforward. 302 00:15:09,385 --> 00:15:14,410 A for loop in C is a chunk of code that just does something again and 303 00:15:14,410 --> 00:15:15,330 again and again. 304 00:15:15,330 --> 00:15:17,590 And the only annoying thing about it is that is that it's a little cryptic 305 00:15:17,590 --> 00:15:21,380 to express yourself in this cyclical pattern, but the world standardized on 306 00:15:21,380 --> 00:15:22,450 the following way. 307 00:15:22,450 --> 00:15:25,760 >> When you use a for loop, you've got a pair of parentheses again. 308 00:15:25,760 --> 00:15:27,570 And notice the two semicolons in there. 309 00:15:27,570 --> 00:15:34,380 Those two semicolons separate three different expressions inside of the 310 00:15:34,380 --> 00:15:35,020 parentheses. 311 00:15:35,020 --> 00:15:37,170 One is the so-called initializations. 312 00:15:37,170 --> 00:15:38,830 One is the so-called condition. 313 00:15:38,830 --> 00:15:40,210 And one is the so-called updates. 314 00:15:40,210 --> 00:15:43,240 >> Looked at in the abstract, this is completely non-obvious, so let's look 315 00:15:43,240 --> 00:15:44,630 at a concrete example. 316 00:15:44,630 --> 00:15:46,720 In Scratch we had a repeat block. 317 00:15:46,720 --> 00:15:50,670 And this block said repeat 10 times, say "hello, world." Let me claim for 318 00:15:50,670 --> 00:15:53,810 now, and we'll come back to this, and this, too, will get more familiar to 319 00:15:53,810 --> 00:15:57,345 you before long, the equivalent C code could be said to be this-- 320 00:15:57,345 --> 00:16:02,320 a for statement and a space, a pair of parentheses, notice the semicolons in 321 00:16:02,320 --> 00:16:05,730 there, which separate three distinct things, the initialization, the 322 00:16:05,730 --> 00:16:07,320 condition, and the update. 323 00:16:07,320 --> 00:16:08,840 >> And take a guess what the first is doing. 324 00:16:08,840 --> 00:16:10,690 Int i = 0. 325 00:16:10,690 --> 00:16:15,120 In rough layman's terms, what is that doing for us, probably? 326 00:16:15,120 --> 00:16:15,590 So yeah. 327 00:16:15,590 --> 00:16:19,630 It's declaring a variable called i, and it's giving it what value? 328 00:16:19,630 --> 00:16:20,220 0. 329 00:16:20,220 --> 00:16:24,280 So it's creating a variable called i, storing the value 0 in it. 330 00:16:24,280 --> 00:16:26,420 That's the so-called initialization. 331 00:16:26,420 --> 00:16:29,360 >> All right, now I claimed before that the middle thing, i 332 00:16:29,360 --> 00:16:31,760 < 10, is the condition. 333 00:16:31,760 --> 00:16:32,730 So what is doing? 334 00:16:32,730 --> 00:16:36,560 Well, what the for loop does when a program with a for loop is run, is 335 00:16:36,560 --> 00:16:41,050 every time the computer runs through this loop, top to bottom, top to 336 00:16:41,050 --> 00:16:43,740 bottom, just like you guys did when counting yourselves and sitting down, 337 00:16:43,740 --> 00:16:47,090 again and again and again, the computer is going 338 00:16:47,090 --> 00:16:48,560 to check that condition. 339 00:16:48,560 --> 00:16:51,140 And if i is less than 10, it'll do it again. 340 00:16:51,140 --> 00:16:53,100 If i is less than 10, it'll do it again. 341 00:16:53,100 --> 00:16:55,030 If i is less than ten, it'll do it again. 342 00:16:55,030 --> 00:16:57,330 So that seems to suggest that hopefully i is changing. 343 00:16:57,330 --> 00:16:59,010 Otherwise, we'd have an infinite loop. 344 00:16:59,010 --> 00:17:02,590 And indeed, i is changing, because the last thing after the semicolon there 345 00:17:02,590 --> 00:17:05,569 is the somewhat cryptic syntax of i++. 346 00:17:05,569 --> 00:17:07,630 But those of you who've done this before, that just means 347 00:17:07,630 --> 00:17:08,609 quite simply what? 348 00:17:08,609 --> 00:17:09,730 >> STUDENTS: [INTERPOSING VOICES]. 349 00:17:09,730 --> 00:17:10,740 >> DAVID MALAN: Add 1 to i. 350 00:17:10,740 --> 00:17:11,819 Increment i. 351 00:17:11,819 --> 00:17:13,910 So we saw a Scratch puzzle piece for that, actually. 352 00:17:13,910 --> 00:17:18,230 It didn't look like i++, but that just means, quite simply, increment the 353 00:17:18,230 --> 00:17:24,040 value i by 1 every time you do this. 354 00:17:24,040 --> 00:17:26,910 So literally, you start by initializing i to 0. 355 00:17:26,910 --> 00:17:29,520 You then check your condition. 356 00:17:29,520 --> 00:17:31,070 Is 0 less than 10? 357 00:17:31,070 --> 00:17:31,730 Yes. 358 00:17:31,730 --> 00:17:32,910 We go through the loop. 359 00:17:32,910 --> 00:17:35,150 >> The next thing I claim the computer's going to do is it's going 360 00:17:35,150 --> 00:17:35,910 to increment i. 361 00:17:35,910 --> 00:17:37,080 So i is now 1. 362 00:17:37,080 --> 00:17:37,940 It checks the condition. 363 00:17:37,940 --> 00:17:39,290 Is 1 less than 10? 364 00:17:39,290 --> 00:17:39,930 Of course. 365 00:17:39,930 --> 00:17:41,030 So does it again. 366 00:17:41,030 --> 00:17:43,580 Then it increments i++ to 2. 367 00:17:43,580 --> 00:17:44,610 Is 2 less than 10? 368 00:17:44,610 --> 00:17:45,230 Yes. 369 00:17:45,230 --> 00:17:46,670 And again and again and again. 370 00:17:46,670 --> 00:17:50,070 And eventually, because of the plus-plussing, we get to i equals 10. 371 00:17:50,070 --> 00:17:51,675 Is 10 less than 10? 372 00:17:51,675 --> 00:17:52,990 Well, obviously not. 373 00:17:52,990 --> 00:17:55,320 And that's the point at which the for loop just stops. 374 00:17:55,320 --> 00:17:58,320 And if you have more code on the screen down below, the computer 375 00:17:58,320 --> 00:18:01,620 proceeds to run that code instead. 376 00:18:01,620 --> 00:18:05,380 >> So again, even though this is going to look and feel a little strange at 377 00:18:05,380 --> 00:18:07,830 first, certainly for those of you who have never programmed before, it 378 00:18:07,830 --> 00:18:11,640 literally reduces conceptually to what's otherwise a very 379 00:18:11,640 --> 00:18:14,330 straightforward puzzle piece in Scratch. 380 00:18:14,330 --> 00:18:19,130 >> All right, I promised that there'd be other analogs in Scratch. 381 00:18:19,130 --> 00:18:20,060 There's this one too. 382 00:18:20,060 --> 00:18:21,700 And we saw this briefly last time. 383 00:18:21,700 --> 00:18:23,530 But remember the forever block in Scratch? 384 00:18:23,530 --> 00:18:25,490 It just does something forever? 385 00:18:25,490 --> 00:18:27,470 I claimed that you could implement this in this way. 386 00:18:27,470 --> 00:18:29,740 And you can actually implement this in a bunch of different ways. 387 00:18:29,740 --> 00:18:34,260 >> But the while loop is just a different way in C of expressing yourself. 388 00:18:34,260 --> 00:18:37,080 At the end of the day, you can't do anything with a while loop that you 389 00:18:37,080 --> 00:18:38,360 can't with a for loop. 390 00:18:38,360 --> 00:18:41,430 So they're at the end of the day functionally the same. 391 00:18:41,430 --> 00:18:43,840 But it allows you to express yourself a little differently in 392 00:18:43,840 --> 00:18:44,850 the following way. 393 00:18:44,850 --> 00:18:49,720 >> With the while loop, it's what's in parentheses that's checked again and 394 00:18:49,720 --> 00:18:51,050 again and again. 395 00:18:51,050 --> 00:18:55,100 And as soon as that expression is false, then the loop stops executing, 396 00:18:55,100 --> 00:18:57,890 and the computer proceeds to run whatever other code you 397 00:18:57,890 --> 00:18:59,230 have in your file. 398 00:18:59,230 --> 00:19:02,180 >> But what's interesting here is that I literally typed true. 399 00:19:02,180 --> 00:19:06,680 And true is a type of Boolean value, a true or false value. 400 00:19:06,680 --> 00:19:09,750 So could true ever become false if I've literally 401 00:19:09,750 --> 00:19:11,970 hard-coded it into my program? 402 00:19:11,970 --> 00:19:12,470 So no. 403 00:19:12,470 --> 00:19:13,730 It's a little strange that I did this. 404 00:19:13,730 --> 00:19:15,190 But true is true. 405 00:19:15,190 --> 00:19:16,320 There's no plus-plussing. 406 00:19:16,320 --> 00:19:17,820 There's no use of variables in here. 407 00:19:17,820 --> 00:19:22,740 So because I hard-coded while true, this loop is always going to evaluate 408 00:19:22,740 --> 00:19:24,090 again and again and again. 409 00:19:24,090 --> 00:19:27,660 >> So how many times is hello world going to print on the screen? 410 00:19:27,660 --> 00:19:28,170 Forever. 411 00:19:28,170 --> 00:19:31,980 An infinite number of times until the battery dies or some other external 412 00:19:31,980 --> 00:19:32,730 event happens. 413 00:19:32,730 --> 00:19:35,880 So this is probably not the best program to write, because if the user 414 00:19:35,880 --> 00:19:38,660 can never quit your program, it's probably not what you intended. 415 00:19:38,660 --> 00:19:41,070 >> But sometimes, programs should have an infinite loop. 416 00:19:41,070 --> 00:19:44,050 For instance, if your computer has a clock on it, it certainly would be 417 00:19:44,050 --> 00:19:48,130 nice if your clock it does keep updating itself forever, albeit once a 418 00:19:48,130 --> 00:19:50,500 second or once a minute or something like that. 419 00:19:50,500 --> 00:19:53,690 So even infinite loops do have their place. 420 00:19:53,690 --> 00:19:54,360 >> All right. 421 00:19:54,360 --> 00:19:55,190 Lastly, this one. 422 00:19:55,190 --> 00:19:57,770 And this one is a little bit different functionally, and we'll come back to 423 00:19:57,770 --> 00:19:59,460 this probably in Pset 1. 424 00:19:59,460 --> 00:20:02,370 But there's another type of loop called a do while loop. 425 00:20:02,370 --> 00:20:07,100 And the only difference here between a do while loop and, say, a while loop, 426 00:20:07,100 --> 00:20:11,120 is that the condition is checked not before you execute the code, but after 427 00:20:11,120 --> 00:20:12,080 you execute the code. 428 00:20:12,080 --> 00:20:15,380 So at the top for the while loop, whereas the do while 429 00:20:15,380 --> 00:20:16,560 checks it at the bottom. 430 00:20:16,560 --> 00:20:21,370 >> And this just means that the while loop will typically execute more times 431 00:20:21,370 --> 00:20:24,630 or fewer times than a while loop? 432 00:20:24,630 --> 00:20:30,810 Potentially more times, because a do while loop is clearly saying do this, 433 00:20:30,810 --> 00:20:34,600 only after which point you check if the condition inside of the 434 00:20:34,600 --> 00:20:35,980 parentheses is true. 435 00:20:35,980 --> 00:20:37,940 >> So we'll see this again probably in Pset 1. 436 00:20:37,940 --> 00:20:41,150 If you ever want to do something at least once and then maybe some more 437 00:20:41,150 --> 00:20:43,350 times, this is a good construct to use. 438 00:20:43,350 --> 00:20:46,970 But a while loop, by contrast, will check its condition first. 439 00:20:46,970 --> 00:20:50,660 And that's it for loops in C, do while, while, and for loops. 440 00:20:50,660 --> 00:20:54,700 And again, they map pretty nicely back to Scratch, except this one doesn't 441 00:20:54,700 --> 00:20:57,350 quite have the same analog in Scratch. 442 00:20:57,350 --> 00:20:58,880 >> Well, how about, now, variables? 443 00:20:58,880 --> 00:21:02,600 So this is how I declared a variable syntactically the other day. 444 00:21:02,600 --> 00:21:06,440 I had an int, which I claimed is an integer. 445 00:21:06,440 --> 00:21:09,690 I then had a variable called counter, and then a semicolon. 446 00:21:09,690 --> 00:21:11,070 So what is this line of code doing? 447 00:21:11,070 --> 00:21:14,540 This is, quite simply, declaring a variable-- 448 00:21:14,540 --> 00:21:17,940 that is, asking the computer, give me some memory, give me some bits-- in 449 00:21:17,940 --> 00:21:19,450 which I can store what? 450 00:21:19,450 --> 00:21:20,300 An int. 451 00:21:20,300 --> 00:21:23,570 And the semicolon just means end of line of code. 452 00:21:23,570 --> 00:21:25,500 >> Now, the second line is probably pretty guessable. 453 00:21:25,500 --> 00:21:29,710 What is counter = 0; doing? 454 00:21:29,710 --> 00:21:31,690 It's assigning 0 to counter. 455 00:21:31,690 --> 00:21:33,470 And so this is the other thing that's a little annoying 456 00:21:33,470 --> 00:21:34,540 versus, say, algebra. 457 00:21:34,540 --> 00:21:39,110 Whereas in algebra, the equal sign means equals, here in C, the equal 458 00:21:39,110 --> 00:21:40,470 sign means assignment. 459 00:21:40,470 --> 00:21:45,380 So it means put what's on the right into what's on the left. 460 00:21:45,380 --> 00:21:49,030 And we'll see another symbol, equals equals, when we want to actually test 461 00:21:49,030 --> 00:21:50,570 for quality, somewhat annoyingly. 462 00:21:50,570 --> 00:21:52,590 >> But this is a little inefficient. 463 00:21:52,590 --> 00:21:55,090 It's kind of annoying that I have to declare a variable and 464 00:21:55,090 --> 00:21:56,110 then give it a value. 465 00:21:56,110 --> 00:21:59,380 So C actually lets us simplify this and just do it all at once. 466 00:21:59,380 --> 00:22:01,330 You can declare the variable on the left. 467 00:22:01,330 --> 00:22:04,590 You can still do the assignment on the right by putting that equal sign in 468 00:22:04,590 --> 00:22:05,030 the middle. 469 00:22:05,030 --> 00:22:08,740 So at the end of the day, these two are equivalent, but this one, frankly, 470 00:22:08,740 --> 00:22:11,840 is probably the better habit to get into just because it's a little less 471 00:22:11,840 --> 00:22:15,040 code, it's a little easier to read, and it just tightens up 472 00:22:15,040 --> 00:22:17,470 your code, so to speak. 473 00:22:17,470 --> 00:22:22,120 >> Any questions thus far on the loops, on variables, on conditions, on 474 00:22:22,120 --> 00:22:27,001 Booleans, either technically or conceptually? 475 00:22:27,001 --> 00:22:28,010 All right. 476 00:22:28,010 --> 00:22:30,690 So now this next one gets a little more interesting. 477 00:22:30,690 --> 00:22:34,790 And this is the example I put up last time that just maps a Scratch example 478 00:22:34,790 --> 00:22:35,820 over to C. 479 00:22:35,820 --> 00:22:36,580 >> So a function. 480 00:22:36,580 --> 00:22:38,110 In layman's terms, what's a function? 481 00:22:38,110 --> 00:22:42,900 482 00:22:42,900 --> 00:22:44,350 Got to be bolder with your answer. 483 00:22:44,350 --> 00:22:45,020 What's a function? 484 00:22:45,020 --> 00:22:46,320 >> STUDENT: Does something. 485 00:22:46,320 --> 00:22:46,780 >> DAVID MALAN: What's that? 486 00:22:46,780 --> 00:22:48,000 >> STUDENT: Does something. 487 00:22:48,000 --> 00:22:48,710 >> DAVID MALAN: Does something. 488 00:22:48,710 --> 00:22:49,000 OK. 489 00:22:49,000 --> 00:22:49,590 We'll start there. 490 00:22:49,590 --> 00:22:51,270 So a function does something. 491 00:22:51,270 --> 00:22:55,160 So it's a piece of code that's essentially implemented elsewhere that 492 00:22:55,160 --> 00:22:56,620 indeed does something. 493 00:22:56,620 --> 00:23:00,180 Now, what's interesting about a function is that it can take input, 494 00:23:00,180 --> 00:23:02,710 and it can produce output. 495 00:23:02,710 --> 00:23:05,090 And let's actually take a look at this. 496 00:23:05,090 --> 00:23:09,030 >> So the man behind the curtain was deliberate a moment ago. 497 00:23:09,030 --> 00:23:10,320 Here we have a table. 498 00:23:10,320 --> 00:23:13,010 But suppose that this actually represents a big box. 499 00:23:13,010 --> 00:23:14,940 So this is a so-called black box. 500 00:23:14,940 --> 00:23:19,800 And in general, in design, in computer science, black boxes just refer to 501 00:23:19,800 --> 00:23:22,510 pieces of functionality that someone has implemented, and you don't 502 00:23:22,510 --> 00:23:26,140 necessarily know or care how it's implemented underneath the hood. 503 00:23:26,140 --> 00:23:28,960 You just care that this black box, which we'll now start calling a 504 00:23:28,960 --> 00:23:30,730 function, does something. 505 00:23:30,730 --> 00:23:34,710 >> So, for instance, if this black box on the stage here represents printf, a 506 00:23:34,710 --> 00:23:38,040 function, I know from previous examples that printf takes one or more 507 00:23:38,040 --> 00:23:40,910 arguments, and the first of those arguments should be a string, like 508 00:23:40,910 --> 00:23:44,780 "hello, world." Well, if I am the person writing the program, and I want 509 00:23:44,780 --> 00:23:50,460 to use printf, I, for instance, might take a piece of blank paper here and a 510 00:23:50,460 --> 00:23:55,060 black marker and write on it-- 511 00:23:55,060 --> 00:23:56,580 I misspelled "world"-- 512 00:23:56,580 --> 00:23:59,560 H-E-L-L-O. 513 00:23:59,560 --> 00:24:04,220 >> So I'll take my black marker and I'll write, as big and legibly as I can on 514 00:24:04,220 --> 00:24:07,260 this piece of paper, "hello, world," And now I claim this is my argument. 515 00:24:07,260 --> 00:24:09,740 This is a string represented with a piece of white paper. 516 00:24:09,740 --> 00:24:13,030 And my input now to the function printf is going to be this. 517 00:24:13,030 --> 00:24:18,250 So I am calling printf, passing that argument as input to it. 518 00:24:18,250 --> 00:24:22,110 519 00:24:22,110 --> 00:24:26,740 >> And now, I don't know how the person who wrote printf did it years ago, but 520 00:24:26,740 --> 00:24:31,110 I do know from its documentation that its purpose in life is to print what I 521 00:24:31,110 --> 00:24:34,410 provide to it as input. 522 00:24:34,410 --> 00:24:40,630 And so, even though this implementation is unbeknownst to me 523 00:24:40,630 --> 00:24:44,170 underneath the hood, now I see, oh, it is done. 524 00:24:44,170 --> 00:24:45,740 It has printed something on the screen. 525 00:24:45,740 --> 00:24:49,070 And now control of the program, if there were more lines of code, would 526 00:24:49,070 --> 00:24:51,070 have now returned to me. 527 00:24:51,070 --> 00:24:52,290 And so the fact that Colton-- 528 00:24:52,290 --> 00:24:55,370 bless his heart, is hiding here under a table in front of all of you-- 529 00:24:55,370 --> 00:24:59,530 is deliberate in that I don't know and I don't care how printf is 530 00:24:59,530 --> 00:25:00,100 implemented. 531 00:25:00,100 --> 00:25:03,390 I just know, again, from its documentation, what it does and how I 532 00:25:03,390 --> 00:25:05,040 am supposed to use it. 533 00:25:05,040 --> 00:25:09,140 >> Now, recall that printf could get a little more sophisticated. 534 00:25:09,140 --> 00:25:12,220 Again, we're just talking about the equivalent of Scratch's Say block. 535 00:25:12,220 --> 00:25:14,230 But I also did this last time. 536 00:25:14,230 --> 00:25:17,270 I wanted to make my hello program a little more dynamic and not just hard 537 00:25:17,270 --> 00:25:19,740 code something like "world," and definitely not hard code something 538 00:25:19,740 --> 00:25:22,520 arbitrary like D-A-V-I-D into the program. 539 00:25:22,520 --> 00:25:27,510 I wanted to ask the user for his or her name, and then do something with 540 00:25:27,510 --> 00:25:29,720 that string that they provide. 541 00:25:29,720 --> 00:25:32,690 >> So there's something a little different here. printf, a moment ago, 542 00:25:32,690 --> 00:25:35,860 did, indeed, do something, but it didn't return anything to me. 543 00:25:35,860 --> 00:25:36,020 Right? 544 00:25:36,020 --> 00:25:38,910 Colton did not hand me anything back, no piece of paper. 545 00:25:38,910 --> 00:25:40,320 There was just a side effect. 546 00:25:40,320 --> 00:25:44,510 My providing "hello, world" as an argument to Colton resulted in the 547 00:25:44,510 --> 00:25:48,420 side effect of a word, some words, appearing on the screen. 548 00:25:48,420 --> 00:25:51,350 >> getstring, though, is a little different. getstring is also a 549 00:25:51,350 --> 00:25:54,590 function, but it returns some value. 550 00:25:54,590 --> 00:25:56,370 It doesn't just have an aesthetic side effect. 551 00:25:56,370 --> 00:26:00,230 It actually gives me, the person calling or using the function, 552 00:26:00,230 --> 00:26:01,320 something back. 553 00:26:01,320 --> 00:26:05,740 So in this case, getstring is called with getstring open 554 00:26:05,740 --> 00:26:07,510 paren, close paren. 555 00:26:07,510 --> 00:26:11,370 Does getstring, therefore, take any arguments or input? 556 00:26:11,370 --> 00:26:12,340 No, it seems not. 557 00:26:12,340 --> 00:26:14,460 Its purpose in life is just to get a string. 558 00:26:14,460 --> 00:26:16,910 It needs no more detail than that. 559 00:26:16,910 --> 00:26:20,430 So let me go ahead and pretend that, again, this black box is not printf, 560 00:26:20,430 --> 00:26:25,160 but getstring, and let me, the person writing this program, call or use 561 00:26:25,160 --> 00:26:29,720 getstring by just writing G-E-T-S-T-R-I-N-G, open paren, close 562 00:26:29,720 --> 00:26:32,170 paren, getstring. 563 00:26:32,170 --> 00:26:36,920 >> Now, I have no idea how the CS50 staff implemented getstring, but I know that 564 00:26:36,920 --> 00:26:41,240 if I wait long enough, it will do its thing underneath the hood, maybe using 565 00:26:41,240 --> 00:26:44,940 some variables, maybe using some conditions, maybe using some loops, 566 00:26:44,940 --> 00:26:48,170 maybe using some functions, maybe using-- 567 00:26:48,170 --> 00:26:52,290 just trying to stall-- maybe using some other programming features. 568 00:26:52,290 --> 00:26:55,350 But if I wait long enough-- in reality, in the computer, this happens 569 00:26:55,350 --> 00:26:56,270 super fast-- 570 00:26:56,270 --> 00:26:59,910 if I wait long enough, this function getstring is going to get a string 571 00:26:59,910 --> 00:27:04,060 from the user, who is presumably typing it out on their keyboard, and 572 00:27:04,060 --> 00:27:08,090 then, when you getstring is done getting those characters from the user 573 00:27:08,090 --> 00:27:14,080 and storing them into a string, that function, getstring, is going to have 574 00:27:14,080 --> 00:27:17,990 ready for me some output which I am going to retrieve by way of the 575 00:27:17,990 --> 00:27:19,470 assignment operator. 576 00:27:19,470 --> 00:27:25,390 >> And if I go, indeed, into the output here, Obosi has generously 577 00:27:25,390 --> 00:27:29,900 participated, without knowing this in advance, by writing his name onto this 578 00:27:29,900 --> 00:27:33,100 variable, which is representing a string. 579 00:27:33,100 --> 00:27:35,640 Now, the assignment operator means, even though this might feel a little 580 00:27:35,640 --> 00:27:38,790 redundant, I actually do need to make my own copy of this. 581 00:27:38,790 --> 00:27:40,700 Because on the left hand side-- whoops!-- 582 00:27:40,700 --> 00:27:45,760 notice that I essentially have string name on the left hand side. 583 00:27:45,760 --> 00:27:48,280 So I'm also going to make my own copy of this. 584 00:27:48,280 --> 00:27:50,990 And this is a bit of a white lie, because we'll see in a week or two's 585 00:27:50,990 --> 00:27:54,100 time that strings are not actually what they appear to be. 586 00:27:54,100 --> 00:27:56,700 >> But for now, here is the return value. 587 00:27:56,700 --> 00:28:00,160 Here is my own copy of it after using the assignment operator. 588 00:28:00,160 --> 00:28:01,790 And now, what do I want to do next? 589 00:28:01,790 --> 00:28:04,080 We now have the second of two lines of code. 590 00:28:04,080 --> 00:28:05,640 So I now want to call printf. 591 00:28:05,640 --> 00:28:08,500 Let's pretend now that the black box is back again to being 592 00:28:08,500 --> 00:28:10,210 printf and not getstring. 593 00:28:10,210 --> 00:28:12,200 >> Printf this time is going to take how many arguments? 594 00:28:12,200 --> 00:28:14,890 595 00:28:14,890 --> 00:28:16,770 So look, it's like two. 596 00:28:16,770 --> 00:28:18,860 There's two commas in there, but one of those commas, it's 597 00:28:18,860 --> 00:28:20,220 inside of the quotes. 598 00:28:20,220 --> 00:28:23,190 So the first argument is literally going to be this. 599 00:28:23,190 --> 00:28:31,850 H-E-L-L-O, comma, percent s, backslash-n. 600 00:28:31,850 --> 00:28:36,560 And now I'm providing not one argument, but two arguments to printf. 601 00:28:36,560 --> 00:28:39,530 >> And what's printf supposed to do with these two inputs after I pass them in 602 00:28:39,530 --> 00:28:42,050 as arguments? 603 00:28:42,050 --> 00:28:45,360 It's supposed to take the second of them, which I called name, so the 604 00:28:45,360 --> 00:28:48,660 second piece of paper I wrote out a moment ago is called name. 605 00:28:48,660 --> 00:28:53,550 It's going to plug of the value inside of that variable into the placeholder, 606 00:28:53,550 --> 00:28:58,310 %s, so that, again, in just a moment's time, we will see a side effect of 607 00:28:58,310 --> 00:29:04,180 having called printf, whereby now we see not "hello, world," but "hello, 608 00:29:04,180 --> 00:29:04,710 Obosi." 609 00:29:04,710 --> 00:29:08,730 >> So a big round of applause to both of our volunteers, only one of whom knew 610 00:29:08,730 --> 00:29:12,010 this would be happening. 611 00:29:12,010 --> 00:29:12,990 All right. 612 00:29:12,990 --> 00:29:16,480 So, perhaps, simple as that was, certainly, if you're already familiar 613 00:29:16,480 --> 00:29:20,190 with such, hopefully you will never forget that particular visual of how 614 00:29:20,190 --> 00:29:21,220 functions work. 615 00:29:21,220 --> 00:29:23,000 >> So there's more than just getstring. 616 00:29:23,000 --> 00:29:26,020 Inside of the CS50 library, there's a whole bunch of functions, all of which 617 00:29:26,020 --> 00:29:29,510 are capitalized just to make clear that we wrote these functions. 618 00:29:29,510 --> 00:29:33,610 Typically, in C, almost every other function you'll use is lowercase. 619 00:29:33,610 --> 00:29:36,190 But we deliberately use capital letters just to make clear that these 620 00:29:36,190 --> 00:29:39,880 are training wheels of sorts that we'll use for just a few weeks that 621 00:29:39,880 --> 00:29:42,890 simplify the process of getting input from the user. 622 00:29:42,890 --> 00:29:46,740 >> The CS50 library does not do anything that you yourselves could not do by 623 00:29:46,740 --> 00:29:50,230 just using C code from an oldschool textbook. 624 00:29:50,230 --> 00:29:53,310 But, again, we use them as training wheels of sorts for just a couple of 625 00:29:53,310 --> 00:29:57,250 weeks so that we can get rid of the complexity that you'll soon understand 626 00:29:57,250 --> 00:30:00,460 yourself of doing something as relatively straightforward as getting 627 00:30:00,460 --> 00:30:02,080 input from a user. 628 00:30:02,080 --> 00:30:05,540 >> So know that you'll have access to GetChar, GetDouble-- 629 00:30:05,540 --> 00:30:06,050 double. 630 00:30:06,050 --> 00:30:07,050 What's-- and then GetFloat. 631 00:30:07,050 --> 00:30:07,600 What's a float? 632 00:30:07,600 --> 00:30:09,382 Let's start there. 633 00:30:09,382 --> 00:30:10,600 >> STUDENT: [INAUDIBLE]. 634 00:30:10,600 --> 00:30:10,790 >> DAVID MALAN: Yeah. 635 00:30:10,790 --> 00:30:12,120 It's a number with a decimal point. 636 00:30:12,120 --> 00:30:15,930 So whereas an int is an integer, which is just a number with 0 through 9, 637 00:30:15,930 --> 00:30:18,940 some number of repetitions thereof, a float is something 638 00:30:18,940 --> 00:30:20,100 with a decimal point. 639 00:30:20,100 --> 00:30:26,090 And a double, meanwhile, is also a number with a decimal point, but more 640 00:30:26,090 --> 00:30:28,160 numbers after the decimal point, potentially. 641 00:30:28,160 --> 00:30:30,040 >> So we'll get back to this before long. 642 00:30:30,040 --> 00:30:34,560 But typically, each of these types of data, each of these types of variables 643 00:30:34,560 --> 00:30:39,380 that a library like ours can return to you, use different numbers of bits to 644 00:30:39,380 --> 00:30:40,290 store information. 645 00:30:40,290 --> 00:30:43,910 Typically, a Char, which just means one character, uses 8 bits. 646 00:30:43,910 --> 00:30:47,490 And that's actually consistent with our byte of volunteers last week who 647 00:30:47,490 --> 00:30:50,410 came up and represented one Ascii character at a time. 648 00:30:50,410 --> 00:30:51,850 So a Char is 8 bits. 649 00:30:51,850 --> 00:30:54,430 >> A float happens to be 32 bits, typically. 650 00:30:54,430 --> 00:30:59,230 And a double, as you might guess, is actually 64 bits, which is just more, 651 00:30:59,230 --> 00:31:01,360 which means you can have bigger numbers or more precision. 652 00:31:01,360 --> 00:31:03,000 But again, more on that another time. 653 00:31:03,000 --> 00:31:06,550 GetLongLong, meanwhile, though stupidly named, is really just a 654 00:31:06,550 --> 00:31:10,770 integer that's twice as big, twice as long, potentially, as a regular 655 00:31:10,770 --> 00:31:12,940 integer, 64 bits instead of 32. 656 00:31:12,940 --> 00:31:14,560 And GetString we've been using. 657 00:31:14,560 --> 00:31:18,870 >> But it turns out, in the CS50 library, which is implemented, as we'll see, in 658 00:31:18,870 --> 00:31:23,560 the form of the two files, one of which is called cs50.h, has two other 659 00:31:23,560 --> 00:31:24,770 data types in it. 660 00:31:24,770 --> 00:31:29,700 Booleans do not exist in C. You can simulate them by just using 0s and 1s 661 00:31:29,700 --> 00:31:30,850 throughout your programs. 662 00:31:30,850 --> 00:31:35,500 But we in the CS50 library have created the symbols "true" and "false" 663 00:31:35,500 --> 00:31:38,580 to represent 1 and 0 so you don't have to hard code something 664 00:31:38,580 --> 00:31:39,810 literally like 1 and 0. 665 00:31:39,810 --> 00:31:40,980 But we'll see those again. 666 00:31:40,980 --> 00:31:42,330 >> String, too, does not exist. 667 00:31:42,330 --> 00:31:44,520 And that's why I mentioned it's a bit of a white lie for now. 668 00:31:44,520 --> 00:31:46,660 But we'll peel back that layer before long. 669 00:31:46,660 --> 00:31:49,540 But for now, a string is a sequence of characters. 670 00:31:49,540 --> 00:31:51,790 >> Meanwhile, in C, you indeed have different data types. 671 00:31:51,790 --> 00:31:53,200 Notice these are lowercase. 672 00:31:53,200 --> 00:31:56,960 So the functions you saw a moment ago are functions written by CS50 that 673 00:31:56,960 --> 00:32:02,310 will return to you a value that falls into one of these categories. 674 00:32:02,310 --> 00:32:06,730 >> A couple cheat sheets just to plant the seed here. printf doesn't just 675 00:32:06,730 --> 00:32:08,600 take %s as placeholders. 676 00:32:08,600 --> 00:32:13,490 It takes %d for decimal integers, or %i would work as well. 677 00:32:13,490 --> 00:32:19,450 %f is floating point values. %c is for a char, if you want to just plug one 678 00:32:19,450 --> 00:32:23,510 character into a preformatted string like we've been doing, you can use %c. 679 00:32:23,510 --> 00:32:28,490 >> And then, somewhat annoyingly, %lld is for a long, long decimal integer, 680 00:32:28,490 --> 00:32:31,050 which just means if you need a really big number and you're using something 681 00:32:31,050 --> 00:32:35,450 called a long long, which we'll come back to in a pset, you can use %lld to 682 00:32:35,450 --> 00:32:40,700 tell printf, plug in a really big integer here by way of its second or 683 00:32:40,700 --> 00:32:41,830 some other argument. 684 00:32:41,830 --> 00:32:45,700 >> And lastly, I promised that there's a few other escape 685 00:32:45,700 --> 00:32:47,670 sequences that printf supports. 686 00:32:47,670 --> 00:32:49,160 We've seen backslash-n. 687 00:32:49,160 --> 00:32:50,510 Backslash-r you may see. 688 00:32:50,510 --> 00:32:51,780 It's sort of an old-school thing. 689 00:32:51,780 --> 00:32:55,920 If you ever used a real typewriter years ago and you pulled on the crank 690 00:32:55,920 --> 00:32:59,810 which not only rotated the wheel to move the lineup, it also moved the 691 00:32:59,810 --> 00:33:03,730 whole thing all the way back over to the left, well, backslash-r just 692 00:33:03,730 --> 00:33:06,480 essentially moves your cursor back to the start of the line 693 00:33:06,480 --> 00:33:07,830 without moving it down. 694 00:33:07,830 --> 00:33:09,780 But again, more on that, perhaps, in the future. 695 00:33:09,780 --> 00:33:13,670 >> Backslash-quote, backslash-double quote, backslash-backslash is the 696 00:33:13,670 --> 00:33:16,600 solution to the tiny little riddle I alluded to earlier. 697 00:33:16,600 --> 00:33:18,920 And backslash-0 is actually quite interesting. 698 00:33:18,920 --> 00:33:21,470 But we'll come back to that before long. 699 00:33:21,470 --> 00:33:25,640 >> So let me go into, now, the CS50 appliance, and let's actually do a 700 00:33:25,640 --> 00:33:28,930 quick warm up with one of the examples we did already and then move on to 701 00:33:28,930 --> 00:33:31,160 something a little more complex. 702 00:33:31,160 --> 00:33:34,980 So if I open up my program called gedit-- 703 00:33:34,980 --> 00:33:36,840 this is my graphical editor. 704 00:33:36,840 --> 00:33:38,460 And I can do that-- 705 00:33:38,460 --> 00:33:39,820 let me close that window there-- 706 00:33:39,820 --> 00:33:42,720 by way of this icon down here, gedit, in the bottom-left 707 00:33:42,720 --> 00:33:44,170 corner next to the menu. 708 00:33:44,170 --> 00:33:48,670 >> I'm going to go ahead and advance, and save this example into, say, John 709 00:33:48,670 --> 00:33:49,660 Harvard's folder. 710 00:33:49,660 --> 00:33:52,690 John Harvard's folder is just his home directory where all of his files live 711 00:33:52,690 --> 00:33:53,340 by default. 712 00:33:53,340 --> 00:33:58,410 And I'm going to save this as a file called hello-0.c. 713 00:33:58,410 --> 00:34:01,260 And I've chosen this name just so it lines up with the sample code on the 714 00:34:01,260 --> 00:34:04,210 course's website and in the YouTube videos online. 715 00:34:04,210 --> 00:34:06,320 >> So now I'm going to begin to write my first program. 716 00:34:06,320 --> 00:34:08,469 Let me zoom in for legibility. 717 00:34:08,469 --> 00:34:12,760 And I'm going to go ahead and say int main void, which is just like that 718 00:34:12,760 --> 00:34:14,900 yellow puzzle piece that starts a program. 719 00:34:14,900 --> 00:34:18,290 I have gotten into the habit over the years of opening my curly brace, then 720 00:34:18,290 --> 00:34:22,000 also closing it and then going back to where I want to put my code, just 721 00:34:22,000 --> 00:34:24,630 because it helps me keep everything balanced, especially as my 722 00:34:24,630 --> 00:34:25,880 program gets long. 723 00:34:25,880 --> 00:34:29,239 >> And now, in here, I'm going to go ahead and say, printf, quote unquote, 724 00:34:29,239 --> 00:34:34,330 hello world, backslash-n, close quote, close parenthesis, semicolon. 725 00:34:34,330 --> 00:34:38,100 So I'm just repeating everything we've been taking for granted thus far. 726 00:34:38,100 --> 00:34:39,270 Now I'm going to zoom out. 727 00:34:39,270 --> 00:34:43,030 >> And this terminal window down here, what am I allowed to do in this black 728 00:34:43,030 --> 00:34:44,389 and white window? 729 00:34:44,389 --> 00:34:46,977 What can I use it for? 730 00:34:46,977 --> 00:34:49,770 So this is where I run commands and where I can compile things. 731 00:34:49,770 --> 00:34:50,620 And I'm going to keep it simple. 732 00:34:50,620 --> 00:34:52,780 I'm going to use a program called Make, which isn't 733 00:34:52,780 --> 00:34:54,020 technically a compiler. 734 00:34:54,020 --> 00:34:56,360 The compiler's called Clang, but we'll come back to that in a 735 00:34:56,360 --> 00:34:57,190 week or two's time. 736 00:34:57,190 --> 00:35:03,410 >> For now, I'm just going to type make hello-0, But those of you who were 737 00:35:03,410 --> 00:35:07,050 comparing in your minds what I just typed to what I should have typed may 738 00:35:07,050 --> 00:35:10,180 know already that I did something wrong here. 739 00:35:10,180 --> 00:35:12,160 Now, there's clearly some errors. 740 00:35:12,160 --> 00:35:16,742 Before I even look at what they are, any thoughts as to what I did wrong? 741 00:35:16,742 --> 00:35:18,590 >> STUDENTS: [INTERPOSING VOICES]. 742 00:35:18,590 --> 00:35:18,840 >> DAVID MALAN: Yeah. 743 00:35:18,840 --> 00:35:20,640 I'm missing the library's header file. 744 00:35:20,640 --> 00:35:24,240 Any of these .h files are called header files, and they collectively 745 00:35:24,240 --> 00:35:25,680 belong to things called libraries. 746 00:35:25,680 --> 00:35:28,030 Libraries are just chunks of code that other people wrote. 747 00:35:28,030 --> 00:35:32,140 So the standard [? i ?] library is a collection of files containing code 748 00:35:32,140 --> 00:35:33,330 that other people wrote. 749 00:35:33,330 --> 00:35:34,820 >> So I'm missing that. 750 00:35:34,820 --> 00:35:36,520 So why am I getting an error? 751 00:35:36,520 --> 00:35:40,840 Well, let me scroll back up in my terminal window here. 752 00:35:40,840 --> 00:35:44,310 And unfortunately, in C, as in a lot of programming languages, especially 753 00:35:44,310 --> 00:35:47,830 if it's all new to you, the error messages are precise, but they're also 754 00:35:47,830 --> 00:35:48,620 quite cryptic. 755 00:35:48,620 --> 00:35:51,720 And the error here, in red, is "implicitly declaring library function 756 00:35:51,720 --> 00:35:57,660 printf with type," and then it scrolls to the next line, "int const char * , 757 00:35:57,660 --> 00:36:00,220 ..." It just gets really overwhelming quickly. 758 00:36:00,220 --> 00:36:04,420 >> But what you should begin to do, if, again, new to all of this, is just to 759 00:36:04,420 --> 00:36:06,010 begin to look for keywords. 760 00:36:06,010 --> 00:36:08,770 Clearly, I might not understand half of the words I'm seeing just yet. 761 00:36:08,770 --> 00:36:10,140 You will, though, in a week's time. 762 00:36:10,140 --> 00:36:11,230 But I see printf. 763 00:36:11,230 --> 00:36:14,310 And that should begin, before long, to jog your memory, all right, printf. 764 00:36:14,310 --> 00:36:15,210 Something's wrong with printf. 765 00:36:15,210 --> 00:36:16,580 Did I spell it wrong? 766 00:36:16,580 --> 00:36:18,130 No, it doesn't look like-- oh. 767 00:36:18,130 --> 00:36:21,350 I can't use it unless I teach the compiler that it exists. 768 00:36:21,350 --> 00:36:25,220 And so, again, go with your instincts, even if you don't actually understand 769 00:36:25,220 --> 00:36:26,510 the actual error messages. 770 00:36:26,510 --> 00:36:30,240 >> And indeed, the solution here is to just include it at the top of the file 771 00:36:30,240 --> 00:36:34,340 like that, resaving my file with Control-S or the File menu. 772 00:36:34,340 --> 00:36:36,730 And if I now go back down here, I'm going to clear this. 773 00:36:36,730 --> 00:36:39,150 Control-L is just a nice way to clear the screen. 774 00:36:39,150 --> 00:36:44,870 And then I'm going to type "make hello 0" Enter, and now I still see a 775 00:36:44,870 --> 00:36:47,710 cryptic sequence of symbols, but we'll come back to that. 776 00:36:47,710 --> 00:36:49,230 That's what Make is doing for you. 777 00:36:49,230 --> 00:36:52,590 It's automating the process of taking a fairly annoying command involving 778 00:36:52,590 --> 00:36:54,050 Clang, the actual compiler. 779 00:36:54,050 --> 00:36:57,460 But the fact that I got no errors means that this thing should work. 780 00:36:57,460 --> 00:37:00,630 >> So I'm now going to do-- let me zoom in again-- 781 00:37:00,630 --> 00:37:07,070 ./hello-0 Enter, and indeed, I see "hello, world." So let's now enhance 782 00:37:07,070 --> 00:37:12,105 this ever so slightly just to replicate the steps we intended. 783 00:37:12,105 --> 00:37:15,370 I'm going to rename this with Save As to hello1.c. 784 00:37:15,370 --> 00:37:20,300 And now I'm going to declare a variable called name, so string name, 785 00:37:20,300 --> 00:37:23,420 and I'm going to put in it the value of quote-unquote 786 00:37:23,420 --> 00:37:27,030 D-A-V-I-D close-quote semicolon. 787 00:37:27,030 --> 00:37:31,150 And now I'm going to replace "world" with what placeholder for a string? 788 00:37:31,150 --> 00:37:32,200 %s. 789 00:37:32,200 --> 00:37:35,040 >> And now, how many arguments should printf take this time? 790 00:37:35,040 --> 00:37:35,700 So two. 791 00:37:35,700 --> 00:37:37,090 So I go outside of the quotes. 792 00:37:37,090 --> 00:37:39,330 I type "name" after a comma. 793 00:37:39,330 --> 00:37:41,550 But I've done something else wrong this time. 794 00:37:41,550 --> 00:37:43,940 But let's assume that I didn't realize that yet. 795 00:37:43,940 --> 00:37:44,910 Let me go up here. 796 00:37:44,910 --> 00:37:48,850 >> And notice, too, I'm getting a little bored of typing "make hello 0" all the 797 00:37:48,850 --> 00:37:50,180 time, "make" and all this. 798 00:37:50,180 --> 00:37:53,270 So, it turns out, in Linus you can often hit the Up arrow on your 799 00:37:53,270 --> 00:37:56,850 keyboard, and you can actually scroll through all of the commands that I've 800 00:37:56,850 --> 00:37:58,830 executed previous to just now. 801 00:37:58,830 --> 00:38:02,040 So if I do that, Up, Up, there's make hello 0. 802 00:38:02,040 --> 00:38:02,610 I don't want that. 803 00:38:02,610 --> 00:38:05,450 I just want to change that to hello 1 this time, Enter. 804 00:38:05,450 --> 00:38:07,620 Eventually, that will save you some time. 805 00:38:07,620 --> 00:38:08,150 >> All right. 806 00:38:08,150 --> 00:38:09,520 Unfortunately, there's is an error. 807 00:38:09,520 --> 00:38:10,980 So let me scroll up. 808 00:38:10,980 --> 00:38:13,120 This looks like I really butchered this program. 809 00:38:13,120 --> 00:38:16,530 I mean, my god, it's two lines of code, and its 10 lines of errors. 810 00:38:16,530 --> 00:38:18,800 But look at the top one first. 811 00:38:18,800 --> 00:38:21,640 Use of undeclared identifier string. 812 00:38:21,640 --> 00:38:23,270 Did I mean standard i n? 813 00:38:23,270 --> 00:38:23,930 No I didn't. 814 00:38:23,930 --> 00:38:24,940 I meant string. 815 00:38:24,940 --> 00:38:29,390 >> But where is the string variable type declared, dd we say? 816 00:38:29,390 --> 00:38:30,730 So it's in the CS50 the library. 817 00:38:30,730 --> 00:38:33,740 So it doesn't suffice, in these first couple of weeks, just to use 818 00:38:33,740 --> 00:38:34,860 what C gives us. 819 00:38:34,860 --> 00:38:38,120 I'm also going to go up here, and I could put it above or below, but I'll 820 00:38:38,120 --> 00:38:40,340 just keep it alphabetical to keep things orderly. 821 00:38:40,340 --> 00:38:43,470 I'm going to include cs50.h, which is pre-installed for 822 00:38:43,470 --> 00:38:44,900 you on the CS50 appliance. 823 00:38:44,900 --> 00:38:47,640 And it's open source, so even people on the internet can use it on their 824 00:38:47,640 --> 00:38:48,450 own computers. 825 00:38:48,450 --> 00:38:50,700 But it comes with the CS50 appliance. 826 00:38:50,700 --> 00:38:55,320 >> So now let me go back and recompile this with make hello 1. 827 00:38:55,320 --> 00:38:55,710 Damn it. 828 00:38:55,710 --> 00:38:57,240 Still another error. 829 00:38:57,240 --> 00:38:59,370 Let me scroll up to the first, though. 830 00:38:59,370 --> 00:39:00,630 This one's a little complex. 831 00:39:00,630 --> 00:39:03,830 Multi-character character constant. 832 00:39:03,830 --> 00:39:04,890 That's not helping me. 833 00:39:04,890 --> 00:39:08,220 But notice, Clang is at least a little bit decent, whereby with a little 834 00:39:08,220 --> 00:39:11,890 green caret symbol, it's saying here is where I screwed up. 835 00:39:11,890 --> 00:39:16,160 Why is it pointing, with that little green arrow, to the single quote next 836 00:39:16,160 --> 00:39:18,290 to my name? 837 00:39:18,290 --> 00:39:20,880 >> So this is one of the things you just get used to, especially if you've been 838 00:39:20,880 --> 00:39:23,980 programming in Python, or JavaScript, or other languages where this detail 839 00:39:23,980 --> 00:39:24,560 doesn't matter. 840 00:39:24,560 --> 00:39:25,740 In C, it does matter. 841 00:39:25,740 --> 00:39:29,520 If you are declaring a string, which is a sequence of 0 or more characters, 842 00:39:29,520 --> 00:39:32,280 you actually must use double quotes. 843 00:39:32,280 --> 00:39:36,670 So I actually need to change this back to open quote, close quote, with 844 00:39:36,670 --> 00:39:37,800 double quotes. 845 00:39:37,800 --> 00:39:41,610 Single quotes do have their place, but only when you're using individual 846 00:39:41,610 --> 00:39:44,100 chars, but more on that another time. 847 00:39:44,100 --> 00:39:46,550 For now, the double quotes are necessary. 848 00:39:46,550 --> 00:39:50,460 >> So now, let me go back to my terminal window, make hello 1. 849 00:39:50,460 --> 00:39:51,450 And who's confident? 850 00:39:51,450 --> 00:39:53,800 Is this program now going to compile correctly? 851 00:39:53,800 --> 00:39:58,250 852 00:39:58,250 --> 00:39:58,540 OK. 853 00:39:58,540 --> 00:39:59,780 So three of us think this. 854 00:39:59,780 --> 00:40:00,280 All right. 855 00:40:00,280 --> 00:40:01,190 Enter. 856 00:40:01,190 --> 00:40:02,440 And it actually did. 857 00:40:02,440 --> 00:40:05,130 So there's no errors this time, even though this program's gotten a bit 858 00:40:05,130 --> 00:40:05,840 more complex. 859 00:40:05,840 --> 00:40:10,110 If I now do dot slash hello 1, Enter, it's going to say "hello, David." 860 00:40:10,110 --> 00:40:11,750 >> But let's do the third iteration of this, where the 861 00:40:11,750 --> 00:40:13,380 program is truly dynamic. 862 00:40:13,380 --> 00:40:16,770 Let me go ahead and change the file name, just for consistency with the 863 00:40:16,770 --> 00:40:20,410 files you'll have available to you afterward online. 864 00:40:20,410 --> 00:40:21,620 Enter. 865 00:40:21,620 --> 00:40:25,510 And now I'm going to go in and not store "David" hard coded here. 866 00:40:25,510 --> 00:40:28,826 What could I do to very simply improve this program? 867 00:40:28,826 --> 00:40:30,520 >> I could call getstring. 868 00:40:30,520 --> 00:40:33,240 It might be a little non-obvious what's about to happen, so I'm going 869 00:40:33,240 --> 00:40:38,470 to actually add another line, printf, and say name, colon, close quote, just 870 00:40:38,470 --> 00:40:40,790 to give the user a prompt on the screen. 871 00:40:40,790 --> 00:40:42,980 And now I'm going to go here, and I'm going to use my keyboard shortcut. 872 00:40:42,980 --> 00:40:47,680 I'm going to go Up, Up, and change hello 1 to hello 2, Enter. 873 00:40:47,680 --> 00:40:49,260 And thankfully, I'm making progress. 874 00:40:49,260 --> 00:40:52,720 >> And now I'm going to go up to dot slash hello and 875 00:40:52,720 --> 00:40:54,690 change that to 2, Enter. 876 00:40:54,690 --> 00:40:55,650 And now my program-- 877 00:40:55,650 --> 00:40:57,700 I'll zoom in-- is getting a little prettier. 878 00:40:57,700 --> 00:41:02,190 Name is going to be, let's say Rob this time, Enter, hello, Rob. 879 00:41:02,190 --> 00:41:03,260 We can do it again. 880 00:41:03,260 --> 00:41:05,360 Name, Lauren, Enter. 881 00:41:05,360 --> 00:41:07,820 Name, Joseph, Enter. 882 00:41:07,820 --> 00:41:11,596 Name, let's try to be difficult, Enter. 883 00:41:11,596 --> 00:41:12,410 Eh. 884 00:41:12,410 --> 00:41:14,680 It's not really a bug. 885 00:41:14,680 --> 00:41:16,090 So it's just a little ugly. 886 00:41:16,090 --> 00:41:18,640 So maybe we could solve this in the future, not now. 887 00:41:18,640 --> 00:41:20,840 >> But how would you instinctively go about addressing 888 00:41:20,840 --> 00:41:21,990 that particular challenge? 889 00:41:21,990 --> 00:41:23,710 It just looks stupid. 890 00:41:23,710 --> 00:41:27,320 How do you avoid things looking stupid? 891 00:41:27,320 --> 00:41:29,890 So we could do, OK, I heard a couple of things, a condition and a loop. 892 00:41:29,890 --> 00:41:33,340 We could use, one, a condition, to check what is the length of the string 893 00:41:33,340 --> 00:41:34,190 the user gave us? 894 00:41:34,190 --> 00:41:37,100 And if it's 0, it's just quote unquote, they just hit Enter, then 895 00:41:37,100 --> 00:41:38,930 maybe I should yell at them and prompt them again. 896 00:41:38,930 --> 00:41:39,970 But how do a prompt them again? 897 00:41:39,970 --> 00:41:44,230 Well, I also heard loop, and I could do that again and again and again 898 00:41:44,230 --> 00:41:47,010 prompting the user for the same thing. 899 00:41:47,010 --> 00:41:48,880 >> Well, let's do one other example using a different 900 00:41:48,880 --> 00:41:50,620 function in the CS50 library. 901 00:41:50,620 --> 00:41:52,330 Let me close this file. 902 00:41:52,330 --> 00:41:53,510 Let me create a new one. 903 00:41:53,510 --> 00:41:58,510 And I'll call it adder.c, just because it's easy to do simple arithmetic, 904 00:41:58,510 --> 00:42:00,890 even though this will be completely underwhelming with what you could do 905 00:42:00,890 --> 00:42:02,250 with any modern computer. 906 00:42:02,250 --> 00:42:03,750 >> But let me go ahead now and-- 907 00:42:03,750 --> 00:42:10,680 I learned my lesson last time-- include cs50.h, include stdio.h, int 908 00:42:10,680 --> 00:42:14,170 main void, which, for now, I'll just take on blind faith, but we'll assume 909 00:42:14,170 --> 00:42:16,380 that will understand what that means before long. 910 00:42:16,380 --> 00:42:21,030 And I'm going to say something like "give me an int." 911 00:42:21,030 --> 00:42:22,140 >> And now, how do I get an int? 912 00:42:22,140 --> 00:42:26,820 I want to ultimately declare a variable called x of type int and 913 00:42:26,820 --> 00:42:29,240 store in it an integer from the user. 914 00:42:29,240 --> 00:42:29,970 So that was a mouthful. 915 00:42:29,970 --> 00:42:35,680 But if someone wants to propose, how do I declare an integer called x? 916 00:42:35,680 --> 00:42:36,310 Int x. 917 00:42:36,310 --> 00:42:37,430 >> So it really is that simple. 918 00:42:37,430 --> 00:42:38,240 Give me an int. 919 00:42:38,240 --> 00:42:39,070 Call it x. 920 00:42:39,070 --> 00:42:40,580 Now I use the assignment operator. 921 00:42:40,580 --> 00:42:43,400 And how do I store from the left a value of the user? 922 00:42:43,400 --> 00:42:47,310 I don't want to call getstring, obviously, but rather getint. 923 00:42:47,310 --> 00:42:49,060 Any arguments? 924 00:42:49,060 --> 00:42:49,350 No. 925 00:42:49,350 --> 00:42:51,870 So it's open paren, close paren immediately, and then a 926 00:42:51,870 --> 00:42:53,440 semicolon ends the line. 927 00:42:53,440 --> 00:42:54,930 >> Now let me do this again. 928 00:42:54,930 --> 00:42:57,070 Give me another int. 929 00:42:57,070 --> 00:43:01,850 This time I'll do int, let's call it y, equals getint. 930 00:43:01,850 --> 00:43:05,060 And now let me do something super-simple like some math. 931 00:43:05,060 --> 00:43:14,340 So printf, the sum of %d is the placeholder for an int. 932 00:43:14,340 --> 00:43:20,030 And %d, period, backslash-n. 933 00:43:20,030 --> 00:43:20,360 >> All right. 934 00:43:20,360 --> 00:43:21,670 So that's not actually math. 935 00:43:21,670 --> 00:43:25,500 But if I want to say the sum of this value plus this value equals this 936 00:43:25,500 --> 00:43:29,320 other value, how many arguments into total should I be giving printf, 937 00:43:29,320 --> 00:43:30,520 ultimately? 938 00:43:30,520 --> 00:43:31,420 So four, right? 939 00:43:31,420 --> 00:43:33,790 This string and then the three values. 940 00:43:33,790 --> 00:43:37,560 So x is the first I want to be plugged in first to the %d. 941 00:43:37,560 --> 00:43:39,270 y is going to be the next. 942 00:43:39,270 --> 00:43:42,030 >> And now I kind of want to say z, but z doesn't exist. 943 00:43:42,030 --> 00:43:43,190 But that's not a big deal. 944 00:43:43,190 --> 00:43:45,440 Because what would you do instinctively, especially if you had a 945 00:43:45,440 --> 00:43:47,820 graphing calculator, what do you type? 946 00:43:47,820 --> 00:43:48,760 How about x plus y? 947 00:43:48,760 --> 00:43:50,200 So it's not an actual variable. 948 00:43:50,200 --> 00:43:51,820 It's just the sum of two other variables. 949 00:43:51,820 --> 00:43:53,170 And that's perfectly legitimate. 950 00:43:53,170 --> 00:43:56,630 C certainly understands simple arithmetic like this. 951 00:43:56,630 --> 00:43:58,450 >> Semicolon, save. 952 00:43:58,450 --> 00:44:02,080 Now let me go down here and type make adder, Enter. 953 00:44:02,080 --> 00:44:04,100 No error, so that's progress too. 954 00:44:04,100 --> 00:44:04,890 Type adder. 955 00:44:04,890 --> 00:44:07,100 And another keyboard shortcut, if you start to get bored 956 00:44:07,100 --> 00:44:08,760 with all the commands-- 957 00:44:08,760 --> 00:44:12,650 if you start typing a command, like dot slash ad, and that's where your 958 00:44:12,650 --> 00:44:16,020 boredom kicks in, you can usually hit Tab to have the computer finish the 959 00:44:16,020 --> 00:44:20,510 rest of the phrase for you if it's not ambiguous what should come after ad. 960 00:44:20,510 --> 00:44:23,950 >> So let me go ahead now and you click Enter. 961 00:44:23,950 --> 00:44:27,490 Give me an int, 1, 2, and thankfully, it's 3. 962 00:44:27,490 --> 00:44:29,900 But as always, testing programs shouldn't reduce to 963 00:44:29,900 --> 00:44:30,820 just trying it once. 964 00:44:30,820 --> 00:44:35,560 Let me try a corner case, like negative 1, give me 1, and that one 965 00:44:35,560 --> 00:44:36,210 checks out as well. 966 00:44:36,210 --> 00:44:38,870 And I probably want to do some more rigorous testing, but I'm pretty 967 00:44:38,870 --> 00:44:40,630 comfortable with where that's at. 968 00:44:40,630 --> 00:44:43,110 >> Well, now let's try another program that this time uses a 969 00:44:43,110 --> 00:44:44,620 bit of other syntax. 970 00:44:44,620 --> 00:44:46,100 Let me create a new file. 971 00:44:46,100 --> 00:44:51,050 I'll call this conditions0.c to line up with some sample code online. 972 00:44:51,050 --> 00:44:55,550 And let me go ahead and do include cs50.h, include 973 00:44:55,550 --> 00:45:00,320 stdio.h, in main void-- 974 00:45:00,320 --> 00:45:01,030 OK. 975 00:45:01,030 --> 00:45:01,850 There we go. 976 00:45:01,850 --> 00:45:03,010 We have our standard boilerplate. 977 00:45:03,010 --> 00:45:08,170 >> And this time I'm going to say printf, "I'd like an int, please," just to 978 00:45:08,170 --> 00:45:10,030 make the prompt a little more friendly. 979 00:45:10,030 --> 00:45:11,620 And now I want to get an int from the user. 980 00:45:11,620 --> 00:45:15,010 I'm going to call it n this time, just because n sounds like number. 981 00:45:15,010 --> 00:45:18,140 getint, and now, what do I want to do with it? 982 00:45:18,140 --> 00:45:21,640 Well, if n is-- and I'll zoom in-- 983 00:45:21,640 --> 00:45:25,930 if n is greater than 0, I want to do the following-- 984 00:45:25,930 --> 00:45:36,060 printf "You picked a positive number." Else, I'm going to type printf "You 985 00:45:36,060 --> 00:45:37,870 picked a negative 986 00:45:37,870 --> 00:45:39,650 number." All right. 987 00:45:39,650 --> 00:45:44,410 So this program, even though I did it fast, looks right syntactically. 988 00:45:44,410 --> 00:45:45,010 Let me try this. 989 00:45:45,010 --> 00:45:46,890 Make condition 0. 990 00:45:46,890 --> 00:45:47,710 Seems to run. 991 00:45:47,710 --> 00:45:49,230 Condition 0, enter. 992 00:45:49,230 --> 00:45:51,910 Let's give it an int of 50. 993 00:45:51,910 --> 00:45:53,160 I picked a positive number. 994 00:45:53,160 --> 00:45:54,230 Let's try it again. 995 00:45:54,230 --> 00:45:54,930 Condition 0. 996 00:45:54,930 --> 00:45:56,260 Negative 50. 997 00:45:56,260 --> 00:45:57,290 I picked a negative number. 998 00:45:57,290 --> 00:46:00,350 >> But now, let me pick what we'll keep calling a corner case, which is more 999 00:46:00,350 --> 00:46:04,702 an interesting case that you think might give you some trouble, 0. 1000 00:46:04,702 --> 00:46:07,940 Now, I'm pretty sure this is one of those corner cases where zero is 1001 00:46:07,940 --> 00:46:12,330 neither positive nor negative, so my program, though in syntactically 1002 00:46:12,330 --> 00:46:15,440 correct-- it compiles, it runs-- is not logically correct. 1003 00:46:15,440 --> 00:46:21,050 So what's the simple fix here if I want to detect, if I want to handle n 1004 00:46:21,050 --> 00:46:23,840 equals 0 separately? 1005 00:46:23,840 --> 00:46:32,980 >> So if n equals 0, then I want to say something like printf "You picked 0." 1006 00:46:32,980 --> 00:46:33,990 Let me try this now. 1007 00:46:33,990 --> 00:46:38,320 Let me go back here, clear my window, and recompile. 1008 00:46:38,320 --> 00:46:38,690 Hm. 1009 00:46:38,690 --> 00:46:39,770 One error generate. 1010 00:46:39,770 --> 00:46:41,630 But I do you want to check if n equals 0. 1011 00:46:41,630 --> 00:46:44,230 1012 00:46:44,230 --> 00:46:47,720 >> So again, another stupid thing to get used to, equal sign is 1013 00:46:47,720 --> 00:46:48,910 the assignment operator. 1014 00:46:48,910 --> 00:46:52,240 So this is actually mistake in that, technically, even though the compiler 1015 00:46:52,240 --> 00:46:56,450 protected me from myself, we'd technically be copying 0 into n, which 1016 00:46:56,450 --> 00:46:57,590 is not what I want. 1017 00:46:57,590 --> 00:47:00,950 I want to test for equality with equals equals. 1018 00:47:00,950 --> 00:47:03,390 And so that now might be my better solution. 1019 00:47:03,390 --> 00:47:07,810 >> Let me actually resave this as, say, conditions1, to be new and improved. 1020 00:47:07,810 --> 00:47:10,480 So now if I recompile this, it would make conditions-- 1021 00:47:10,480 --> 00:47:16,960 whoops-- make conditions1 dot slash conditions1, Enter, "I'd like an int, 1022 00:47:16,960 --> 00:47:18,760 please." I'm going to go ahead and type 50. 1023 00:47:18,760 --> 00:47:19,660 It's still good. 1024 00:47:19,660 --> 00:47:21,200 Negative 50, still good. 1025 00:47:21,200 --> 00:47:24,920 0, and it, indeed, detected that I picked 0. 1026 00:47:24,920 --> 00:47:28,200 >> So what more can I now do with this? 1027 00:47:28,200 --> 00:47:32,280 Well, we can certainly do increasingly complex things. 1028 00:47:32,280 --> 00:47:37,240 But what I'd like to propose is that we end on this note here. 1029 00:47:37,240 --> 00:47:41,680 If we pull up this, you'll see, perhaps, a favorite cartoon of yours 1030 00:47:41,680 --> 00:47:43,326 up until today you might never have understood. 1031 00:47:43,326 --> 00:47:46,550 1032 00:47:46,550 --> 00:47:49,520 And that's about the quality of laughter you should get with CS jokes. 1033 00:47:49,520 --> 00:47:51,060 So that's about right. 1034 00:47:51,060 --> 00:47:54,900 >> But more compellingly, I thought I'd give us a little teaser for Monday. 1035 00:47:54,900 --> 00:47:56,430 So we've started using integers. 1036 00:47:56,430 --> 00:47:57,510 We've referred to floats. 1037 00:47:57,510 --> 00:48:00,120 We've even referred to doubles, which, again, give you numbers 1038 00:48:00,120 --> 00:48:01,390 after decimal points. 1039 00:48:01,390 --> 00:48:04,230 >> But it turns out that floats and doubles, and, really, computers, 1040 00:48:04,230 --> 00:48:08,470 fundamentally, cannot express some values precisely. 1041 00:48:08,470 --> 00:48:12,840 We know from math classes that you can have any number of numbers after the 1042 00:48:12,840 --> 00:48:13,510 decimal point. 1043 00:48:13,510 --> 00:48:16,460 And you can even put a vertical bar to say these go on forever. 1044 00:48:16,460 --> 00:48:18,810 >> Unfortunately, you can't really do that in a computer. 1045 00:48:18,810 --> 00:48:20,400 And so corner cases arise. 1046 00:48:20,400 --> 00:48:23,120 For instance, suppose you're doing something financial. 1047 00:48:23,120 --> 00:48:25,700 You're doing mathematics with percentages, and 1048 00:48:25,700 --> 00:48:27,030 with dollars and cents. 1049 00:48:27,030 --> 00:48:29,230 And those cents don't necessarily line up. 1050 00:48:29,230 --> 00:48:34,120 Well, it turns out that fractions of pennies can start to add up when 1051 00:48:34,120 --> 00:48:37,250 modeled with a computer system in such a way that clever 1052 00:48:37,250 --> 00:48:39,100 humans can take advantage. 1053 00:48:39,100 --> 00:48:42,650 >> And those of you who haven't seen, I'd like to give you a 30-second teaser of 1054 00:48:42,650 --> 00:48:48,290 a wonderful film known as Office Space, which will paint a picture of a 1055 00:48:48,290 --> 00:48:51,205 problem we shall tackle first thing on Monday. 1056 00:48:51,205 --> 00:48:53,960 If we could raise the volume a little bit, I give you 30 1057 00:48:53,960 --> 00:48:55,495 seconds of Office Space. 1058 00:48:55,495 --> 00:48:55,770 >> [VIDEO PLAYBACK] 1059 00:48:55,770 --> 00:48:59,000 >> -I mean, you, you haven't been showing up and you get to keep your job. 1060 00:48:59,000 --> 00:49:00,620 >> -Actually, I'm being promoted. 1061 00:49:00,620 --> 00:49:03,240 >> -I could program a virus that'd rip that place off big time. 1062 00:49:03,240 --> 00:49:04,430 >> -Well, how does it work? 1063 00:49:04,430 --> 00:49:06,782 >> -Every time there's a bank transaction where interest is computed, and there 1064 00:49:06,782 --> 00:49:10,160 are thousands a day, the computer ends up with these fractions of a cent. 1065 00:49:10,160 --> 00:49:12,030 >> -But I'm not going to do anything illegal. 1066 00:49:12,030 --> 00:49:12,660 >> -Illegal? 1067 00:49:12,660 --> 00:49:15,075 Samir, this is America. 1068 00:49:15,075 --> 00:49:16,570 >> -We have to swear to God. 1069 00:49:16,570 --> 00:49:19,070 >> -If nobody knows about this but us, all right? 1070 00:49:19,070 --> 00:49:21,920 No family members, no girlfriends, nobody. 1071 00:49:21,920 --> 00:49:22,700 >> -Of course. 1072 00:49:22,700 --> 00:49:23,320 >> -Agreed. 1073 00:49:23,320 --> 00:49:24,280 >> -Don't worry, man. 1074 00:49:24,280 --> 00:49:25,640 I won't tell anyone either. 1075 00:49:25,640 --> 00:49:26,504 >> [END VIDEO PLAYBACK] 1076 00:49:26,504 --> 00:49:30,030 >> DAVID MALAN: So this, then, is CS50, that was Office Space, and it will all 1077 00:49:30,030 --> 00:49:31,465 make sense on Monday. 1078 00:49:31,465 --> 00:49:33,940 See you then. 1079 00:49:33,940 --> 00:49:37,880 >> NARRATOR: At the next CS50, Rob adjusts to being off the 1080 00:49:37,880 --> 00:49:39,130 Harvard meal plan. 1081 00:49:39,130 --> 00:49:56,160