1 00:00:00,000 --> 00:00:07,170 2 00:00:07,170 --> 00:00:09,100 >> LUCAS FREITAS: OK, I guess now we're live. 3 00:00:09,100 --> 00:00:11,220 Welcome to CS50 Supersection. 4 00:00:11,220 --> 00:00:12,880 We're Less Comfortable. 5 00:00:12,880 --> 00:00:13,870 My name is Lucas Freitas. 6 00:00:13,870 --> 00:00:16,230 I'm one CS50 Head TFs. 7 00:00:16,230 --> 00:00:17,523 This is Lauren. 8 00:00:17,523 --> 00:00:19,002 >> LAUREN CARVALHO: Hi, I'm Lauren. 9 00:00:19,002 --> 00:00:21,330 I'm also Head TF for CS50. 10 00:00:21,330 --> 00:00:24,730 >> LUCAS FREITAS: Today we're just going to give an overview of what all you 11 00:00:24,730 --> 00:00:28,440 guys have learned in week one, hopefully to give you guys a good 12 00:00:28,440 --> 00:00:32,580 sense to what you should know for problem set one. 13 00:00:32,580 --> 00:00:34,120 >> Just a couple announcements. 14 00:00:34,120 --> 00:00:36,490 First of all, office hours. 15 00:00:36,490 --> 00:00:41,580 Office hours are happening from Monday to Thursday in different houses in 16 00:00:41,580 --> 00:00:43,450 Annenberg each day. 17 00:00:43,450 --> 00:00:46,680 They're a very good resource for you guys to come and ask questions, and 18 00:00:46,680 --> 00:00:49,470 talk to TFs, and even exchange ideas with other students. 19 00:00:49,470 --> 00:00:55,420 So be sure to use that resource in your favor. 20 00:00:55,420 --> 00:00:59,790 >> The second thing is the CS50 Appliance. 21 00:00:59,790 --> 00:01:01,390 For the last [INAUDIBLE] 22 00:01:01,390 --> 00:01:04,180 you guys didn't have to actually have an appliance. 23 00:01:04,180 --> 00:01:06,910 You just did everything in your personal computer. 24 00:01:06,910 --> 00:01:10,330 But for this problem, you actually have to have a virtual machine, so be 25 00:01:10,330 --> 00:01:14,680 sure to download the CS50 Appliance and set it up. 26 00:01:14,680 --> 00:01:18,670 >> And the third thing is be sure to start the problem set ASAP just 27 00:01:18,670 --> 00:01:22,740 because it's much easier to find help when you're in the 28 00:01:22,740 --> 00:01:23,730 beginning of the week. 29 00:01:23,730 --> 00:01:27,540 Because people tend to wait until the last second to start the problem set, 30 00:01:27,540 --> 00:01:29,730 and then you go to office hours, and there's a lot of people. 31 00:01:29,730 --> 00:01:33,020 Where if you go on Monday, you're going to see that you can talk to TFs 32 00:01:33,020 --> 00:01:35,870 pretty much for the whole night, and they're going be more than 33 00:01:35,870 --> 00:01:37,790 happy to help you. 34 00:01:37,790 --> 00:01:40,720 >> LAUREN CARVALHO: Can I just stop you for just one second? 35 00:01:40,720 --> 00:01:43,330 Has everyone been able to download the CS50 Appliance? 36 00:01:43,330 --> 00:01:44,255 Have people tried? 37 00:01:44,255 --> 00:01:47,120 >> LUCAS FREITAS: To download, by the way, you can get instructions at 38 00:01:47,120 --> 00:01:48,045 cs50.net/appliance. 39 00:01:48,045 --> 00:01:48,817 >> LAUREN CARVALHO: Right. 40 00:01:48,817 --> 00:01:53,120 And it does take a little while for our sys admins to get you a license 41 00:01:53,120 --> 00:01:55,450 key, so sign up for the free trial first. 42 00:01:55,450 --> 00:01:59,080 That way you'll have it for 30 days, and then wait for your license key 43 00:01:59,080 --> 00:02:02,650 which should give it to you for a year. 44 00:02:02,650 --> 00:02:04,100 >> LUCAS FREITAS: First of all, let's talk a little bit 45 00:02:04,100 --> 00:02:06,180 about the CS50 Appliance. 46 00:02:06,180 --> 00:02:10,770 The CS50 Appliance is actually a virtual machine that you're going to 47 00:02:10,770 --> 00:02:15,040 run in your own computer which is basically running another computer 48 00:02:15,040 --> 00:02:16,010 inside your own computer. 49 00:02:16,010 --> 00:02:16,800 Which is pretty cool. 50 00:02:16,800 --> 00:02:19,420 And actually it's used everywhere. 51 00:02:19,420 --> 00:02:23,150 People who to tech would know that this is used all the time. 52 00:02:23,150 --> 00:02:23,250 you? 53 00:02:23,250 --> 00:02:26,200 Can have multiple computers in one device. 54 00:02:26,200 --> 00:02:27,760 >> Let's actually take a look at the Appliance. 55 00:02:27,760 --> 00:02:32,680 56 00:02:32,680 --> 00:02:36,640 When you go to the Appliance after registering, you're going to have to 57 00:02:36,640 --> 00:02:42,035 put your name and log in to Harvard so we can get you all registered. 58 00:02:42,035 --> 00:02:46,570 You going to see basically just a wallpaper, and then Home and Trash, 59 00:02:46,570 --> 00:02:48,980 and then these little icons. 60 00:02:48,980 --> 00:02:51,930 This three icons are actually the most important things that you can have in 61 00:02:51,930 --> 00:02:53,000 your Appliance. 62 00:02:53,000 --> 00:03:00,270 >> The first one is gedit, which is a text editor, just like Notepad or like 63 00:03:00,270 --> 00:03:01,440 any kind of text editor. 64 00:03:01,440 --> 00:03:09,750 It's just something that we use to write code and then do stuff with it. 65 00:03:09,750 --> 00:03:11,770 >> The second thing that we have here is Chrome. 66 00:03:11,770 --> 00:03:13,910 So if you need to use the internet in the Appliance, that's 67 00:03:13,910 --> 00:03:15,030 what you can use. 68 00:03:15,030 --> 00:03:22,000 >> And the third thing is the terminal, which is actually very sweet. 69 00:03:22,000 --> 00:03:26,420 People usually are kind of scared of the terminal because it's this very 70 00:03:26,420 --> 00:03:32,320 old fashioned application in which you just basically type everything with 71 00:03:32,320 --> 00:03:32,940 your keyboard. 72 00:03:32,940 --> 00:03:34,730 There is not a lot of interaction with your mouse. 73 00:03:34,730 --> 00:03:39,780 But actually the terminal does very good things. 74 00:03:39,780 --> 00:03:43,990 >> First of all, let's just go through some commands that you 75 00:03:43,990 --> 00:03:48,765 can use in the terminal. 76 00:03:48,765 --> 00:03:51,995 >> LAUREN CARVALHO: And feel free to stop us if you have any questions. 77 00:03:51,995 --> 00:03:52,830 >> LUCAS FREITAS: Yeah. 78 00:03:52,830 --> 00:04:01,950 So the first command that we have for the terminal is ls. 79 00:04:01,950 --> 00:04:06,450 ls basically means list everything, all the files and all the directors, 80 00:04:06,450 --> 00:04:09,540 that I have at the place that I am at. 81 00:04:09,540 --> 00:04:15,200 So if I do ls here, I'm going to see that I have some directories. 82 00:04:15,200 --> 00:04:18,670 I have the desktop, the downloads, the logs, and the VHOSTs. 83 00:04:18,670 --> 00:04:21,260 Those are in blue because they are all directories. 84 00:04:21,260 --> 00:04:24,160 >> Then you have some other files. 85 00:04:24,160 --> 00:04:27,840 You have like hello and hello.c, which are just files, and they're in 86 00:04:27,840 --> 00:04:28,910 different colors. 87 00:04:28,910 --> 00:04:33,960 So this is a very important command just you can see what are the files 88 00:04:33,960 --> 00:04:37,690 and directories that you have in the directory that 89 00:04:37,690 --> 00:04:39,410 you're at in the terminal. 90 00:04:39,410 --> 00:04:44,580 When you see jharvard@appliance and then the little symbol there, the 91 00:04:44,580 --> 00:04:49,185 tilde, that just means that that's the directory that you are at the moment. 92 00:04:49,185 --> 00:04:52,970 >> LAUREN CARVALHO: So typing ls is almost like clicking on the folder 93 00:04:52,970 --> 00:04:57,380 that is listed right there, in it shows you all of the content that are 94 00:04:57,380 --> 00:04:58,886 within that folder. 95 00:04:58,886 --> 00:05:04,232 >> AUDIENCE: Is there text after the hello.c? 96 00:05:04,232 --> 00:05:10,070 >> LAUREN CARVALHO: Oh, you can't see that, because it's blue. 97 00:05:10,070 --> 00:05:12,490 >> LUCAS FREITAS: It says logs and VHOSTs. 98 00:05:12,490 --> 00:05:14,450 >> LAUREN CARVALHO: Those are just other folders. 99 00:05:14,450 --> 00:05:16,890 I'm sorry, it's really hard to see. 100 00:05:16,890 --> 00:05:19,090 Maybe if we turned off the lights? 101 00:05:19,090 --> 00:05:23,002 Colton, do you want to try and find the light switch? 102 00:05:23,002 --> 00:05:24,960 Sorry about that. 103 00:05:24,960 --> 00:05:25,675 I didn't think about it. 104 00:05:25,675 --> 00:05:27,340 >> LUCAS FREITAS: But it's basically-- 105 00:05:27,340 --> 00:05:30,310 yeah, if you could-- 106 00:05:30,310 --> 00:05:32,278 >> LAUREN CARVALHO: Zoom in your screen. 107 00:05:32,278 --> 00:05:34,738 >> LUCAS FREITAS: I don't have that feature. 108 00:05:34,738 --> 00:05:35,988 >> LAUREN CARVALHO: [INAUDIBLE]. 109 00:05:35,988 --> 00:05:39,528 110 00:05:39,528 --> 00:05:40,001 OK. 111 00:05:40,001 --> 00:05:44,270 Well, there's just a lot of natural light in this room as well. 112 00:05:44,270 --> 00:05:48,726 >> LUCAS FREITAS: Let's just assume that there are two other directories there. 113 00:05:48,726 --> 00:05:49,780 >> LAUREN CARVALHO: Just bear with us. 114 00:05:49,780 --> 00:05:54,200 This is the only part that you'll be looking at the terminal. 115 00:05:54,200 --> 00:06:00,160 >> LUCAS FREITAS: The next command is basically maybe you want to organize 116 00:06:00,160 --> 00:06:01,080 your files-- 117 00:06:01,080 --> 00:06:05,015 for example, all your C files in one directory- but you don't have a 118 00:06:05,015 --> 00:06:07,180 directory just for C files yet. 119 00:06:07,180 --> 00:06:11,590 You can basically create a directory using the terminal as well, which is 120 00:06:11,590 --> 00:06:15,670 the same thing as right clicking and saying New Folder. 121 00:06:15,670 --> 00:06:20,640 The command is make directory, which is mkdir, so basically "make" and 122 00:06:20,640 --> 00:06:21,260 "directory." 123 00:06:21,260 --> 00:06:22,680 >> And then you can put the name of the directory. 124 00:06:22,680 --> 00:06:32,490 Let's say that I wanted to be called, I don't know, let's say pset1 Doing 125 00:06:32,490 --> 00:06:33,900 this, I just created a directory. 126 00:06:33,900 --> 00:06:35,910 Let's just check that it is there. 127 00:06:35,910 --> 00:06:40,870 So if I do ls, now I see logs, and then pset1, and then VHOSTs. 128 00:06:40,870 --> 00:06:44,360 So I have the new directory there. 129 00:06:44,360 --> 00:06:46,670 >> And then after you have a new directory, maybe you want to navigate 130 00:06:46,670 --> 00:06:51,760 into it, so the next command this cd, which just basically means go to a 131 00:06:51,760 --> 00:06:53,400 specific directory. 132 00:06:53,400 --> 00:06:58,377 So if I want to go to pset1, I can just do cd pset1. 133 00:06:58,377 --> 00:07:01,550 >> LAUREN CARVALHO: cd stands for change directory, [INAUDIBLE] able to 134 00:07:01,550 --> 00:07:02,230 remember that. 135 00:07:02,230 --> 00:07:05,460 >> LUCAS FREITAS: And now you see that instead of just having jharvard 136 00:07:05,460 --> 00:07:09,150 appliance, I have jharvard appliance, and then pset1, because it's showing 137 00:07:09,150 --> 00:07:14,150 me where I am in this computer. 138 00:07:14,150 --> 00:07:17,510 >> Now if I want to go back to the directory that was before this one, I 139 00:07:17,510 --> 00:07:22,950 can do a cd dot dot, which just means go to the directory that I was before, 140 00:07:22,950 --> 00:07:26,290 like the parent of this directory, basically. 141 00:07:26,290 --> 00:07:30,460 And now I see that I'm back to jharvard. 142 00:07:30,460 --> 00:07:36,050 >> The next thing that I can do it is rm, which is remove a file. 143 00:07:36,050 --> 00:07:39,590 For example, let's first do ls to see what I have here. 144 00:07:39,590 --> 00:07:41,790 I have hello and hello.c. 145 00:07:41,790 --> 00:07:43,780 Let's say that I want to remove them. 146 00:07:43,780 --> 00:07:48,280 I can do rm hello. 147 00:07:48,280 --> 00:07:52,770 It's going to say "remove regular file hello?" And I say "yes." And now if I 148 00:07:52,770 --> 00:07:56,410 do ls, I see that it's not there anymore. 149 00:07:56,410 --> 00:07:59,700 >> If I try to use rm for a directory, though, it's not going 150 00:07:59,700 --> 00:08:00,800 to let you do anything. 151 00:08:00,800 --> 00:08:02,960 It's going to say, hey, you cannot remove a directory. 152 00:08:02,960 --> 00:08:08,100 So we have this other come called rmdir, which just basically means 153 00:08:08,100 --> 00:08:09,800 remove directory. 154 00:08:09,800 --> 00:08:12,842 I can, for example, remove pset1. 155 00:08:12,842 --> 00:08:15,770 And if I do ls, I don't have it anymore. 156 00:08:15,770 --> 00:08:18,430 >> Any questions about this? 157 00:08:18,430 --> 00:08:20,200 No? 158 00:08:20,200 --> 00:08:20,595 Yeah? 159 00:08:20,595 --> 00:08:23,954 >> AUDIENCE: Is it important that you leave a space after-- whenever you 160 00:08:23,954 --> 00:08:26,540 start typing something in? 161 00:08:26,540 --> 00:08:26,830 >> LUCAS FREITAS: Yeah. 162 00:08:26,830 --> 00:08:32,080 If I do like cd and the directory, I have to put a space. 163 00:08:32,080 --> 00:08:33,350 What space are you talking about? 164 00:08:33,350 --> 00:08:34,979 >> LAUREN CARVALHO: The space between cd and [INAUDIBLE]. 165 00:08:34,979 --> 00:08:37,710 >> LUCAS FREITAS: Yeah, you'll have to put the spaces because cd is the 166 00:08:37,710 --> 00:08:42,770 command, and then the next string that you type is basically the directory. 167 00:08:42,770 --> 00:08:45,020 So it's like an argument. 168 00:08:45,020 --> 00:08:45,360 Yeah? 169 00:08:45,360 --> 00:08:47,952 >> AUDIENCE: When you list, all those are different directories, right? 170 00:08:47,952 --> 00:08:50,125 >> LUCAS FREITAS: Yeah, they are in the directory. 171 00:08:50,125 --> 00:08:53,524 >> AUDIENCE: What is the differences among the directories? 172 00:08:53,524 --> 00:08:55,390 >> LUCAS FREITAS: They're just different folders. 173 00:08:55,390 --> 00:08:57,366 Directory is a folder in your computer. 174 00:08:57,366 --> 00:08:58,120 >> AUDIENCE: Open folder. 175 00:08:58,120 --> 00:09:00,140 >> LUCAS FREITAS: Yeah, it's just an open folder. 176 00:09:00,140 --> 00:09:03,400 >> LAUREN CARVALHO: Over here, the ones that are in blue are actual 177 00:09:03,400 --> 00:09:07,107 directories that you can get into and then see a bunch of things that are 178 00:09:07,107 --> 00:09:10,248 within that, which is unfortunate because you can't actually read the 179 00:09:10,248 --> 00:09:11,290 ones in blue. 180 00:09:11,290 --> 00:09:16,166 And the ones in white are just files that are within the current directory. 181 00:09:16,166 --> 00:09:17,870 >> LUCAS FREITAS: So directories are folders. 182 00:09:17,870 --> 00:09:18,810 The other ones are files. 183 00:09:18,810 --> 00:09:22,810 >> AUDIENCE: What's the command to reset it or to clear it? 184 00:09:22,810 --> 00:09:23,650 LUCAS FREITAS: To clear? 185 00:09:23,650 --> 00:09:24,362 LAUREN CARVALHO: To clear? 186 00:09:24,362 --> 00:09:27,985 LUCAS FREITAS: I think it's "clear." Yeah. 187 00:09:27,985 --> 00:09:32,370 So you just type clear if your screen's too messy. 188 00:09:32,370 --> 00:09:33,700 Any other questions? 189 00:09:33,700 --> 00:09:36,720 And you will learn a bunch of other commands just naturally during the 190 00:09:36,720 --> 00:09:38,405 semester because they're very useful. 191 00:09:38,405 --> 00:09:42,380 192 00:09:42,380 --> 00:09:46,155 >> So now let's talk about compiling. 193 00:09:46,155 --> 00:09:51,510 194 00:09:51,510 --> 00:09:54,730 We talked about pseudocode and source code and object code. 195 00:09:54,730 --> 00:09:59,020 And there's a lot of codes, and maybe you guys are confused about that. 196 00:09:59,020 --> 00:10:02,190 Let's talk a little bit about what happens when you're writing a program, 197 00:10:02,190 --> 00:10:06,620 and you compile it, and it becomes an executable file. 198 00:10:06,620 --> 00:10:10,190 Those are all words that you guys probably heard, and maybe you're not 199 00:10:10,190 --> 00:10:12,130 really sure what is going on. 200 00:10:12,130 --> 00:10:15,540 >> Let's say, for example, that I want to write a program that says "hello." The 201 00:10:15,540 --> 00:10:19,670 first thing that comes in your mind is you kind of try to think of how you're 202 00:10:19,670 --> 00:10:23,000 going to do that in your mind, and you're probably thinking in English or 203 00:10:23,000 --> 00:10:25,550 some other language, like natural language, probably. 204 00:10:25,550 --> 00:10:28,770 And then you think, well, if I want to, for example, write a program that 205 00:10:28,770 --> 00:10:32,630 says "hello," I would start a program, I will print hello, and then I will 206 00:10:32,630 --> 00:10:35,270 quit the program. 207 00:10:35,270 --> 00:10:36,350 This is pseudocode. 208 00:10:36,350 --> 00:10:41,740 This is just basically you're trying to come up with a sequence of events 209 00:10:41,740 --> 00:10:46,510 that you're going to do to be able to write a program. 210 00:10:46,510 --> 00:10:49,610 >> Then you have the source code, and the source code doesn't have to 211 00:10:49,610 --> 00:10:53,240 specifically be in C. We're talking about source code in C because that's 212 00:10:53,240 --> 00:10:56,760 the language we're learning, but course code can be pretty much any 213 00:10:56,760 --> 00:10:59,160 programming language that you have. 214 00:10:59,160 --> 00:11:01,230 Later in the semester, you're going to learn other languages. 215 00:11:01,230 --> 00:11:03,230 That's also source code. 216 00:11:03,230 --> 00:11:07,380 >> For example, here, this is my source code, and then I'm including a 217 00:11:07,380 --> 00:11:10,170 library, and then I start main, and then I print. 218 00:11:10,170 --> 00:11:14,510 And Laura is going to talk a little bit about all of this. 219 00:11:14,510 --> 00:11:16,190 So this is the source code. 220 00:11:16,190 --> 00:11:19,590 >> And what is the object code? 221 00:11:19,590 --> 00:11:22,500 The object is a bunch of zeros and ones, basically. 222 00:11:22,500 --> 00:11:27,680 What happens is you initially have pseudocode, which is all in English. 223 00:11:27,680 --> 00:11:29,020 And I mean, you understand it. 224 00:11:29,020 --> 00:11:31,260 Probably your friends understand it, and most people would 225 00:11:31,260 --> 00:11:32,560 understand the idea. 226 00:11:32,560 --> 00:11:36,770 But computers are really dumb, and they don't understand English, so you 227 00:11:36,770 --> 00:11:41,320 have translate that into something, usually translated into some 228 00:11:41,320 --> 00:11:42,460 programming language. 229 00:11:42,460 --> 00:11:46,840 >> And you can think of a programming language as kind of like a language 230 00:11:46,840 --> 00:11:50,970 that you tell your computer how to speak and to understand. 231 00:11:50,970 --> 00:11:54,160 You're basically writing something in a language that both of you and your 232 00:11:54,160 --> 00:11:55,190 computer understand. 233 00:11:55,190 --> 00:11:56,470 That's the source code. 234 00:11:56,470 --> 00:12:00,580 So going form pseudocode to source code, it's up to you. 235 00:12:00,580 --> 00:12:03,130 It's basically you doing your program. 236 00:12:03,130 --> 00:12:06,740 >> But the problem is that the computer doesn't even 237 00:12:06,740 --> 00:12:08,360 understand programming language. 238 00:12:08,360 --> 00:12:10,320 It doesn't understand source code at all. 239 00:12:10,320 --> 00:12:12,520 It actually just understands zeros and ones. 240 00:12:12,520 --> 00:12:16,570 So we have this very magic thing called a compiler, which basically is 241 00:12:16,570 --> 00:12:19,755 going to get the source called that you wrote in a language the both you 242 00:12:19,755 --> 00:12:25,320 and your computer speak, and now you're going to transform that in 243 00:12:25,320 --> 00:12:26,270 zeros and ones. 244 00:12:26,270 --> 00:12:30,860 So basically what the compiler does is it gets source code, in this case C, 245 00:12:30,860 --> 00:12:34,100 it is going to transform it into zeroes and ones that the computer 246 00:12:34,100 --> 00:12:37,440 understands and can make an executable file out of it. 247 00:12:37,440 --> 00:12:40,900 >> Does that make sense? 248 00:12:40,900 --> 00:12:45,300 You can just think of the compiler as some kind of translator that is 249 00:12:45,300 --> 00:12:50,280 helping you explain to the computer exactly what it's supposed to do. 250 00:12:50,280 --> 00:12:52,310 >> But how do I compile. 251 00:12:52,310 --> 00:12:59,680 Let's go back to the Appliance, and let's say that I'm doing that program. 252 00:12:59,680 --> 00:13:04,220 First I'm going to create a directory here, and I'm going to call it 253 00:13:04,220 --> 00:13:11,130 "practice." Now I'm going to navigate to the directory, so cd practice. 254 00:13:11,130 --> 00:13:13,210 Now I'm inside practice. 255 00:13:13,210 --> 00:13:16,860 >> Now I want to start gedit, so how do I do that? 256 00:13:16,860 --> 00:13:21,920 I can either click here, or I can just literally type "gedit," and it's going 257 00:13:21,920 --> 00:13:24,290 to open for me. 258 00:13:24,290 --> 00:13:28,100 >> Now I'm going to type my program, so exactly what I wrote in the slide. 259 00:13:28,100 --> 00:13:30,360 I'm declaring a library. 260 00:13:30,360 --> 00:13:35,440 I'm doing int main, void. 261 00:13:35,440 --> 00:13:39,180 And now I'm doing printf, hello. 262 00:13:39,180 --> 00:13:46,530 263 00:13:46,530 --> 00:13:50,170 Now I'm going to save this. 264 00:13:50,170 --> 00:13:53,665 Let me put it into practice, and I'm going to call this hello.c. 265 00:13:53,665 --> 00:13:57,710 266 00:13:57,710 --> 00:13:59,590 So now it's saved. 267 00:13:59,590 --> 00:14:03,250 >> If I want to compile it, I actually can do it in two different places. 268 00:14:03,250 --> 00:14:07,090 I can either do it in a terminal window, so this big one, or you can 269 00:14:07,090 --> 00:14:09,860 use gedit that actually has a terminal window right here. 270 00:14:09,860 --> 00:14:17,890 So you can very conveniently just do your code, and then you can just type 271 00:14:17,890 --> 00:14:20,670 whatever here, and try to compile it. 272 00:14:20,670 --> 00:14:23,450 >> So how do I compile code? 273 00:14:23,450 --> 00:14:27,495 The easiest way that we have is by using make, which is a function that 274 00:14:27,495 --> 00:14:29,590 is basically going to compile your code. 275 00:14:29,590 --> 00:14:34,170 You just have to say make hello, so "make" and the name of the program. 276 00:14:34,170 --> 00:14:35,420 >> Oops. 277 00:14:35,420 --> 00:14:38,250 278 00:14:38,250 --> 00:14:39,280 This is awkward. 279 00:14:39,280 --> 00:14:41,738 >> LAUREN CARVALHO: Can you [INAUDIBLE]? 280 00:14:41,738 --> 00:14:45,182 281 00:14:45,182 --> 00:14:45,990 >> LUCAS FREITAS: Sorry. 282 00:14:45,990 --> 00:14:49,960 I didn't save it in the right directory. 283 00:14:49,960 --> 00:14:52,130 Come to practice, and now save it as hello.c. 284 00:14:52,130 --> 00:14:54,750 I'm sorry. 285 00:14:54,750 --> 00:14:56,250 Now it's saved. 286 00:14:56,250 --> 00:14:58,300 >> Let me just do ls and be sure that it's here. 287 00:14:58,300 --> 00:14:59,460 OK, now it's here. 288 00:14:59,460 --> 00:15:05,930 So now if I do make hello, it spits a lot of stuff, but then it just gives 289 00:15:05,930 --> 00:15:08,870 me another line for the terminal, which means that it was successful. 290 00:15:08,870 --> 00:15:13,160 So now if I do an ls again, I actually see this file in green, which is the 291 00:15:13,160 --> 00:15:15,470 executable file. 292 00:15:15,470 --> 00:15:21,700 >> And then if I want to run it, all that I have to do is dot slash hello. 293 00:15:21,700 --> 00:15:25,830 It's going to say hello right here, so it's exactly what I'm asking it to. 294 00:15:25,830 --> 00:15:29,620 >> There is one more way of compiling, which is using clang. 295 00:15:29,620 --> 00:15:35,740 Clang is just a compiler that we have, and it's a little bit more verbose to 296 00:15:35,740 --> 00:15:36,390 compile it. 297 00:15:36,390 --> 00:15:40,180 But it works just as well. 298 00:15:40,180 --> 00:15:46,810 So if I do clang, and then -o, and then the name that I want to give to 299 00:15:46,810 --> 00:15:50,400 the executable file, so let's say "hello." And then the file that I want 300 00:15:50,400 --> 00:15:53,460 to compile, hello.c. 301 00:15:53,460 --> 00:15:55,900 It's also going to compile. 302 00:15:55,900 --> 00:15:59,020 And you have the file here, and I can just execute it as well. 303 00:15:59,020 --> 00:16:00,610 So both of them work. 304 00:16:00,610 --> 00:16:03,270 >> Does that make sense? 305 00:16:03,270 --> 00:16:06,020 Any questions? 306 00:16:06,020 --> 00:16:09,020 >> AUDIENCE: Can you zoom in a little bit? 307 00:16:09,020 --> 00:16:09,913 >> LUCAS FREITAS: Sorry? 308 00:16:09,913 --> 00:16:11,135 >> AUDIENCE: Zoom in? 309 00:16:11,135 --> 00:16:13,310 >> LAUREN CARVALHO: I think you can just command plus. 310 00:16:13,310 --> 00:16:14,295 >> LUCAS FREITAS: Command what? 311 00:16:14,295 --> 00:16:16,060 >> LAUREN CARVALHO: Command plus. 312 00:16:16,060 --> 00:16:17,000 >> LUCAS FREITAS: No. 313 00:16:17,000 --> 00:16:19,350 >> LAUREN CARVALHO: Oh, that's not going to work because-- 314 00:16:19,350 --> 00:16:25,548 it's because we set the resolution for this thing. 315 00:16:25,548 --> 00:16:27,450 >> LUCAS FREITAS: I don't think we can zoom in. 316 00:16:27,450 --> 00:16:30,852 317 00:16:30,852 --> 00:16:34,130 Yeah, we cannot zoom. 318 00:16:34,130 --> 00:16:36,200 Do you have another question? 319 00:16:36,200 --> 00:16:36,680 Sorry. 320 00:16:36,680 --> 00:16:39,170 >> AUDIENCE: Can you repeat clang? 321 00:16:39,170 --> 00:16:40,630 >> LUCAS FREITAS: The clang? 322 00:16:40,630 --> 00:16:44,190 Clang is the same thing as make, but the difference is that first I'm going 323 00:16:44,190 --> 00:16:48,460 to write clang, which is the command, then I'm going to do -0, all, which is 324 00:16:48,460 --> 00:16:51,680 just what we're going to learn later as a flag. 325 00:16:51,680 --> 00:16:54,440 So just think that you always have to put the -o. 326 00:16:54,440 --> 00:16:58,690 >> And then first, you're going to put the name of the executable file, 327 00:16:58,690 --> 00:17:00,680 basically the file that you're going to create. 328 00:17:00,680 --> 00:17:06,430 So I can say, for example, hello2 and then the file that I want to compile, 329 00:17:06,430 --> 00:17:07,800 which is the .c file. 330 00:17:07,800 --> 00:17:09,240 So that's hello.c. 331 00:17:09,240 --> 00:17:10,871 Does that make sense? 332 00:17:10,871 --> 00:17:12,635 >> AUDIENCE: Hello what? 333 00:17:12,635 --> 00:17:13,076 What is that? 334 00:17:13,076 --> 00:17:15,960 >> LUCAS FREITAS: Hello.c. 335 00:17:15,960 --> 00:17:19,849 And if I compile it, and if I do ls, I also have a 336 00:17:19,849 --> 00:17:22,010 hello2 that I just compiled. 337 00:17:22,010 --> 00:17:26,339 And if I try to run it, it's going to do exactly the same thing as hello, 338 00:17:26,339 --> 00:17:27,089 just print hello. 339 00:17:27,089 --> 00:17:31,660 >> AUDIENCE: How come for the make you can put hello.c after? 340 00:17:31,660 --> 00:17:33,880 >> LUCAS FREITAS: In make, you don't have to put the .c. 341 00:17:33,880 --> 00:17:36,140 In make, you just say the name of the program. 342 00:17:36,140 --> 00:17:37,750 You don't have to say .c. 343 00:17:37,750 --> 00:17:39,020 But for clang, you should. 344 00:17:39,020 --> 00:17:44,040 >> AUDIENCE: With clang do you also have to add the [INAUDIBLE] 345 00:17:44,040 --> 00:17:44,480 >> LAUREN CARVALHO: Yeah. 346 00:17:44,480 --> 00:17:46,180 So you also have to library. 347 00:17:46,180 --> 00:17:51,170 >> LUCAS FREITAS: Yeah, that's something we going to use later. 348 00:17:51,170 --> 00:17:54,850 That's called linking, and I think we're going to talk about it later. 349 00:17:54,850 --> 00:17:55,930 Another question? 350 00:17:55,930 --> 00:17:59,927 >> AUDIENCE: Do you put a dot slash clang? 351 00:17:59,927 --> 00:18:01,060 >> LUCAS FREITAS: No. 352 00:18:01,060 --> 00:18:06,040 The dot slash is just to open the executable file. 353 00:18:06,040 --> 00:18:09,440 So clang, you just write "clang," and then the if you want to open the 354 00:18:09,440 --> 00:18:12,640 executable file that you just compiled, then you do dot 355 00:18:12,640 --> 00:18:13,780 slash and the name. 356 00:18:13,780 --> 00:18:15,100 Does that make sense? 357 00:18:15,100 --> 00:18:18,635 358 00:18:18,635 --> 00:18:22,435 >> AUDIENCE: If you were to just change [INAUDIBLE] to hello world, do you 359 00:18:22,435 --> 00:18:23,870 have to recompile it? 360 00:18:23,870 --> 00:18:24,750 >> LUCAS FREITAS: Yeah. 361 00:18:24,750 --> 00:18:36,650 if I want to say, for example, "hello there," I can just save it now, and I 362 00:18:36,650 --> 00:18:38,020 just compile it again. 363 00:18:38,020 --> 00:18:41,910 So let me do a make hello. 364 00:18:41,910 --> 00:18:48,490 And if I do hello, it's going to say "hello there." Makes sense? 365 00:18:48,490 --> 00:18:50,280 Any other questions? 366 00:18:50,280 --> 00:18:54,384 >> AUDIENCE: Do you have to include the standard I/O to coordinate the work? 367 00:18:54,384 --> 00:18:55,480 >> LUCAS FREITAS: Yeah. 368 00:18:55,480 --> 00:18:58,050 The standard I/O is basically the library that has an 369 00:18:58,050 --> 00:19:00,050 implementation for printf. 370 00:19:00,050 --> 00:19:03,450 371 00:19:03,450 --> 00:19:06,910 So that thing right on the top includes stdio. 372 00:19:06,910 --> 00:19:11,920 That is including a library that basically has a bunch of different 373 00:19:11,920 --> 00:19:13,260 tools that you can use. 374 00:19:13,260 --> 00:19:13,480 one? 375 00:19:13,480 --> 00:19:17,820 Of them is printf that is a function that basically prints 376 00:19:17,820 --> 00:19:21,290 messages to the screen. 377 00:19:21,290 --> 00:19:23,200 >> Any other questions? 378 00:19:23,200 --> 00:19:24,700 No? 379 00:19:24,700 --> 00:19:25,060 OK. 380 00:19:25,060 --> 00:19:31,620 So now let's talk about-- 381 00:19:31,620 --> 00:19:33,948 >> LAUREN CARVALHO: Let's just go to the style guide. 382 00:19:33,948 --> 00:19:35,896 Do you want to just find it in here? 383 00:19:35,896 --> 00:19:37,880 >> LUCAS FREITAS: So we're going to talk about style. 384 00:19:37,880 --> 00:19:47,006 385 00:19:47,006 --> 00:19:50,430 >> LAUREN CARVALHO: Oh, David didn't finish. 386 00:19:50,430 --> 00:19:51,230 OK. 387 00:19:51,230 --> 00:19:53,870 Why don't we come back to style at the very end? 388 00:19:53,870 --> 00:19:56,220 It's actually been updated as we speak. 389 00:19:56,220 --> 00:19:56,810 >> LUCAS FREITAS: Yeah. 390 00:19:56,810 --> 00:19:58,300 Let's way for David to finish that. 391 00:19:58,300 --> 00:20:07,150 392 00:20:07,150 --> 00:20:08,600 >> LAUREN CARVALHO: Can we switch actually? 393 00:20:08,600 --> 00:20:14,920 394 00:20:14,920 --> 00:20:17,200 >> So I hope everyone's been enjoying Scratch. 395 00:20:17,200 --> 00:20:21,950 Unfortunately, it's time to move away from this-- 396 00:20:21,950 --> 00:20:25,280 unfortunately it's time to move away from these pretty programming blocks 397 00:20:25,280 --> 00:20:28,530 on to C. But luckily, all of the programming constructs that you've 398 00:20:28,530 --> 00:20:32,370 been working with in Scratch are directly translatable to C, and we're 399 00:20:32,370 --> 00:20:35,130 going to start to learn about some of them right now. 400 00:20:35,130 --> 00:20:39,200 Let's first talk about conditions in Boolean expressions. 401 00:20:39,200 --> 00:20:41,944 >> This is running on reserve battery power. 402 00:20:41,944 --> 00:20:43,912 >> LUCAS FREITAS: How is that not working? 403 00:20:43,912 --> 00:20:47,850 404 00:20:47,850 --> 00:20:48,010 OK. 405 00:20:48,010 --> 00:20:50,256 You go there, I'm going to get the-- 406 00:20:50,256 --> 00:20:53,700 407 00:20:53,700 --> 00:20:57,390 >> LAUREN CARVALHO: So conditions are important because they allow us to 408 00:20:57,390 --> 00:21:00,588 introduce logic into our programs. 409 00:21:00,588 --> 00:21:04,620 Here's an example of a condition within a C program. 410 00:21:04,620 --> 00:21:06,130 Let's note how this-- 411 00:21:06,130 --> 00:21:11,324 So this condition block right here, this printf statement will not execute 412 00:21:11,324 --> 00:21:14,860 unless the condition within the parentheses, n greater than 0, 413 00:21:14,860 --> 00:21:16,800 evaluates to true. 414 00:21:16,800 --> 00:21:20,780 >> Now, all of the conditions within f statements are Boolean expressions, 415 00:21:20,780 --> 00:21:22,970 which means that there are only two options. 416 00:21:22,970 --> 00:21:26,856 They could either evaluate to true, or they can evaluate to false. 417 00:21:26,856 --> 00:21:33,174 So here, so this makes sense, right? n greater than 0, means it is a positive 418 00:21:33,174 --> 00:21:41,160 number, which means that it will print out "you picked a positive number." 419 00:21:41,160 --> 00:21:44,630 >> There are lots of Boolean expressions that can form the condition with an 420 00:21:44,630 --> 00:21:47,190 init statement. 421 00:21:47,190 --> 00:21:48,520 We have the less than. 422 00:21:48,520 --> 00:21:50,950 This is less than or equal to. 423 00:21:50,950 --> 00:21:52,200 This is just a funny [INAUDIBLE]. 424 00:21:52,200 --> 00:21:54,800 425 00:21:54,800 --> 00:22:01,630 If you want the printf to occur if it's greater than or equal to 0, then 426 00:22:01,630 --> 00:22:03,345 you would choose this. 427 00:22:03,345 --> 00:22:07,750 So the equal just appends right after the greater than sign. 428 00:22:07,750 --> 00:22:12,030 >> Equal equal, which is different from just one equal sign, equal equal 429 00:22:12,030 --> 00:22:16,430 actually means that whatever's on the left hand side of this is the same as 430 00:22:16,430 --> 00:22:20,610 what's on the right hand side of this where one equal sign is what? 431 00:22:20,610 --> 00:22:23,333 What is just one equal sign? 432 00:22:23,333 --> 00:22:24,275 Assignment. 433 00:22:24,275 --> 00:22:25,985 >> LUCAS FREITAS: Who said that? 434 00:22:25,985 --> 00:22:27,170 Is that candy? 435 00:22:27,170 --> 00:22:27,370 Oh, no. 436 00:22:27,370 --> 00:22:27,885 Sorry. 437 00:22:27,885 --> 00:22:28,210 Sorry. 438 00:22:28,210 --> 00:22:30,400 You both get candy. 439 00:22:30,400 --> 00:22:31,510 Oh, OK! 440 00:22:31,510 --> 00:22:33,046 Three people get candy! 441 00:22:33,046 --> 00:22:37,393 >> LAUREN CARVALHO: So this one right here is the bang operator preceding 442 00:22:37,393 --> 00:22:40,240 the equal sign, so that's not equal to. 443 00:22:40,240 --> 00:22:46,560 So if this was within my condition, the body of the condition would 444 00:22:46,560 --> 00:22:49,615 execute only if whatever's in the left is not the same as 445 00:22:49,615 --> 00:22:52,540 what's on the right. 446 00:22:52,540 --> 00:22:57,680 >> I can also have just the exclamation point. 447 00:22:57,680 --> 00:23:01,250 Actually we'll see in a feature slide how this would be used. 448 00:23:01,250 --> 00:23:03,830 >> LUCAS FREITAS: Any questions? 449 00:23:03,830 --> 00:23:06,450 No? 450 00:23:06,450 --> 00:23:10,460 >> LAUREN CARVALHO: We can combine these Boolean expressions to construct more 451 00:23:10,460 --> 00:23:12,500 intricate conditional statements. 452 00:23:12,500 --> 00:23:19,680 So here, if whatever's on the left hand side of the double bars or 453 00:23:19,680 --> 00:23:23,160 whatever is on the right hand side of the double bars evaluates to true, 454 00:23:23,160 --> 00:23:25,700 then we'll print out invalid. 455 00:23:25,700 --> 00:23:29,503 And then down here, if whatever's on the left hand side of the two 456 00:23:29,503 --> 00:23:33,480 ampersands and whatever's on the right hand side of the two ampersands 457 00:23:33,480 --> 00:23:37,470 evaluates to true, then we'll print out valid. 458 00:23:37,470 --> 00:23:42,195 The double bars are called the logical OR operator the double ampersands are 459 00:23:42,195 --> 00:23:44,190 called the logical AND operator. 460 00:23:44,190 --> 00:23:44,648 Yes? 461 00:23:44,648 --> 00:23:47,850 >> AUDIENCE: Are those just lower case l's? 462 00:23:47,850 --> 00:23:48,620 >> LAUREN CARVALHO: No. 463 00:23:48,620 --> 00:23:50,890 They're actually-- 464 00:23:50,890 --> 00:23:55,134 you know the backlash, like above your Return key? 465 00:23:55,134 --> 00:23:56,585 Do you see that? 466 00:23:56,585 --> 00:23:57,940 >> LUCAS FREITAS: It's actually a bar. 467 00:23:57,940 --> 00:24:02,100 >> LAUREN CARVALHO: It's actually just two bars. 468 00:24:02,100 --> 00:24:05,724 >> AUDIENCE: Can you use the equal equal for strings? 469 00:24:05,724 --> 00:24:07,000 >> LUCAS FREITAS: No. 470 00:24:07,000 --> 00:24:09,130 We're going to learn later something about that. 471 00:24:09,130 --> 00:24:10,735 >> LAUREN CARVALHO: There's a function that you can use. 472 00:24:10,735 --> 00:24:12,644 But you don't have to worry that just yet. 473 00:24:12,644 --> 00:24:15,530 474 00:24:15,530 --> 00:24:18,270 >> What if I give this program the number 5? 475 00:24:18,270 --> 00:24:20,850 What will print out? 476 00:24:20,850 --> 00:24:22,100 >> AUDIENCE: [INAUDIBLE] 477 00:24:22,100 --> 00:24:27,780 478 00:24:27,780 --> 00:24:28,275 valid. 479 00:24:28,275 --> 00:24:29,770 >> LAUREN CARVALHO: Valid? 480 00:24:29,770 --> 00:24:30,135 Yeah. 481 00:24:30,135 --> 00:24:34,092 I don't know who got that. 482 00:24:34,092 --> 00:24:35,036 >> [LAUGHTER] 483 00:24:35,036 --> 00:24:37,990 >> LAUREN CARVALHO: That's kind of scary [INAUDIBLE]. 484 00:24:37,990 --> 00:24:40,900 >> AUDIENCE: So the double and, that just means and? 485 00:24:40,900 --> 00:24:41,740 >> LAUREN CARVALHO: Yeah. 486 00:24:41,740 --> 00:24:46,160 So number 5 is both greater than 0 and less than 100. 487 00:24:46,160 --> 00:24:48,970 >> AUDIENCE: Why would it be one? 488 00:24:48,970 --> 00:24:51,350 >> LAUREN CARVALHO: One ampersand is actually something different, and it's 489 00:24:51,350 --> 00:24:53,390 a bitwise operator, which you'll learn about later. 490 00:24:53,390 --> 00:24:58,250 But it's something that actually works with the bits of a given number. 491 00:24:58,250 --> 00:25:00,872 >> LUCAS FREITAS: Just think they don't have to worry about it for now. 492 00:25:00,872 --> 00:25:04,590 >> LAUREN CARVALHO: I think it's probably coming up next week. 493 00:25:04,590 --> 00:25:06,420 >> LUCAS FREITAS: Any other questions? 494 00:25:06,420 --> 00:25:07,990 No? 495 00:25:07,990 --> 00:25:12,480 >> LAUREN CARVALHO: So we've seen that an it block can stand alone, but it can 496 00:25:12,480 --> 00:25:15,370 also be paired with and else block. 497 00:25:15,370 --> 00:25:18,880 So in this case, if n is greater than 0, we're going to print out "You 498 00:25:18,880 --> 00:25:23,855 picked a positive number." else, meaning if it is 0 or if it's less 499 00:25:23,855 --> 00:25:27,170 than 0, we're going to print out, "You picked a negative number." 500 00:25:27,170 --> 00:25:30,390 >> The interesting thing here is that these are exclusive of each other. 501 00:25:30,390 --> 00:25:33,940 There's never going to be a case in which this and this 502 00:25:33,940 --> 00:25:35,544 can both print out. 503 00:25:35,544 --> 00:25:38,190 504 00:25:38,190 --> 00:25:41,030 >> I'm sure you worked with the if/else box in Scratch, so this should be 505 00:25:41,030 --> 00:25:42,392 pretty familiar. 506 00:25:42,392 --> 00:25:42,874 Yes? 507 00:25:42,874 --> 00:25:48,522 >> AUDIENCE: Do we need the space between int n equals GetInt then space in the 508 00:25:48,522 --> 00:25:49,460 if statement? 509 00:25:49,460 --> 00:25:51,565 Like we use a line [INAUDIBLE] 510 00:25:51,565 --> 00:25:52,790 >> LAUREN CARVALHO: Oh, the space here? 511 00:25:52,790 --> 00:25:56,742 >> AUDIENCE: Like that, but you double space. 512 00:25:56,742 --> 00:25:58,718 No, go up. 513 00:25:58,718 --> 00:25:59,706 Go up. 514 00:25:59,706 --> 00:26:00,610 >> LUCAS FREITAS: Just the line? 515 00:26:00,610 --> 00:26:01,140 >> AUDIENCE: Yeah. 516 00:26:01,140 --> 00:26:02,870 >> LUCAS FREITAS: Just the blank line? 517 00:26:02,870 --> 00:26:03,270 >> AUDIENCE: Yeah. 518 00:26:03,270 --> 00:26:04,070 >> LAUREN CARVALHO: Blank line? 519 00:26:04,070 --> 00:26:04,470 No. 520 00:26:04,470 --> 00:26:06,320 That's just a stylistic thing. 521 00:26:06,320 --> 00:26:09,360 >> LUCAS FREITAS: It's just like sometimes you just skip some lines so 522 00:26:09,360 --> 00:26:12,330 that the code doesn't look like a huge block of text. 523 00:26:12,330 --> 00:26:15,800 >> LAUREN CARVALHO: I think what happened was that we had a comment here, but I 524 00:26:15,800 --> 00:26:18,220 took it out so that you guys would have to think about the code rather 525 00:26:18,220 --> 00:26:19,920 than just read my comment. 526 00:26:19,920 --> 00:26:24,170 In general, spacing will not really affect the way your program runs. 527 00:26:24,170 --> 00:26:26,070 It's more of a stylistic thing. 528 00:26:26,070 --> 00:26:29,460 And when we have the style guide updated and ready to go by the end of 529 00:26:29,460 --> 00:26:33,260 supersection, we'll talk about that more. 530 00:26:33,260 --> 00:26:34,660 >> There's something wrong with this program, right? 531 00:26:34,660 --> 00:26:38,980 Because 0 is not really a negative number. 532 00:26:38,980 --> 00:26:40,270 So let's see if we can fix that. 533 00:26:40,270 --> 00:26:45,710 534 00:26:45,710 --> 00:26:48,370 >> You can probably imagine with this says, right? 535 00:26:48,370 --> 00:26:52,850 If n is greater than 0, we print, "You picked a positive number." Then else 536 00:26:52,850 --> 00:26:57,340 if n is less than 0, "You picked a negative number." else, "You picked 537 00:26:57,340 --> 00:27:01,040 0." 538 00:27:01,040 --> 00:27:06,030 >> Now the interesting thing is that this could go on forever. 539 00:27:06,030 --> 00:27:10,020 You can have an if block, an else if block, another else if block, else if, 540 00:27:10,020 --> 00:27:14,790 else if, else if, else, basically as long as you want. 541 00:27:14,790 --> 00:27:17,110 And when constructed like this, these are all going to be 542 00:27:17,110 --> 00:27:19,206 exclusive of each other. 543 00:27:19,206 --> 00:27:19,650 Yes? 544 00:27:19,650 --> 00:27:24,130 >> AUDIENCE: So why can't you just use another if instead of [INAUDIBLE]? 545 00:27:24,130 --> 00:27:26,990 >> LAUREN CARVALHO: In this case, yes I could use another if, and it would be 546 00:27:26,990 --> 00:27:29,190 absolutely fine because the conditions themselves are 547 00:27:29,190 --> 00:27:30,600 exclusive of each other. 548 00:27:30,600 --> 00:27:33,560 But if the conditions were not exclusive of each other, this would 549 00:27:33,560 --> 00:27:37,596 force them to never both evaluate, if that makes sense. 550 00:27:37,596 --> 00:27:44,195 >> AUDIENCE: In that case, could we nest the second if in the first if? 551 00:27:44,195 --> 00:27:46,160 >> LAUREN CARVALHO: If-- what do you mean? 552 00:27:46,160 --> 00:27:49,660 >> AUDIENCE: If the two conditions are not mutually exclusive? 553 00:27:49,660 --> 00:27:54,700 >> LAUREN CARVALHO: If the conditions are not mutually exclusive? 554 00:27:54,700 --> 00:27:58,000 But you want only one of them to run? 555 00:27:58,000 --> 00:28:00,260 >> AUDIENCE: You want just if instead of else. 556 00:28:00,260 --> 00:28:01,020 >> LAUREN CARVALHO: Yeah. 557 00:28:01,020 --> 00:28:03,630 There are always going to be lots of ways that you can do this. 558 00:28:03,630 --> 00:28:06,600 You can definitely nest them. 559 00:28:06,600 --> 00:28:10,390 And you'll see in a future slide that you can have multiple ifs, just one 560 00:28:10,390 --> 00:28:11,680 after another. 561 00:28:11,680 --> 00:28:14,840 But this is a way of logically organizing the flow of your program. 562 00:28:14,840 --> 00:28:18,000 >> LUCAS FREITAS: And there's one more thing, is that you asked about putting 563 00:28:18,000 --> 00:28:20,110 if and then another if. 564 00:28:20,110 --> 00:28:25,240 That would work perfectly, but it's bad design because basically if you 565 00:28:25,240 --> 00:28:29,320 check the first if, and you notice that the number is greater than 0, you 566 00:28:29,320 --> 00:28:30,660 can just finish the program. 567 00:28:30,660 --> 00:28:33,080 You don't have to check if it's less than 0, right? 568 00:28:33,080 --> 00:28:37,510 >> But if you do if if, it's going to check both, while if I do if else is, 569 00:28:37,510 --> 00:28:39,260 it's going to check if it's greater than 0. 570 00:28:39,260 --> 00:28:43,500 If it is, it's not going to even look at the other lines of the code. 571 00:28:43,500 --> 00:28:48,560 So you're basically checking it like twice unnecessarily, so that's why we 572 00:28:48,560 --> 00:28:50,750 do if else if. 573 00:28:50,750 --> 00:28:52,350 Any other questions? 574 00:28:52,350 --> 00:28:56,547 >> AUDIENCE: So the functionality of the else if is the same as 575 00:28:56,547 --> 00:28:57,790 if it was just if? 576 00:28:57,790 --> 00:28:58,470 >> LAUREN CARVALHO: No. 577 00:28:58,470 --> 00:29:02,720 You're actually going to see in my next slide an example in which it can 578 00:29:02,720 --> 00:29:03,270 be different. 579 00:29:03,270 --> 00:29:05,690 In this case, it's almost the same. 580 00:29:05,690 --> 00:29:10,190 581 00:29:10,190 --> 00:29:15,410 >> So here, we have an example of multiple if statements, and the 582 00:29:15,410 --> 00:29:20,020 functionality is not the same as if I had just said if 583 00:29:20,020 --> 00:29:23,120 else, if else, if else. 584 00:29:23,120 --> 00:29:26,970 Let's just think about what would print out if I enter the number 95 585 00:29:26,970 --> 00:29:27,715 into this program. 586 00:29:27,715 --> 00:29:29,764 >> AUDIENCE: [INAUDIBLE] 587 00:29:29,764 --> 00:29:31,102 >> LAUREN CARVALHO: And then? 588 00:29:31,102 --> 00:29:32,340 >> AUDIENCE: [INAUDIBLE] 589 00:29:32,340 --> 00:29:33,680 >> LAUREN CARVALHO: Yeah. 590 00:29:33,680 --> 00:29:37,430 So in this case, these conditions are not mutually exclusive of each other. 591 00:29:37,430 --> 00:29:41,040 And because there's nothing stopping this condition from evaluating if this 592 00:29:41,040 --> 00:29:44,730 one already has, they're just all going to print out. 593 00:29:44,730 --> 00:29:46,222 So how could I fix this? 594 00:29:46,222 --> 00:29:47,608 >> AUDIENCE: else ifs? 595 00:29:47,608 --> 00:29:48,910 >> LAUREN CARVALHO: You can use else ifs. 596 00:29:48,910 --> 00:29:50,590 I could change the order of these, right? 597 00:29:50,590 --> 00:29:53,800 If I did this one first? 598 00:29:53,800 --> 00:29:55,120 Well, actually, no. 599 00:29:55,120 --> 00:29:57,100 That wouldn't work for 95. 600 00:29:57,100 --> 00:29:58,490 >> LUCAS FREITAS: No, just the else ifs. 601 00:29:58,490 --> 00:30:01,720 >> LAUREN CARVALHO: The else ifs would have fixed this. 602 00:30:01,720 --> 00:30:07,050 Does that clear up the confusion between ifs and else ifs? 603 00:30:07,050 --> 00:30:11,480 >> AUDIENCE: I just wanted to ask you, if you did use else if, it would stop? 604 00:30:11,480 --> 00:30:11,840 >> LAUREN CARVALHO: Right. 605 00:30:11,840 --> 00:30:13,330 So only this first one. 606 00:30:13,330 --> 00:30:16,535 If I entered the number 95, only this would have printed out, and it never 607 00:30:16,535 --> 00:30:17,934 would have progressed to the else. 608 00:30:17,934 --> 00:30:22,570 >> LUCAS FREITAS: Just kind of think of it as if this happens, do this. 609 00:30:22,570 --> 00:30:26,990 The else is like if not, check for this, and then do that. 610 00:30:26,990 --> 00:30:28,690 So like they're all mutually exclusive. 611 00:30:28,690 --> 00:30:30,270 You cannot get into two over those. 612 00:30:30,270 --> 00:30:32,800 613 00:30:32,800 --> 00:30:37,630 >> AUDIENCE: Can you use a logical AND for that situation? 614 00:30:37,630 --> 00:30:39,370 >> LAUREN CARVALHO: Yeah. 615 00:30:39,370 --> 00:30:41,400 That's another way that we could have fixed this problem. 616 00:30:41,400 --> 00:30:43,470 We're could have defined ranges, right? 617 00:30:43,470 --> 00:30:49,410 So I could have said if n is greater than 90 and less than-- 618 00:30:49,410 --> 00:30:52,135 well, I guess this one doesn't really matter, but here I could have said if 619 00:30:52,135 --> 00:30:53,960 n is greater than 80 and less than 90. 620 00:30:53,960 --> 00:30:55,570 Greater than 70, less than 80. 621 00:30:55,570 --> 00:30:59,077 That would have also worked. 622 00:30:59,077 --> 00:30:59,891 >> [LAUGHTER] 623 00:30:59,891 --> 00:31:00,980 >> LUCAS FREITAS: She's scared. 624 00:31:00,980 --> 00:31:02,070 >> LAUREN CARVALHO: Don't be afraid. 625 00:31:02,070 --> 00:31:04,570 >> LUCAS FREITAS: It's not that it will be wrong. 626 00:31:04,570 --> 00:31:08,630 It's just that it's unnecessarily verbose. 627 00:31:08,630 --> 00:31:13,480 Like in computer science, one should be as simple as possible as long as 628 00:31:13,480 --> 00:31:14,730 you have the functionality. 629 00:31:14,730 --> 00:31:16,725 630 00:31:16,725 --> 00:31:17,835 Do you want the candy? 631 00:31:17,835 --> 00:31:19,084 >> AUDIENCE: Don't need it. 632 00:31:19,084 --> 00:31:20,340 >> LAUREN CARVALHO: You're scaring everyone, Lucas. 633 00:31:20,340 --> 00:31:21,590 >> LUCAS FREITAS: Sorry. 634 00:31:21,590 --> 00:31:24,650 635 00:31:24,650 --> 00:31:29,510 >> LAUREN CARVALHO: David also mentioned these switch statement in class. 636 00:31:29,510 --> 00:31:33,180 Basically what's going on is the switch statement takes a variable. 637 00:31:33,180 --> 00:31:35,760 In this case, we're taking an integer. 638 00:31:35,760 --> 00:31:39,220 >> If the integer is 1, we're going to print out, "You picked a small 639 00:31:39,220 --> 00:31:42,000 number." If the integer is 1, "You picked a medium number." If the 640 00:31:42,000 --> 00:31:45,730 integer is 3, "You picked a high number." And I also had a default case 641 00:31:45,730 --> 00:31:50,020 down here, but it's not showing up, which is too bad. 642 00:31:50,020 --> 00:31:53,120 643 00:31:53,120 --> 00:31:55,360 >> Basically, this is just another syntax. 644 00:31:55,360 --> 00:31:58,640 We could have implemented this exact same thing with if and else if 645 00:31:58,640 --> 00:31:59,390 statements. 646 00:31:59,390 --> 00:32:05,530 But when there are lots of different conditions and lots of different 647 00:32:05,530 --> 00:32:10,350 variations on what you want to do for each input, this is an option. 648 00:32:10,350 --> 00:32:13,262 So it's just a good syntax to know. 649 00:32:13,262 --> 00:32:15,099 Yes? 650 00:32:15,099 --> 00:32:21,380 >> AUDIENCE: Why is there not a backslash n in the first one? 651 00:32:21,380 --> 00:32:23,340 >> LAUREN CARVALHO: For up there? 652 00:32:23,340 --> 00:32:27,600 There's no backslash n up there because the GetInt-- 653 00:32:27,600 --> 00:32:30,140 if you try this, it'll give you this prompt. 654 00:32:30,140 --> 00:32:32,290 Give me an integer between 1 and 3. 655 00:32:32,290 --> 00:32:35,550 And if I put the backlash n, it would take me to the next line, but I just 656 00:32:35,550 --> 00:32:38,690 wanted them to type the integer right after the colon. 657 00:32:38,690 --> 00:32:40,120 You could have put the backlash n. 658 00:32:40,120 --> 00:32:42,374 It's not a big deal. 659 00:32:42,374 --> 00:32:43,288 Yeah? 660 00:32:43,288 --> 00:32:50,525 >> AUDIENCE: For this code, is this case one, is that because 661 00:32:50,525 --> 00:32:51,760 it's the first case? 662 00:32:51,760 --> 00:32:52,260 Because-- 663 00:32:52,260 --> 00:32:53,400 >> LAUREN CARVALHO: No, no, no. 664 00:32:53,400 --> 00:32:56,480 So this is give me an integer between 1 and 3. 665 00:32:56,480 --> 00:32:59,070 If n is 1, I print this. 666 00:32:59,070 --> 00:33:02,740 If I had asked for letters, I could have said give me a letter between A 667 00:33:02,740 --> 00:33:04,935 and C. Then this would be case A. 668 00:33:04,935 --> 00:33:05,910 >> AUDIENCE: [INAUDIBLE]. 669 00:33:05,910 --> 00:33:08,090 The case would just [INAUDIBLE] 670 00:33:08,090 --> 00:33:11,260 >> LAUREN CARVALHO: Yeah, right. 671 00:33:11,260 --> 00:33:11,550 Yeah? 672 00:33:11,550 --> 00:33:14,861 >> AUDIENCE: What if you wanted to have more of a relational expression? 673 00:33:14,861 --> 00:33:17,645 So instead of saying n equals 1, [INAUDIBLE] 674 00:33:17,645 --> 00:33:21,050 n is greater than 1 in the first case? 675 00:33:21,050 --> 00:33:26,630 >> LAUREN CARVALHO: Actually, if I do not define some of these cases, this would 676 00:33:26,630 --> 00:33:28,500 be carry down. 677 00:33:28,500 --> 00:33:30,680 So actually, maybe this isn't what you're asking. 678 00:33:30,680 --> 00:33:36,240 >> LUCAS FREITAS: I think she's asking if the case was n greater than 0, case 679 00:33:36,240 --> 00:33:37,150 less than 0. 680 00:33:37,150 --> 00:33:39,210 >> LAUREN CARVALHO: Maybe the switch statement is not really what you want 681 00:33:39,210 --> 00:33:39,880 to use in that case. 682 00:33:39,880 --> 00:33:42,420 >> LUCAS FREITAS: The switch statement is just for value, so you're checking of 683 00:33:42,420 --> 00:33:45,370 the value is like this or that. 684 00:33:45,370 --> 00:33:47,025 So you can not actually put conditions. 685 00:33:47,025 --> 00:33:50,988 686 00:33:50,988 --> 00:33:55,920 >> AUDIENCE: What if the person types with [INAUDIBLE]? 687 00:33:55,920 --> 00:33:58,240 >> LAUREN CARVALHO: That would have been my default case, which unfortunately 688 00:33:58,240 --> 00:33:58,910 you can't see. 689 00:33:58,910 --> 00:34:02,240 >> LUCAS FREITAS: [INAUDIBLE] make them see. 690 00:34:02,240 --> 00:34:03,690 >> LAUREN CARVALHO: Actually, you know what? 691 00:34:03,690 --> 00:34:07,795 Let's just go to study 50, and I will just zoom in on the slides on study 692 00:34:07,795 --> 00:34:10,610 50, because this is just annoying. 693 00:34:10,610 --> 00:34:14,938 So let me actually go to condition. 694 00:34:14,938 --> 00:34:17,860 This is just going to take a little while. 695 00:34:17,860 --> 00:34:26,940 696 00:34:26,940 --> 00:34:27,840 >> All right. 697 00:34:27,840 --> 00:34:29,590 So, I'm sorry. 698 00:34:29,590 --> 00:34:33,625 At least now we'll be able to see everything without it cutting off. 699 00:34:33,625 --> 00:34:36,230 700 00:34:36,230 --> 00:34:36,969 All right. 701 00:34:36,969 --> 00:34:43,800 >> So if they put in a 10, we would default here to my default case, and I 702 00:34:43,800 --> 00:34:46,510 would just print invalid because I asked them to enter an integer 703 00:34:46,510 --> 00:34:49,460 between 1 and 3. 704 00:34:49,460 --> 00:34:50,650 Any other questions? 705 00:34:50,650 --> 00:34:51,081 Yeah? 706 00:34:51,081 --> 00:34:57,590 >> AUDIENCE: Let's say for case one, case three, say you wanted it to be all the 707 00:34:57,590 --> 00:35:00,950 same thing, so you picked a number that's not a medium number. 708 00:35:00,950 --> 00:35:03,840 In that case, would it be redundant? 709 00:35:03,840 --> 00:35:08,371 Would you just change the text in both of them, or would there be some 710 00:35:08,371 --> 00:35:08,617 redundancy? 711 00:35:08,617 --> 00:35:11,920 Is there any way to combine [INAUDIBLE]? 712 00:35:11,920 --> 00:35:15,810 >> LAUREN CARVALHO: There are ways to combine them, but I believe you can 713 00:35:15,810 --> 00:35:18,730 only combine adjacent ones. 714 00:35:18,730 --> 00:35:22,250 So if I did not define anything for case two, this would 715 00:35:22,250 --> 00:35:24,130 apply to both cases. 716 00:35:24,130 --> 00:35:28,110 Oh, if I did not put anything for case one, this would apply to both 717 00:35:28,110 --> 00:35:30,620 case one and two. 718 00:35:30,620 --> 00:35:33,616 If that makes sense. 719 00:35:33,616 --> 00:35:35,872 >> AUDIENCE: Can you repeat that one? 720 00:35:35,872 --> 00:35:36,670 >> LUCAS FREITAS: OK. 721 00:35:36,670 --> 00:35:39,950 If you did basically, like, you have the same thing, but you don't put 722 00:35:39,950 --> 00:35:46,820 anything here, you'd be either for case one or case two, do this. 723 00:35:46,820 --> 00:35:47,930 Right? 724 00:35:47,930 --> 00:35:54,010 And this break means if you get into this case, just leave. 725 00:35:54,010 --> 00:35:55,030 So that's what the brake means. 726 00:35:55,030 --> 00:35:59,755 So if I just did this, you look, it's case one, and you would just do this. 727 00:35:59,755 --> 00:36:02,110 If it was case two, it would also just come here and do that. 728 00:36:02,110 --> 00:36:03,544 >> AUDIENCE: But you can do it? 729 00:36:03,544 --> 00:36:05,140 If there's something for case two [INAUDIBLE] 730 00:36:05,140 --> 00:36:07,095 >> LAUREN CARVALHO: I don't believe you can do that. 731 00:36:07,095 --> 00:36:09,380 I don't think you can skip around. 732 00:36:09,380 --> 00:36:11,070 Can you do that, Rob? 733 00:36:11,070 --> 00:36:13,300 I don't think so. 734 00:36:13,300 --> 00:36:16,548 >> AUDIENCE: So only one number can go after a case, right? 735 00:36:16,548 --> 00:36:21,410 There can't be case one, comma, two? 736 00:36:21,410 --> 00:36:21,970 >> LUCAS FREITAS: No, you can't. 737 00:36:21,970 --> 00:36:25,085 You definitely can't. 738 00:36:25,085 --> 00:36:28,770 >> LAUREN CARVALHO: I mean, the point is this is an option, and this might not 739 00:36:28,770 --> 00:36:31,940 fit your use case, so then you would use the if conditions. 740 00:36:31,940 --> 00:36:33,906 >> AUDIENCE: So why would you do this? 741 00:36:33,906 --> 00:36:34,314 The switch? 742 00:36:34,314 --> 00:36:38,540 >> LAUREN CARVALHO: I would use this if you have very defined-- 743 00:36:38,540 --> 00:36:39,730 actually, Lucas, is you're blocking. 744 00:36:39,730 --> 00:36:40,982 >> LUCAS FREITAS: Oh, sorry. 745 00:36:40,982 --> 00:36:44,200 >> LAUREN CARVALHO: If you have very defined behaviors that you want for 746 00:36:44,200 --> 00:36:48,105 each and every input, this might be a little more concise than writing out 747 00:36:48,105 --> 00:36:49,530 an if statement for each one. 748 00:36:49,530 --> 00:36:51,210 >> AUDIENCE: What does the break mean again? 749 00:36:51,210 --> 00:36:53,180 >> LAUREN CARVALHO: The break means stop. 750 00:36:53,180 --> 00:36:56,860 Remember, if I didn't have this break here, we would have just fallen down 751 00:36:56,860 --> 00:36:58,110 into the next case. 752 00:36:58,110 --> 00:37:00,820 >> LUCAS FREITAS: Break means to get out of the switch statement. 753 00:37:00,820 --> 00:37:03,400 Because if it's 1, I want to print this and leave. 754 00:37:03,400 --> 00:37:04,640 I don't want to check anything else. 755 00:37:04,640 --> 00:37:05,790 >> AUDIENCE: You don't want to print anything else. 756 00:37:05,790 --> 00:37:06,730 >> LUCAS FREITAS: Yeah. 757 00:37:06,730 --> 00:37:08,260 Because it's mutually exclusive. 758 00:37:08,260 --> 00:37:11,378 >> AUDIENCE: And does this only proceed to the next case if the first case 759 00:37:11,378 --> 00:37:11,850 isn't true? 760 00:37:11,850 --> 00:37:14,120 Or does it test all the cases at the same time? 761 00:37:14,120 --> 00:37:16,300 >> LUCAS FREITAS: No, it checks sequentially. 762 00:37:16,300 --> 00:37:18,070 Sequentially, yeah. 763 00:37:18,070 --> 00:37:21,840 One more question, then we've got to move on so we can cover everything. 764 00:37:21,840 --> 00:37:22,300 Yeah? 765 00:37:22,300 --> 00:37:25,280 >> AUDIENCE: So for the case, could have done something besides just the one? 766 00:37:25,280 --> 00:37:27,680 Could you give it a range? 767 00:37:27,680 --> 00:37:28,580 >> LUCAS FREITAS: No. 768 00:37:28,580 --> 00:37:29,900 >> LAUREN CARVALHO: I don't think you can give it a range. 769 00:37:29,900 --> 00:37:31,430 >> LUCAS FREITAS: You cannot you cannot give it a range. 770 00:37:31,430 --> 00:37:33,190 >> AUDIENCE: The case is specific [INAUDIBLE]. 771 00:37:33,190 --> 00:37:34,840 >> LUCAS FREITAS: Switch is sort of values. 772 00:37:34,840 --> 00:37:38,400 You doing different things depending on a value, a specific value. 773 00:37:38,400 --> 00:37:41,050 You cannot do range, you cannot do anything else. 774 00:37:41,050 --> 00:37:43,055 It's just another tool to see. 775 00:37:43,055 --> 00:37:46,570 >> LAUREN CARVALHO: Most people don't use switch statements very much in CS50, 776 00:37:46,570 --> 00:37:48,760 but it's there. 777 00:37:48,760 --> 00:37:49,920 >> All right. 778 00:37:49,920 --> 00:37:53,870 So the next thing we're going to talk about is the ternary operator. 779 00:37:53,870 --> 00:37:56,650 This is just another type of conditional. 780 00:37:56,650 --> 00:38:01,460 Basically what's going on here is I'm asking for an integer, and if n is 781 00:38:01,460 --> 00:38:05,550 greater than 100, I'm going to print out "high." If it's not, I'm going to 782 00:38:05,550 --> 00:38:07,010 print out "low." 783 00:38:07,010 --> 00:38:11,230 >> So what it's doing is the condition is right here in the parentheses, and you 784 00:38:11,230 --> 00:38:13,550 can kind of think of the question as I'm asking whether 785 00:38:13,550 --> 00:38:15,030 this is true or not. 786 00:38:15,030 --> 00:38:18,485 If it's true, I'm going to do this first thing, and if it's not true, I'm 787 00:38:18,485 --> 00:38:20,020 going to do this second thing. 788 00:38:20,020 --> 00:38:23,520 789 00:38:23,520 --> 00:38:27,960 >> Any questions about the ternary operator? 790 00:38:27,960 --> 00:38:30,880 >> LUCAS FREITAS: Just want note from David is that whenever you have a 791 00:38:30,880 --> 00:38:33,346 condition-- 792 00:38:33,346 --> 00:38:35,690 >> LAUREN CARVALHO: I guess David's watching via the live stream. 793 00:38:35,690 --> 00:38:37,210 >> LUCAS FREITAS: He's doing life stream right now. 794 00:38:37,210 --> 00:38:41,730 So whenever you have a condition, you're supposed to put curly braces, 795 00:38:41,730 --> 00:38:45,320 just like we have in a switch statement here. 796 00:38:45,320 --> 00:38:46,750 We have curly braces here. 797 00:38:46,750 --> 00:38:49,560 For whatever conditions, you're supposed to also put that. 798 00:38:49,560 --> 00:38:50,820 >> LAUREN CARVALHO: You'll see this in the style guide. 799 00:38:50,820 --> 00:38:52,900 We don't actually have the style guide right now. 800 00:38:52,900 --> 00:38:53,930 I think David's still working. 801 00:38:53,930 --> 00:38:55,100 >> LUCAS FREITAS: He said that it's just working now. 802 00:38:55,100 --> 00:38:56,120 >> LAUREN CARVALHO: Oh, is it working now? 803 00:38:56,120 --> 00:38:57,520 OK. 804 00:38:57,520 --> 00:39:02,060 So apparently this year in the style guide, even if you just have one line, 805 00:39:02,060 --> 00:39:06,040 you've got to put those curly braces in. 806 00:39:06,040 --> 00:39:09,480 You don't necessarily need to for the functionality of the program. 807 00:39:09,480 --> 00:39:12,210 And maybe we should just take a break after the conditions and go straight 808 00:39:12,210 --> 00:39:14,620 to the style guide to clear up some of these things? 809 00:39:14,620 --> 00:39:15,010 >> LUCAS FREITAS: Yeah. 810 00:39:15,010 --> 00:39:15,775 Do you have a question? 811 00:39:15,775 --> 00:39:17,580 >> AUDIENCE: No, I'll just wait till the style guide. 812 00:39:17,580 --> 00:39:19,578 >> AUDIENCE: Could you go back to the ternary operator? 813 00:39:19,578 --> 00:39:23,360 >> LAUREN CARVALHO: Ternary, yes. 814 00:39:23,360 --> 00:39:29,010 >> LUCAS FREITAS: Ternary operator is basically an if else statement, but 815 00:39:29,010 --> 00:39:33,590 instead of having to have the two lines of checking an if and then the 816 00:39:33,590 --> 00:39:36,600 else, you just have one line. 817 00:39:36,600 --> 00:39:40,930 So basically if I want to give a string a value of high or low, and I 818 00:39:40,930 --> 00:39:46,270 want to do a high if n is greater than 100 or low if it's less, what I can do 819 00:39:46,270 --> 00:39:47,730 is this [INAUDIBLE] 820 00:39:47,730 --> 00:39:53,060 the string is going to be, and then I ask the program is n greater than 100? 821 00:39:53,060 --> 00:39:55,750 >> So that's why I have this expression in parentheses and then 822 00:39:55,750 --> 00:39:56,580 the question mark. 823 00:39:56,580 --> 00:39:59,110 I'm asking is it greater than 100? 824 00:39:59,110 --> 00:40:02,310 If it is, it's going to be high. 825 00:40:02,310 --> 00:40:03,850 If it's not, it's going to be low. 826 00:40:03,850 --> 00:40:07,310 >> So basically what you think is you ask a question, so you put in parentheses 827 00:40:07,310 --> 00:40:08,620 the condition. 828 00:40:08,620 --> 00:40:11,160 Then you put the question mark because you're asking it. 829 00:40:11,160 --> 00:40:15,240 And if this is true, it's going to be in the first value [INAUDIBLE] here. 830 00:40:15,240 --> 00:40:18,176 If it's not, I'm going to put the value that is after the colon. 831 00:40:18,176 --> 00:40:21,580 Does it make sense? 832 00:40:21,580 --> 00:40:24,730 >> It's also something that is like a tool in C but you don't have to 833 00:40:24,730 --> 00:40:25,700 specifically use it. 834 00:40:25,700 --> 00:40:29,890 But CS people usually like that because it just saves lines. 835 00:40:29,890 --> 00:40:31,050 >> LAUREN CARVALHO: It's very elegant. 836 00:40:31,050 --> 00:40:32,951 It does a lot in just one statement. 837 00:40:32,951 --> 00:40:36,077 >> AUDIENCE: So the percent sign is just whatever the results on 838 00:40:36,077 --> 00:40:36,799 the previous line? 839 00:40:36,799 --> 00:40:38,260 >> LAUREN CARVALHO: Oh, I'm sorry. 840 00:40:38,260 --> 00:40:42,670 This percent sign is actually a placeholder for this s. 841 00:40:42,670 --> 00:40:46,160 In a printf statement, you've got this string here, which is what's going to 842 00:40:46,160 --> 00:40:48,440 print out, but you can't just put this s in there. 843 00:40:48,440 --> 00:40:50,290 Otherwise it would print out the s literally. 844 00:40:50,290 --> 00:40:54,070 So we put a percent here to keep this place held for whatever 845 00:40:54,070 --> 00:40:57,590 comes after the comma. 846 00:40:57,590 --> 00:40:59,470 >> So for a string, you've got percent s. 847 00:40:59,470 --> 00:41:00,950 For an integer, percent i. 848 00:41:00,950 --> 00:41:03,330 For a float, percent f. 849 00:41:03,330 --> 00:41:06,960 You'll see those as we see more examples. 850 00:41:06,960 --> 00:41:09,230 But maybe you should talk about the style guide for a little while, now 851 00:41:09,230 --> 00:41:10,480 that it's up. 852 00:41:10,480 --> 00:41:21,040 853 00:41:21,040 --> 00:41:24,010 >> LUCAS FREITAS: If you go to cs50.net/style, you're going to see 854 00:41:24,010 --> 00:41:25,760 the style guide. 855 00:41:25,760 --> 00:41:32,420 For problem sets, your score for the problem set is composed of scope, 856 00:41:32,420 --> 00:41:35,510 which basically means if you did all the parts of problem set or if you 857 00:41:35,510 --> 00:41:37,170 attempted something. 858 00:41:37,170 --> 00:41:41,120 The second thing is correctness, which means is your program doing what it 859 00:41:41,120 --> 00:41:44,520 was supposed to be doing, or does it have bugs, or is it not doing what 860 00:41:44,520 --> 00:41:45,620 it's supposed to do. 861 00:41:45,620 --> 00:41:51,420 >> The third thing is design, which means are you implementing this well, or are 862 00:41:51,420 --> 00:41:54,520 you using unnecessary statements? 863 00:41:54,520 --> 00:41:59,400 So for example, if you want to repeat a number 10 times, are you writing a 864 00:41:59,400 --> 00:42:03,040 for loop or are you just doing 10 times the same statement, just doing 865 00:42:03,040 --> 00:42:04,060 copy and paste. 866 00:42:04,060 --> 00:42:05,760 So design is for that. 867 00:42:05,760 --> 00:42:07,420 >> And finally, you have style. 868 00:42:07,420 --> 00:42:12,000 Style is just like judging if your code looks 869 00:42:12,000 --> 00:42:14,050 readable and kind of pretty. 870 00:42:14,050 --> 00:42:16,520 It just basically means is your code readable? 871 00:42:16,520 --> 00:42:20,560 Would someone else who was looking are your code be able to understand what 872 00:42:20,560 --> 00:42:21,250 it's doing? 873 00:42:21,250 --> 00:42:25,360 Does it have comments and the like? 874 00:42:25,360 --> 00:42:30,430 >> Let's talk about the style that you're supposed to use. 875 00:42:30,430 --> 00:42:32,200 The first thing is comments. 876 00:42:32,200 --> 00:42:35,280 Comments are very important for your code because sometimes you're doing 877 00:42:35,280 --> 00:42:38,750 your program, and you know exactly what you want to do so you just start 878 00:42:38,750 --> 00:42:43,120 coding, just type up a bunch of C. But if someone else is looking are you're 879 00:42:43,120 --> 00:42:47,500 code, they're not going to be able to understand it because they don't know 880 00:42:47,500 --> 00:42:51,420 the things that are in your head, like the pesudocode, so maybe they will 881 00:42:51,420 --> 00:42:53,970 have a hard time to understand what the code is doing. 882 00:42:53,970 --> 00:42:57,900 >> So comments are really useful to explain to someone who's looking at 883 00:42:57,900 --> 00:43:00,880 your code what it's doing, and actually even for you, 884 00:43:00,880 --> 00:43:01,880 they're very useful. 885 00:43:01,880 --> 00:43:05,740 As you go to harder and hard Psets, you're going to see that the code gets 886 00:43:05,740 --> 00:43:10,590 longer, and then comments are really important so you remember what each 887 00:43:10,590 --> 00:43:13,320 part of your program does. 888 00:43:13,320 --> 00:43:16,920 >> To put a comment, you can just do a slash slash and then write a comment. 889 00:43:16,920 --> 00:43:21,250 And the whole line is part of a comment, and whatever you write down, 890 00:43:21,250 --> 00:43:24,550 it's not going to be used for your code. 891 00:43:24,550 --> 00:43:27,993 It's basically like the compiler is going to ignore that. 892 00:43:27,993 --> 00:43:29,290 Does that make sense? 893 00:43:29,290 --> 00:43:31,710 >> AUDIENCE: What do you do again to write a comment? 894 00:43:31,710 --> 00:43:33,260 >> LUCAS FREITAS: This? 895 00:43:33,260 --> 00:43:36,020 The slash slash? 896 00:43:36,020 --> 00:43:39,810 Whenever you type slash slash, whatever is in that line is not going 897 00:43:39,810 --> 00:43:42,260 to be considered to be C. Yeah? 898 00:43:42,260 --> 00:43:45,910 >> AUDIENCE: So after the comment, you just enter and start a new line? 899 00:43:45,910 --> 00:43:47,120 >> LUCAS FREITAS: Yeah, exactly. 900 00:43:47,120 --> 00:43:50,010 >> LAUREN CARVALHO: The slash slash is for single line comments. 901 00:43:50,010 --> 00:43:52,910 >> LUCAS FREITAS: For example, here we have-- 902 00:43:52,910 --> 00:43:54,160 let me zoom in-- 903 00:43:54,160 --> 00:43:56,530 904 00:43:56,530 --> 00:44:01,180 for example, here I'm declaring a float, but maybe if you just put this 905 00:44:01,180 --> 00:44:04,980 in your code, like you have no idea what this is doing, like actually 906 00:44:04,980 --> 00:44:06,950 someone who's looking at this code is going to be, like, what the 907 00:44:06,950 --> 00:44:09,190 heck is going on? 908 00:44:09,190 --> 00:44:12,890 >> If you put a comment saying "convert Fahrenheit to Celsius, it makes much 909 00:44:12,890 --> 00:44:13,450 more sense. 910 00:44:13,450 --> 00:44:15,480 You're, like, OK, you're doing a conversion. 911 00:44:15,480 --> 00:44:16,730 Does that make sense? 912 00:44:16,730 --> 00:44:19,860 913 00:44:19,860 --> 00:44:24,960 So it's just something that helps people ready your code and also make 914 00:44:24,960 --> 00:44:28,290 you be able to organizing your code better. 915 00:44:28,290 --> 00:44:33,080 >> The second thing is that you can do multi-line comments, so have one 916 00:44:33,080 --> 00:44:34,850 comment for a bunch of lines. 917 00:44:34,850 --> 00:44:38,880 Instead of having to put slash slash, the first line comments, slash slash, 918 00:44:38,880 --> 00:44:43,590 the second line, and so on, you can use the slash star. 919 00:44:43,590 --> 00:44:46,330 The way that you do it is that for the first line of the comment, you're 920 00:44:46,330 --> 00:44:49,650 going to do a slash star star, so just like that. 921 00:44:49,650 --> 00:44:56,900 Just like here we have the beginning of a program, you usually put who you 922 00:44:56,900 --> 00:44:59,530 are, what this program does, and stuff like that. 923 00:44:59,530 --> 00:45:01,900 It's kind of like a header for the program. 924 00:45:01,900 --> 00:45:04,540 >> Again, it's not something that a compiler is going to care about, but 925 00:45:04,540 --> 00:45:07,570 it's important for you to know what this program does or for other people 926 00:45:07,570 --> 00:45:09,960 to know what your program is doing. 927 00:45:09,960 --> 00:45:17,480 So you do slash star star, and then the last line you're going to do star 928 00:45:17,480 --> 00:45:22,250 slash to be able to close that comment. 929 00:45:22,250 --> 00:45:26,140 So basically for all the other lines, you can just write whatever, and this 930 00:45:26,140 --> 00:45:27,390 is all going to be ignored. 931 00:45:27,390 --> 00:45:32,920 932 00:45:32,920 --> 00:45:37,780 >> And also, just as style, you see that each of the lines has a star, and all 933 00:45:37,780 --> 00:45:39,380 the stars are aligned. 934 00:45:39,380 --> 00:45:41,605 These are important things so that your code looks pretty. 935 00:45:41,605 --> 00:45:42,855 Sounds good? 936 00:45:42,855 --> 00:45:45,320 937 00:45:45,320 --> 00:45:47,290 >> Now, conditions. 938 00:45:47,290 --> 00:45:52,240 What David was saying that we should actually put in those slides, is that 939 00:45:52,240 --> 00:45:55,810 when you have any kind of if statements or any kind of conditions, 940 00:45:55,810 --> 00:46:00,700 you should use curly braces to show that this is what was going to happen 941 00:46:00,700 --> 00:46:02,730 when you have the condition. 942 00:46:02,730 --> 00:46:07,560 Technically, when you have a condition and then the next line is just one 943 00:46:07,560 --> 00:46:12,110 line, you don't technically have to put the curly braces. 944 00:46:12,110 --> 00:46:16,930 >> Basically the compiler would understand it anyway as implicit. 945 00:46:16,930 --> 00:46:21,100 But for CS50, we ask you guys to put the curly braces just so that your 946 00:46:21,100 --> 00:46:24,560 code is more organized and more readable. 947 00:46:24,560 --> 00:46:28,200 Because that code gets longer if you don't have curly braces and such. 948 00:46:28,200 --> 00:46:29,150 It gets really messy. 949 00:46:29,150 --> 00:46:31,410 >> LAUREN CARVALHO: This is exactly what David just pointed out 950 00:46:31,410 --> 00:46:33,000 in my previous slides. 951 00:46:33,000 --> 00:46:36,690 It definitely works without the curly braces, but for stylistic reasons 952 00:46:36,690 --> 00:46:38,840 we're going to ask that you do put those in. 953 00:46:38,840 --> 00:46:42,400 >> And when you start running style50, which is kind of the check 50 for 954 00:46:42,400 --> 00:46:44,860 style, it will remind you to do this. 955 00:46:44,860 --> 00:46:49,710 >> LUCAS FREITAS: And another thing is remember to put the curly braces along 956 00:46:49,710 --> 00:46:50,610 in each line. 957 00:46:50,610 --> 00:46:52,490 So you do, for example, l's. 958 00:46:52,490 --> 00:46:56,910 Go to the next line, open the curly brace, then you go to next line. 959 00:46:56,910 --> 00:46:59,560 Indentation, which is four spaces. 960 00:46:59,560 --> 00:47:03,570 Then you write whatever, then another line, and close the curly brace. 961 00:47:03,570 --> 00:47:06,870 So be sure that both those curly braces are in different lines. 962 00:47:06,870 --> 00:47:11,548 >> AUDIENCE: So if this code had like 10 else ifs, this should 963 00:47:11,548 --> 00:47:13,040 be 10 sets of brackets? 964 00:47:13,040 --> 00:47:14,610 >> LUCAS FREITAS: Yeah, for each of them. 965 00:47:14,610 --> 00:47:14,930 Yeah. 966 00:47:14,930 --> 00:47:17,980 So if, else if, and so on. 967 00:47:17,980 --> 00:47:21,990 >> AUDIENCE: Can you not press Tab to indent? 968 00:47:21,990 --> 00:47:25,350 >> LUCAS FREITAS: We're asking you guys to use four spaces. 969 00:47:25,350 --> 00:47:30,060 Some text editors translate tab as four spaces automatically. 970 00:47:30,060 --> 00:47:34,850 gedit doesn't, so you have to do space space space space. 971 00:47:34,850 --> 00:47:37,920 This is also something that some programmers only do tabs, some 972 00:47:37,920 --> 00:47:40,560 programmers only do four spaces, and some other people even do 973 00:47:40,560 --> 00:47:41,900 two or three spaces. 974 00:47:41,900 --> 00:47:44,040 It's just that here we're asking you guys to do four spaces. 975 00:47:44,040 --> 00:47:47,556 >> AUDIENCE: There's almost definitely got to be a way for gedit to 976 00:47:47,556 --> 00:47:48,645 automatically [INAUDIBLE]. 977 00:47:48,645 --> 00:47:50,480 Like no [INAUDIBLE] does that. 978 00:47:50,480 --> 00:47:51,150 >> LUCAS FREITAS: Yeah. 979 00:47:51,150 --> 00:47:53,165 I think you can go to Settings. 980 00:47:53,165 --> 00:47:55,270 >> AUDIENCE: It already does all of that. 981 00:47:55,270 --> 00:47:56,410 >> LUCAS FREITAS: It already does? 982 00:47:56,410 --> 00:47:56,890 OK. 983 00:47:56,890 --> 00:48:00,854 So just be sure that you have four spaces. 984 00:48:00,854 --> 00:48:05,880 >> AUDIENCE: Do we have to put the curly brackets in for loops as well? 985 00:48:05,880 --> 00:48:07,470 >> LUCAS FREITAS: Yes. 986 00:48:07,470 --> 00:48:11,610 >> LAUREN CARVALHO: Whenever your encapsulating a block of code, just 987 00:48:11,610 --> 00:48:13,570 please use the curly braces. 988 00:48:13,570 --> 00:48:18,040 And before you think that we're just being sticklers about this for no good 989 00:48:18,040 --> 00:48:23,450 reason, if you guys do go on to get tech internships at big companies, 990 00:48:23,450 --> 00:48:26,020 they will give you a style guide, and they will tell you this is what your 991 00:48:26,020 --> 00:48:27,220 code needs to look like. 992 00:48:27,220 --> 00:48:31,120 And even though you learned to code like this, maybe in your internship 993 00:48:31,120 --> 00:48:32,680 you'll have to do something completely different. 994 00:48:32,680 --> 00:48:35,115 They do expect you to conform to their style. 995 00:48:35,115 --> 00:48:39,800 >> LUCAS FREITAS: And one more thing is that do you notice how i have like x, 996 00:48:39,800 --> 00:48:42,290 space, less, space, 0? 997 00:48:42,290 --> 00:48:44,640 Whenever you have an operator-- 998 00:48:44,640 --> 00:48:50,490 it can be like plus, minus, times, less, greater, whatever operator-- 999 00:48:50,490 --> 00:48:57,560 you have to put a space on the left and the right of the operator. 1000 00:48:57,560 --> 00:49:00,570 >> LAUREN CARVALHO: Those are called binary operands. 1001 00:49:00,570 --> 00:49:03,720 Just make sure you always space out your binary operands. 1002 00:49:03,720 --> 00:49:05,465 >> LUCAS FREITAS: This is not a binary operand. 1003 00:49:05,465 --> 00:49:07,010 >> LAUREN CARVALHO: That is because it takes two. 1004 00:49:07,010 --> 00:49:08,260 >> LUCAS FREITAS: Oh, yeah. 1005 00:49:08,260 --> 00:49:13,340 1006 00:49:13,340 --> 00:49:14,590 >> OK, the switches. 1007 00:49:14,590 --> 00:49:18,180 1008 00:49:18,180 --> 00:49:23,390 First, you have to see that just like in an if or else statement, you have 1009 00:49:23,390 --> 00:49:28,280 to put the curly brace on its own line and under the curly brace here. 1010 00:49:28,280 --> 00:49:32,240 Notice that there is a single space after switch, just like in if and else 1011 00:49:32,240 --> 00:49:35,360 we had a single space between that and the parentheses. 1012 00:49:35,360 --> 00:49:36,610 So you have to put that. 1013 00:49:36,610 --> 00:49:40,380 1014 00:49:40,380 --> 00:49:48,270 >> And there's no space right after each of the parentheses or before the end 1015 00:49:48,270 --> 00:49:49,080 of the parentheses. 1016 00:49:49,080 --> 00:49:52,612 So you see that it's all together, and also like up here. 1017 00:49:52,612 --> 00:49:53,940 >> Yeah? 1018 00:49:53,940 --> 00:49:58,040 >> LAUREN CARVALHO: I think we need to move on. 1019 00:49:58,040 --> 00:50:02,340 >> LUCAS FREITAS: So this is part of the style guide that you have for CS50. 1020 00:50:02,340 --> 00:50:06,520 Be sure to go to cs50.net/style and take a look at this. 1021 00:50:06,520 --> 00:50:09,250 These are all very important for your style score. 1022 00:50:09,250 --> 00:50:12,740 1023 00:50:12,740 --> 00:50:15,670 >> Let me go back to the statement. 1024 00:50:15,670 --> 00:50:17,070 >> LAUREN CARVALHO: I'd rather just use style50. 1025 00:50:17,070 --> 00:50:18,920 We're just going to go straight to loops. 1026 00:50:18,920 --> 00:50:27,580 1027 00:50:27,580 --> 00:50:28,300 All right. 1028 00:50:28,300 --> 00:50:33,720 >> So let's get back to the content, now that we've gone over the style guide. 1029 00:50:33,720 --> 00:50:37,220 Loops, you've seen them in Scratch, and we're going to talk about them in 1030 00:50:37,220 --> 00:50:39,210 C. 1031 00:50:39,210 --> 00:50:44,400 >> In a for loop, there are three portions to the 1032 00:50:44,400 --> 00:50:45,440 declaration of this for loop. 1033 00:50:45,440 --> 00:50:49,040 You've got your initialization in which you set your variables. 1034 00:50:49,040 --> 00:50:52,160 You've got your condition, which are checking to see whether something is 1035 00:50:52,160 --> 00:50:54,670 true or false and whether the loop should continue. 1036 00:50:54,670 --> 00:50:59,390 And you've got your update, which is updating your variables. 1037 00:50:59,390 --> 00:51:05,310 >> Let's just take a quick look at some C code of this for loop in action. 1038 00:51:05,310 --> 00:51:08,860 What's going on is here in the initialization, we're setting i to 0. 1039 00:51:08,860 --> 00:51:11,820 In the condition we're checking whether i is less than 10. 1040 00:51:11,820 --> 00:51:14,982 And in the update we're incrementing i. 1041 00:51:14,982 --> 00:51:20,490 So what will result from this for loop? 1042 00:51:20,490 --> 00:51:24,344 >> LUCAS FREITAS: Actually, first of all, do you guys know what i++ is? 1043 00:51:24,344 --> 00:51:25,850 >> AUDIENCE: Increased by 1. 1044 00:51:25,850 --> 00:51:26,870 >> LUCAS FREITAS: Increased by 1, yes. 1045 00:51:26,870 --> 00:51:30,020 So this is the same thing as doing i equals i plus 1. 1046 00:51:30,020 --> 00:51:35,890 And notice that we don't have a space between the i and the ++. 1047 00:51:35,890 --> 00:51:37,600 >> So do you guys know what this will do? 1048 00:51:37,600 --> 00:51:39,060 Can someone tell me? 1049 00:51:39,060 --> 00:51:41,463 And maybe get some candy because of that? 1050 00:51:41,463 --> 00:51:42,912 >> AUDIENCE: [INAUDIBLE] 1051 00:51:42,912 --> 00:51:44,850 >> LUCAS FREITAS: OK. 1052 00:51:44,850 --> 00:51:45,400 [INAUDIBLE] 1053 00:51:45,400 --> 00:51:47,450 show everybody later [INAUDIBLE]. 1054 00:51:47,450 --> 00:51:49,565 Yeah, it's going to show "This is CS50" 10 times. 1055 00:51:49,565 --> 00:51:52,150 >> LAUREN CARVALHO: I guess it was in the title of the slide. 1056 00:51:52,150 --> 00:51:53,830 >> LUCAS FREITAS: Oh, yeah. 1057 00:51:53,830 --> 00:51:54,670 >> [LAUGHTER] 1058 00:51:54,670 --> 00:51:56,790 >> LUCAS FREITAS: That helps. 1059 00:51:56,790 --> 00:51:58,370 >> LAUREN CARVALHO: So those are for loops. 1060 00:51:58,370 --> 00:52:03,200 We're going to see some more examples of for loops later on. 1061 00:52:03,200 --> 00:52:06,510 But let's actually take a look at the second type of loop you'll encounter 1062 00:52:06,510 --> 00:52:08,920 in C, and it's called the while loop. 1063 00:52:08,920 --> 00:52:11,120 >> In a while loop, you've just got a condition. 1064 00:52:11,120 --> 00:52:14,790 The condition is checked in the beginning, and if that condition 1065 00:52:14,790 --> 00:52:18,450 evaluates to true, then the code within the body of the while block 1066 00:52:18,450 --> 00:52:20,380 will execute. 1067 00:52:20,380 --> 00:52:23,000 And the code will continue to execute so long as that 1068 00:52:23,000 --> 00:52:24,450 condition remains true. 1069 00:52:24,450 --> 00:52:28,350 1070 00:52:28,350 --> 00:52:33,320 >> Here's an example of a while loop in C. We're going to set count to 10 to 1071 00:52:33,320 --> 00:52:34,580 begin with. 1072 00:52:34,580 --> 00:52:41,800 As long as count is greater than 0, we're going to keep printing out this 1073 00:52:41,800 --> 00:52:43,360 variable here. 1074 00:52:43,360 --> 00:52:47,080 We're going to decrement count and then check the condition again. 1075 00:52:47,080 --> 00:52:51,370 Print, decrement, check the condition again. 1076 00:52:51,370 --> 00:52:54,530 >> Here we have an example that pertains to your question earlier. 1077 00:52:54,530 --> 00:52:57,340 We had the percent s when we were printing out a string, and now that 1078 00:52:57,340 --> 00:53:03,420 we're printing out an int, we have a percent i as our placeholder instead. 1079 00:53:03,420 --> 00:53:09,250 The letters should all be pretty intuitive, s for string, i for int. 1080 00:53:09,250 --> 00:53:15,400 >> So if this code executes, you should count down from 10 to 0, printing out 1081 00:53:15,400 --> 00:53:15,925 each integer. 1082 00:53:15,925 --> 00:53:19,460 >> LUCAS FREITAS: And you can think of the percent as it feels like a blank 1083 00:53:19,460 --> 00:53:23,960 space, and then you're putting this variable here in that blank space. 1084 00:53:23,960 --> 00:53:27,640 So for example, here I will have just a blank, and then I'm putting the 1085 00:53:27,640 --> 00:53:30,262 count right there, the value of the count. 1086 00:53:30,262 --> 00:53:32,710 Does that make sense? 1087 00:53:32,710 --> 00:53:35,860 >> Could I just do like printf and then quotes, count? 1088 00:53:35,860 --> 00:53:36,840 Would that work? 1089 00:53:36,840 --> 00:53:37,980 >> AUDIENCE: No. 1090 00:53:37,980 --> 00:53:41,190 >> LUCAS FREITAS: No, because it will actually print the word "count" 1091 00:53:41,190 --> 00:53:42,850 instead of printing the value. 1092 00:53:42,850 --> 00:53:46,080 This is how you make it print the values toward the variable instead of 1093 00:53:46,080 --> 00:53:47,670 just the name of the variable. 1094 00:53:47,670 --> 00:53:48,530 Any questions? 1095 00:53:48,530 --> 00:53:53,060 >> AUDIENCE: Why do you use for as a opposed to while? 1096 00:53:53,060 --> 00:53:56,130 >> LAUREN CARVALHO: I would use a for loop if I know exactly how many 1097 00:53:56,130 --> 00:53:59,520 iterations I want my loop to run. 1098 00:53:59,520 --> 00:54:02,810 In this case, it doesn't matter because, I mean, I was just trying to 1099 00:54:02,810 --> 00:54:05,310 think of a really simple example for this slide. 1100 00:54:05,310 --> 00:54:09,990 But let's say that you want something to keep going while-- 1101 00:54:09,990 --> 00:54:14,480 >> LUCAS FREITAS: For example, you're asking for input until the person 1102 00:54:14,480 --> 00:54:15,370 gives you the right input. 1103 00:54:15,370 --> 00:54:19,720 So like while the input is not OK, keep asking for it. 1104 00:54:19,720 --> 00:54:22,920 Or like while you're parents don't give you money, keep asking for it. 1105 00:54:22,920 --> 00:54:23,430 >> [LAUGHTER] 1106 00:54:23,430 --> 00:54:25,390 >> LAUREN CARVALHO: You don't know how many times you'll have to do it. 1107 00:54:25,390 --> 00:54:27,292 >> LUCAS FREITAS: Because you don't know how many times that's 1108 00:54:27,292 --> 00:54:28,010 going to keep going. 1109 00:54:28,010 --> 00:54:30,397 >> LAUREN CARVALHO: But in a for loop, you need to know how many times. 1110 00:54:30,397 --> 00:54:33,640 1111 00:54:33,640 --> 00:54:33,860 Yeah? 1112 00:54:33,860 --> 00:54:38,520 >> AUDIENCE: Just to clarify, the percent says this is something about to 1113 00:54:38,520 --> 00:54:42,415 happen, the i's for the integer, and the backslash n-- 1114 00:54:42,415 --> 00:54:43,910 >> LAUREN CARVALHO: Means? 1115 00:54:43,910 --> 00:54:45,820 Who knows what the backslash n means? 1116 00:54:45,820 --> 00:54:47,770 New line, yeah. 1117 00:54:47,770 --> 00:54:48,130 Exactly. 1118 00:54:48,130 --> 00:54:50,500 >> LUCAS FREITAS: You can think of n as new line. 1119 00:54:50,500 --> 00:54:52,258 >> LAUREN CARVALHO: Nice. 1120 00:54:52,258 --> 00:54:55,775 >> AUDIENCE: What's that count dash dash [INAUDIBLE]? 1121 00:54:55,775 --> 00:55:00,240 >> LUCAS FREITAS: Count minus minus is the same thing as plus plus, but 1122 00:55:00,240 --> 00:55:02,160 instead of increasing, it's decreasing by 1. 1123 00:55:02,160 --> 00:55:04,720 1124 00:55:04,720 --> 00:55:07,185 >> LAUREN CARVALHO: Any more while loop questions? 1125 00:55:07,185 --> 00:55:09,460 No? 1126 00:55:09,460 --> 00:55:13,910 Oh, we were supposed to have skipped that. 1127 00:55:13,910 --> 00:55:14,950 >> This is a do while loop. 1128 00:55:14,950 --> 00:55:18,950 Very similar to a while loop except in this case we check the condition after 1129 00:55:18,950 --> 00:55:23,430 the code in the body of the loop executes once. 1130 00:55:23,430 --> 00:55:27,170 So we execute the code first no matter what. 1131 00:55:27,170 --> 00:55:31,956 Then we check the condition, execute it again if the condition is true. 1132 00:55:31,956 --> 00:55:35,450 1133 00:55:35,450 --> 00:55:38,950 >> You'll find yourself using this when you're asking for user input. 1134 00:55:38,950 --> 00:55:42,080 Because you're always going to have to ask once, and then depending on what 1135 00:55:42,080 --> 00:55:46,570 the user input is, you might have to reprompt and ask again and again if 1136 00:55:46,570 --> 00:55:50,670 the input was not what you expected it to be. 1137 00:55:50,670 --> 00:55:53,610 >> AUDIENCE: So it's like a form for people to fill out. 1138 00:55:53,610 --> 00:55:56,010 >> LAUREN CARVALHO: Yeah, exactly. 1139 00:55:56,010 --> 00:55:59,720 You'll find, I think, in your very first Pset, you're going to have to 1140 00:55:59,720 --> 00:56:04,010 use a do well loop to collect input from the user. 1141 00:56:04,010 --> 00:56:08,728 >> AUDIENCE: Depending on the situation, your while loops will tend to run more 1142 00:56:08,728 --> 00:56:12,805 often than while loops, like by one or something? 1143 00:56:12,805 --> 00:56:14,080 >> LAUREN CARVALHO: If it's the exact-- 1144 00:56:14,080 --> 00:56:17,400 >> LUCAS FREITAS: They should always run the exact amount of times that you 1145 00:56:17,400 --> 00:56:19,130 want it to run. 1146 00:56:19,130 --> 00:56:23,860 Like we use do while because we really wanted to get the input at least once. 1147 00:56:23,860 --> 00:56:27,220 In while loops, sometimes you don't want to execute it at all if it's not 1148 00:56:27,220 --> 00:56:28,540 the case to execute. 1149 00:56:28,540 --> 00:56:31,300 So it depends on the design. 1150 00:56:31,300 --> 00:56:35,900 I mean, you can either use do while or while as long as your code does that 1151 00:56:35,900 --> 00:56:36,905 it's supposed to do. 1152 00:56:36,905 --> 00:56:38,470 >> LAUREN CARVALHO: But I do think the answer to your 1153 00:56:38,470 --> 00:56:40,540 question is probably yes. 1154 00:56:40,540 --> 00:56:42,190 Let's say the condition was false. 1155 00:56:42,190 --> 00:56:45,498 In one case it would run once, and another case it would run zero times. 1156 00:56:45,498 --> 00:56:47,370 So, yeah. 1157 00:56:47,370 --> 00:56:49,260 >> Any more questions? 1158 00:56:49,260 --> 00:56:50,320 Yes? 1159 00:56:50,320 --> 00:56:56,443 >> AUDIENCE: Can the loop condition be a for loop in terms of a variable other 1160 00:56:56,443 --> 00:56:57,385 than [INAUDIBLE]. 1161 00:56:57,385 --> 00:57:01,700 >> LAUREN CARVALHO: Yeah, it can be anything. 1162 00:57:01,700 --> 00:57:06,420 Probably for the beginning Psets, you won't have to encounter that case, but 1163 00:57:06,420 --> 00:57:09,878 definitely it can be anything. 1164 00:57:09,878 --> 00:57:12,680 >> LUCAS FREITAS: I think we're done here. 1165 00:57:12,680 --> 00:57:13,290 >> LAUREN CARVALHO: Are we? 1166 00:57:13,290 --> 00:57:13,480 No? 1167 00:57:13,480 --> 00:57:13,870 >> LUCAS FREITAS: Loops? 1168 00:57:13,870 --> 00:57:14,600 Yeah. 1169 00:57:14,600 --> 00:57:16,280 >> LAUREN CARVALHO: We should not be. 1170 00:57:16,280 --> 00:57:17,760 Oh, we are done with loops. 1171 00:57:17,760 --> 00:57:19,250 OK. 1172 00:57:19,250 --> 00:57:23,180 >> Actually, because we've been talking so much, let's actually all navigate 1173 00:57:23,180 --> 00:57:28,950 over to study.cs50.net/conditions. 1174 00:57:28,950 --> 00:57:34,460 And you're going to navigate down to the problems widget. 1175 00:57:34,460 --> 00:57:39,090 Well, actually, while people are getting this set up, the CS50 Study is 1176 00:57:39,090 --> 00:57:42,620 going to be a tool that your TFs will use to prepare for sections and that 1177 00:57:42,620 --> 00:57:45,230 you guys can use if you miss sections. 1178 00:57:45,230 --> 00:57:48,980 It's going to contain a bunch of slides, some tips and tricks for each 1179 00:57:48,980 --> 00:57:54,030 topic, and some practice problems with solutions. 1180 00:57:54,030 --> 00:57:57,200 >> Why doesn't everyone just take a look at this problem right here. 1181 00:57:57,200 --> 00:58:01,630 1182 00:58:01,630 --> 00:58:04,790 >> I'm sorry, I should have reminded people to bring laptops today. 1183 00:58:04,790 --> 00:58:07,050 But if you don't have a laptop, that's OK. 1184 00:58:07,050 --> 00:58:10,140 You can just jot this down on a piece of paper or look over 1185 00:58:10,140 --> 00:58:12,890 your neighbor's shoulder. 1186 00:58:12,890 --> 00:58:16,380 >> Let's try and write a program that prompts the user to take a guess, the 1187 00:58:16,380 --> 00:58:19,250 average number of children per family unit in the United States. 1188 00:58:19,250 --> 00:58:22,265 That number is 2, by the way, as you can see from these examples. 1189 00:58:22,265 --> 00:58:24,770 1190 00:58:24,770 --> 00:58:29,030 >> We want to print out average number of children per family in the US, then we 1191 00:58:29,030 --> 00:58:31,080 want to what? 1192 00:58:31,080 --> 00:58:35,480 We want to collect input from the user. 1193 00:58:35,480 --> 00:58:41,290 And then depending on what the user's input is, you're going to print out 1194 00:58:41,290 --> 00:58:44,850 either this is correct or this is incorrect. 1195 00:58:44,850 --> 00:58:48,040 >> And the very first thing that you'll see, if you click the little arrow at 1196 00:58:48,040 --> 00:58:52,190 the bottom of the problem widget is a spot to jot down some pseudocode. 1197 00:58:52,190 --> 00:58:53,340 So you're not programming yet. 1198 00:58:53,340 --> 00:58:56,740 You're just getting your ideas in order. 1199 00:58:56,740 --> 00:58:59,620 >> Does anyone want to help Lucas come up with the right 1200 00:58:59,620 --> 00:59:01,270 pseudocode for this problem? 1201 00:59:01,270 --> 00:59:04,900 >> LUCAS FREITAS: What is the first thing that we should probably do here? 1202 00:59:04,900 --> 00:59:07,010 What do you guys think? 1203 00:59:07,010 --> 00:59:08,260 >> AUDIENCE: [INAUDIBLE] 1204 00:59:08,260 --> 00:59:12,060 1205 00:59:12,060 --> 00:59:13,290 >> LUCAS FREITAS: Yeah, OK. 1206 00:59:13,290 --> 00:59:16,610 So, print. 1207 00:59:16,610 --> 00:59:19,480 Just say the print this average number of people? 1208 00:59:19,480 --> 00:59:20,470 OK. 1209 00:59:20,470 --> 00:59:23,450 Print question, let's say. 1210 00:59:23,450 --> 00:59:25,951 After I print this question, what should I do? 1211 00:59:25,951 --> 00:59:27,875 >> AUDIENCE: Get input. 1212 00:59:27,875 --> 00:59:30,690 >> LUCAS FREITAS: Get input. 1213 00:59:30,690 --> 00:59:32,864 Anything else? 1214 00:59:32,864 --> 00:59:34,114 >> AUDIENCE: Condition. 1215 00:59:34,114 --> 00:59:36,770 1216 00:59:36,770 --> 00:59:38,980 >> LUCAS FREITAS: Condition. 1217 00:59:38,980 --> 00:59:41,325 >> LAUREN CARVALHO: What is that condition going to be? 1218 00:59:41,325 --> 00:59:43,550 >> AUDIENCE: The number of [INAUDIBLE]. 1219 00:59:43,550 --> 00:59:44,440 >> LAUREN CARVALHO: Right. 1220 00:59:44,440 --> 00:59:46,945 >> LUCAS FREITAS: So what am I going to check in the condition? 1221 00:59:46,945 --> 00:59:48,750 >> AUDIENCE: If it equals 2. 1222 00:59:48,750 --> 00:59:50,640 >> LAUREN CARVALHO: If it equals 2. 1223 00:59:50,640 --> 00:59:51,890 Exactly. 1224 00:59:51,890 --> 00:59:58,340 1225 00:59:58,340 --> 01:00:00,140 >> LUCAS FREITAS: Is there anything else that I should do? 1226 01:00:00,140 --> 01:00:01,390 >> AUDIENCE: Count. 1227 01:00:01,390 --> 01:00:05,184 1228 01:00:05,184 --> 01:00:08,110 >> LUCAS FREITAS: So if it's equal to 2, let's say-- 1229 01:00:08,110 --> 01:00:14,250 1230 01:00:14,250 --> 01:00:17,960 let me just write if input equals to 2, then what I'm going 1231 01:00:17,960 --> 01:00:23,665 to do is print correct. 1232 01:00:23,665 --> 01:00:26,230 1233 01:00:26,230 --> 01:00:31,826 And then else, I'm going to print incorrect. 1234 01:00:31,826 --> 01:00:34,600 1235 01:00:34,600 --> 01:00:37,720 Anything else? 1236 01:00:37,720 --> 01:00:38,670 No? 1237 01:00:38,670 --> 01:00:40,500 >> LAUREN CARVALHO: So you can click on the-- 1238 01:00:40,500 --> 01:00:42,290 does everyone have this down? 1239 01:00:42,290 --> 01:00:44,680 >> LUCAS FREITAS: Does it make sense? 1240 01:00:44,680 --> 01:00:46,226 So now let's try to code this. 1241 01:00:46,226 --> 01:00:50,140 >> LAUREN CARVALHO: If you click the little arrow, it actually saves your 1242 01:00:50,140 --> 01:00:51,900 pseudocode, which is really exciting. 1243 01:00:51,900 --> 01:00:56,480 And you can make those into comments if you want just to remind yourself of 1244 01:00:56,480 --> 01:00:57,075 what to do. 1245 01:00:57,075 --> 01:01:01,555 >> LUCAS FREITAS: So for example, first I'm going to print the question, so 1246 01:01:01,555 --> 01:01:05,930 how am I going to do that, actually? 1247 01:01:05,930 --> 01:01:07,050 printf. 1248 01:01:07,050 --> 01:01:12,130 So printf, and the question is average number of children per 1249 01:01:12,130 --> 01:01:14,110 family in the US. 1250 01:01:14,110 --> 01:01:19,070 1251 01:01:19,070 --> 01:01:21,790 >> Then I'm going to get input from the user. 1252 01:01:21,790 --> 01:01:29,050 1253 01:01:29,050 --> 01:01:31,324 So how can I do that? 1254 01:01:31,324 --> 01:01:32,200 >> AUDIENCE: Get Int. 1255 01:01:32,200 --> 01:01:33,076 >> LAUREN CARVALHO: GetInt. 1256 01:01:33,076 --> 01:01:33,890 >> LUCAS FREITAS: OK. 1257 01:01:33,890 --> 01:01:35,220 So first declare, right? 1258 01:01:35,220 --> 01:01:39,300 int, let's call it n equals GetInt. 1259 01:01:39,300 --> 01:01:42,610 1260 01:01:42,610 --> 01:01:44,670 >> And now the condition. 1261 01:01:44,670 --> 01:01:49,250 So let me say that I'm going to here check answer. 1262 01:01:49,250 --> 01:01:57,290 So first, if input, which is n, equals to-- how am I going to write that? 1263 01:01:57,290 --> 01:01:58,480 >> AUDIENCE: Equal equal. 1264 01:01:58,480 --> 01:02:00,000 >> LUCAS FREITAS: Perfect. 1265 01:02:00,000 --> 01:02:00,670 So n. 1266 01:02:00,670 --> 01:02:04,600 And notice that I do a space, equals, equals, space, true. 1267 01:02:04,600 --> 01:02:06,930 And now what should I do? 1268 01:02:06,930 --> 01:02:08,820 Something very important for style. 1269 01:02:08,820 --> 01:02:10,070 The curly braces. 1270 01:02:10,070 --> 01:02:16,570 1271 01:02:16,570 --> 01:02:17,795 >> And then I'm going to print. 1272 01:02:17,795 --> 01:02:19,790 How do I print? 1273 01:02:19,790 --> 01:02:22,500 printf. 1274 01:02:22,500 --> 01:02:24,568 >> AUDIENCE: Could it be capitalized? 1275 01:02:24,568 --> 01:02:27,166 1276 01:02:27,166 --> 01:02:28,380 >> LUCAS FREITAS: No. 1277 01:02:28,380 --> 01:02:31,000 It's supposed to be all lowercase. 1278 01:02:31,000 --> 01:02:32,250 So that is correct. 1279 01:02:32,250 --> 01:02:40,990 1280 01:02:40,990 --> 01:02:44,870 >> Would this suffice for the printf statement, or am I missing anything? 1281 01:02:44,870 --> 01:02:46,265 >> AUDIENCE: New line. 1282 01:02:46,265 --> 01:02:50,220 >> LUCAS FREITAS: New line, yeah. 1283 01:02:50,220 --> 01:02:51,250 And now else. 1284 01:02:51,250 --> 01:02:57,290 >> AUDIENCE: Is there a space after the printf, or no? 1285 01:02:57,290 --> 01:03:00,820 >> LAUREN CARVALHO: We should check the style guide. 1286 01:03:00,820 --> 01:03:02,260 >> AUDIENCE: It shouldn't be. 1287 01:03:02,260 --> 01:03:03,510 >> LUCAS FREITAS: Shouldn't be, OK. 1288 01:03:03,510 --> 01:03:05,820 1289 01:03:05,820 --> 01:03:13,605 So else, printf, "That is incorrect." 1290 01:03:13,605 --> 01:03:17,105 >> LAUREN CARVALHO: Oops, your quotes. 1291 01:03:17,105 --> 01:03:17,950 >> LUCAS FREITAS: OK. 1292 01:03:17,950 --> 01:03:18,600 Anything else? 1293 01:03:18,600 --> 01:03:20,770 Does it look good? 1294 01:03:20,770 --> 01:03:21,890 >> LAUREN CARVALHO: Shall we try it? 1295 01:03:21,890 --> 01:03:23,190 >> LUCAS FREITAS: Yeah, let's run it. 1296 01:03:23,190 --> 01:03:25,880 1297 01:03:25,880 --> 01:03:29,070 So it's compiling, and it seems that it works. 1298 01:03:29,070 --> 01:03:31,465 Let's try two children. 1299 01:03:31,465 --> 01:03:34,110 That is correct. 1300 01:03:34,110 --> 01:03:37,300 And now let's run again, but now instead of putting two children, let's 1301 01:03:37,300 --> 01:03:40,990 say that I'm going to put like five. 1302 01:03:40,990 --> 01:03:41,515 That is incorrect. 1303 01:03:41,515 --> 01:03:42,670 So it seems that it works. 1304 01:03:42,670 --> 01:03:44,160 >> LAUREN CARVALHO: Why don't you show them what happens if you put something 1305 01:03:44,160 --> 01:03:45,055 weird, like some letters. 1306 01:03:45,055 --> 01:03:45,790 >> LUCAS FREITAS: Oh, yeah. 1307 01:03:45,790 --> 01:03:47,430 Let's also check that. 1308 01:03:47,430 --> 01:03:51,170 What if the user is trying to mess with your program, and they just 1309 01:03:51,170 --> 01:03:57,430 write, I don't know two. 1310 01:03:57,430 --> 01:03:58,320 It's going to say retry. 1311 01:03:58,320 --> 01:04:00,780 >> LAUREN CARVALHO: So GetInt is asking them to retry. 1312 01:04:00,780 --> 01:04:02,115 We didn't have to take care of that ourselves. 1313 01:04:02,115 --> 01:04:02,750 >> LUCAS FREITAS: Yeah. 1314 01:04:02,750 --> 01:04:06,853 So unless I actually put a number, it's going to keep asking me to retry. 1315 01:04:06,853 --> 01:04:08,330 >> AUDIENCE: How did you run it? 1316 01:04:08,330 --> 01:04:10,570 >> LUCAS FREITAS: Just click this star button. 1317 01:04:10,570 --> 01:04:11,940 >> LAUREN CARVALHO: You should go up. 1318 01:04:11,940 --> 01:04:13,320 There you go. 1319 01:04:13,320 --> 01:04:15,550 >> LUCAS FREITAS: The star button right there. 1320 01:04:15,550 --> 01:04:16,040 Yeah? 1321 01:04:16,040 --> 01:04:19,960 >> AUDIENCE: Does it prompt [INAUDIBLE] with decimal numbers too? 1322 01:04:19,960 --> 01:04:21,235 >> LAUREN CARVALHO: I believe it will. 1323 01:04:21,235 --> 01:04:22,660 >> LUCAS FREITAS: If it does what? 1324 01:04:22,660 --> 01:04:24,990 >> LAUREN CARVALHO: If they type in a decimal number, I think 1325 01:04:24,990 --> 01:04:25,880 it's going to prompt. 1326 01:04:25,880 --> 01:04:26,586 >> LUCAS FREITAS: Let's see. 1327 01:04:26,586 --> 01:04:28,360 >> LAUREN CARVALHO: Because it's not an int. 1328 01:04:28,360 --> 01:04:32,056 1329 01:04:32,056 --> 01:04:33,442 >> LUCAS FREITAS: Yeah. 1330 01:04:33,442 --> 01:04:37,370 >> LAUREN CARVALHO: So it it's not an int, Get Int will ask you to retry. 1331 01:04:37,370 --> 01:04:40,795 >> AUDIENCE: So where in that code would be the decimal place that prompts the 1332 01:04:40,795 --> 01:04:46,005 user that they have to enter an integer specifically? 1333 01:04:46,005 --> 01:04:48,480 >> LAUREN CARVALHO: GetInt is actually doing that for us. 1334 01:04:48,480 --> 01:04:50,600 >> AUDIENCE: No, I mean if we wanted to enter [INAUDIBLE] 1335 01:04:50,600 --> 01:04:55,070 >> LUCAS FREITAS: You can say like average number of children in the US. 1336 01:04:55,070 --> 01:04:56,120 I don't know. 1337 01:04:56,120 --> 01:05:01,761 We can say, for example, "Please input an integer." 1338 01:05:01,761 --> 01:05:04,270 >> LAUREN CARVALHO: An integer equal to the average number. 1339 01:05:04,270 --> 01:05:06,190 I mean, it's just how you word it, right? 1340 01:05:06,190 --> 01:05:06,520 >> LUCAS FREITAS: Yeah. 1341 01:05:06,520 --> 01:05:08,420 This is not part of actually coding. 1342 01:05:08,420 --> 01:05:11,000 It's more of like whatever you're going to want to 1343 01:05:11,000 --> 01:05:13,987 write in your program. 1344 01:05:13,987 --> 01:05:17,723 >> AUDIENCE: How would we have run it if it was actually in gedit [INAUDIBLE]. 1345 01:05:17,723 --> 01:05:19,130 Instead of pressing play? 1346 01:05:19,130 --> 01:05:21,180 >> LUCAS FREITAS: We will first compile it. 1347 01:05:21,180 --> 01:05:25,080 We can do it by doing make and the name of the file. 1348 01:05:25,080 --> 01:05:29,896 And then to run it, we'll do dot slash and the name of the program. 1349 01:05:29,896 --> 01:05:36,588 >> AUDIENCE: I guess what I meant was if we wanted the program to respond to 1350 01:05:36,588 --> 01:05:42,010 their incorrect, invalid entry, you shouldn't put in-- 1351 01:05:42,010 --> 01:05:45,710 >> LUCAS FREITAS: We will not be able to do that because the code this says 1352 01:05:45,710 --> 01:05:50,350 retry comes from Get Int which is a function that is already implemented 1353 01:05:50,350 --> 01:05:55,790 for us via the CS50 library, and since it's already implemented there, we 1354 01:05:55,790 --> 01:05:58,340 cannot just [INAUDIBLE]. 1355 01:05:58,340 --> 01:06:00,780 >> LAUREN CARVALHO: If you don't use GetInt, then you can do that. 1356 01:06:00,780 --> 01:06:01,490 >> LUCAS FREITAS: Yeah. 1357 01:06:01,490 --> 01:06:03,750 But if you're using GetInt, you can't. 1358 01:06:03,750 --> 01:06:05,427 Any other questions? 1359 01:06:05,427 --> 01:06:12,938 >> AUDIENCE: Could you have gone with like GetFloat and then be able to put 1360 01:06:12,938 --> 01:06:14,381 it in a decimal? 1361 01:06:14,381 --> 01:06:15,343 >> LUCAS FREITAS: Yeah. 1362 01:06:15,343 --> 01:06:15,824 >> AUDIENCE: OK. 1363 01:06:15,824 --> 01:06:19,210 Then you just could input float a equals-- 1364 01:06:19,210 --> 01:06:19,950 >> LAUREN CARVALHO: Yeah. 1365 01:06:19,950 --> 01:06:22,740 Exactly the same except a float instead of an int. 1366 01:06:22,740 --> 01:06:27,372 >> LUCAS FREITAS: Just put it here and then GetFloat there. 1367 01:06:27,372 --> 01:06:29,795 >> AUDIENCE: As you will see or have seen-- 1368 01:06:29,795 --> 01:06:31,327 >> LAUREN CARVALHO: we have not gotten there yet. 1369 01:06:31,327 --> 01:06:36,490 >> AUDIENCE: --doing inequality with floats doesn't [INAUDIBLE] 1370 01:06:36,490 --> 01:06:40,290 >> AUDIENCE: What do the semicolons mean? 1371 01:06:40,290 --> 01:06:43,220 >> LAUREN CARVALHO: The semicolon just means that this statement is complete, 1372 01:06:43,220 --> 01:06:45,030 and we're going to go onto the next slide. 1373 01:06:45,030 --> 01:06:47,040 >> AUDIENCE: So it's necessary every time? 1374 01:06:47,040 --> 01:06:48,330 >> LAUREN CARVALHO: Yeah. 1375 01:06:48,330 --> 01:06:53,130 Well, OK, so not for these conditions, because they're 1376 01:06:53,130 --> 01:06:54,090 encapsulating something. 1377 01:06:54,090 --> 01:07:00,290 So basically if something is causing you to then create a curly brace 1378 01:07:00,290 --> 01:07:03,000 block, don't put a semicolon at the end. 1379 01:07:03,000 --> 01:07:04,200 >> LUCAS FREITAS: But everything else, yes. 1380 01:07:04,200 --> 01:07:06,670 When you finish a statement, put the semicolon. 1381 01:07:06,670 --> 01:07:10,380 And if you forget semicolons, the compiler's going to get angry at you, 1382 01:07:10,380 --> 01:07:12,640 and you're going to have to try to fix it. 1383 01:07:12,640 --> 01:07:16,520 >> LAUREN CARVALHO: If I put a semicolon here, that would stop this, and I 1384 01:07:16,520 --> 01:07:18,780 would never get to execute what's inside, by the way. 1385 01:07:18,780 --> 01:07:21,113 So it's one of those common bugs. 1386 01:07:21,113 --> 01:07:22,390 >> AUDIENCE: Always executing. 1387 01:07:22,390 --> 01:07:22,950 >> LAUREN CARVALHO: Yeah. 1388 01:07:22,950 --> 01:07:25,860 In this case, we would always execute what's inside. 1389 01:07:25,860 --> 01:07:29,050 >> AUDIENCE: Your style [INAUDIBLE] attach that for you, thought. 1390 01:07:29,050 --> 01:07:29,802 >> LAUREN CARVALHO: Oh, that's true. 1391 01:07:29,802 --> 01:07:34,298 >> AUDIENCE: [INAUDIBLE] single line thing [INAUDIBLE] 1392 01:07:34,298 --> 01:07:35,286 >> AUDIENCE: Yes? 1393 01:07:35,286 --> 01:07:37,756 >> AUDIENCE: Can you have multiple inputs in one line? 1394 01:07:37,756 --> 01:07:41,214 So like can you ask users to print two things? 1395 01:07:41,214 --> 01:07:42,530 >> LAUREN CARVALHO: Not using-- 1396 01:07:42,530 --> 01:07:44,520 >> LUCAS FREITAS: No. 1397 01:07:44,520 --> 01:07:47,040 Let's say that I also want to get this integer m. 1398 01:07:47,040 --> 01:07:49,558 I have to do it in a different line. 1399 01:07:49,558 --> 01:07:54,992 >> AUDIENCE: If you were to run that, how [INAUDIBLE]? 1400 01:07:54,992 --> 01:07:59,438 It would print the prompt there [INAUDIBLE] 1401 01:07:59,438 --> 01:08:00,940 and then you'd enter one and-- 1402 01:08:00,940 --> 01:08:01,840 >> LAUREN CARVALHO: Well, run it. 1403 01:08:01,840 --> 01:08:04,365 I mean, there's nothing stopping us from just running it and seeing what 1404 01:08:04,365 --> 01:08:06,610 would happen. 1405 01:08:06,610 --> 01:08:08,080 >> LUCAS FREITAS: I think it's going to complains at us 1406 01:08:08,080 --> 01:08:08,977 because we're not using-- 1407 01:08:08,977 --> 01:08:09,751 >> LAUREN CARVALHO: Oops. 1408 01:08:09,751 --> 01:08:12,580 >> LUCAS FREITAS: It complained at us right now because we're not using this 1409 01:08:12,580 --> 01:08:21,640 variable, but if I did, for example, n is equals equals to, and m equals 1410 01:08:21,640 --> 01:08:26,849 equals to, then what will happen in this case? 1411 01:08:26,849 --> 01:08:28,830 What would this program do? 1412 01:08:28,830 --> 01:08:30,330 >> AUDIENCE: I'm just wondering how it would-- 1413 01:08:30,330 --> 01:08:31,550 >> LAUREN CARVALHO: She just wants to know how it looks 1414 01:08:31,550 --> 01:08:34,189 like, I think, right? 1415 01:08:34,189 --> 01:08:36,819 So it took one of them, and then it took the second one. 1416 01:08:36,819 --> 01:08:41,279 1417 01:08:41,279 --> 01:08:43,069 >> I think we should move on, because we have one more 1418 01:08:43,069 --> 01:08:44,689 slide show to go before-- 1419 01:08:44,689 --> 01:08:46,269 do you want to pull up math? 1420 01:08:46,269 --> 01:08:47,558 >> LUCAS FREITAS: Yeah, math. 1421 01:08:47,558 --> 01:09:00,033 1422 01:09:00,033 --> 01:09:02,206 >> LAUREN CARVALHO: Give me just one second. 1423 01:09:02,206 --> 01:09:07,390 1424 01:09:07,390 --> 01:09:14,260 >> There are a few different numerical variables in C. Numerical data in C 1425 01:09:14,260 --> 01:09:18,130 can be classified into different types, and these are the ones you'll 1426 01:09:18,130 --> 01:09:19,570 see the most often. 1427 01:09:19,570 --> 01:09:20,910 >> And int is a whole number. 1428 01:09:20,910 --> 01:09:23,189 You can't have any decimals. 1429 01:09:23,189 --> 01:09:27,754 In the Appliance, it takes 32 bits to store an int. 1430 01:09:27,754 --> 01:09:30,450 >> A float can have decimals. 1431 01:09:30,450 --> 01:09:34,330 It also takes 32 bits to store a float in the Appliance. 1432 01:09:34,330 --> 01:09:42,270 >> A double is a number with a decimal point that takes 64 bits to store it, 1433 01:09:42,270 --> 01:09:44,470 which means that you can just have greater precision because 1434 01:09:44,470 --> 01:09:46,426 you can have more-- 1435 01:09:46,426 --> 01:09:47,785 what do you call it? 1436 01:09:47,785 --> 01:09:48,640 >> LUCAS FREITAS: Yeah, that's more. 1437 01:09:48,640 --> 01:09:52,140 >> LAUREN CARVALHO: More places after the decimal point. 1438 01:09:52,140 --> 01:09:57,140 And a long long is a whole number that takes 64 bits to 1439 01:09:57,140 --> 01:09:58,395 sore it in the Appliance. 1440 01:09:58,395 --> 01:10:00,278 >> LUCAS FREITAS: So it can be larger than ints can be. 1441 01:10:00,278 --> 01:10:03,320 1442 01:10:03,320 --> 01:10:08,380 >> LAUREN CARVALHO: The thing about data types in C is that they tell you what 1443 01:10:08,380 --> 01:10:11,050 kind of data you can store in your variable. 1444 01:10:11,050 --> 01:10:15,860 They tell you how you can use this variable in your program. 1445 01:10:15,860 --> 01:10:19,650 It's very important to just keep track of what data type you're using, and if 1446 01:10:19,650 --> 01:10:23,070 you do need to cast from a data type to another, make sure that you know 1447 01:10:23,070 --> 01:10:25,250 exactly what happens when that casting takes place. 1448 01:10:25,250 --> 01:10:28,520 And you'll see an example later on in these slides of a 1449 01:10:28,520 --> 01:10:31,490 mistake that can occur. 1450 01:10:31,490 --> 01:10:35,300 >> Let's take a look at some math in C and think really carefully, I guess, 1451 01:10:35,300 --> 01:10:38,470 about what's going on when this math is taking place. 1452 01:10:38,470 --> 01:10:43,552 In the very first line, what's happening when I declare x? 1453 01:10:43,552 --> 01:10:48,921 >> AUDIENCE: You're allowing x to be something? 1454 01:10:48,921 --> 01:10:49,700 >> LAUREN CARVALHO: Yeah. 1455 01:10:49,700 --> 01:10:54,040 Well, I'm really asking the operating system for 32 bits of memory, right? 1456 01:10:54,040 --> 01:10:57,890 In which I can store an int, and I'm going to call that int x. 1457 01:10:57,890 --> 01:11:01,370 >> LUCAS FREITAS: Basically going in memory and finding a place that has 32 1458 01:11:01,370 --> 01:11:05,290 bits and saying, hey, I want this space, and it's going to be called x. 1459 01:11:05,290 --> 01:11:08,590 So you're just pretty much finding a box in your memory to 1460 01:11:08,590 --> 01:11:10,440 store values for x. 1461 01:11:10,440 --> 01:11:13,980 >> LAUREN CARVALHO: And then I initialize x, which means that I just store the 1462 01:11:13,980 --> 01:11:20,270 value of 2 in that place, that chunk of memory, that we asked operating 1463 01:11:20,270 --> 01:11:21,860 system for. 1464 01:11:21,860 --> 01:11:25,090 And down here what we're doing is we're actually declaring and 1465 01:11:25,090 --> 01:11:29,060 initializing y in one line instead of in two lines like we did up here, 1466 01:11:29,060 --> 01:11:33,960 which is also a perfectly fine, We're adding 1 to x, and we're storing that 1467 01:11:33,960 --> 01:11:34,950 value in y. 1468 01:11:34,950 --> 01:11:39,360 >> LUCAS FREITAS: So basically what this line does, it finds now a box for y, 1469 01:11:39,360 --> 01:11:41,990 and then its value, the thing that you're going to put inside the box, is 1470 01:11:41,990 --> 01:11:43,800 going to be the value of x plus 1. 1471 01:11:43,800 --> 01:11:45,850 So it has this box for y. 1472 01:11:45,850 --> 01:11:49,980 It gets the value of x, looks here, and see that it's 2. 1473 01:11:49,980 --> 01:11:53,035 Put it there, plus 1, so it puts a 3 there. 1474 01:11:53,035 --> 01:11:54,290 Does that make sense? 1475 01:11:54,290 --> 01:11:55,780 So y would be 2 in this case. 1476 01:11:55,780 --> 01:11:58,993 1477 01:11:58,993 --> 01:12:00,770 >> LAUREN CARVALHO: All right, cool. 1478 01:12:00,770 --> 01:12:06,010 We saw some addition, so let's now think about division in C. Here I'm 1479 01:12:06,010 --> 01:12:11,490 declaring and initializing a float called answer and dividing 1 by 10, 1480 01:12:11,490 --> 01:12:14,860 and then I'm printing out the answer to two decimal places. 1481 01:12:14,860 --> 01:12:18,800 And note here, in my printf statement, when I want to print something to two 1482 01:12:18,800 --> 01:12:25,600 decimal places, %.2f is what I want to use. 1483 01:12:25,600 --> 01:12:28,880 1484 01:12:28,880 --> 01:12:31,760 So the point 2 is just saying I need two places after the 1485 01:12:31,760 --> 01:12:34,980 decimal printed out. 1486 01:12:34,980 --> 01:12:38,740 >> Now, what's going to happen if I run this code? 1487 01:12:38,740 --> 01:12:40,500 Does anyone have an idea? 1488 01:12:40,500 --> 01:12:42,721 What would print out? 1489 01:12:42,721 --> 01:12:45,010 >> LUCAS FREITAS: What are we expecting to get. 1490 01:12:45,010 --> 01:12:46,810 >> LAUREN CARVALHO: What do we want, first of all? 1491 01:12:46,810 --> 01:12:48,871 >> LUCAS FREITAS: [INAUDIBLE] 1492 01:12:48,871 --> 01:12:51,120 >> LAUREN CARVALHO: 0.1, yeah. 1493 01:12:51,120 --> 01:12:52,325 But what will print out? 1494 01:12:52,325 --> 01:12:53,275 >> AUDIENCE: [INAUDIBLE] 1495 01:12:53,275 --> 01:12:56,760 >> LAUREN CARVALHO: Well, no, actually. 1496 01:12:56,760 --> 01:13:01,400 This is what I was talking about why you need to keep very careful track of 1497 01:13:01,400 --> 01:13:02,550 your data types. 1498 01:13:02,550 --> 01:13:05,000 So, what data type is this? 1499 01:13:05,000 --> 01:13:06,250 Is an int, right? 1500 01:13:06,250 --> 01:13:07,600 There's no decimal place. 1501 01:13:07,600 --> 01:13:08,770 10 is also an int. 1502 01:13:08,770 --> 01:13:09,930 There's no decimal place. 1503 01:13:09,930 --> 01:13:15,770 >> If I divide an int by an int, I get an int which is not 0.1, because ints 1504 01:13:15,770 --> 01:13:18,630 can't have decimal places, but just 0. 1505 01:13:18,630 --> 01:13:21,005 Because an int just truncates off the decimal places. 1506 01:13:21,005 --> 01:13:24,280 >> LUCAS FREITAS: So whenever you do operations with ints, you're going to 1507 01:13:24,280 --> 01:13:25,990 get an int as the answer. 1508 01:13:25,990 --> 01:13:28,930 So basically, what the computer is doing, it's divides 1 by 1509 01:13:28,930 --> 01:13:31,520 10, and it gets 0.1. 1510 01:13:31,520 --> 01:13:34,870 But then it's like, wait, this is supposed to be an integer, so you just 1511 01:13:34,870 --> 01:13:37,045 ignore the 0.1, so it's now just 0. 1512 01:13:37,045 --> 01:13:41,782 >> LAUREN CARVALHO: So when I print this out, I get 0.00 rather than 0.10. 1513 01:13:41,782 --> 01:13:44,075 >> LUCAS FREITAS: Does that make sense? 1514 01:13:44,075 --> 01:13:45,530 >> LAUREN CARVALHO: That's kind of unfortunate, right? 1515 01:13:45,530 --> 01:13:47,290 >> LUCAS FREITAS: So how can we fix this? 1516 01:13:47,290 --> 01:13:48,620 >> LAUREN CARVALHO: Let's try and fix that. 1517 01:13:48,620 --> 01:13:51,570 1518 01:13:51,570 --> 01:13:54,810 Here's an example of where I'm actually casting the 1 1519 01:13:54,810 --> 01:13:56,650 and the 10 to floats. 1520 01:13:56,650 --> 01:13:58,800 So now, 1 is no longer an int. 1521 01:13:58,800 --> 01:14:00,020 1 is a float. 1522 01:14:00,020 --> 01:14:02,590 By the way, this is just the syntax for when you want to cast from one 1523 01:14:02,590 --> 01:14:03,410 data type to another. 1524 01:14:03,410 --> 01:14:07,140 You can just put the data type in parentheses and have that just precede 1525 01:14:07,140 --> 01:14:10,860 the value that you're casting. 1526 01:14:10,860 --> 01:14:15,800 >> So here, I'm dividing a float by a float to get a float answer. 1527 01:14:15,800 --> 01:14:19,535 And yes, this would in fact print 0.10. 1528 01:14:19,535 --> 01:14:20,470 Does that make sense? 1529 01:14:20,470 --> 01:14:24,110 >> LUCAS FREITAS: So type casting is just converting from one type to the other. 1530 01:14:24,110 --> 01:14:29,460 So float 1 is actually 1.000. 1531 01:14:29,460 --> 01:14:33,400 Not exactly this, but it's something with decimal places 1532 01:14:33,400 --> 01:14:34,715 that is actually 1. 1533 01:14:34,715 --> 01:14:35,960 Does that make sense? 1534 01:14:35,960 --> 01:14:36,260 Yeah? 1535 01:14:36,260 --> 01:14:40,990 >> AUDIENCE: What if you did float answer equals 1.0 divided by 10? 1536 01:14:40,990 --> 01:14:43,940 >> LAUREN CARVALHO: You're actually going to see that in the next slide. 1537 01:14:43,940 --> 01:14:47,150 That's actually our very next slide. 1538 01:14:47,150 --> 01:14:50,740 This is actually a nicer than type casting, I think. 1539 01:14:50,740 --> 01:14:53,470 So these two are floats just by nature of the fact that they 1540 01:14:53,470 --> 01:14:54,926 have decimal places. 1541 01:14:54,926 --> 01:14:55,300 Yes? 1542 01:14:55,300 --> 01:14:57,670 >> AUDIENCE: What if you did [INAUDIBLE] 1543 01:14:57,670 --> 01:14:59,480 divided by 10? 1544 01:14:59,480 --> 01:15:02,270 >> LAUREN CARVALHO: Actually a float divided by an int actually gives you a 1545 01:15:02,270 --> 01:15:03,670 float, so that would work fine. 1546 01:15:03,670 --> 01:15:08,070 1547 01:15:08,070 --> 01:15:09,120 >> Wait, does everyone understand this? 1548 01:15:09,120 --> 01:15:12,720 Because this is going to be a really big deal in Pset1, actually. 1549 01:15:12,720 --> 01:15:13,130 Yes? 1550 01:15:13,130 --> 01:15:14,762 >> AUDIENCE: Could you just quickly go over the red part? 1551 01:15:14,762 --> 01:15:16,380 >> LAUREN CARVALHO: The red part, yeah. 1552 01:15:16,380 --> 01:15:17,250 Lucas, do you want to? 1553 01:15:17,250 --> 01:15:18,020 >> LUCAS FREITAS: OK. 1554 01:15:18,020 --> 01:15:20,570 So remember that the percent is a placeholder. 1555 01:15:20,570 --> 01:15:22,150 It's like a blank space. 1556 01:15:22,150 --> 01:15:26,240 So let's try to understand everything that's going on here. 1557 01:15:26,240 --> 01:15:28,170 Percent means that it's a placeholder. 1558 01:15:28,170 --> 01:15:33,640 Then you have the f, which means that this placeholder is for a float. 1559 01:15:33,640 --> 01:15:38,870 The last thing that we have here is a .2, which means I want to have two 1560 01:15:38,870 --> 01:15:42,410 decimal places for the float. 1561 01:15:42,410 --> 01:15:48,510 >> If I just had %f, they it will put a bunch of decimal places. 1562 01:15:48,510 --> 01:15:54,170 So I can just put .2 or .1 or .5, or whatever, and it's going to put that 1563 01:15:54,170 --> 01:15:55,950 number of decimal places. 1564 01:15:55,950 --> 01:15:59,950 The only thing is that if you put like .100 or .1,000, it's not actually 1565 01:15:59,950 --> 01:16:04,200 going to put all those decimal places because it doesn't have enough memory 1566 01:16:04,200 --> 01:16:06,432 to have that precision. 1567 01:16:06,432 --> 01:16:07,955 >> Any other questions? 1568 01:16:07,955 --> 01:16:08,450 Yeah? 1569 01:16:08,450 --> 01:16:12,970 >> AUDIENCE: So float just allows you to go beyond integers? 1570 01:16:12,970 --> 01:16:13,720 >> LUCAS FREITAS: Yes. 1571 01:16:13,720 --> 01:16:17,345 >> LAUREN CARVALHO: Well, a float just allows you to have decimal places. 1572 01:16:17,345 --> 01:16:20,460 >> LUCAS FREITAS: It just means that you now can have decimal numbers. 1573 01:16:20,460 --> 01:16:25,728 >> AUDIENCE: Why would we ever use integers? 1574 01:16:25,728 --> 01:16:27,870 >> LUCAS FREITAS: Why would you use integers? 1575 01:16:27,870 --> 01:16:31,810 Maybe if you're counting from 0 to 10, you want to use actually an integer. 1576 01:16:31,810 --> 01:16:33,370 You don't need to use a float. 1577 01:16:33,370 --> 01:16:35,430 In some situations, you don't have to actually-- 1578 01:16:35,430 --> 01:16:37,040 >> LAUREN CARVALHO: Maybe you don't want decimals. 1579 01:16:37,040 --> 01:16:38,600 >> LUCAS FREITAS: Maybe you don't want to use a float. 1580 01:16:38,600 --> 01:16:42,660 >> LAUREN CARVALHO: The other thing is, remember, both put a float and an int 1581 01:16:42,660 --> 01:16:46,170 take 32 bits to store them, and maybe you don't want to waste bits on the 1582 01:16:46,170 --> 01:16:48,062 decimal places. 1583 01:16:48,062 --> 01:16:49,750 >> LUCAS FREITAS: Yeah. 1584 01:16:49,750 --> 01:16:51,910 So like, just think about this. 1585 01:16:51,910 --> 01:16:55,200 You have 32 bits for both an int and a float. 1586 01:16:55,200 --> 01:16:59,400 For an int, all those 32 bits go to the integer part. 1587 01:16:59,400 --> 01:17:03,460 If you use a float with 32 bits, you have part of this memory being used 1588 01:17:03,460 --> 01:17:06,820 for the number, for the whole number, and the other part being used for the 1589 01:17:06,820 --> 01:17:08,300 decimal places. 1590 01:17:08,300 --> 01:17:12,370 So that means that you cannot get to very large numbers with floats because 1591 01:17:12,370 --> 01:17:16,020 the float's already using a lot of memory for the decimal places. 1592 01:17:16,020 --> 01:17:17,014 Yeah? 1593 01:17:17,014 --> 01:17:21,487 >> AUDIENCE: That first answer, what exactly is it doing? 1594 01:17:21,487 --> 01:17:25,214 Is it setting the value of float equal to that placeholder to that you can 1595 01:17:25,214 --> 01:17:26,954 recall it in the next line? 1596 01:17:26,954 --> 01:17:28,942 It says float answer. 1597 01:17:28,942 --> 01:17:29,770 >> LUCAS FREITAS: Yeah. 1598 01:17:29,770 --> 01:17:37,200 So, float answer means you're getting now a little bucket in the memory that 1599 01:17:37,200 --> 01:17:40,050 holds a float, and that's going to be called answer. 1600 01:17:40,050 --> 01:17:42,040 So that's what float answer does. 1601 01:17:42,040 --> 01:17:44,720 >> And then you say that inside of this, you're going to put 1602 01:17:44,720 --> 01:17:46,470 the result of this. 1603 01:17:46,470 --> 01:17:52,610 So you're going to do 1.0 divided by 10.0, you get 0.1, and then you put 1604 01:17:52,610 --> 01:17:53,490 that inside of answer. 1605 01:17:53,490 --> 01:17:57,540 >> LAUREN CARVALHO: So in one line, we're declaring and initializing this float 1606 01:17:57,540 --> 01:18:00,070 called answer. 1607 01:18:00,070 --> 01:18:04,470 >> LUCAS FREITAS: Interestingly, although this is just one line, it's doing 1608 01:18:04,470 --> 01:18:05,570 multiple operations. 1609 01:18:05,570 --> 01:18:08,800 It's just a way of C letting you use less code. 1610 01:18:08,800 --> 01:18:12,310 It's the same thing as the ternary operator, how you can just do an 1611 01:18:12,310 --> 01:18:13,810 [INAUDIBLE] in one line. 1612 01:18:13,810 --> 01:18:18,860 It's actually doing the same thing as doing float answer, semicolon, and 1613 01:18:18,860 --> 01:18:23,060 then answer equals this, but instead of making you do two lines for that, 1614 01:18:23,060 --> 01:18:24,160 it's like a shortcut. 1615 01:18:24,160 --> 01:18:24,520 Yeah? 1616 01:18:24,520 --> 01:18:27,929 >> AUDIENCE: So if you ran this, it would be 0.10? 1617 01:18:27,929 --> 01:18:30,120 >> LUCAS FREITAS: Yes. 1618 01:18:30,120 --> 01:18:30,530 Yeah? 1619 01:18:30,530 --> 01:18:34,540 >> AUDIENCE: Is answer actually code, or is it just the name? 1620 01:18:34,540 --> 01:18:36,010 >> LAUREN CARVALHO: This is actually code. 1621 01:18:36,010 --> 01:18:37,750 This is what we're going to name our variable. 1622 01:18:37,750 --> 01:18:39,770 >> AUDIENCE: So it's just a name [INAUDIBLE]. 1623 01:18:39,770 --> 01:18:40,270 >> LAUREN CARVALHO: Yeah. 1624 01:18:40,270 --> 01:18:44,670 I could have said float x equals 1.0. 1625 01:18:44,670 --> 01:18:47,160 >> LUCAS FREITAS: The thing is that the name of the variable is 1626 01:18:47,160 --> 01:18:48,400 something that you use. 1627 01:18:48,400 --> 01:18:49,690 It's important for you. 1628 01:18:49,690 --> 01:18:52,660 For the actual program, the only thing that matters is the value that you 1629 01:18:52,660 --> 01:18:55,570 have inside of the bucket. 1630 01:18:55,570 --> 01:18:59,270 The reason why you have variables and names of variables is so you'd know 1631 01:18:59,270 --> 01:19:01,910 what you're dealing with, like what kind of data you're 1632 01:19:01,910 --> 01:19:03,040 accessing at a time. 1633 01:19:03,040 --> 01:19:07,580 But for the code itself, if you call it answer or question or anything, 1634 01:19:07,580 --> 01:19:09,530 pretty much, it doesn't matter. 1635 01:19:09,530 --> 01:19:10,060 It doesn't matter. 1636 01:19:10,060 --> 01:19:12,910 As long as you're consistent in every occurrence it and so you call it 1637 01:19:12,910 --> 01:19:13,620 something else. 1638 01:19:13,620 --> 01:19:14,880 Yeah? 1639 01:19:14,880 --> 01:19:17,208 >> AUDIENCE: Do the variables go beyond the function? 1640 01:19:17,208 --> 01:19:20,902 Like if you were to create a bracket [INAUDIBLE], could you just 1641 01:19:20,902 --> 01:19:21,806 [INAUDIBLE] answer again? 1642 01:19:21,806 --> 01:19:22,550 >> LAUREN CARVALHO: No. 1643 01:19:22,550 --> 01:19:23,300 >> AUDIENCE: [INAUDIBLE] 1644 01:19:23,300 --> 01:19:24,200 >> LAUREN CARVALHO: No. 1645 01:19:24,200 --> 01:19:26,930 That would be outside of the scope of this particular variable. 1646 01:19:26,930 --> 01:19:29,940 And actually, that's kind of interesting, because when we have 1647 01:19:29,940 --> 01:19:33,010 things like loops or these if conditions, if you declare a variable 1648 01:19:33,010 --> 01:19:36,330 within another set of curly braces, it really only exists 1649 01:19:36,330 --> 01:19:39,800 within those curly braces. 1650 01:19:39,800 --> 01:19:43,088 >> AUDIENCE: One last note about the [INAUDIBLE] floats. 1651 01:19:43,088 --> 01:19:46,076 Why would you ever want to use ints? 1652 01:19:46,076 --> 01:19:49,562 This is more of a side note than anything, but your computer is 1653 01:19:49,562 --> 01:19:53,048 significantly better at handling ints than it is at handling floats. 1654 01:19:53,048 --> 01:19:55,040 It's like an order of magnitude faster. 1655 01:19:55,040 --> 01:20:00,360 So if we just handled only floats, things would be a lot slower. 1656 01:20:00,360 --> 01:20:03,080 >> LAUREN CARVALHO: Actually, there seems to be a problem with floats. 1657 01:20:03,080 --> 01:20:06,400 >> LUCAS FREITAS: Kind of the reason for that, you can think, is how in binary 1658 01:20:06,400 --> 01:20:08,480 you can represent integers by putting 0's and 1's. 1659 01:20:08,480 --> 01:20:09,980 >> LAUREN CARVALHO: That's the next thing. 1660 01:20:09,980 --> 01:20:10,480 >> LUCAS FREITAS: OK. 1661 01:20:10,480 --> 01:20:10,740 Awesome. 1662 01:20:10,740 --> 01:20:11,480 >> LAUREN CARVALHO: Just hold that thought. 1663 01:20:11,480 --> 01:20:13,800 >> LUCAS FREITAS: OK, I'll tell you later. 1664 01:20:13,800 --> 01:20:16,920 >> LAUREN CARVALHO: Before we get to what Lucas was just saying, operator 1665 01:20:16,920 --> 01:20:17,420 precedence. 1666 01:20:17,420 --> 01:20:18,220 This is common sense. 1667 01:20:18,220 --> 01:20:20,830 You guys have done this enough. 1668 01:20:20,830 --> 01:20:24,705 So, just if something's in parentheses, it will happen first. 1669 01:20:24,705 --> 01:20:27,970 It's multiplication takes precedence over addition, 1670 01:20:27,970 --> 01:20:30,330 subtraction, things like that. 1671 01:20:30,330 --> 01:20:34,700 So actually, just does anyone want to tell me what number one actually 1672 01:20:34,700 --> 01:20:36,674 evaluates to? 1673 01:20:36,674 --> 01:20:37,638 >> AUDIENCE: 27. 1674 01:20:37,638 --> 01:20:39,090 >> LAUREN CARVALHO: 27? 1675 01:20:39,090 --> 01:20:39,550 Yeah. 1676 01:20:39,550 --> 01:20:41,975 >> LUCAS FREITAS: Let me give candies to people. 1677 01:20:41,975 --> 01:20:42,850 >> LAUREN CARVALHO: All right. 1678 01:20:42,850 --> 01:20:44,100 What about number two? 1679 01:20:44,100 --> 01:20:46,880 1680 01:20:46,880 --> 01:20:48,340 What about number two? 1681 01:20:48,340 --> 01:20:49,280 >> AUDIENCE: 22. 1682 01:20:49,280 --> 01:20:50,680 >> LAUREN CARVALHO: 22, yeah. 1683 01:20:50,680 --> 01:20:51,570 I don't know who got that. 1684 01:20:51,570 --> 01:20:52,694 Number three? 1685 01:20:52,694 --> 01:20:54,050 >> AUDIENCE: 10. 1686 01:20:54,050 --> 01:20:55,300 >> LAUREN CARVALHO: 10, yes. 1687 01:20:55,300 --> 01:20:58,150 1688 01:20:58,150 --> 01:20:58,490 Cool. 1689 01:20:58,490 --> 01:21:01,280 So modulo. 1690 01:21:01,280 --> 01:21:03,700 Lucas, do you want to explain modulo? 1691 01:21:03,700 --> 01:21:07,150 >> LUCAS FREITAS: Modulo is this operator that is actually very useful for a 1692 01:21:07,150 --> 01:21:09,470 bunch of problems. 1693 01:21:09,470 --> 01:21:15,270 If I do 55 modulo 10, what it does this let's say that I want to 1694 01:21:15,270 --> 01:21:16,970 divide 55 by 10. 1695 01:21:16,970 --> 01:21:18,240 What do I get? 1696 01:21:18,240 --> 01:21:21,380 It feels just like if I'm actually just doing the division, what is the 1697 01:21:21,380 --> 01:21:22,450 first time that I got the integer? 1698 01:21:22,450 --> 01:21:23,190 >> AUDIENCE: 5. 1699 01:21:23,190 --> 01:21:24,080 >> LUCAS FREITAS: 5. 1700 01:21:24,080 --> 01:21:26,230 And then the remainder is 5, right? 1701 01:21:26,230 --> 01:21:29,250 So it's like 5 times 10 plus 5. 1702 01:21:29,250 --> 01:21:35,570 Basically, what modulo does, it tries to divide this number by this one, and 1703 01:21:35,570 --> 01:21:37,440 it sees what is the remainder of the division. 1704 01:21:37,440 --> 01:21:40,080 >> LAUREN CARVALHO: So it just returns the remainder. 1705 01:21:40,080 --> 01:21:41,920 So number one is what? 1706 01:21:41,920 --> 01:21:42,560 5. 1707 01:21:42,560 --> 01:21:43,336 Number two? 1708 01:21:43,336 --> 01:21:44,150 >> AUDIENCE: 3. 1709 01:21:44,150 --> 01:21:44,934 >> LAUREN CARVALHO: 3. 1710 01:21:44,934 --> 01:21:46,240 Number three? 1711 01:21:46,240 --> 01:21:47,190 0. 1712 01:21:47,190 --> 01:21:48,410 And four is 1. 1713 01:21:48,410 --> 01:21:48,665 Exactly. 1714 01:21:48,665 --> 01:21:49,915 >> LUCAS FREITAS: Simple, right? 1715 01:21:49,915 --> 01:21:54,270 1716 01:21:54,270 --> 01:21:56,560 >> LAUREN CARVALHO: Why don't we skip this just because we don't really have 1717 01:21:56,560 --> 01:21:59,780 time, and people seem to understand. 1718 01:21:59,780 --> 01:22:03,330 So here's what Lucas was just talking about. 1719 01:22:03,330 --> 01:22:08,030 >> LUCAS FREITAS: Another reason why I would use ints instead of floats is 1720 01:22:08,030 --> 01:22:10,840 that floats are very imprecise. 1721 01:22:10,840 --> 01:22:12,720 And why are they imprecise? 1722 01:22:12,720 --> 01:22:17,140 David said that to represent an integer, for example, we use 0's and 1723 01:22:17,140 --> 01:22:17,810 1's, right? 1724 01:22:17,810 --> 01:22:22,370 And then you basically do the multiplication, do all those 1725 01:22:22,370 --> 01:22:30,000 operations to know what decimal number you have instead of the binary. 1726 01:22:30,000 --> 01:22:33,040 >> What happens now if I try to put decimal points? 1727 01:22:33,040 --> 01:22:37,680 Actually, for example, if I have the number like 3.33333? 1728 01:22:37,680 --> 01:22:40,250 It feels like 10 over 3. 1729 01:22:40,250 --> 01:22:44,950 How am I going to store all the 3's that I have after that? 1730 01:22:44,950 --> 01:22:47,120 It's really hard, right? 1731 01:22:47,120 --> 01:22:49,580 You cannot just tell the computer, hey, I have a 3, and 1732 01:22:49,580 --> 01:22:50,520 then you do that forever. 1733 01:22:50,520 --> 01:22:52,810 Actually the computer is not smart enough for this. 1734 01:22:52,810 --> 01:22:58,300 >> So basically all your computer does is that it tries to put as many digits as 1735 01:22:58,300 --> 01:23:00,950 it can up to the memory that you have. 1736 01:23:00,950 --> 01:23:05,890 So let's say that it's going to say 3.3333, and that's it. 1737 01:23:05,890 --> 01:23:10,690 So it's not able to actually express precisely what that float is. 1738 01:23:10,690 --> 01:23:13,240 >> LAUREN CARVALHO: We have a finite number of bits, which means that not 1739 01:23:13,240 --> 01:23:17,910 every decimal number is going to be able to be represented precisely. 1740 01:23:17,910 --> 01:23:24,910 And so in this example, if we are printing out 0.1, we're now printing 1741 01:23:24,910 --> 01:23:26,475 out 20 places. 1742 01:23:26,475 --> 01:23:30,250 >> LUCAS FREITAS: Just like I was telling you guys, you could put any number. 1743 01:23:30,250 --> 01:23:36,520 But if you just did the two places, you'd get 0.10, but if you put 20 1744 01:23:36,520 --> 01:23:43,630 places, it's going to show you a bunch of 's because this is what you can put 1745 01:23:43,630 --> 01:23:45,720 in the memory, right? 1746 01:23:45,720 --> 01:23:48,800 >> But right here, up to the 0, and then is just put it a 1747 01:23:48,800 --> 01:23:50,030 bunch of random numbers. 1748 01:23:50,030 --> 01:23:52,590 This is kind of like some-- 1749 01:23:52,590 --> 01:23:56,190 >> LAUREN CARVALHO: This is because we're not able to represent this decimal 1750 01:23:56,190 --> 01:23:58,320 precisely with the number of bits we have. 1751 01:23:58,320 --> 01:23:59,730 >> LUCAS FREITAS: And then the computer is basically 1752 01:23:59,730 --> 01:24:01,470 giving you a trash number. 1753 01:24:01,470 --> 01:24:04,530 This is not actually the value of the number. 1754 01:24:04,530 --> 01:24:08,242 >> AUDIENCE: It's as close as it can get [INAUDIBLE] 1755 01:24:08,242 --> 01:24:09,613 using the [INAUDIBLE]. 1756 01:24:09,613 --> 01:24:12,685 1757 01:24:12,685 --> 01:24:18,830 >> LAUREN CARVALHO: So the implication of this is that let's say that instead of 1758 01:24:18,830 --> 01:24:27,680 being 0.100014, whatever, what if it was 0.0999-- 1759 01:24:27,680 --> 01:24:30,470 well, OK. 1760 01:24:30,470 --> 01:24:38,280 Let's pretend that the actual answer to this was 0.9999999. 1761 01:24:38,280 --> 01:24:40,740 That's very close to 1, but it's not exactly 1. 1762 01:24:40,740 --> 01:24:42,840 It's 0.99999. 1763 01:24:42,840 --> 01:24:48,140 >> That means that if I take that 0.999, and I cast it to an int, I get 0. 1764 01:24:48,140 --> 01:24:49,460 I don't get 1. 1765 01:24:49,460 --> 01:24:53,050 >> LUCAS FREITAS: Because remember, casting from float to int, you just 1766 01:24:53,050 --> 01:24:54,880 ignore all the decimal places. 1767 01:24:54,880 --> 01:24:56,400 You just ignore them. 1768 01:24:56,400 --> 01:25:00,170 There's no such thing as rounding up or running down when you typecast. 1769 01:25:00,170 --> 01:25:03,310 Actually, there is this function that does rounding, which is very 1770 01:25:03,310 --> 01:25:08,840 important, but you just think that when you do typecasting, it's just 1771 01:25:08,840 --> 01:25:09,720 going to delete everything. 1772 01:25:09,720 --> 01:25:13,970 So even if you have 0.99999, it's going to just give 0 if you typecast 1773 01:25:13,970 --> 01:25:14,800 [INAUDIBLE] an integer. 1774 01:25:14,800 --> 01:25:17,480 >> LAUREN CARVALHO: So just be really aware of floating point in precision, 1775 01:25:17,480 --> 01:25:20,410 especially when you start to use floats in Pset1. 1776 01:25:20,410 --> 01:25:22,790 But the walk through should warn you of that as well. 1777 01:25:22,790 --> 01:25:26,100 >> LUCAS FREITAS: And actually it's really hard, for example, if I did 1778 01:25:26,100 --> 01:25:31,570 float answer 1 equals 1.0 over 10, and then float answer 2 1779 01:25:31,570 --> 01:25:35,540 equals 10.0 over 100.0. 1780 01:25:35,540 --> 01:25:36,890 They are the same number, right? 1781 01:25:36,890 --> 01:25:38,540 0.1, right? 1782 01:25:38,540 --> 01:25:41,700 1 over 10 or 10 over 100. 1783 01:25:41,700 --> 01:25:46,360 But now if I try to see the equality, like if answer 1 is equal to answer 2, 1784 01:25:46,360 --> 01:25:49,300 maybe it's not actually going to be the same thing. 1785 01:25:49,300 --> 01:25:50,720 So floats are kind of like that. 1786 01:25:50,720 --> 01:25:51,970 They're like doing equality. 1787 01:25:51,970 --> 01:25:55,136