All language subtitles for 2022_fall_section1-720p-en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English Download
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese Download
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 00:00:00,000 --> 00:00:04,645 1 00:00:04,645 --> 00:00:06,020 CARTER ZENKE: Hello, one and all. 2 00:00:06,020 --> 00:00:10,610 And welcome to CS50's very first super section for week 1. 3 00:00:10,610 --> 00:00:12,005 My name is Carter Zenke. 4 00:00:12,005 --> 00:00:13,130 I'm the course's preceptor. 5 00:00:13,130 --> 00:00:14,990 I'm joined here by many of our wonderful staff members 6 00:00:14,990 --> 00:00:16,698 that are going to stand up and say hello. 7 00:00:16,698 --> 00:00:19,140 [APPLAUSE] 8 00:00:19,140 --> 00:00:21,680 Yeah, a round of applause for these folks. 9 00:00:21,680 --> 00:00:22,430 They're wonderful. 10 00:00:22,430 --> 00:00:25,650 They'll be helping you through this super section today. 11 00:00:25,650 --> 00:00:27,740 This is our first super section for the course. 12 00:00:27,740 --> 00:00:29,990 On a normal week, you actually attend your own section 13 00:00:29,990 --> 00:00:31,310 online by course's head teaching fellow. 14 00:00:31,310 --> 00:00:32,509 You signed up for those online at Harvard. 15 00:00:32,509 --> 00:00:34,290 And you'll attend those starting next week. 16 00:00:34,290 --> 00:00:35,998 But this week, given the odd schedule, we 17 00:00:35,998 --> 00:00:38,940 thought we'd come together for one large section overall. 18 00:00:38,940 --> 00:00:40,710 So here are these details here. 19 00:00:40,710 --> 00:00:44,040 You're going to email if you've got any questions, heads@cs50.harvard.edu. 20 00:00:44,040 --> 00:00:46,880 And all of our slides and intros for today will be at this URL 21 00:00:46,880 --> 00:00:51,205 down here on the course website, going to the super section page there. 22 00:00:51,205 --> 00:00:53,330 To kick things off, though, I thought we'd actually 23 00:00:53,330 --> 00:00:57,208 have you all talk to each other and think about these two questions. 24 00:00:57,208 --> 00:00:59,750 So think back to lecture and think about this first question. 25 00:00:59,750 --> 00:01:02,180 What did you find exciting from that lecture? 26 00:01:02,180 --> 00:01:04,610 And what are you still confused about? 27 00:01:04,610 --> 00:01:06,480 What do you still have questions about? 28 00:01:06,480 --> 00:01:10,520 So turn to somebody, perhaps who you don't know and talk about those two 29 00:01:10,520 --> 00:01:12,020 questions for 2 minutes here. 30 00:01:12,020 --> 00:01:13,700 One, what are you excited about? 31 00:01:13,700 --> 00:01:17,210 Two, what do you still have questions about? 32 00:01:17,210 --> 00:01:19,910 Let's come back from these conversations. 33 00:01:19,910 --> 00:01:23,100 So glad you are having this wonderful discussion here. 34 00:01:23,100 --> 00:01:25,610 And let's hear from a few different groups 35 00:01:25,610 --> 00:01:29,400 on what you were thinking about for these questions here. 36 00:01:29,400 --> 00:01:30,410 Thank you, all. 37 00:01:30,410 --> 00:01:33,455 Let's actually turn to this left-hand section. 38 00:01:33,455 --> 00:01:35,330 Could one group over here tell me what you're 39 00:01:35,330 --> 00:01:37,997 excited about for this week and one thing you're still wondering 40 00:01:37,997 --> 00:01:39,578 or you have questions about? 41 00:01:39,578 --> 00:01:41,120 One group from this side of the room? 42 00:01:41,120 --> 00:01:46,960 43 00:01:46,960 --> 00:01:49,460 Can I narrow it down to somebody from the front row perhaps? 44 00:01:49,460 --> 00:01:50,670 What are you interested in? 45 00:01:50,670 --> 00:01:52,878 What do you still have questions about for this week? 46 00:01:52,878 --> 00:01:59,620 47 00:01:59,620 --> 00:02:01,540 Yeah, go ahead. 48 00:02:01,540 --> 00:02:06,850 STUDENT: I think it's more of the-- and less of the code, the syntax itself. 49 00:02:06,850 --> 00:02:10,580 Because that, you can kind of pick up quite quickly. 50 00:02:10,580 --> 00:02:16,450 I think the hardest thing is probably to how you construct efficient code. 51 00:02:16,450 --> 00:02:18,988 You sometimes just have to think in a different way. 52 00:02:18,988 --> 00:02:19,780 CARTER ZENKE: Nice. 53 00:02:19,780 --> 00:02:23,550 STUDENT: That's something that I guess, we'll 54 00:02:23,550 --> 00:02:26,043 really take some time to come to terms. 55 00:02:26,043 --> 00:02:27,210 CARTER ZENKE: Totally, yeah. 56 00:02:27,210 --> 00:02:29,130 So there's a question, how do you write efficient code? 57 00:02:29,130 --> 00:02:31,440 And later today, we'll actually work on the course's lab, 58 00:02:31,440 --> 00:02:33,440 this practice problem to help you see how we can 59 00:02:33,440 --> 00:02:35,130 write efficient code in that instance. 60 00:02:35,130 --> 00:02:38,297 You can work together on this practice problem and get ideas from each other 61 00:02:38,297 --> 00:02:39,150 as we go through. 62 00:02:39,150 --> 00:02:41,580 Let's hear from maybe one more group over on this side, 63 00:02:41,580 --> 00:02:44,160 maybe somebody in the second row back here, if you mind. 64 00:02:44,160 --> 00:02:46,650 One thing you're interested in, yeah, go ahead. 65 00:02:46,650 --> 00:02:49,830 STUDENT: I'm really interested in also this kind of stuff, 66 00:02:49,830 --> 00:02:54,030 but also creating stuff, just coding to have a the final product. 67 00:02:54,030 --> 00:02:58,350 And now I'll get opportunities to make a final product. 68 00:02:58,350 --> 00:03:00,570 And somebody else to think about lecture, 69 00:03:00,570 --> 00:03:02,910 I feel like there's so much syntax to learn. 70 00:03:02,910 --> 00:03:05,820 And that's sort of giving me pause. 71 00:03:05,820 --> 00:03:08,750 It's like, I don't even know what's out there. 72 00:03:08,750 --> 00:03:13,640 I think somebody said, actually in the orientation, 73 00:03:13,640 --> 00:03:15,860 sometimes like students will go something 74 00:03:15,860 --> 00:03:18,110 all the way out because they don't even know that there's a program for it 75 00:03:18,110 --> 00:03:18,770 already. 76 00:03:18,770 --> 00:03:20,030 CARTER ZENKE: Yeah, exactly. 77 00:03:20,030 --> 00:03:22,573 So there's this idea of how you take this idea you have 78 00:03:22,573 --> 00:03:24,740 and what you want to build and turn into the syntax, 79 00:03:24,740 --> 00:03:27,380 like actually write to make the computer do what you want it to do, right? 80 00:03:27,380 --> 00:03:29,100 So all these are good questions. 81 00:03:29,100 --> 00:03:31,740 And here are a few we'll actually dive in to today. 82 00:03:31,740 --> 00:03:33,980 The first is, why are we using C? 83 00:03:33,980 --> 00:03:37,280 Why we're using this ancient language to start off programming with? 84 00:03:37,280 --> 00:03:39,802 And how do we learn and read its syntax here? 85 00:03:39,802 --> 00:03:43,010 How can we actually learn how to create and read these variables, these loops 86 00:03:43,010 --> 00:03:44,750 and conditionals, and so on. 87 00:03:44,750 --> 00:03:48,260 As we go through that, we'll talk about this particular instance of a data type 88 00:03:48,260 --> 00:03:53,100 because C requires us to tell what type each pieces of our data are. 89 00:03:53,100 --> 00:03:56,150 We'll also talk about compiling C programs, taking them from source code 90 00:03:56,150 --> 00:03:57,140 to machine code. 91 00:03:57,140 --> 00:03:59,015 And then finally, at the very end, we'll talk 92 00:03:59,015 --> 00:04:02,720 about how many years it will take to double our number of llamas 93 00:04:02,720 --> 00:04:04,087 that we have in the lab. 94 00:04:04,087 --> 00:04:06,920 So to kick things off here, we can go back to actually lecture zero, 95 00:04:06,920 --> 00:04:09,510 I think, back to our contact application. 96 00:04:09,510 --> 00:04:13,820 So in this application we stored names and phone numbers and other things too. 97 00:04:13,820 --> 00:04:16,430 What else could we store in a contact's application? 98 00:04:16,430 --> 00:04:19,430 Could I ask maybe somebody from the second row, in this middle row here? 99 00:04:19,430 --> 00:04:23,330 What can we store in a contact's application? 100 00:04:23,330 --> 00:04:23,962 Yeah, go ahead. 101 00:04:23,962 --> 00:04:24,920 STUDENT: Phone numbers. 102 00:04:24,920 --> 00:04:27,128 CARTER ZENKE: Phone numbers, right, other things too. 103 00:04:27,128 --> 00:04:29,142 Maybe somebody in the row up now. 104 00:04:29,142 --> 00:04:30,238 STUDENT: Emails. 105 00:04:30,238 --> 00:04:31,280 CARTER ZENKE: Emails, OK. 106 00:04:31,280 --> 00:04:31,880 Other things? 107 00:04:31,880 --> 00:04:33,240 Let's go one row up again. 108 00:04:33,240 --> 00:04:34,250 What else can we store? 109 00:04:34,250 --> 00:04:35,120 STUDENT: Addresses. 110 00:04:35,120 --> 00:04:35,990 CARTER ZENKE: Addresses, nice. 111 00:04:35,990 --> 00:04:38,390 So all these things we can store in our contact's application. 112 00:04:38,390 --> 00:04:40,130 But let's say you want to store the number of times 113 00:04:40,130 --> 00:04:43,040 I want to actually have made a call before in my application, on my phone, 114 00:04:43,040 --> 00:04:43,540 right? 115 00:04:43,540 --> 00:04:47,180 So here we have an instance of a variable, some number or some value 116 00:04:47,180 --> 00:04:48,110 that can change. 117 00:04:48,110 --> 00:04:50,870 And I'm going to give a name to it, in this case, calls, 118 00:04:50,870 --> 00:04:52,522 number of times I called somebody here. 119 00:04:52,522 --> 00:04:54,980 So this is our mental representation of what a variable is. 120 00:04:54,980 --> 00:04:57,080 It's some name for a value that can change. 121 00:04:57,080 --> 00:05:00,382 But in C, we have a particular syntax I'll use to make variables. 122 00:05:00,382 --> 00:05:02,840 As you all go through and work on the course's problem set, 123 00:05:02,840 --> 00:05:05,840 build your own programs, you really should get familiar with this syntax 124 00:05:05,840 --> 00:05:07,560 and how to read and write it here. 125 00:05:07,560 --> 00:05:11,395 So here we have the syntax in C to create this variable named calls. 126 00:05:11,395 --> 00:05:14,520 And there are some components to it that we should actually dive into here. 127 00:05:14,520 --> 00:05:16,910 So the first part is the variable's name. 128 00:05:16,910 --> 00:05:18,530 This name is calls. 129 00:05:18,530 --> 00:05:21,380 Notice how it's in the middle of this sentence here. 130 00:05:21,380 --> 00:05:25,010 We also have the variables type, this int type. 131 00:05:25,010 --> 00:05:27,577 And what does this int type stand for? 132 00:05:27,577 --> 00:05:29,660 What kind of number are we going to store in here? 133 00:05:29,660 --> 00:05:30,980 Could I ask somebody from down here? 134 00:05:30,980 --> 00:05:31,580 Yeah, go ahead. 135 00:05:31,580 --> 00:05:32,413 STUDENT: An integer. 136 00:05:32,413 --> 00:05:34,580 CARTER ZENKE: An integer, so a whole number, right? 137 00:05:34,580 --> 00:05:37,310 Then we have the value, in this case, 4, that value we're 138 00:05:37,310 --> 00:05:38,990 putting inside this variable here. 139 00:05:38,990 --> 00:05:43,460 But there's one piece we're missing, we haven't talked about yet. 140 00:05:43,460 --> 00:05:45,420 What haven't we named here yet? 141 00:05:45,420 --> 00:05:47,670 Could I ask somebody from maybe this side of the room? 142 00:05:47,670 --> 00:05:49,370 Take a guess? 143 00:05:49,370 --> 00:05:52,940 We talked about the name of the variable, the type, the value. 144 00:05:52,940 --> 00:05:55,430 What syntax haven't we shown yet? 145 00:05:55,430 --> 00:05:56,150 Yeah, go ahead. 146 00:05:56,150 --> 00:05:58,040 STUDENT: [INAUDIBLE] semicolon [INAUDIBLE].. 147 00:05:58,040 --> 00:05:59,498 CARTER ZENKE: The semicolon, right? 148 00:05:59,498 --> 00:06:03,200 So this is a statement in C. And every closing statement that we have, 149 00:06:03,200 --> 00:06:05,130 we want to include a semicolon there. 150 00:06:05,130 --> 00:06:07,130 The other thing we haven't talked about, the one 151 00:06:07,130 --> 00:06:08,390 piece we haven't talked about here, could I 152 00:06:08,390 --> 00:06:10,220 ask somebody from down here now? 153 00:06:10,220 --> 00:06:13,100 We have the semicolon, the value, the name, the data type. 154 00:06:13,100 --> 00:06:14,000 One more thing, yeah. 155 00:06:14,000 --> 00:06:14,660 STUDENT: The equals sign. 156 00:06:14,660 --> 00:06:17,810 CARTER ZENKE: The equals sign, and is it an equals sign, could I ask you? 157 00:06:17,810 --> 00:06:18,710 No, you're shaking your head. 158 00:06:18,710 --> 00:06:19,100 STUDENT: It's and assignment. 159 00:06:19,100 --> 00:06:21,142 CARTER ZENKE: It's an assignment operator, right? 160 00:06:21,142 --> 00:06:24,290 So we'll say this is going to assign the value 4 to the space we've 161 00:06:24,290 --> 00:06:26,150 created for this variable named call. 162 00:06:26,150 --> 00:06:28,010 So if you say this in English here, we're 163 00:06:28,010 --> 00:06:30,540 going to create an integer variable. 164 00:06:30,540 --> 00:06:32,540 Notice how this type aligns with this type here. 165 00:06:32,540 --> 00:06:34,820 Named calls, the name lines up here. 166 00:06:34,820 --> 00:06:39,240 That gets, or that kind of stores this value 4, in this instance. 167 00:06:39,240 --> 00:06:41,360 So let's try again with another value here. 168 00:06:41,360 --> 00:06:47,300 Int x equals sign 50, and let's actually say this one all together, if we could. 169 00:06:47,300 --> 00:06:49,430 How do we say we create a what? 170 00:06:49,430 --> 00:06:50,150 STUDENT: Integer. 171 00:06:50,150 --> 00:06:52,025 CARTER ZENKE: (CLASS REPEATING AFTER) Integer 172 00:06:52,025 --> 00:06:56,190 that called x that gets the value 50. 173 00:06:56,190 --> 00:06:56,690 Nice. 174 00:06:56,690 --> 00:06:58,470 That's what it would be in English here. 175 00:06:58,470 --> 00:06:58,970 Amazing. 176 00:06:58,970 --> 00:07:01,280 Yeah, high fives all around. 177 00:07:01,280 --> 00:07:04,888 So why does C care so much about data types though? 178 00:07:04,888 --> 00:07:07,430 The very first thing we say here is not the name of the error 179 00:07:07,430 --> 00:07:09,870 but actually the data type. 180 00:07:09,870 --> 00:07:12,800 So we saw briefly this idea in lecture, but let's 181 00:07:12,800 --> 00:07:14,690 go ahead and talk to the person next to you. 182 00:07:14,690 --> 00:07:17,545 Why do you think C cares so much about these data types? 183 00:07:17,545 --> 00:07:19,670 Why is it the first thing we tell the computer when 184 00:07:19,670 --> 00:07:20,795 we make this variable here? 185 00:07:20,795 --> 00:07:22,940 We'll come back in just a minute. 186 00:07:22,940 --> 00:07:27,230 OK, let's come back and hear some ideas for why does C 187 00:07:27,230 --> 00:07:29,240 care so much about data types? 188 00:07:29,240 --> 00:07:32,990 Why is it the very first thing we put in a given line of code 189 00:07:32,990 --> 00:07:35,460 to make a variable in this case? 190 00:07:35,460 --> 00:07:38,060 And can we go back to maybe this side of the room now. 191 00:07:38,060 --> 00:07:40,430 And anyone in particular like to share what 192 00:07:40,430 --> 00:07:44,580 your group talked about, what ideas you had for this question here? 193 00:07:44,580 --> 00:07:45,900 Yeah, go ahead. 194 00:07:45,900 --> 00:07:48,660 STUDENT: Basically, we said that C wants to know data type. 195 00:07:48,660 --> 00:07:51,670 That's in order restrict the values that we input. 196 00:07:51,670 --> 00:07:54,685 For instance, if we're talking about [INAUDIBLE] input 197 00:07:54,685 --> 00:07:56,610 3.5, so for [INAUDIBLE]. 198 00:07:56,610 --> 00:07:58,257 So it limits [INAUDIBLE]. 199 00:07:58,257 --> 00:08:00,840 CARTER ZENKE: Yeah, there's some value for specificity, right? 200 00:08:00,840 --> 00:08:02,880 I want to be able to make sure that I'm not 201 00:08:02,880 --> 00:08:06,240 putting in some value I couldn't store in this variable, like a text, 202 00:08:06,240 --> 00:08:07,620 for example. 203 00:08:07,620 --> 00:08:08,610 Other reasons too. 204 00:08:08,610 --> 00:08:10,230 Could we go to maybe down here? 205 00:08:10,230 --> 00:08:11,085 Yeah. 206 00:08:11,085 --> 00:08:13,210 STUDENT: The computer needs to know how much memory 207 00:08:13,210 --> 00:08:15,940 to allocate to the [INAUDIBLE]. 208 00:08:15,940 --> 00:08:18,965 CARTER ZENKE: Yeah, and why would that matter? 209 00:08:18,965 --> 00:08:20,590 Can I ask you, get a little deeper too? 210 00:08:20,590 --> 00:08:21,850 Why does this size matter? 211 00:08:21,850 --> 00:08:27,160 STUDENT: Because I think an integer is a lot less size than-- 212 00:08:27,160 --> 00:08:29,800 a lot less [INAUDIBLE]. 213 00:08:29,800 --> 00:08:31,180 CARTER ZENKE: Totally, so using-- 214 00:08:31,180 --> 00:08:33,815 or storing different values makes that value take away 215 00:08:33,815 --> 00:08:35,440 different space in a computer's memory. 216 00:08:35,440 --> 00:08:38,710 So integers take up maybe 32 bits, but a character 217 00:08:38,710 --> 00:08:40,115 might take up only eight bits. 218 00:08:40,115 --> 00:08:41,990 And so it tells the compiler how much memory, 219 00:08:41,990 --> 00:08:44,200 how much space to reserve here too. 220 00:08:44,200 --> 00:08:47,560 One other reason that we saw a little bit in lecture as well is we 221 00:08:47,560 --> 00:08:52,870 have some binary here, these eight bits that can represent a certain, well, 222 00:08:52,870 --> 00:08:55,260 really anything. 223 00:08:55,260 --> 00:08:56,580 What does this represent here? 224 00:08:56,580 --> 00:08:59,050 Anyone know off the top of their head? 225 00:08:59,050 --> 00:08:59,670 Yeah. 226 00:08:59,670 --> 00:09:00,740 STUDENT: No, sorry. 227 00:09:00,740 --> 00:09:02,085 CARTER ZENKE: Oh, no, OK. 228 00:09:02,085 --> 00:09:03,210 I'd be surprised if you do. 229 00:09:03,210 --> 00:09:08,822 But one thing this represents is the number or the integer 65, right? 230 00:09:08,822 --> 00:09:10,530 But as we saw in lecture a little bit too 231 00:09:10,530 --> 00:09:14,880 is this can also represent the letter A. Those same binary bits can represent 232 00:09:14,880 --> 00:09:18,150 both 65 and this character A. So it's important we 233 00:09:18,150 --> 00:09:22,210 tell C which thing we're talking about here by giving us our data types. 234 00:09:22,210 --> 00:09:24,690 So that's the reason we have these data types here. 235 00:09:24,690 --> 00:09:28,860 If we want to go further into variables, we can update them if we'd like. 236 00:09:28,860 --> 00:09:34,230 And here we have on this very first line the declaration and initialization 237 00:09:34,230 --> 00:09:35,610 of this variable calls. 238 00:09:35,610 --> 00:09:38,260 And down below we just update that value. 239 00:09:38,260 --> 00:09:41,922 So notice the type is on that top line, but it's not on that next line. 240 00:09:41,922 --> 00:09:44,880 And why would that be, if I can ask somebody from this side of the room 241 00:09:44,880 --> 00:09:45,510 now? 242 00:09:45,510 --> 00:09:50,360 Why is it on that top line but not that second line? 243 00:09:50,360 --> 00:09:52,225 Yeah, go ahead. 244 00:09:52,225 --> 00:09:54,600 STUDENT: On the first line you're initiating the variable 245 00:09:54,600 --> 00:09:58,160 and telling the computer what the data type is. 246 00:09:58,160 --> 00:10:01,160 On the second line, you're reassigning it to the new value, 247 00:10:01,160 --> 00:10:03,458 and it already knows that that variable [INAUDIBLE].. 248 00:10:03,458 --> 00:10:06,500 CARTER ZENKE: Yeah, so the computer is pretty good at remembering things, 249 00:10:06,500 --> 00:10:07,000 right? 250 00:10:07,000 --> 00:10:10,010 And once you've told it that this variable has a certain type, no need 251 00:10:10,010 --> 00:10:10,550 to tell it again. 252 00:10:10,550 --> 00:10:11,540 It already knows that. 253 00:10:11,540 --> 00:10:13,680 We can just keep going as we go. 254 00:10:13,680 --> 00:10:16,880 So here we're going to initialize and declare the variable calls. 255 00:10:16,880 --> 00:10:20,870 But when we use it again, we don't have to tell it the type as we go through. 256 00:10:20,870 --> 00:10:23,630 Now, if we wanted to change that value, we could do it like this. 257 00:10:23,630 --> 00:10:25,730 We also have some operators at our disposal. 258 00:10:25,730 --> 00:10:28,820 We could add 1 to it using this plus sign. 259 00:10:28,820 --> 00:10:31,070 We could even subtract some numbers. 260 00:10:31,070 --> 00:10:34,340 We could maybe multiply some numbers using that star operator. 261 00:10:34,340 --> 00:10:36,360 We could even divide some numbers, and so on. 262 00:10:36,360 --> 00:10:37,610 So feel free to take a look at those. 263 00:10:37,610 --> 00:10:40,360 If you go through the course, I'm sure these come top of your head 264 00:10:40,360 --> 00:10:41,220 as we go through. 265 00:10:41,220 --> 00:10:47,240 But when it comes to assigning some variable, what looks like a function, 266 00:10:47,240 --> 00:10:49,357 right, that's where things get a little bit weird. 267 00:10:49,357 --> 00:10:52,190 We saw in lecture we're trying to get some input from a user, right? 268 00:10:52,190 --> 00:10:56,870 And we didn't say that maybe string name equals quote "Carter." 269 00:10:56,870 --> 00:11:00,200 We said it equals this, maybe, function, get string. 270 00:11:00,200 --> 00:11:03,290 Or here, if we wanted to store a value in calls, get int. 271 00:11:03,290 --> 00:11:06,560 And in this case, before we're reading left to right. 272 00:11:06,560 --> 00:11:09,210 But in this case, it's actually better to read right to left 273 00:11:09,210 --> 00:11:12,120 when you see this function call on the right-hand side. 274 00:11:12,120 --> 00:11:14,120 So what's happening here is, when you have 275 00:11:14,120 --> 00:11:16,245 this function on the right-hand side, this function 276 00:11:16,245 --> 00:11:19,370 is wanting to run and then give us back some return 277 00:11:19,370 --> 00:11:23,100 value that will be stored inside this variable on the left-hand side. 278 00:11:23,100 --> 00:11:26,300 So before anything else, we run this function on the right-hand side 279 00:11:26,300 --> 00:11:29,720 with some certain arguments, some input to that function 280 00:11:29,720 --> 00:11:31,550 tell it what exactly to do. 281 00:11:31,550 --> 00:11:32,840 That function runs. 282 00:11:32,840 --> 00:11:35,760 Maybe it asks the user for this integer number here. 283 00:11:35,760 --> 00:11:40,110 And we then store that value right where that function was called. 284 00:11:40,110 --> 00:11:42,660 And it then goes into that variable. 285 00:11:42,660 --> 00:11:46,290 And now we're back to simple variable assignment here. 286 00:11:46,290 --> 00:11:50,682 So usually, if you're simply assigning numbers, you can read left to right. 287 00:11:50,682 --> 00:11:53,390 But if you have these function calls, best to read right to left, 288 00:11:53,390 --> 00:11:56,390 and knowing that your function runs first and then assigns that value 289 00:11:56,390 --> 00:11:58,110 as we go through. 290 00:11:58,110 --> 00:12:00,335 So questions on this before we dive even deeper. 291 00:12:00,335 --> 00:12:10,640 292 00:12:10,640 --> 00:12:12,510 Seeing none. 293 00:12:12,510 --> 00:12:16,160 So now that we have these values stored in these variables, 294 00:12:16,160 --> 00:12:19,160 it's probably worthwhile to think about how we print them to the screen. 295 00:12:19,160 --> 00:12:21,590 And here's what we saw a little bit in lecture as well, 296 00:12:21,590 --> 00:12:26,340 this % and then this i here. 297 00:12:26,340 --> 00:12:28,310 And what is this doing for us? 298 00:12:28,310 --> 00:12:30,695 Could I ask somebody from this middle section here. 299 00:12:30,695 --> 00:12:32,570 Let's go to the first row, if you don't mind. 300 00:12:32,570 --> 00:12:33,070 Yeah. 301 00:12:33,070 --> 00:12:35,732 STUDENT: It's a placeholder for our variable call. 302 00:12:35,732 --> 00:12:37,690 CARTER ZENKE: Yeah, placeholder for your calls. 303 00:12:37,690 --> 00:12:39,120 And why is it %i? 304 00:12:39,120 --> 00:12:39,620 Do you know? 305 00:12:39,620 --> 00:12:41,968 STUDENT: Because it's an integer [INAUDIBLE].. 306 00:12:41,968 --> 00:12:42,760 CARTER ZENKE: Yeah. 307 00:12:42,760 --> 00:12:44,630 STUDENT: i represents [INAUDIBLE]. 308 00:12:44,630 --> 00:12:46,010 CARTER ZENKE: Right, so %i. 309 00:12:46,010 --> 00:12:48,440 This % is kind of saying like, hey, this is a placeholder. 310 00:12:48,440 --> 00:12:50,810 And that i specifies what kind of variable 311 00:12:50,810 --> 00:12:52,830 can go inside of that placeholder there. 312 00:12:52,830 --> 00:12:55,460 So because call is an integer, it's %i. 313 00:12:55,460 --> 00:12:58,670 And that's called, more specifically a format code, as we might have said. 314 00:12:58,670 --> 00:13:01,520 And we have this value that we can store in that format code there. 315 00:13:01,520 --> 00:13:04,817 We could have multiple format codes and multiple values. 316 00:13:04,817 --> 00:13:06,150 We just separate them by commas. 317 00:13:06,150 --> 00:13:11,060 So you can imagine, for example, I have %i here and maybe %i later on. 318 00:13:11,060 --> 00:13:13,860 I could have calls and then comma, some other variable. 319 00:13:13,860 --> 00:13:15,980 And those would go inside those placeholders one 320 00:13:15,980 --> 00:13:21,020 by one, aligning with whatever order I put them in as we go through. 321 00:13:21,020 --> 00:13:24,590 Now, it's not worth memorizing any of these, 322 00:13:24,590 --> 00:13:27,725 but there are a great number of format codes for different data types. 323 00:13:27,725 --> 00:13:29,600 And as you work with a variety of data types, 324 00:13:29,600 --> 00:13:31,640 best you can maybe reference these once in a while. 325 00:13:31,640 --> 00:13:33,110 So here on the left-hand side, we have number 326 00:13:33,110 --> 00:13:35,690 format codes, like ints and longs, floats and doubles, 327 00:13:35,690 --> 00:13:38,420 and the right-hand side some chars and strings, 328 00:13:38,420 --> 00:13:40,100 where chars are individual characters. 329 00:13:40,100 --> 00:13:43,770 And strings are collections of characters as we go through. 330 00:13:43,770 --> 00:13:49,440 One thing we haven't quite seen as much yet is this long and double. 331 00:13:49,440 --> 00:13:54,100 Anyone want to hazard a guess as to what that might be here? 332 00:13:54,100 --> 00:13:56,620 Could I ask somebody from this middle row again? 333 00:13:56,620 --> 00:13:59,145 334 00:13:59,145 --> 00:14:00,895 What is this long and double doing for us? 335 00:14:00,895 --> 00:14:05,710 336 00:14:05,710 --> 00:14:06,400 Yeah, go ahead. 337 00:14:06,400 --> 00:14:11,834 STUDENT: Does the long [INAUDIBLE] more room [INAUDIBLE] 338 00:14:11,834 --> 00:14:16,023 you have to have the long to [INAUDIBLE]?? 339 00:14:16,023 --> 00:14:17,190 CARTER ZENKE: Yeah, exactly. 340 00:14:17,190 --> 00:14:22,242 So here we had an integer that could store up to four billion values. 341 00:14:22,242 --> 00:14:23,950 But when that's not enough, we might want 342 00:14:23,950 --> 00:14:27,150 to have a longer value, in this case, called a long, 343 00:14:27,150 --> 00:14:29,410 that will have twice as much space for us. 344 00:14:29,410 --> 00:14:32,322 We can store twice as many different combinations as we go through. 345 00:14:32,322 --> 00:14:33,280 Really, more than that. 346 00:14:33,280 --> 00:14:36,310 We're just having twice as many bits to represent that information. 347 00:14:36,310 --> 00:14:39,330 And similarly, for the float, that's a decimal number. 348 00:14:39,330 --> 00:14:43,030 But this double is called a double precision floating point. 349 00:14:43,030 --> 00:14:46,260 So we'd have twice as many decimals, twice many binary digits 350 00:14:46,260 --> 00:14:48,630 representing that decimal number there for us 351 00:14:48,630 --> 00:14:52,610 as well, so just more space for us to use as we go through. 352 00:14:52,610 --> 00:14:54,740 Now, what we'll do here is a brief exercise 353 00:14:54,740 --> 00:15:01,107 and invite you to load up code.cs50.io on your own laptop here. 354 00:15:01,107 --> 00:15:03,440 And you should see something that looks a bit like this. 355 00:15:03,440 --> 00:15:06,125 Maybe you have a File Explorer on the left-hand side. 356 00:15:06,125 --> 00:15:11,083 357 00:15:11,083 --> 00:15:13,250 But you should certainly have a terminal down below. 358 00:15:13,250 --> 00:15:13,958 I see a question. 359 00:15:13,958 --> 00:15:14,753 Yeah. 360 00:15:14,753 --> 00:15:17,200 STUDENT: I'm not sure if this was a typo to be like this. 361 00:15:17,200 --> 00:15:18,778 But if you go back to the slide-- 362 00:15:18,778 --> 00:15:19,820 CARTER ZENKE: Yeah, yeah. 363 00:15:19,820 --> 00:15:22,950 364 00:15:22,950 --> 00:15:26,514 STUDENT: So the double says f, and the float says f. 365 00:15:26,514 --> 00:15:27,450 Is that correct? 366 00:15:27,450 --> 00:15:28,700 CARTER ZENKE: That is correct. 367 00:15:28,700 --> 00:15:31,770 So both the float and the double have the same format code, yeah. 368 00:15:31,770 --> 00:15:32,550 Good question. 369 00:15:32,550 --> 00:15:35,847 STUDENT: So how does the computer [INAUDIBLE] 370 00:15:35,847 --> 00:15:38,508 recognize between [INAUDIBLE]? 371 00:15:38,508 --> 00:15:40,800 CARTER ZENKE: How does the computer recognize each one? 372 00:15:40,800 --> 00:15:45,160 In this case, it doesn't quite matter because, in this case, 373 00:15:45,160 --> 00:15:47,580 the float and the double are both decimal numbers. 374 00:15:47,580 --> 00:15:51,390 And so the computer knows when it sees %f, I'm printing a decimal number. 375 00:15:51,390 --> 00:15:53,950 It just so happens to be that the double is twice as long. 376 00:15:53,950 --> 00:15:56,400 And so it just prints that many more-- 377 00:15:56,400 --> 00:15:59,310 well, it can print that many more spaces after the decimal point 378 00:15:59,310 --> 00:16:01,230 if you'd like it to. 379 00:16:01,230 --> 00:16:03,450 Yeah, other questions here before we move on? 380 00:16:03,450 --> 00:16:08,070 381 00:16:08,070 --> 00:16:10,950 All right, so we'll work on a brief exercise here. 382 00:16:10,950 --> 00:16:15,260 And once you've loaded up your IDE in code.cs50.io, 383 00:16:15,260 --> 00:16:18,380 you go ahead and create an application called phonebook.c. 384 00:16:18,380 --> 00:16:21,800 And the goal is to prompt the user for these three things 385 00:16:21,800 --> 00:16:24,680 and then print them back out to the user as confirmation 386 00:16:24,680 --> 00:16:27,540 that this data is stored in your program. 387 00:16:27,540 --> 00:16:32,810 So if I wanted to do this, I would first go down to my terminal down below. 388 00:16:32,810 --> 00:16:35,225 And I would make a new file. 389 00:16:35,225 --> 00:16:37,100 What would I do to make a new file down here? 390 00:16:37,100 --> 00:16:40,730 Could I ask somebody from maybe this right-hand bottom side? 391 00:16:40,730 --> 00:16:44,720 How can I make a new file in this program here? 392 00:16:44,720 --> 00:16:45,290 Yeah. 393 00:16:45,290 --> 00:16:46,838 STUDENT: Code and file name. 394 00:16:46,838 --> 00:16:48,630 CARTER ZENKE: Yeah, code and the file name. 395 00:16:48,630 --> 00:16:51,860 So I could do code, if it will load here. 396 00:16:51,860 --> 00:16:54,510 397 00:16:54,510 --> 00:16:55,160 Let me refresh. 398 00:16:55,160 --> 00:17:00,270 But on your own, you could certainly do code and in the file name here. 399 00:17:00,270 --> 00:17:04,520 And for our purposes, I'll do code space maybe phonebook.c. 400 00:17:04,520 --> 00:17:09,123 But you could call your program whatever else you'd like to call it here. 401 00:17:09,123 --> 00:17:10,790 And maybe yours is doing the same thing. 402 00:17:10,790 --> 00:17:12,290 We'll be back in just a minute here. 403 00:17:12,290 --> 00:17:17,200 404 00:17:17,200 --> 00:17:22,008 Now, once you have your file, what tends to go at the very top of that file? 405 00:17:22,008 --> 00:17:23,550 What's the first thing you might add? 406 00:17:23,550 --> 00:17:25,560 We saw this in lecture. 407 00:17:25,560 --> 00:17:26,280 Yeah, go ahead. 408 00:17:26,280 --> 00:17:27,000 STUDENT: The header. 409 00:17:27,000 --> 00:17:29,375 CARTER ZENKE: The header files or these libraries, right? 410 00:17:29,375 --> 00:17:32,150 And do you remember the syntax for that? 411 00:17:32,150 --> 00:17:37,330 STUDENT: Hashtag include, and then the caret thing. 412 00:17:37,330 --> 00:17:41,080 CARTER ZENKE: Yeah, hashtag include this little caret thing. 413 00:17:41,080 --> 00:17:42,700 Let's see if this one is loaded now. 414 00:17:42,700 --> 00:17:46,030 Not quite, but this hashtag include is saying, 415 00:17:46,030 --> 00:17:49,690 I'm going to try to get some file in my computer's memory 416 00:17:49,690 --> 00:17:54,130 that has these functions declared for me that I will then use in my own program 417 00:17:54,130 --> 00:17:54,830 here. 418 00:17:54,830 --> 00:18:01,990 So what two header files might you want to include in your program here? 419 00:18:01,990 --> 00:18:02,590 Yeah. 420 00:18:02,590 --> 00:18:04,592 STUDENT: Standardio.h [INAUDIBLE]. 421 00:18:04,592 --> 00:18:07,300 CARTER ZENKE: Nice, so two off the bat that you often want to use 422 00:18:07,300 --> 00:18:11,440 are standardio.h or stdio.h cs50.h. 423 00:18:11,440 --> 00:18:13,720 Both of these have some pretty common functions 424 00:18:13,720 --> 00:18:15,880 you'll be using as you write your own program. 425 00:18:15,880 --> 00:18:22,030 426 00:18:22,030 --> 00:18:26,770 And we'll wait for this to continue connecting, and what we'll actually do 427 00:18:26,770 --> 00:18:29,770 is, now that you maybe hopefully have your file open, 428 00:18:29,770 --> 00:18:33,340 you have your header files at the top, we'll take some time, 429 00:18:33,340 --> 00:18:35,670 let's say maybe 10 minutes, to work on this on our own. 430 00:18:35,670 --> 00:18:36,820 I'll put the slide back up. 431 00:18:36,820 --> 00:18:39,070 And we'll come back in those 10 minutes and share 432 00:18:39,070 --> 00:18:40,510 how you approached this problem. 433 00:18:40,510 --> 00:18:43,000 Maybe if the staff would like to run around and help you as you work. 434 00:18:43,000 --> 00:18:44,208 Feel free to raise your hand. 435 00:18:44,208 --> 00:18:46,070 We'll come around and help you out. 436 00:18:46,070 --> 00:18:47,980 All right, let's come back. 437 00:18:47,980 --> 00:18:51,148 And we'll be working in a slightly different environment 438 00:18:51,148 --> 00:18:53,690 just because the internet is not quite what we want it to be. 439 00:18:53,690 --> 00:18:56,773 But we're going to make here our own phone book file where we can actually 440 00:18:56,773 --> 00:18:58,310 store the data we want to store. 441 00:18:58,310 --> 00:19:00,545 So if you remember our slide back here, we 442 00:19:00,545 --> 00:19:02,170 wanted to store a few different things. 443 00:19:02,170 --> 00:19:07,630 You wanted to store the user's name, their phone number, 444 00:19:07,630 --> 00:19:09,460 and I think it was their address. 445 00:19:09,460 --> 00:19:14,980 Let me actually take a look at that slide again, if we go back over here. 446 00:19:14,980 --> 00:19:17,390 A name, an age, and a phone number. 447 00:19:17,390 --> 00:19:21,550 So here we have this template for our program, 448 00:19:21,550 --> 00:19:23,650 but what are we missing right now? 449 00:19:23,650 --> 00:19:25,720 We have our header files, these libraries where 450 00:19:25,720 --> 00:19:27,220 I have functions we're going to use. 451 00:19:27,220 --> 00:19:29,665 What else are we missing in this program right here? 452 00:19:29,665 --> 00:19:30,790 Could I ask somebody from-- 453 00:19:30,790 --> 00:19:31,450 Yeah, down here. 454 00:19:31,450 --> 00:19:32,350 STUDENT: Int main void. 455 00:19:32,350 --> 00:19:33,808 CARTER ZENKE: Int main void, right. 456 00:19:33,808 --> 00:19:37,330 So I'll type int main and void up here. 457 00:19:37,330 --> 00:19:39,340 And this just symbolizes-- this is the kind 458 00:19:39,340 --> 00:19:41,650 of when flag clicked block in Scratch. 459 00:19:41,650 --> 00:19:43,900 This is going to be the main part of our program here. 460 00:19:43,900 --> 00:19:46,330 We're defining this new function called Main that 461 00:19:46,330 --> 00:19:49,160 will be the main part of our program. 462 00:19:49,160 --> 00:19:51,340 Now, the first thing we want to do is probably 463 00:19:51,340 --> 00:19:54,200 prompt the user for some information. 464 00:19:54,200 --> 00:20:00,100 So what functions did you all use to get the user's name, or that contact's name 465 00:20:00,100 --> 00:20:01,420 we're trying to story here? 466 00:20:01,420 --> 00:20:02,020 Could I ask over here? 467 00:20:02,020 --> 00:20:02,650 Yeah, go ahead. 468 00:20:02,650 --> 00:20:03,400 STUDENT: Get string. 469 00:20:03,400 --> 00:20:04,442 CARTER ZENKE: Get string. 470 00:20:04,442 --> 00:20:07,855 And how did you make your variable in this case? 471 00:20:07,855 --> 00:20:11,450 STUDENT: So because our variable is a string, I put string. 472 00:20:11,450 --> 00:20:12,928 And then I titled my variable name. 473 00:20:12,928 --> 00:20:13,720 CARTER ZENKE: Nice. 474 00:20:13,720 --> 00:20:18,958 STUDENT: And then I said equals get string what's your name. 475 00:20:18,958 --> 00:20:20,750 CARTER ZENKE: Get string, what's your name. 476 00:20:20,750 --> 00:20:21,708 So something like this? 477 00:20:21,708 --> 00:20:23,320 STUDENT: With a space after the name. 478 00:20:23,320 --> 00:20:25,820 CARTER ZENKE: Yeah, and why would you have that space there? 479 00:20:25,820 --> 00:20:26,540 STUDENT: Just for readability. 480 00:20:26,540 --> 00:20:27,240 CARTER ZENKE: Just readability, right? 481 00:20:27,240 --> 00:20:29,900 If we don't have that space there, if we maybe remove this, 482 00:20:29,900 --> 00:20:31,970 the user will be typing in their name right up 483 00:20:31,970 --> 00:20:33,887 against that question mark there, which is not 484 00:20:33,887 --> 00:20:36,000 what we want to have happen here. 485 00:20:36,000 --> 00:20:39,080 So it seems pretty logical to represent a name with a string. 486 00:20:39,080 --> 00:20:40,873 But how about the age here? 487 00:20:40,873 --> 00:20:42,540 Can I go maybe to this side of the room? 488 00:20:42,540 --> 00:20:47,840 How did you all work on getting the users the contact's age? 489 00:20:47,840 --> 00:20:48,440 Yeah. 490 00:20:48,440 --> 00:20:57,780 STUDENT: I did int age equals get int, and then in brackets I had-- 491 00:20:57,780 --> 00:21:00,450 CARTER ZENKE: What's your age with a space, right? 492 00:21:00,450 --> 00:21:02,980 And then the semicolon. 493 00:21:02,980 --> 00:21:03,480 Perfect. 494 00:21:03,480 --> 00:21:05,200 So that'll close that statement. 495 00:21:05,200 --> 00:21:07,800 And again, this function on the right will be called. 496 00:21:07,800 --> 00:21:10,590 It will run and then give us back some return value, which is 497 00:21:10,590 --> 00:21:15,900 whatever the user typed in, and store it inside of this variable named age. 498 00:21:15,900 --> 00:21:17,460 And then comes one with some-- 499 00:21:17,460 --> 00:21:19,300 question here? 500 00:21:19,300 --> 00:21:27,280 STUDENT: Can we also write string age [INAUDIBLE]?? 501 00:21:27,280 --> 00:21:29,906 CARTER ZENKE: Yeah, so did you try that one here? 502 00:21:29,906 --> 00:21:34,283 STUDENT: Yeah, what's the [INAUDIBLE]? 503 00:21:34,283 --> 00:21:35,450 CARTER ZENKE: Good question. 504 00:21:35,450 --> 00:21:38,330 So we could do either of these here. 505 00:21:38,330 --> 00:21:39,520 One is an integer. 506 00:21:39,520 --> 00:21:41,290 One is a string. 507 00:21:41,290 --> 00:21:45,140 But you could imagine, if I later wanted to do something like this, 508 00:21:45,140 --> 00:21:49,780 let's say I wanted to actually increase every person's age in my phone by one, 509 00:21:49,780 --> 00:21:51,520 maybe it's their birthday next year. 510 00:21:51,520 --> 00:21:56,107 So I say age equals age plus 1. 511 00:21:56,107 --> 00:21:58,190 Is that going to work with a string, do you think? 512 00:21:58,190 --> 00:21:58,930 STUDENT: No. 513 00:21:58,930 --> 00:22:04,750 CARTER ZENKE: No, because I'm going to add 1 to this quote unquote maybe 40, 514 00:22:04,750 --> 00:22:05,650 for example. 515 00:22:05,650 --> 00:22:07,713 And I can't add numbers to strings. 516 00:22:07,713 --> 00:22:09,130 That's not going to happen for me. 517 00:22:09,130 --> 00:22:11,800 So it's best when you're making these to just think 518 00:22:11,800 --> 00:22:15,650 about what you want to do with them as you work in your program here. 519 00:22:15,650 --> 00:22:18,340 So I want to maybe change my age as I go through. 520 00:22:18,340 --> 00:22:24,550 So I'll actually maybe make this an integer here and say get int instead. 521 00:22:24,550 --> 00:22:25,830 And now for the phone number. 522 00:22:25,830 --> 00:22:28,560 If you go to this middle section here, how did you all 523 00:22:28,560 --> 00:22:32,090 choose to represent the phone number? 524 00:22:32,090 --> 00:22:33,500 Yeah. 525 00:22:33,500 --> 00:22:36,670 STUDENT: Long number is equal to get long. 526 00:22:36,670 --> 00:22:39,610 CARTER ZENKE: Long number is equal to get long. 527 00:22:39,610 --> 00:22:41,485 And you'd ask maybe what's your phone number. 528 00:22:41,485 --> 00:22:42,710 STUDENT: Yeah. 529 00:22:42,710 --> 00:22:44,668 CARTER ZENKE: And I think I'm most curious here 530 00:22:44,668 --> 00:22:46,840 about why you chose a long. 531 00:22:46,840 --> 00:22:49,550 Why would you have that? 532 00:22:49,550 --> 00:22:50,540 Yeah. 533 00:22:50,540 --> 00:22:53,279 STUDENT: Because it's more than 2 billion, 534 00:22:53,279 --> 00:22:56,703 so you need the extra [INAUDIBLE]. 535 00:22:56,703 --> 00:22:58,620 CARTER ZENKE: Yeah, so a phone number is what? 536 00:22:58,620 --> 00:23:01,060 Maybe 10 digits long. 537 00:23:01,060 --> 00:23:05,580 And so if I had any phone number that begins with anything higher than two, 538 00:23:05,580 --> 00:23:07,447 that's going to be higher than two billion, 539 00:23:07,447 --> 00:23:10,030 which is the highest positive number I can have as an integer. 540 00:23:10,030 --> 00:23:11,760 So I want to actually have a long here. 541 00:23:11,760 --> 00:23:12,390 Yeah, question. 542 00:23:12,390 --> 00:23:14,360 STUDENT: Could you use a string to-- 543 00:23:14,360 --> 00:23:16,360 CARTER ZENKE: You absolutely could use a string. 544 00:23:16,360 --> 00:23:20,983 And why would maybe you advocate for using a string? 545 00:23:20,983 --> 00:23:24,280 STUDENT: [INAUDIBLE] 546 00:23:24,280 --> 00:23:26,140 CARTER ZENKE: Nice, so hyphens. 547 00:23:26,140 --> 00:23:30,130 You could also include, if you're maybe in different countries, 548 00:23:30,130 --> 00:23:34,510 plus 1 for this country code plus other country codes as well. 549 00:23:34,510 --> 00:23:37,990 You could even have parentheses if you'd like to put the area code in front. 550 00:23:37,990 --> 00:23:39,070 Yeah. 551 00:23:39,070 --> 00:23:44,110 STUDENT: Also, I think that if you use an integer or a long, 552 00:23:44,110 --> 00:23:48,960 you are supposed to be able to calculate stuff based on that number. 553 00:23:48,960 --> 00:23:52,630 And usually, you don't add stuff or divide the phone number. 554 00:23:52,630 --> 00:23:58,008 So there is no actual purpose to ask for a [INAUDIBLE].. 555 00:23:58,008 --> 00:23:59,300 CARTER ZENKE: Yeah, definitely. 556 00:23:59,300 --> 00:24:01,758 So we tend not to add things to phone numbers. 557 00:24:01,758 --> 00:24:04,550 So why store it as number we could add things to or subtract things 558 00:24:04,550 --> 00:24:06,500 to or divide or whatever we'd like, right? 559 00:24:06,500 --> 00:24:08,990 Good question there. 560 00:24:08,990 --> 00:24:10,760 So we have it as a string here. 561 00:24:10,760 --> 00:24:14,120 The string also preserves things like leading zeros. 562 00:24:14,120 --> 00:24:17,388 Maybe there's an odd chance that somebody has a 000 number, 563 00:24:17,388 --> 00:24:20,180 and we could store that with a string but not, for example, a long. 564 00:24:20,180 --> 00:24:23,750 We tend to remove those leading zeros in that case. 565 00:24:23,750 --> 00:24:24,770 Yeah. 566 00:24:24,770 --> 00:24:27,690 STUDENT: But [INAUDIBLE] to use the long [INAUDIBLE] one 567 00:24:27,690 --> 00:24:29,520 that doesn't really require that? 568 00:24:29,520 --> 00:24:31,860 CARTER ZENKE: If I were to say maybe long age here? 569 00:24:31,860 --> 00:24:32,970 STUDENT: Yeah. 570 00:24:32,970 --> 00:24:36,150 CARTER ZENKE: And maybe get long to have parity. 571 00:24:36,150 --> 00:24:38,280 Nothing bad would happen here. 572 00:24:38,280 --> 00:24:41,940 I'm probably just using twice as much space as I would need to. 573 00:24:41,940 --> 00:24:45,490 So generally, I don't think people have an age above two billion. 574 00:24:45,490 --> 00:24:49,380 And so I would probably want to just use a regular integer to store that value. 575 00:24:49,380 --> 00:24:51,420 Nice. 576 00:24:51,420 --> 00:24:55,230 Other questions on these representations of this information here? 577 00:24:55,230 --> 00:24:58,700 578 00:24:58,700 --> 00:25:00,920 OK, so we'll go with this integer. 579 00:25:00,920 --> 00:25:03,392 And then just real quickly to review our format code, 580 00:25:03,392 --> 00:25:05,600 so let's say I wanted to print out some of this data. 581 00:25:05,600 --> 00:25:07,160 I could say printf. 582 00:25:07,160 --> 00:25:11,100 And let's say, maybe age is-- 583 00:25:11,100 --> 00:25:14,090 what would I then say? 584 00:25:14,090 --> 00:25:15,590 Maybe just call it out. 585 00:25:15,590 --> 00:25:16,425 STUDENT: %i. 586 00:25:16,425 --> 00:25:17,300 CARTER ZENKE: %i, OK. 587 00:25:17,300 --> 00:25:18,050 Age is %i. 588 00:25:18,050 --> 00:25:20,720 I'm going to put period here. 589 00:25:20,720 --> 00:25:24,830 Name is %s. 590 00:25:24,830 --> 00:25:32,430 And let's go ahead and say phone number is %s, all right? 591 00:25:32,430 --> 00:25:34,500 And now to store this, as we saw earlier, 592 00:25:34,500 --> 00:25:37,587 I could simply just put these variables in the same order 593 00:25:37,587 --> 00:25:39,670 I want them to show up in those placeholders here. 594 00:25:39,670 --> 00:25:41,450 So I could say, OK, age is first. 595 00:25:41,450 --> 00:25:44,170 So I'll say age, then name. 596 00:25:44,170 --> 00:25:45,000 So I'll say name. 597 00:25:45,000 --> 00:25:47,880 And then finally number, so I'll say number and close everything out 598 00:25:47,880 --> 00:25:49,590 with the semicolon here. 599 00:25:49,590 --> 00:25:53,580 And I were to make this, I just type make phone book and run it. 600 00:25:53,580 --> 00:25:57,240 I would then see this information printed back out to me on the screen 601 00:25:57,240 --> 00:25:58,620 as such. 602 00:25:58,620 --> 00:26:00,945 So questions on this before we move on. 603 00:26:00,945 --> 00:26:04,510 604 00:26:04,510 --> 00:26:10,590 All right, so one of the other building blocks 605 00:26:10,590 --> 00:26:12,990 that we have besides these variables and representations 606 00:26:12,990 --> 00:26:14,853 here are these conditionals and these loops. 607 00:26:14,853 --> 00:26:16,770 And we saw these in lecture as well, but we'll 608 00:26:16,770 --> 00:26:19,780 use these now for the course's lab to work on together. 609 00:26:19,780 --> 00:26:24,180 So first thing here, we have this question. 610 00:26:24,180 --> 00:26:27,600 Have I called less than one time? 611 00:26:27,600 --> 00:26:32,280 And if I have, then I'll say call more often. 612 00:26:32,280 --> 00:26:34,500 But just to get some vocabulary down here, 613 00:26:34,500 --> 00:26:38,880 this on the inside of that conditional is our Boolean expression. 614 00:26:38,880 --> 00:26:40,170 It's either yes or no. 615 00:26:40,170 --> 00:26:43,750 Have I called less than one time or not? 616 00:26:43,750 --> 00:26:46,100 On the outside here is that conditional itself. 617 00:26:46,100 --> 00:26:49,550 So conditionals have these Boolean expressions on the inside of them. 618 00:26:49,550 --> 00:26:51,550 I could make this a little more advanced though, 619 00:26:51,550 --> 00:26:55,970 and I could have this else in the middle. 620 00:26:55,970 --> 00:26:58,540 And what would that do for me? 621 00:26:58,540 --> 00:27:02,260 Could I ever execute these two things at the same time? 622 00:27:02,260 --> 00:27:03,930 Could I ask over here? 623 00:27:03,930 --> 00:27:06,025 Could I ever do these two things at the same time? 624 00:27:06,025 --> 00:27:06,900 Shaking your head no. 625 00:27:06,900 --> 00:27:07,230 Right. 626 00:27:07,230 --> 00:27:09,170 So these two things are mutually exclusive. 627 00:27:09,170 --> 00:27:12,780 If I have this else here, I'm saying it's either one or the other. 628 00:27:12,780 --> 00:27:15,450 If calls is less than 1, I will do that first thing. 629 00:27:15,450 --> 00:27:18,420 If it's not, I will do that second thing. 630 00:27:18,420 --> 00:27:21,660 You can chain these statements together if you saw-- even in Scratch. 631 00:27:21,660 --> 00:27:25,140 You could say if and then else if and then else if, 632 00:27:25,140 --> 00:27:26,400 and finally, it's long else. 633 00:27:26,400 --> 00:27:30,480 In general, we have these if statements followed by these else 634 00:27:30,480 --> 00:27:33,870 if statements, followed by if you need them, that final else to say, 635 00:27:33,870 --> 00:27:37,815 like a catch all for everything else that we could happen have happen here. 636 00:27:37,815 --> 00:27:40,440 For loops though, if you wanted to do something multiple times, 637 00:27:40,440 --> 00:27:43,300 we have something that looks a bit like this, this while loop. 638 00:27:43,300 --> 00:27:45,450 And what would this print to the screen? 639 00:27:45,450 --> 00:27:47,760 Could I ask somebody from maybe this side of the room? 640 00:27:47,760 --> 00:27:49,968 What would you see on the screen with this loop here? 641 00:27:49,968 --> 00:27:56,110 642 00:27:56,110 --> 00:27:56,740 Yeah, go ahead. 643 00:27:56,740 --> 00:27:59,660 STUDENT: 0, 1, 2, 3 up to 9. 644 00:27:59,660 --> 00:28:01,410 CARTER ZENKE: Up to 9, and why not the 10? 645 00:28:01,410 --> 00:28:03,598 STUDENT: Because it's while i is less than 10. 646 00:28:03,598 --> 00:28:04,390 CARTER ZENKE: Nice. 647 00:28:04,390 --> 00:28:07,660 Yeah, so we wouldn't see the 10 because we're going to stop while-- 648 00:28:07,660 --> 00:28:11,840 we're going to stop if this is not true. 649 00:28:11,840 --> 00:28:14,650 And so 10 is not less than 10, so we would stop 650 00:28:14,650 --> 00:28:17,560 and we wouldn't do the code on the inside, right? 651 00:28:17,560 --> 00:28:21,500 So to break things down even further, we have our initialization of this loop. 652 00:28:21,500 --> 00:28:23,560 I is first set to 0. 653 00:28:23,560 --> 00:28:25,303 Then we have our Boolean expression, this 654 00:28:25,303 --> 00:28:27,220 question we're going to ask, either yes or no. 655 00:28:27,220 --> 00:28:29,000 Is this case true or is it not? 656 00:28:29,000 --> 00:28:33,460 And then finally, this implementation, changing that value as we go through. 657 00:28:33,460 --> 00:28:36,010 And finally, when our Boolean expression is no longer true, 658 00:28:36,010 --> 00:28:38,865 we will not be inside of that loop there. 659 00:28:38,865 --> 00:28:39,740 This is really handy. 660 00:28:39,740 --> 00:28:43,150 We can actually try to make our code run a certain number of times. 661 00:28:43,150 --> 00:28:46,270 And this is so handy that C actually has our own syntax 662 00:28:46,270 --> 00:28:48,950 to write this kind of loop in even abbreviated format. 663 00:28:48,950 --> 00:28:53,770 So here we call a for loop that has all those same elements but now just 664 00:28:53,770 --> 00:28:54,910 on one clean line. 665 00:28:54,910 --> 00:28:58,670 We have this initialization, this Boolean expression, 666 00:28:58,670 --> 00:29:00,910 and then this incrementation here. 667 00:29:00,910 --> 00:29:03,160 And we'll do whatever a piece of code is on the inside 668 00:29:03,160 --> 00:29:06,640 as long as this Boolean expression is true. 669 00:29:06,640 --> 00:29:11,070 So questions on this kind of syntax for these loops? 670 00:29:11,070 --> 00:29:12,775 Pretty good. 671 00:29:12,775 --> 00:29:15,150 And finally, I just wanted to get a little more advanced. 672 00:29:15,150 --> 00:29:16,440 We have this-- yeah, go ahead. 673 00:29:16,440 --> 00:29:19,290 STUDENT: I have a question [INAUDIBLE]. 674 00:29:19,290 --> 00:29:25,193 Why int [INAUDIBLE] parentheses [INAUDIBLE]?? 675 00:29:25,193 --> 00:29:26,360 CARTER ZENKE: Good question. 676 00:29:26,360 --> 00:29:27,360 So you saw the int here? 677 00:29:27,360 --> 00:29:28,180 STUDENT: Yeah. 678 00:29:28,180 --> 00:29:29,972 CARTER ZENKE: And why is that there, right, 679 00:29:29,972 --> 00:29:34,540 because previously, we had the int i already done for us, right? 680 00:29:34,540 --> 00:29:37,280 So these are actually two different contexts. 681 00:29:37,280 --> 00:29:41,230 So here, I have created this variable named i, 682 00:29:41,230 --> 00:29:43,570 and I'm going to use it inside of this loop. 683 00:29:43,570 --> 00:29:46,900 And this variable i, because it's outside of this while loop, 684 00:29:46,900 --> 00:29:48,940 will persist as we go through my program. 685 00:29:48,940 --> 00:29:51,430 So I could use this variable i again. 686 00:29:51,430 --> 00:29:56,200 In the for loop though, if I were to create this variable i anew, 687 00:29:56,200 --> 00:29:58,900 I can only use it inside this for loop. 688 00:29:58,900 --> 00:30:01,340 I could not use it outside of that for loop. 689 00:30:01,340 --> 00:30:04,030 So as long as it's inside for loop, I can't use whatever 690 00:30:04,030 --> 00:30:05,860 variable I create here outside of it. 691 00:30:05,860 --> 00:30:09,040 It has to be limited to the inside of that for loop there. 692 00:30:09,040 --> 00:30:10,420 Does that help? 693 00:30:10,420 --> 00:30:11,433 Great. 694 00:30:11,433 --> 00:30:12,475 Other questions here too. 695 00:30:12,475 --> 00:30:15,810 696 00:30:15,810 --> 00:30:18,713 STUDENT: I see no semicolon at the end of the for loop. 697 00:30:18,713 --> 00:30:20,630 CARTER ZENKE: Yeah, why is there no semicolon? 698 00:30:20,630 --> 00:30:24,680 Well, probably a rule of thumb is this. 699 00:30:24,680 --> 00:30:27,770 Semicolons come after complete statements. 700 00:30:27,770 --> 00:30:31,970 And this for loop is kind of setting up some set of statements. 701 00:30:31,970 --> 00:30:35,550 It's saying, I'm going to eventually have something I want to do, 702 00:30:35,550 --> 00:30:36,750 but it's not there yet. 703 00:30:36,750 --> 00:30:38,960 So I'm going to actually have these braces 704 00:30:38,960 --> 00:30:43,140 to tell what statements I want to execute sequentially as I go through. 705 00:30:43,140 --> 00:30:46,670 So you'll get used to this as you go through and write more code. 706 00:30:46,670 --> 00:30:49,250 But in general, statements end with a semicolon, 707 00:30:49,250 --> 00:30:51,530 but things that set up statements generally don't. 708 00:30:51,530 --> 00:30:55,120 They might have these braces, yeah. 709 00:30:55,120 --> 00:30:56,203 Yeah. 710 00:30:56,203 --> 00:30:59,584 STUDENT: So you mentioned [INAUDIBLE] variable 711 00:30:59,584 --> 00:31:02,010 i only exists within the parameter. 712 00:31:02,010 --> 00:31:03,270 CARTER ZENKE: Exactly. 713 00:31:03,270 --> 00:31:07,730 STUDENT: Does that mean when you do multiple for loops in a row 714 00:31:07,730 --> 00:31:09,592 not with [INAUDIBLE]? 715 00:31:09,592 --> 00:31:11,300 CARTER ZENKE: Yeah, it's a good question. 716 00:31:11,300 --> 00:31:17,570 So let's say if I go back to my actual programming environment here, 717 00:31:17,570 --> 00:31:20,270 let's say I have a loop. 718 00:31:20,270 --> 00:31:25,040 And I'm going to say four int i equals 0. i is less than 10. 719 00:31:25,040 --> 00:31:26,930 i++. 720 00:31:26,930 --> 00:31:31,370 So now, I can access i inside of these two braces. 721 00:31:31,370 --> 00:31:32,750 That's something called scope. 722 00:31:32,750 --> 00:31:33,958 It's a scope of the variable. 723 00:31:33,958 --> 00:31:35,900 It persists in this space here. 724 00:31:35,900 --> 00:31:38,310 I can't use i out here. 725 00:31:38,310 --> 00:31:42,577 So I can't access it there, but I could, for example, have another for loop 726 00:31:42,577 --> 00:31:43,160 on the inside. 727 00:31:43,160 --> 00:31:47,030 I could say for int j equals i. 728 00:31:47,030 --> 00:31:49,670 That's totally valid because i's inside this loop. 729 00:31:49,670 --> 00:31:53,420 Maybe j is less than 10 in this case, and then j++. 730 00:31:53,420 --> 00:31:55,760 And I do something else in that. 731 00:31:55,760 --> 00:31:59,720 And actually, j is not accessible here. 732 00:31:59,720 --> 00:32:03,560 I can't do that, but it is accessible inside that loop there. 733 00:32:03,560 --> 00:32:07,878 734 00:32:07,878 --> 00:32:08,920 Other questions here too? 735 00:32:08,920 --> 00:32:10,108 Yeah. 736 00:32:10,108 --> 00:32:11,400 STUDENT: I'm a little confused. 737 00:32:11,400 --> 00:32:15,910 Where's the part that, I guess, senses what i is? 738 00:32:15,910 --> 00:32:19,600 It just appears [INAUDIBLE]. 739 00:32:19,600 --> 00:32:23,380 CARTER ZENKE: Yeah, can you tell me what you mean by senses what i is? 740 00:32:23,380 --> 00:32:29,715 STUDENT: Right, so because it's like a Boolean expression, 741 00:32:29,715 --> 00:32:36,540 right, whether i is greater than 1 or less than 1, like that kind of thing. 742 00:32:36,540 --> 00:32:41,260 So where's the input of whether it's going to be 1 or i what value it is? 743 00:32:41,260 --> 00:32:44,030 CARTER ZENKE: Yeah, so probably best to think of it like this, 744 00:32:44,030 --> 00:32:46,630 where my code is read top to bottom. 745 00:32:46,630 --> 00:32:50,870 And when it gets to this for loop, it's read kind of left to right. 746 00:32:50,870 --> 00:32:55,330 So the first time I get to this for loop, I'm going to make this variable i 747 00:32:55,330 --> 00:32:57,190 and set it to 0. 748 00:32:57,190 --> 00:33:01,480 Then I'm going to ask the question, is i less than 10? 749 00:33:01,480 --> 00:33:04,930 OK, it is, so I'll actually go inside this for loop 750 00:33:04,930 --> 00:33:08,740 and maybe run this code on the inside. 751 00:33:08,740 --> 00:33:15,880 Once I get to the bottom of this for loop here, I'll then go to the top. 752 00:33:15,880 --> 00:33:16,660 And I'll do this. 753 00:33:16,660 --> 00:33:18,987 I'll increase i by 1. 754 00:33:18,987 --> 00:33:20,320 And I'll ask the question again. 755 00:33:20,320 --> 00:33:21,170 Is i less than 10? 756 00:33:21,170 --> 00:33:21,670 It is. 757 00:33:21,670 --> 00:33:23,230 OK, I'll do this code again. 758 00:33:23,230 --> 00:33:26,635 Once it finishes, I'll increase i by 1 again, ask the question, keep going, 759 00:33:26,635 --> 00:33:27,260 and keep going. 760 00:33:27,260 --> 00:33:29,167 Does that help? 761 00:33:29,167 --> 00:33:30,000 STUDENT: OK, got it. 762 00:33:30,000 --> 00:33:34,240 So the input was i equals 0 first, and then [INAUDIBLE].. 763 00:33:34,240 --> 00:33:36,790 CARTER ZENKE: Exactly, this is our initialization. 764 00:33:36,790 --> 00:33:40,870 This is the first time for loop runs, set it to this value. 765 00:33:40,870 --> 00:33:44,860 Every time this code on the inside finishes running, ask-- 766 00:33:44,860 --> 00:33:49,150 or actually, before we start running this code, ask this question. 767 00:33:49,150 --> 00:33:51,810 If this is true, run this code. 768 00:33:51,810 --> 00:33:55,380 And we get to the bottom, increase i by 1 or do whatever is here, yeah. 769 00:33:55,380 --> 00:33:56,880 STUDENT: This should do it 10 times? 770 00:33:56,880 --> 00:33:58,880 CARTER ZENKE: This should do it 10 times, right? 771 00:33:58,880 --> 00:34:03,420 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. 772 00:34:03,420 --> 00:34:07,366 It won't do 10 because that is not less than 10. 773 00:34:07,366 --> 00:34:09,570 STUDENT: Is this called a do while? 774 00:34:09,570 --> 00:34:13,050 CARTER ZENKE: It's called a for loop just because of the 4 on the-- right 775 00:34:13,050 --> 00:34:13,792 there. 776 00:34:13,792 --> 00:34:17,100 Yeah, but we do have the do while loop, if we could go just quickly to that one 777 00:34:17,100 --> 00:34:18,699 here. 778 00:34:18,699 --> 00:34:22,560 The do while loop has the words do while in it, right? 779 00:34:22,560 --> 00:34:27,360 And what this is going to do is actually not ask a question first. 780 00:34:27,360 --> 00:34:32,010 Just unconditionally, run this piece of code and then ask the question later. 781 00:34:32,010 --> 00:34:34,860 And this is handy for trying to get some input from the user 782 00:34:34,860 --> 00:34:37,739 because it'll always ask the user that question. 783 00:34:37,739 --> 00:34:39,270 What input do you want to give me? 784 00:34:39,270 --> 00:34:43,230 And then maybe reprompt them, depending on whether that input is invalid or not 785 00:34:43,230 --> 00:34:44,790 what you want it to be, all right? 786 00:34:44,790 --> 00:34:47,790 So down here we would say this is the condition under which our input is 787 00:34:47,790 --> 00:34:48,540 invalid. 788 00:34:48,540 --> 00:34:52,620 And we should go back up and do it all over again. 789 00:34:52,620 --> 00:34:55,520 So questions on these kinds of loops still. 790 00:34:55,520 --> 00:34:59,870 791 00:34:59,870 --> 00:35:00,870 Yeah. 792 00:35:00,870 --> 00:35:04,890 STUDENT: Would this be good, the do-while loop if you were to-- 793 00:35:04,890 --> 00:35:07,595 you were asking a question within the while 794 00:35:07,595 --> 00:35:10,383 loop that didn't say you're doing [INAUDIBLE] 795 00:35:10,383 --> 00:35:15,330 same with [INAUDIBLE] while loop give a given number? 796 00:35:15,330 --> 00:35:18,840 Would there be an easier way to terminate the function if say 797 00:35:18,840 --> 00:35:21,133 you don't [INAUDIBLE] the right age? 798 00:35:21,133 --> 00:35:23,175 CARTER ZENKE: And by terminate do you mean like-- 799 00:35:23,175 --> 00:35:24,840 STUDENT: Terminate the funct-- 800 00:35:24,840 --> 00:35:26,100 like it just stops. 801 00:35:26,100 --> 00:35:30,580 If you use-- say you're looking for the age range from 25 to 30, 802 00:35:30,580 --> 00:35:35,360 and then if it's in there, you go to the while loop. 803 00:35:35,360 --> 00:35:37,640 Would you then have the while loop-- would there 804 00:35:37,640 --> 00:35:42,210 be a way to stop the while loop from [INAUDIBLE]?? 805 00:35:42,210 --> 00:35:43,960 CARTER ZENKE: And so, if I'm understanding 806 00:35:43,960 --> 00:35:47,140 what you're asking correctly, it's often better 807 00:35:47,140 --> 00:35:51,280 to check for what we don't want to have happen than to check for what 808 00:35:51,280 --> 00:35:52,880 it is we do want to have happen. 809 00:35:52,880 --> 00:35:55,370 So for example, I might not want to do this. 810 00:35:55,370 --> 00:36:00,160 I might not want to say int age is get int age, here. 811 00:36:00,160 --> 00:36:06,940 And then ask the question, if, let's say, age is between-- what did you say, 812 00:36:06,940 --> 00:36:08,320 like 15 and 20? 813 00:36:08,320 --> 00:36:09,100 STUDENT: Yeah. 814 00:36:09,100 --> 00:36:15,970 CARTER ZENKE: If age is between 15 and 20, then do the rest of my program 815 00:36:15,970 --> 00:36:19,420 because what I'm doing here is I'm separating my program from-- 816 00:36:19,420 --> 00:36:22,000 I'm indenting it by a lot, and maybe I have more conditions. 817 00:36:22,000 --> 00:36:25,360 Maybe I also want to say, if the name starts with A, 818 00:36:25,360 --> 00:36:28,270 and then it keeps getting more and more indented. 819 00:36:28,270 --> 00:36:31,040 So what I could do instead is I could do something like this. 820 00:36:31,040 --> 00:36:34,730 I could say, well, let me do this. 821 00:36:34,730 --> 00:36:37,210 Let me create this variable named age. 822 00:36:37,210 --> 00:36:40,690 Let me get some value for it. 823 00:36:40,690 --> 00:36:41,360 Whoops. 824 00:36:41,360 --> 00:36:43,160 Let me get some value for it. 825 00:36:43,160 --> 00:36:47,740 And while the age is not what I want, let me keep reprompting. 826 00:36:47,740 --> 00:36:49,550 Is that answering your question? 827 00:36:49,550 --> 00:36:50,800 STUDENT: Yeah, i got it, yeah. 828 00:36:50,800 --> 00:36:57,000 CARTER ZENKE: OK, so age is like less than 15, or age is greater than 20. 829 00:36:57,000 --> 00:37:00,830 Let me reprompt, right? 830 00:37:00,830 --> 00:37:01,330 OK. 831 00:37:01,330 --> 00:37:02,366 Yeah. 832 00:37:02,366 --> 00:37:11,560 STUDENT: Is that [INAUDIBLE] to write age less, equal, between? 833 00:37:11,560 --> 00:37:17,200 Or is it better time to say age less [INAUDIBLE]?? 834 00:37:17,200 --> 00:37:19,430 CARTER ZENKE: Probably depends on the context. 835 00:37:19,430 --> 00:37:23,530 So the question was, is it better style to use this less than or this less than 836 00:37:23,530 --> 00:37:25,090 or equal to? 837 00:37:25,090 --> 00:37:28,930 In general, when you're writing for loops, where you just want something 838 00:37:28,930 --> 00:37:31,240 to repeat a certain number of times, we tend 839 00:37:31,240 --> 00:37:34,330 to use the less than and always start at 0. 840 00:37:34,330 --> 00:37:36,490 So let's say I want something to repeat five times. 841 00:37:36,490 --> 00:37:41,530 I'll say int i equals 0, i is less than 5, i++. 842 00:37:41,530 --> 00:37:44,290 And the math on this just works, right? 843 00:37:44,290 --> 00:37:47,950 0, 1, 2, 3, 4, that's five times. 844 00:37:47,950 --> 00:37:52,210 If I were to do something like this, let's say int i is 1, 845 00:37:52,210 --> 00:37:54,880 then I'd have to do less than or equal to 5. 846 00:37:54,880 --> 00:37:58,960 And because programmers start counting at 0, meaning this absence of anything. 847 00:37:58,960 --> 00:38:00,610 It's starting from the very basics. 848 00:38:00,610 --> 00:38:03,527 We're not going to actually have this less than or equal to sign here. 849 00:38:03,527 --> 00:38:04,660 Is that helpful? 850 00:38:04,660 --> 00:38:06,330 Yeah. 851 00:38:06,330 --> 00:38:06,830 OK. 852 00:38:06,830 --> 00:38:10,860 853 00:38:10,860 --> 00:38:15,340 All right, so with these building blocks of these loops, 854 00:38:15,340 --> 00:38:19,240 we'll spend some time working on our lab for the next 10 minutes or so. 855 00:38:19,240 --> 00:38:23,620 In our lab-- if I go back to my slides here-- 856 00:38:23,620 --> 00:38:26,680 we're going to be asking this question of how many llamas 857 00:38:26,680 --> 00:38:28,030 do we currently have? 858 00:38:28,030 --> 00:38:31,600 And how many years will take us to get to an aspirational number of llamas, 859 00:38:31,600 --> 00:38:34,160 some gold number of llamas for ourselves here? 860 00:38:34,160 --> 00:38:37,675 So if we look at this, you can find the lab at this URL right here. 861 00:38:37,675 --> 00:38:38,800 It's on the course website. 862 00:38:38,800 --> 00:38:40,160 Go to the lab page. 863 00:38:40,160 --> 00:38:43,090 And in that lab, you'll find these questions. 864 00:38:43,090 --> 00:38:44,080 Actually, sorry. 865 00:38:44,080 --> 00:38:48,040 Within this lab page, you're going to work on this problem all together. 866 00:38:48,040 --> 00:38:51,650 But we'll get started working on this up front here. 867 00:38:51,650 --> 00:38:53,530 So the first thing that we'll do for this lab 868 00:38:53,530 --> 00:38:56,710 is think about how we're going to work an example ourselves, 869 00:38:56,710 --> 00:38:59,740 and then walk through and write our code. 870 00:38:59,740 --> 00:39:03,460 When you're working on your problems, often good to think 871 00:39:03,460 --> 00:39:05,950 about how you're going to write your algorithm first, 872 00:39:05,950 --> 00:39:07,840 and then write your code. 873 00:39:07,840 --> 00:39:11,830 So these seven steps can help you, whether you're working on the lab, 874 00:39:11,830 --> 00:39:13,640 in the problem set, or so on. 875 00:39:13,640 --> 00:39:16,810 And notice how more than half of these are 876 00:39:16,810 --> 00:39:20,500 about writing things down, thinking through them, not necessarily 877 00:39:20,500 --> 00:39:22,120 programming. 878 00:39:22,120 --> 00:39:26,630 But these final three steps are all about translating that idea into code. 879 00:39:26,630 --> 00:39:28,450 So we'll do that here today. 880 00:39:28,450 --> 00:39:31,112 If we look at our llamas, we have a certain number of llamas. 881 00:39:31,112 --> 00:39:33,070 We want to figure out how many years it'll take 882 00:39:33,070 --> 00:39:35,140 to get to another number of llamas. 883 00:39:35,140 --> 00:39:39,490 And we know that every year, maybe a third of our llamas are born, 884 00:39:39,490 --> 00:39:43,300 and 1/4 of our llamas sadly pass away, right? 885 00:39:43,300 --> 00:39:46,640 And let's take a look at this particular example here. 886 00:39:46,640 --> 00:39:48,970 We have 12 llamas. 887 00:39:48,970 --> 00:39:53,770 And so in this current year, 12 over 3 new llamas will be born, 888 00:39:53,770 --> 00:39:56,480 and 12 over 4 llamas will pass away. 889 00:39:56,480 --> 00:40:00,550 And the question is, how many years will it take to get to 13 llamas? 890 00:40:00,550 --> 00:40:03,940 So to work this example, we have these 12 llamas. 891 00:40:03,940 --> 00:40:05,380 And now, what would we do? 892 00:40:05,380 --> 00:40:08,290 We want to birth a 1/3 llamas. 893 00:40:08,290 --> 00:40:09,760 So how many more will we add here? 894 00:40:09,760 --> 00:40:10,605 STUDENT: Four. 895 00:40:10,605 --> 00:40:12,105 CARTER ZENKE: Four, OK, let's add 4. 896 00:40:12,105 --> 00:40:14,740 897 00:40:14,740 --> 00:40:16,330 And how many would pass away? 898 00:40:16,330 --> 00:40:17,230 STUDENT: Three. 899 00:40:17,230 --> 00:40:20,080 CARTER ZENKE: Three, so we'll take away 3 here. 900 00:40:20,080 --> 00:40:22,900 And we are at 13 llamas now, right? 901 00:40:22,900 --> 00:40:24,760 And that was one year overall. 902 00:40:24,760 --> 00:40:27,730 We birthed our four more llamas, and three llamas passed away. 903 00:40:27,730 --> 00:40:30,230 Now, we're at 13. 904 00:40:30,230 --> 00:40:33,850 So as you go and write this program, we'll get started up here in a minute. 905 00:40:33,850 --> 00:40:37,150 We want to first prompt the user for this starting number of llamas, 906 00:40:37,150 --> 00:40:40,930 ask them for a goal number of llamas, and then do some math perhaps 907 00:40:40,930 --> 00:40:43,330 in a loop to figure out how many years it 908 00:40:43,330 --> 00:40:46,030 will take to get to that goal number of llamas, 909 00:40:46,030 --> 00:40:48,950 and finally, print out that number of years. 910 00:40:48,950 --> 00:40:55,390 So if you are in your code space, you might go over to your environment here. 911 00:40:55,390 --> 00:41:01,450 You might type code population.C to open up that file. 912 00:41:01,450 --> 00:41:03,100 Here I have mine. 913 00:41:03,100 --> 00:41:07,060 You might then type, include standardio.h to print something 914 00:41:07,060 --> 00:41:08,470 to the screen later on. 915 00:41:08,470 --> 00:41:13,730 And you might also do the cs50 library, cs50.h here as well. 916 00:41:13,730 --> 00:41:16,890 What else do we need for this boilerplate code? 917 00:41:16,890 --> 00:41:17,662 STUDENT: Int main. 918 00:41:17,662 --> 00:41:19,120 CARTER ZENKE: Int main void, right? 919 00:41:19,120 --> 00:41:23,800 So I'll say int main void. 920 00:41:23,800 --> 00:41:26,830 And I'll leave this rest up to you, but make sure you're asking-- 921 00:41:26,830 --> 00:41:32,110 your prompting the user for a starting number of llamas, 922 00:41:32,110 --> 00:41:37,090 prompting them for an ending number of llamas, and then 923 00:41:37,090 --> 00:41:41,170 finally, maybe using some kind of loop to figure out how many years it 924 00:41:41,170 --> 00:41:43,795 will take to get to the goal. 925 00:41:43,795 --> 00:41:47,430 926 00:41:47,430 --> 00:41:56,220 And keep in mind that every year a 1/3 of our current llamas are born 927 00:41:56,220 --> 00:42:00,080 and 1/4 pass away. 928 00:42:00,080 --> 00:42:05,900 All right, so work on this for, let's say, 10 minutes or so. 929 00:42:05,900 --> 00:42:08,960 And we'll come back and work the example together. 930 00:42:08,960 --> 00:42:12,410 OK, so I hope you've made some good progress on this lab. 931 00:42:12,410 --> 00:42:15,440 Let's come back and work the example together. 932 00:42:15,440 --> 00:42:18,980 We had a few steps to work through, the first one being prompting the user 933 00:42:18,980 --> 00:42:21,740 for a starting number of llamas. 934 00:42:21,740 --> 00:42:26,000 And could I ask maybe a group from this side of the room, maybe 935 00:42:26,000 --> 00:42:30,350 towards the back, if you don't mind, how did you prompt the user 936 00:42:30,350 --> 00:42:33,890 for your starting number of llamas? 937 00:42:33,890 --> 00:42:34,820 Yeah, go ahead. 938 00:42:34,820 --> 00:42:40,220 STUDENT: So the first [INAUDIBLE] start. 939 00:42:40,220 --> 00:42:43,290 And we ran a do-while loop. 940 00:42:43,290 --> 00:42:48,000 So it says starts equals get int and then start size. 941 00:42:48,000 --> 00:42:51,360 And then the condition was while the start was less than 9. 942 00:42:51,360 --> 00:42:54,780 CARTER ZENKE: Gotcha, so while start is less than 9. 943 00:42:54,780 --> 00:42:57,810 And why did you choose less than 9 for that case? 944 00:42:57,810 --> 00:43:00,960 STUDENT: Because the lab said that the minimum number that the start 945 00:43:00,960 --> 00:43:04,770 size could be was 9 or else the population would fail to grow. 946 00:43:04,770 --> 00:43:07,710 CARTER ZENKE: Right, so we want to reprompt the user if they give us 947 00:43:07,710 --> 00:43:10,262 a number less than 9, right? 948 00:43:10,262 --> 00:43:11,220 That seems pretty good. 949 00:43:11,220 --> 00:43:14,970 And then similarly, how could we prompt them for an ending number of llamas? 950 00:43:14,970 --> 00:43:17,580 Could I ask maybe in the middle this time, 951 00:43:17,580 --> 00:43:19,920 somebody from maybe the back few rows, how 952 00:43:19,920 --> 00:43:22,935 did you try to prompt for an ending number of llamas here? 953 00:43:22,935 --> 00:43:26,900 954 00:43:26,900 --> 00:43:28,640 Yeah, go ahead. 955 00:43:28,640 --> 00:43:31,768 STUDENT: I created an int variable called n. 956 00:43:31,768 --> 00:43:32,560 CARTER ZENKE: Nice. 957 00:43:32,560 --> 00:43:35,552 STUDENT: And then I used a do-while loop. 958 00:43:35,552 --> 00:43:42,015 [INAUDIBLE] had n equals get int, and then end size. 959 00:43:42,015 --> 00:43:48,647 And then the condition for the while was while end was less than start. 960 00:43:48,647 --> 00:43:50,480 CARTER ZENKE: Yeah, and then just to ask you 961 00:43:50,480 --> 00:43:53,380 why would you have that in your conditional down there? 962 00:43:53,380 --> 00:43:57,510 STUDENT: Because in the lab it wanted us to keep 963 00:43:57,510 --> 00:44:02,930 prompting if the ending size that the user inputs is less than starting 964 00:44:02,930 --> 00:44:03,523 point. 965 00:44:03,523 --> 00:44:04,690 CARTER ZENKE: Yeah, totally. 966 00:44:04,690 --> 00:44:07,923 So we can't have fewer llamas than we began with. 967 00:44:07,923 --> 00:44:10,090 So just checking to make sure that is the case here. 968 00:44:10,090 --> 00:44:13,260 And if not, we reprompt the user. 969 00:44:13,260 --> 00:44:17,130 And then our final bit down here was keeping track of how many years 970 00:44:17,130 --> 00:44:20,310 it will take to get to our goal number of llamas now stored 971 00:44:20,310 --> 00:44:22,440 in this variable called end. 972 00:44:22,440 --> 00:44:25,380 And how did you all choose to represent the year, 973 00:44:25,380 --> 00:44:30,090 or how many years have passed in your program? 974 00:44:30,090 --> 00:44:33,230 Could I ask somebody from this side of the room now? 975 00:44:33,230 --> 00:44:36,290 What kind of variable did you use to count the number of years here? 976 00:44:36,290 --> 00:44:39,250 977 00:44:39,250 --> 00:44:42,190 Maybe somebody from the first few rows. 978 00:44:42,190 --> 00:44:42,910 Yeah, go ahead. 979 00:44:42,910 --> 00:44:43,868 STUDENT: As an integer. 980 00:44:43,868 --> 00:44:47,035 CARTER ZENKE: As an integer, and did you give it a special name or anything? 981 00:44:47,035 --> 00:44:48,370 STUDENT: i called it int years. 982 00:44:48,370 --> 00:44:49,570 CARTER ZENKE: Int years, OK. 983 00:44:49,570 --> 00:44:51,880 And what did you set it first to? 984 00:44:51,880 --> 00:44:52,720 STUDENT: Zero. 985 00:44:52,720 --> 00:44:54,700 CARTER ZENKE: Zero, OK. 986 00:44:54,700 --> 00:44:57,550 And now we have some math to do. 987 00:44:57,550 --> 00:44:59,983 If we look maybe-- 988 00:44:59,983 --> 00:45:01,150 this group [INAUDIBLE] mind. 989 00:45:01,150 --> 00:45:03,730 How did you all add llamas, subtract them? 990 00:45:03,730 --> 00:45:07,060 How did you adjust your population of llamas as the years went on? 991 00:45:07,060 --> 00:45:12,910 992 00:45:12,910 --> 00:45:17,695 Any ideas for how you might do it or how you did do it? 993 00:45:17,695 --> 00:45:18,320 Yeah, go ahead. 994 00:45:18,320 --> 00:45:20,392 STUDENT: Just add 112 llama each year. 995 00:45:20,392 --> 00:45:22,100 CARTER ZENKE: Added 112 llamas each year. 996 00:45:22,100 --> 00:45:22,600 Nice. 997 00:45:22,600 --> 00:45:25,322 So can you tell me, if we look at our program here, 998 00:45:25,322 --> 00:45:28,280 we have a starting number of llamas, so how would you adjust that here? 999 00:45:28,280 --> 00:45:31,320 1000 00:45:31,320 --> 00:45:33,772 STUDENT: You would go, like you said, at start-- 1001 00:45:33,772 --> 00:45:34,605 CARTER ZENKE: Start. 1002 00:45:34,605 --> 00:45:37,450 STUDENT: It would be start equals-- 1003 00:45:37,450 --> 00:45:38,830 I'm sorry, plus equals. 1004 00:45:38,830 --> 00:45:40,163 CARTER ZENKE: Start plus equals. 1005 00:45:40,163 --> 00:45:41,410 STUDENT: Start divided by 12. 1006 00:45:41,410 --> 00:45:43,930 CARTER ZENKE: Start divided by 12. 1007 00:45:43,930 --> 00:45:46,150 And how did you get start divided by 12? 1008 00:45:46,150 --> 00:45:48,820 What kind of math did you do to get there? 1009 00:45:48,820 --> 00:45:51,105 STUDENT: Common denominator of 1/3 and 1/4. 1010 00:45:51,105 --> 00:45:53,530 And then you're adding 1/3 and subtracting 1/4. 1011 00:45:53,530 --> 00:45:55,655 CARTER ZENKE: Nice, so ultimately, every year we're 1012 00:45:55,655 --> 00:45:59,680 just adding 1/12 new llamas after we add a 1/3 and subtract a 1/4. 1013 00:45:59,680 --> 00:46:03,430 It comes out to 1/12 new llamas every year. 1014 00:46:03,430 --> 00:46:08,050 But we need some kind of loop to run this. 1015 00:46:08,050 --> 00:46:10,300 Anyone want to help me out here with this final piece? 1016 00:46:10,300 --> 00:46:13,520 How do we have the loop to increase this-- 1017 00:46:13,520 --> 00:46:16,460 llama, know when to stop too. 1018 00:46:16,460 --> 00:46:17,030 Yeah. 1019 00:46:17,030 --> 00:46:21,729 STUDENT: We could do while start is less end [INAUDIBLE] start. 1020 00:46:21,729 --> 00:46:26,457 And after that [INAUDIBLE] add [INAUDIBLE].. 1021 00:46:26,457 --> 00:46:28,165 CARTER ZENKE: Yeah, and so add 1 to year? 1022 00:46:28,165 --> 00:46:28,870 STUDENT: Yeah. 1023 00:46:28,870 --> 00:46:33,730 CARTER ZENKE: Yeah, so we could say years++ for increase years by 1. 1024 00:46:33,730 --> 00:46:37,940 And at the very end we ultimately want to print out the number of years. 1025 00:46:37,940 --> 00:46:40,840 So I'll say printf %i. 1026 00:46:40,840 --> 00:46:44,410 And maybe just to clarify, years percent and then years 1027 00:46:44,410 --> 00:46:49,100 down at the bottom here, with a backslash n at the end. 1028 00:46:49,100 --> 00:46:56,410 And if I make this make population, run ./population, 1029 00:46:56,410 --> 00:46:59,310 I'll type in maybe 12 and 13. 1030 00:46:59,310 --> 00:47:02,460 And I get one year, as we expected. 1031 00:47:02,460 --> 00:47:03,645 Yeah, question or comment. 1032 00:47:03,645 --> 00:47:07,503 STUDENT: What is the plus equal [INAUDIBLE] start plus equal start? 1033 00:47:07,503 --> 00:47:08,670 CARTER ZENKE: Good question. 1034 00:47:08,670 --> 00:47:11,260 This is basically shorthand for doing this. 1035 00:47:11,260 --> 00:47:17,430 I could say start equals start + start over 12 because I'm trying 1036 00:47:17,430 --> 00:47:19,750 to add to start as we go through. 1037 00:47:19,750 --> 00:47:22,620 So this plus equals is simply saying, let 1038 00:47:22,620 --> 00:47:27,298 me add to what start currently is this value on the right-hand side. 1039 00:47:27,298 --> 00:47:28,215 Does that makes sense? 1040 00:47:28,215 --> 00:47:28,870 STUDENT: Yes, thanks. 1041 00:47:28,870 --> 00:47:29,745 CARTER ZENKE: Gotcha. 1042 00:47:29,745 --> 00:47:34,460 1043 00:47:34,460 --> 00:47:39,120 Now, there's one question here, which is, let's say, I have-- oh, yeah, 1044 00:47:39,120 --> 00:47:40,370 go ahead, somewhere over here. 1045 00:47:40,370 --> 00:47:41,560 Yeah. 1046 00:47:41,560 --> 00:47:44,900 STUDENT: I have a question regarding this start in line 12. 1047 00:47:44,900 --> 00:47:49,370 We have one part that is the [INAUDIBLE] form 1048 00:47:49,370 --> 00:47:53,130 and one part that is [INAUDIBLE] our time. 1049 00:47:53,130 --> 00:47:58,790 Therefore, you need to round the first part and then round the second part 1050 00:47:58,790 --> 00:48:04,910 because you cannot have half a llama die and 1/3 being alive. 1051 00:48:04,910 --> 00:48:09,920 So in some cases, where the number is divisible by one part of it 1052 00:48:09,920 --> 00:48:13,040 and not divisible by the other, could create some differences. 1053 00:48:13,040 --> 00:48:17,300 CARTER ZENKE: Yeah, so maybe it is better for us to add start over 3 1054 00:48:17,300 --> 00:48:21,067 and then subtract maybe start over 4 1055 00:48:21,067 --> 00:48:22,400 STUDENT: Yeah, but what if you-- 1056 00:48:22,400 --> 00:48:23,150 CARTER ZENKE: --and keep going. 1057 00:48:23,150 --> 00:48:24,950 STUDENT: --round these numbers? 1058 00:48:24,950 --> 00:48:30,050 I guess divided by 3 [INAUDIBLE] or 3.3333. 1059 00:48:30,050 --> 00:48:34,080 So you need to round it 3 because, otherwise, 1060 00:48:34,080 --> 00:48:36,678 you cannot have 1/3 of a llama [INAUDIBLE].. 1061 00:48:36,678 --> 00:48:38,970 CARTER ZENKE: Yeah, we don't want 1/3 third of a llama. 1062 00:48:38,970 --> 00:48:41,230 So we got to round this in some way. 1063 00:48:41,230 --> 00:48:44,400 And I think what we want to do is always round down, 1064 00:48:44,400 --> 00:48:48,720 which brings us to some feature or, in some cases, a bug in C-- 1065 00:48:48,720 --> 00:48:51,210 you might run into this-- called truncation. 1066 00:48:51,210 --> 00:48:54,900 And we didn't see this as much in lecture, which to highlight it here, 1067 00:48:54,900 --> 00:49:00,940 in C, when you divide two integers, you will always get back an integer. 1068 00:49:00,940 --> 00:49:07,050 So if I divide perhaps maybe 2 and 3, 2 divided by 3, both integers, 1069 00:49:07,050 --> 00:49:08,760 I won't get 2/3. 1070 00:49:08,760 --> 00:49:10,260 I'll get 0. 1071 00:49:10,260 --> 00:49:13,240 Always round down, cut off the rest of that data there. 1072 00:49:13,240 --> 00:49:19,440 So to show an example of this, I might do code truncation.c. 1073 00:49:19,440 --> 00:49:25,480 And I will have the same boilerplate code, standardio.h. 1074 00:49:25,480 --> 00:49:28,420 I'll include cs50.h. 1075 00:49:28,420 --> 00:49:30,850 And I'll say int main void. 1076 00:49:30,850 --> 00:49:35,680 And I'll type in maybe int a is, let's say, 2. 1077 00:49:35,680 --> 00:49:37,540 Int b is 3. 1078 00:49:37,540 --> 00:49:42,020 And let's print out the result of dividing them like this. 1079 00:49:42,020 --> 00:49:46,210 Let's say-- actually, sorry, int c is a divided by b. 1080 00:49:46,210 --> 00:49:51,100 And I'll say this gets the result c here. 1081 00:49:51,100 --> 00:49:58,300 I'll say make truncation and do ./truncation. 1082 00:49:58,300 --> 00:50:02,380 And I'll get 0 and not 2/3. 1083 00:50:02,380 --> 00:50:04,300 So always rounding down here. 1084 00:50:04,300 --> 00:50:07,150 If I do want the decimal though, what I can do 1085 00:50:07,150 --> 00:50:12,170 is I can convert on the fly one of these to a decimal. 1086 00:50:12,170 --> 00:50:16,240 So I could say, actually, I want a to be a float 1087 00:50:16,240 --> 00:50:18,650 and then complete this division. 1088 00:50:18,650 --> 00:50:20,980 And as long as one of your numbers is a float, 1089 00:50:20,980 --> 00:50:22,940 you'll get a float in the very end. 1090 00:50:22,940 --> 00:50:25,660 So I'll say maybe %f here. 1091 00:50:25,660 --> 00:50:27,950 I'll do make truncation. 1092 00:50:27,950 --> 00:50:28,450 Whoops. 1093 00:50:28,450 --> 00:50:32,980 Let me say this is actually now a float in the end. 1094 00:50:32,980 --> 00:50:41,440 Make truncation, I'll say ./truncation, and I will get that 0.6667. 1095 00:50:41,440 --> 00:50:52,270 If I were to not do this, if I were to not do this, I would get 0 all around. 1096 00:50:52,270 --> 00:50:55,000 So just something to keep in mind as we go through here. 1097 00:50:55,000 --> 00:50:58,337 Happy to stick around and answer any questions that you all have, 1098 00:50:58,337 --> 00:51:01,420 but this should bring us to the end of our lab today and our super section 1099 00:51:01,420 --> 00:51:01,850 as well. 1100 00:51:01,850 --> 00:51:02,540 Thank you all for coming. 1101 00:51:02,540 --> 00:51:03,832 It's wonderful to see you here. 1102 00:51:03,832 --> 00:51:06,060 Hope to see you next week in section. 1103 00:51:06,060 --> 00:51:08,000 87014

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.