>> LUCAS FREITAS: OK, I guess now we're live.
Welcome to CS50 Supersection.
We're Less Comfortable.
My name is Lucas Freitas.
I'm one CS50 Head TFs.
This is Lauren.
>> LAUREN CARVALHO: Hi, I'm Lauren.
I'm also Head TF for CS50.
>> LUCAS FREITAS: Today we're just going to give an overview of what all you
guys have learned in week one, hopefully to give you guys a good
sense to what you should know for problem set one.
>> Just a couple announcements.
First of all, office hours.
Office hours are happening from Monday to Thursday in different houses in
Annenberg each day.
They're a very good resource for you guys to come and ask questions, and
talk to TFs, and even exchange ideas with other students.
So be sure to use that resource in your favor.
>> The second thing is the CS50 Appliance.
For the last [INAUDIBLE]
you guys didn't have to actually have an appliance.
You just did everything in your personal computer.
But for this problem, you actually have to have a virtual machine, so be
sure to download the CS50 Appliance and set it up.
>> And the third thing is be sure to start the problem set ASAP just
because it's much easier to find help when you're in the
beginning of the week.
Because people tend to wait until the last second to start the problem set,
and then you go to office hours, and there's a lot of people.
Where if you go on Monday, you're going to see that you can talk to TFs
pretty much for the whole night, and they're going be more than
happy to help you.
>> LAUREN CARVALHO: Can I just stop you for just one second?
Has everyone been able to download the CS50 Appliance?
Have people tried?
>> LUCAS FREITAS: To download, by the way, you can get instructions at
cs50.net/appliance.
>> LAUREN CARVALHO: Right.
And it does take a little while for our sys admins to get you a license
key, so sign up for the free trial first.
That way you'll have it for 30 days, and then wait for your license key
which should give it to you for a year.
>> LUCAS FREITAS: First of all, let's talk a little bit
about the CS50 Appliance.
The CS50 Appliance is actually a virtual machine that you're going to
run in your own computer which is basically running another computer
inside your own computer.
Which is pretty cool.
And actually it's used everywhere.
People who to tech would know that this is used all the time.
you?
Can have multiple computers in one device.
>> Let's actually take a look at the Appliance.
When you go to the Appliance after registering, you're going to have to
put your name and log in to Harvard so we can get you all registered.
You going to see basically just a wallpaper, and then Home and Trash,
and then these little icons.
This three icons are actually the most important things that you can have in
your Appliance.
>> The first one is gedit, which is a text editor, just like Notepad or like
any kind of text editor.
It's just something that we use to write code and then do stuff with it.
>> The second thing that we have here is Chrome.
So if you need to use the internet in the Appliance, that's
what you can use.
>> And the third thing is the terminal, which is actually very sweet.
People usually are kind of scared of the terminal because it's this very
old fashioned application in which you just basically type everything with
your keyboard.
There is not a lot of interaction with your mouse.
But actually the terminal does very good things.
>> First of all, let's just go through some commands that you
can use in the terminal.
>> LAUREN CARVALHO: And feel free to stop us if you have any questions.
>> LUCAS FREITAS: Yeah.
So the first command that we have for the terminal is ls.
ls basically means list everything, all the files and all the directors,
that I have at the place that I am at.
So if I do ls here, I'm going to see that I have some directories.
I have the desktop, the downloads, the logs, and the VHOSTs.
Those are in blue because they are all directories.
>> Then you have some other files.
You have like hello and hello.c, which are just files, and they're in
different colors.
So this is a very important command just you can see what are the files
and directories that you have in the directory that
you're at in the terminal.
When you see jharvard@appliance and then the little symbol there, the
tilde, that just means that that's the directory that you are at the moment.
>> LAUREN CARVALHO: So typing ls is almost like clicking on the folder
that is listed right there, in it shows you all of the content that are
within that folder.
>> AUDIENCE: Is there text after the hello.c?
>> LAUREN CARVALHO: Oh, you can't see that, because it's blue.
>> LUCAS FREITAS: It says logs and VHOSTs.
>> LAUREN CARVALHO: Those are just other folders.
I'm sorry, it's really hard to see.
Maybe if we turned off the lights?
Colton, do you want to try and find the light switch?
Sorry about that.
I didn't think about it.
>> LUCAS FREITAS: But it's basically--
yeah, if you could--
>> LAUREN CARVALHO: Zoom in your screen.
>> LUCAS FREITAS: I don't have that feature.
>> LAUREN CARVALHO: [INAUDIBLE].
OK.
Well, there's just a lot of natural light in this room as well.
>> LUCAS FREITAS: Let's just assume that there are two other directories there.
>> LAUREN CARVALHO: Just bear with us.
This is the only part that you'll be looking at the terminal.
>> LUCAS FREITAS: The next command is basically maybe you want to organize
your files--
for example, all your C files in one directory- but you don't have a
directory just for C files yet.
You can basically create a directory using the terminal as well, which is
the same thing as right clicking and saying New Folder.
The command is make directory, which is mkdir, so basically "make" and
"directory."
>> And then you can put the name of the directory.
Let's say that I wanted to be called, I don't know, let's say pset1 Doing
this, I just created a directory.
Let's just check that it is there.
So if I do ls, now I see logs, and then pset1, and then VHOSTs.
So I have the new directory there.
>> And then after you have a new directory, maybe you want to navigate
into it, so the next command this cd, which just basically means go to a
specific directory.
So if I want to go to pset1, I can just do cd pset1.
>> LAUREN CARVALHO: cd stands for change directory, [INAUDIBLE] able to
remember that.
>> LUCAS FREITAS: And now you see that instead of just having jharvard
appliance, I have jharvard appliance, and then pset1, because it's showing
me where I am in this computer.
>> Now if I want to go back to the directory that was before this one, I
can do a cd dot dot, which just means go to the directory that I was before,
like the parent of this directory, basically.
And now I see that I'm back to jharvard.
>> The next thing that I can do it is rm, which is remove a file.
For example, let's first do ls to see what I have here.
I have hello and hello.c.
Let's say that I want to remove them.
I can do rm hello.
It's going to say "remove regular file hello?" And I say "yes." And now if I
do ls, I see that it's not there anymore.
>> If I try to use rm for a directory, though, it's not going
to let you do anything.
It's going to say, hey, you cannot remove a directory.
So we have this other come called rmdir, which just basically means
remove directory.
I can, for example, remove pset1.
And if I do ls, I don't have it anymore.
>> Any questions about this?
No?
Yeah?
>> AUDIENCE: Is it important that you leave a space after-- whenever you
start typing something in?
>> LUCAS FREITAS: Yeah.
If I do like cd and the directory, I have to put a space.
What space are you talking about?
>> LAUREN CARVALHO: The space between cd and [INAUDIBLE].
>> LUCAS FREITAS: Yeah, you'll have to put the spaces because cd is the
command, and then the next string that you type is basically the directory.
So it's like an argument.
Yeah?
>> AUDIENCE: When you list, all those are different directories, right?
>> LUCAS FREITAS: Yeah, they are in the directory.
>> AUDIENCE: What is the differences among the directories?
>> LUCAS FREITAS: They're just different folders.
Directory is a folder in your computer.
>> AUDIENCE: Open folder.
>> LUCAS FREITAS: Yeah, it's just an open folder.
>> LAUREN CARVALHO: Over here, the ones that are in blue are actual
directories that you can get into and then see a bunch of things that are
within that, which is unfortunate because you can't actually read the
ones in blue.
And the ones in white are just files that are within the current directory.
>> LUCAS FREITAS: So directories are folders.
The other ones are files.
>> AUDIENCE: What's the command to reset it or to clear it?
LUCAS FREITAS: To clear?
LAUREN CARVALHO: To clear?
LUCAS FREITAS: I think it's "clear." Yeah.
So you just type clear if your screen's too messy.
Any other questions?
And you will learn a bunch of other commands just naturally during the
semester because they're very useful.
>> So now let's talk about compiling.
We talked about pseudocode and source code and object code.
And there's a lot of codes, and maybe you guys are confused about that.
Let's talk a little bit about what happens when you're writing a program,
and you compile it, and it becomes an executable file.
Those are all words that you guys probably heard, and maybe you're not
really sure what is going on.
>> Let's say, for example, that I want to write a program that says "hello." The
first thing that comes in your mind is you kind of try to think of how you're
going to do that in your mind, and you're probably thinking in English or
some other language, like natural language, probably.
And then you think, well, if I want to, for example, write a program that
says "hello," I would start a program, I will print hello, and then I will
quit the program.
This is pseudocode.
This is just basically you're trying to come up with a sequence of events
that you're going to do to be able to write a program.
>> Then you have the source code, and the source code doesn't have to
specifically be in C. We're talking about source code in C because that's
the language we're learning, but course code can be pretty much any
programming language that you have.
Later in the semester, you're going to learn other languages.
That's also source code.
>> For example, here, this is my source code, and then I'm including a
library, and then I start main, and then I print.
And Laura is going to talk a little bit about all of this.
So this is the source code.
>> And what is the object code?
The object is a bunch of zeros and ones, basically.
What happens is you initially have pseudocode, which is all in English.
And I mean, you understand it.
Probably your friends understand it, and most people would
understand the idea.
But computers are really dumb, and they don't understand English, so you
have translate that into something, usually translated into some
programming language.
>> And you can think of a programming language as kind of like a language
that you tell your computer how to speak and to understand.
You're basically writing something in a language that both of you and your
computer understand.
That's the source code.
So going form pseudocode to source code, it's up to you.
It's basically you doing your program.
>> But the problem is that the computer doesn't even
understand programming language.
It doesn't understand source code at all.
It actually just understands zeros and ones.
So we have this very magic thing called a compiler, which basically is
going to get the source called that you wrote in a language the both you
and your computer speak, and now you're going to transform that in
zeros and ones.
So basically what the compiler does is it gets source code, in this case C,
it is going to transform it into zeroes and ones that the computer
understands and can make an executable file out of it.
>> Does that make sense?
You can just think of the compiler as some kind of translator that is
helping you explain to the computer exactly what it's supposed to do.
>> But how do I compile.
Let's go back to the Appliance, and let's say that I'm doing that program.
First I'm going to create a directory here, and I'm going to call it
"practice." Now I'm going to navigate to the directory, so cd practice.
Now I'm inside practice.
>> Now I want to start gedit, so how do I do that?
I can either click here, or I can just literally type "gedit," and it's going
to open for me.
>> Now I'm going to type my program, so exactly what I wrote in the slide.
I'm declaring a library.
I'm doing int main, void.
And now I'm doing printf, hello.
Now I'm going to save this.
Let me put it into practice, and I'm going to call this hello.c.
So now it's saved.
>> If I want to compile it, I actually can do it in two different places.
I can either do it in a terminal window, so this big one, or you can
use gedit that actually has a terminal window right here.
So you can very conveniently just do your code, and then you can just type
whatever here, and try to compile it.
>> So how do I compile code?
The easiest way that we have is by using make, which is a function that
is basically going to compile your code.
You just have to say make hello, so "make" and the name of the program.
>> Oops.
This is awkward.
>> LAUREN CARVALHO: Can you [INAUDIBLE]?
>> LUCAS FREITAS: Sorry.
I didn't save it in the right directory.
Come to practice, and now save it as hello.c.
I'm sorry.
Now it's saved.
>> Let me just do ls and be sure that it's here.
OK, now it's here.
So now if I do make hello, it spits a lot of stuff, but then it just gives
me another line for the terminal, which means that it was successful.
So now if I do an ls again, I actually see this file in green, which is the
executable file.
>> And then if I want to run it, all that I have to do is dot slash hello.
It's going to say hello right here, so it's exactly what I'm asking it to.
>> There is one more way of compiling, which is using clang.
Clang is just a compiler that we have, and it's a little bit more verbose to
compile it.
But it works just as well.
So if I do clang, and then -o, and then the name that I want to give to
the executable file, so let's say "hello." And then the file that I want
to compile, hello.c.
It's also going to compile.
And you have the file here, and I can just execute it as well.
So both of them work.
>> Does that make sense?
Any questions?
>> AUDIENCE: Can you zoom in a little bit?
>> LUCAS FREITAS: Sorry?
>> AUDIENCE: Zoom in?
>> LAUREN CARVALHO: I think you can just command plus.
>> LUCAS FREITAS: Command what?
>> LAUREN CARVALHO: Command plus.
>> LUCAS FREITAS: No.
>> LAUREN CARVALHO: Oh, that's not going to work because--
it's because we set the resolution for this thing.
>> LUCAS FREITAS: I don't think we can zoom in.
Yeah, we cannot zoom.
Do you have another question?
Sorry.
>> AUDIENCE: Can you repeat clang?
>> LUCAS FREITAS: The clang?
Clang is the same thing as make, but the difference is that first I'm going
to write clang, which is the command, then I'm going to do -0, all, which is
just what we're going to learn later as a flag.
So just think that you always have to put the -o.
>> And then first, you're going to put the name of the executable file,
basically the file that you're going to create.
So I can say, for example, hello2 and then the file that I want to compile,
which is the .c file.
So that's hello.c.
Does that make sense?
>> AUDIENCE: Hello what?
What is that?
>> LUCAS FREITAS: Hello.c.
And if I compile it, and if I do ls, I also have a
hello2 that I just compiled.
And if I try to run it, it's going to do exactly the same thing as hello,
just print hello.
>> AUDIENCE: How come for the make you can put hello.c after?
>> LUCAS FREITAS: In make, you don't have to put the .c.
In make, you just say the name of the program.
You don't have to say .c.
But for clang, you should.
>> AUDIENCE: With clang do you also have to add the [INAUDIBLE]
>> LAUREN CARVALHO: Yeah.
So you also have to library.
>> LUCAS FREITAS: Yeah, that's something we going to use later.
That's called linking, and I think we're going to talk about it later.
Another question?
>> AUDIENCE: Do you put a dot slash clang?
>> LUCAS FREITAS: No.
The dot slash is just to open the executable file.
So clang, you just write "clang," and then the if you want to open the
executable file that you just compiled, then you do dot
slash and the name.
Does that make sense?
>> AUDIENCE: If you were to just change [INAUDIBLE] to hello world, do you
have to recompile it?
>> LUCAS FREITAS: Yeah.
if I want to say, for example, "hello there," I can just save it now, and I
just compile it again.
So let me do a make hello.
And if I do hello, it's going to say "hello there." Makes sense?
Any other questions?
>> AUDIENCE: Do you have to include the standard I/O to coordinate the work?
>> LUCAS FREITAS: Yeah.
The standard I/O is basically the library that has an
implementation for printf.
So that thing right on the top includes stdio.
That is including a library that basically has a bunch of different
tools that you can use.
one?
Of them is printf that is a function that basically prints
messages to the screen.
>> Any other questions?
No?
OK.
So now let's talk about--
>> LAUREN CARVALHO: Let's just go to the style guide.
Do you want to just find it in here?
>> LUCAS FREITAS: So we're going to talk about style.
>> LAUREN CARVALHO: Oh, David didn't finish.
OK.
Why don't we come back to style at the very end?
It's actually been updated as we speak.
>> LUCAS FREITAS: Yeah.
Let's way for David to finish that.
>> LAUREN CARVALHO: Can we switch actually?
>> So I hope everyone's been enjoying Scratch.
Unfortunately, it's time to move away from this--
unfortunately it's time to move away from these pretty programming blocks
on to C. But luckily, all of the programming constructs that you've
been working with in Scratch are directly translatable to C, and we're
going to start to learn about some of them right now.
Let's first talk about conditions in Boolean expressions.
>> This is running on reserve battery power.
>> LUCAS FREITAS: How is that not working?
OK.
You go there, I'm going to get the--
>> LAUREN CARVALHO: So conditions are important because they allow us to
introduce logic into our programs.
Here's an example of a condition within a C program.
Let's note how this--
So this condition block right here, this printf statement will not execute
unless the condition within the parentheses, n greater than 0,
evaluates to true.
>> Now, all of the conditions within f statements are Boolean expressions,
which means that there are only two options.
They could either evaluate to true, or they can evaluate to false.
So here, so this makes sense, right? n greater than 0, means it is a positive
number, which means that it will print out "you picked a positive number."
>> There are lots of Boolean expressions that can form the condition with an
init statement.
We have the less than.
This is less than or equal to.
This is just a funny [INAUDIBLE].
If you want the printf to occur if it's greater than or equal to 0, then
you would choose this.
So the equal just appends right after the greater than sign.
>> Equal equal, which is different from just one equal sign, equal equal
actually means that whatever's on the left hand side of this is the same as
what's on the right hand side of this where one equal sign is what?
What is just one equal sign?
Assignment.
>> LUCAS FREITAS: Who said that?
Is that candy?
Oh, no.
Sorry.
You both get candy.
Oh, OK!
Three people get candy!
>> LAUREN CARVALHO: So this one right here is the bang operator preceding
the equal sign, so that's not equal to.
So if this was within my condition, the body of the condition would
execute only if whatever's in the left is not the same as
what's on the right.
>> I can also have just the exclamation point.
Actually we'll see in a feature slide how this would be used.
>> LUCAS FREITAS: Any questions?
No?
>> LAUREN CARVALHO: We can combine these Boolean expressions to construct more
intricate conditional statements.
So here, if whatever's on the left hand side of the double bars or
whatever is on the right hand side of the double bars evaluates to true,
then we'll print out invalid.
And then down here, if whatever's on the left hand side of the two
ampersands and whatever's on the right hand side of the two ampersands
evaluates to true, then we'll print out valid.
The double bars are called the logical OR operator the double ampersands are
called the logical AND operator.
Yes?
>> AUDIENCE: Are those just lower case l's?
>> LAUREN CARVALHO: No.
They're actually--
you know the backlash, like above your Return key?
Do you see that?
>> LUCAS FREITAS: It's actually a bar.
>> LAUREN CARVALHO: It's actually just two bars.
>> AUDIENCE: Can you use the equal equal for strings?
>> LUCAS FREITAS: No.
We're going to learn later something about that.
>> LAUREN CARVALHO: There's a function that you can use.
But you don't have to worry that just yet.
>> What if I give this program the number 5?
What will print out?
>> AUDIENCE: [INAUDIBLE]
valid.
>> LAUREN CARVALHO: Valid?
Yeah.
I don't know who got that.
>> [LAUGHTER]
>> LAUREN CARVALHO: That's kind of scary [INAUDIBLE].
>> AUDIENCE: So the double and, that just means and?
>> LAUREN CARVALHO: Yeah.
So number 5 is both greater than 0 and less than 100.
>> AUDIENCE: Why would it be one?
>> LAUREN CARVALHO: One ampersand is actually something different, and it's
a bitwise operator, which you'll learn about later.
But it's something that actually works with the bits of a given number.
>> LUCAS FREITAS: Just think they don't have to worry about it for now.
>> LAUREN CARVALHO: I think it's probably coming up next week.
>> LUCAS FREITAS: Any other questions?
No?
>> LAUREN CARVALHO: So we've seen that an it block can stand alone, but it can
also be paired with and else block.
So in this case, if n is greater than 0, we're going to print out "You
picked a positive number." else, meaning if it is 0 or if it's less
than 0, we're going to print out, "You picked a negative number."
>> The interesting thing here is that these are exclusive of each other.
There's never going to be a case in which this and this
can both print out.
>> I'm sure you worked with the if/else box in Scratch, so this should be
pretty familiar.
Yes?
>> AUDIENCE: Do we need the space between int n equals GetInt then space in the
if statement?
Like we use a line [INAUDIBLE]
>> LAUREN CARVALHO: Oh, the space here?
>> AUDIENCE: Like that, but you double space.
No, go up.
Go up.
>> LUCAS FREITAS: Just the line?
>> AUDIENCE: Yeah.
>> LUCAS FREITAS: Just the blank line?
>> AUDIENCE: Yeah.
>> LAUREN CARVALHO: Blank line?
No.
That's just a stylistic thing.
>> LUCAS FREITAS: It's just like sometimes you just skip some lines so
that the code doesn't look like a huge block of text.
>> LAUREN CARVALHO: I think what happened was that we had a comment here, but I
took it out so that you guys would have to think about the code rather
than just read my comment.
In general, spacing will not really affect the way your program runs.
It's more of a stylistic thing.
And when we have the style guide updated and ready to go by the end of
supersection, we'll talk about that more.
>> There's something wrong with this program, right?
Because 0 is not really a negative number.
So let's see if we can fix that.
>> You can probably imagine with this says, right?
If n is greater than 0, we print, "You picked a positive number." Then else
if n is less than 0, "You picked a negative number." else, "You picked
0."
>> Now the interesting thing is that this could go on forever.
You can have an if block, an else if block, another else if block, else if,
else if, else if, else, basically as long as you want.
And when constructed like this, these are all going to be
exclusive of each other.
Yes?
>> AUDIENCE: So why can't you just use another if instead of [INAUDIBLE]?
>> LAUREN CARVALHO: In this case, yes I could use another if, and it would be
absolutely fine because the conditions themselves are
exclusive of each other.
But if the conditions were not exclusive of each other, this would
force them to never both evaluate, if that makes sense.
>> AUDIENCE: In that case, could we nest the second if in the first if?
>> LAUREN CARVALHO: If-- what do you mean?
>> AUDIENCE: If the two conditions are not mutually exclusive?
>> LAUREN CARVALHO: If the conditions are not mutually exclusive?
But you want only one of them to run?
>> AUDIENCE: You want just if instead of else.
>> LAUREN CARVALHO: Yeah.
There are always going to be lots of ways that you can do this.
You can definitely nest them.
And you'll see in a future slide that you can have multiple ifs, just one
after another.
But this is a way of logically organizing the flow of your program.
>> LUCAS FREITAS: And there's one more thing, is that you asked about putting
if and then another if.
That would work perfectly, but it's bad design because basically if you
check the first if, and you notice that the number is greater than 0, you
can just finish the program.
You don't have to check if it's less than 0, right?
>> But if you do if if, it's going to check both, while if I do if else is,
it's going to check if it's greater than 0.
If it is, it's not going to even look at the other lines of the code.
So you're basically checking it like twice unnecessarily, so that's why we
do if else if.
Any other questions?
>> AUDIENCE: So the functionality of the else if is the same as
if it was just if?
>> LAUREN CARVALHO: No.
You're actually going to see in my next slide an example in which it can
be different.
In this case, it's almost the same.
>> So here, we have an example of multiple if statements, and the
functionality is not the same as if I had just said if
else, if else, if else.
Let's just think about what would print out if I enter the number 95
into this program.
>> AUDIENCE: [INAUDIBLE]
>> LAUREN CARVALHO: And then?
>> AUDIENCE: [INAUDIBLE]
>> LAUREN CARVALHO: Yeah.
So in this case, these conditions are not mutually exclusive of each other.
And because there's nothing stopping this condition from evaluating if this
one already has, they're just all going to print out.
So how could I fix this?
>> AUDIENCE: else ifs?
>> LAUREN CARVALHO: You can use else ifs.
I could change the order of these, right?
If I did this one first?
Well, actually, no.
That wouldn't work for 95.
>> LUCAS FREITAS: No, just the else ifs.
>> LAUREN CARVALHO: The else ifs would have fixed this.
Does that clear up the confusion between ifs and else ifs?
>> AUDIENCE: I just wanted to ask you, if you did use else if, it would stop?
>> LAUREN CARVALHO: Right.
So only this first one.
If I entered the number 95, only this would have printed out, and it never
would have progressed to the else.
>> LUCAS FREITAS: Just kind of think of it as if this happens, do this.
The else is like if not, check for this, and then do that.
So like they're all mutually exclusive.
You cannot get into two over those.
>> AUDIENCE: Can you use a logical AND for that situation?
>> LAUREN CARVALHO: Yeah.
That's another way that we could have fixed this problem.
We're could have defined ranges, right?
So I could have said if n is greater than 90 and less than--
well, I guess this one doesn't really matter, but here I could have said if
n is greater than 80 and less than 90.
Greater than 70, less than 80.
That would have also worked.
>> [LAUGHTER]
>> LUCAS FREITAS: She's scared.
>> LAUREN CARVALHO: Don't be afraid.
>> LUCAS FREITAS: It's not that it will be wrong.
It's just that it's unnecessarily verbose.
Like in computer science, one should be as simple as possible as long as
you have the functionality.
Do you want the candy?
>> AUDIENCE: Don't need it.
>> LAUREN CARVALHO: You're scaring everyone, Lucas.
>> LUCAS FREITAS: Sorry.
>> LAUREN CARVALHO: David also mentioned these switch statement in class.
Basically what's going on is the switch statement takes a variable.
In this case, we're taking an integer.
>> If the integer is 1, we're going to print out, "You picked a small
number." If the integer is 1, "You picked a medium number." If the
integer is 3, "You picked a high number." And I also had a default case
down here, but it's not showing up, which is too bad.
>> Basically, this is just another syntax.
We could have implemented this exact same thing with if and else if
statements.
But when there are lots of different conditions and lots of different
variations on what you want to do for each input, this is an option.
So it's just a good syntax to know.
Yes?
>> AUDIENCE: Why is there not a backslash n in the first one?
>> LAUREN CARVALHO: For up there?
There's no backslash n up there because the GetInt--
if you try this, it'll give you this prompt.
Give me an integer between 1 and 3.
And if I put the backlash n, it would take me to the next line, but I just
wanted them to type the integer right after the colon.
You could have put the backlash n.
It's not a big deal.
Yeah?
>> AUDIENCE: For this code, is this case one, is that because
it's the first case?
Because--
>> LAUREN CARVALHO: No, no, no.
So this is give me an integer between 1 and 3.
If n is 1, I print this.
If I had asked for letters, I could have said give me a letter between A
and C. Then this would be case A.
>> AUDIENCE: [INAUDIBLE].
The case would just [INAUDIBLE]
>> LAUREN CARVALHO: Yeah, right.
Yeah?
>> AUDIENCE: What if you wanted to have more of a relational expression?
So instead of saying n equals 1, [INAUDIBLE]
n is greater than 1 in the first case?
>> LAUREN CARVALHO: Actually, if I do not define some of these cases, this would
be carry down.
So actually, maybe this isn't what you're asking.
>> LUCAS FREITAS: I think she's asking if the case was n greater than 0, case
less than 0.
>> LAUREN CARVALHO: Maybe the switch statement is not really what you want
to use in that case.
>> LUCAS FREITAS: The switch statement is just for value, so you're checking of
the value is like this or that.
So you can not actually put conditions.
>> AUDIENCE: What if the person types with [INAUDIBLE]?
>> LAUREN CARVALHO: That would have been my default case, which unfortunately
you can't see.
>> LUCAS FREITAS: [INAUDIBLE] make them see.
>> LAUREN CARVALHO: Actually, you know what?
Let's just go to study 50, and I will just zoom in on the slides on study
50, because this is just annoying.
So let me actually go to condition.
This is just going to take a little while.
>> All right.
So, I'm sorry.
At least now we'll be able to see everything without it cutting off.
All right.
>> So if they put in a 10, we would default here to my default case, and I
would just print invalid because I asked them to enter an integer
between 1 and 3.
Any other questions?
Yeah?
>> AUDIENCE: Let's say for case one, case three, say you wanted it to be all the
same thing, so you picked a number that's not a medium number.
In that case, would it be redundant?
Would you just change the text in both of them, or would there be some
redundancy?
Is there any way to combine [INAUDIBLE]?
>> LAUREN CARVALHO: There are ways to combine them, but I believe you can
only combine adjacent ones.
So if I did not define anything for case two, this would
apply to both cases.
Oh, if I did not put anything for case one, this would apply to both
case one and two.
If that makes sense.
>> AUDIENCE: Can you repeat that one?
>> LUCAS FREITAS: OK.
If you did basically, like, you have the same thing, but you don't put
anything here, you'd be either for case one or case two, do this.
Right?
And this break means if you get into this case, just leave.
So that's what the brake means.
So if I just did this, you look, it's case one, and you would just do this.
If it was case two, it would also just come here and do that.
>> AUDIENCE: But you can do it?
If there's something for case two [INAUDIBLE]
>> LAUREN CARVALHO: I don't believe you can do that.
I don't think you can skip around.
Can you do that, Rob?
I don't think so.
>> AUDIENCE: So only one number can go after a case, right?
There can't be case one, comma, two?
>> LUCAS FREITAS: No, you can't.
You definitely can't.
>> LAUREN CARVALHO: I mean, the point is this is an option, and this might not
fit your use case, so then you would use the if conditions.
>> AUDIENCE: So why would you do this?
The switch?
>> LAUREN CARVALHO: I would use this if you have very defined--
actually, Lucas, is you're blocking.
>> LUCAS FREITAS: Oh, sorry.
>> LAUREN CARVALHO: If you have very defined behaviors that you want for
each and every input, this might be a little more concise than writing out
an if statement for each one.
>> AUDIENCE: What does the break mean again?
>> LAUREN CARVALHO: The break means stop.
Remember, if I didn't have this break here, we would have just fallen down
into the next case.
>> LUCAS FREITAS: Break means to get out of the switch statement.
Because if it's 1, I want to print this and leave.
I don't want to check anything else.
>> AUDIENCE: You don't want to print anything else.
>> LUCAS FREITAS: Yeah.
Because it's mutually exclusive.
>> AUDIENCE: And does this only proceed to the next case if the first case
isn't true?
Or does it test all the cases at the same time?
>> LUCAS FREITAS: No, it checks sequentially.
Sequentially, yeah.
One more question, then we've got to move on so we can cover everything.
Yeah?
>> AUDIENCE: So for the case, could have done something besides just the one?
Could you give it a range?
>> LUCAS FREITAS: No.
>> LAUREN CARVALHO: I don't think you can give it a range.
>> LUCAS FREITAS: You cannot you cannot give it a range.
>> AUDIENCE: The case is specific [INAUDIBLE].
>> LUCAS FREITAS: Switch is sort of values.
You doing different things depending on a value, a specific value.
You cannot do range, you cannot do anything else.
It's just another tool to see.
>> LAUREN CARVALHO: Most people don't use switch statements very much in CS50,
but it's there.
>> All right.
So the next thing we're going to talk about is the ternary operator.
This is just another type of conditional.
Basically what's going on here is I'm asking for an integer, and if n is
greater than 100, I'm going to print out "high." If it's not, I'm going to
print out "low."
>> So what it's doing is the condition is right here in the parentheses, and you
can kind of think of the question as I'm asking whether
this is true or not.
If it's true, I'm going to do this first thing, and if it's not true, I'm
going to do this second thing.
>> Any questions about the ternary operator?
>> LUCAS FREITAS: Just want note from David is that whenever you have a
condition--
>> LAUREN CARVALHO: I guess David's watching via the live stream.
>> LUCAS FREITAS: He's doing life stream right now.
So whenever you have a condition, you're supposed to put curly braces,
just like we have in a switch statement here.
We have curly braces here.
For whatever conditions, you're supposed to also put that.
>> LAUREN CARVALHO: You'll see this in the style guide.
We don't actually have the style guide right now.
I think David's still working.
>> LUCAS FREITAS: He said that it's just working now.
>> LAUREN CARVALHO: Oh, is it working now?
OK.
So apparently this year in the style guide, even if you just have one line,
you've got to put those curly braces in.
You don't necessarily need to for the functionality of the program.
And maybe we should just take a break after the conditions and go straight
to the style guide to clear up some of these things?
>> LUCAS FREITAS: Yeah.
Do you have a question?
>> AUDIENCE: No, I'll just wait till the style guide.
>> AUDIENCE: Could you go back to the ternary operator?
>> LAUREN CARVALHO: Ternary, yes.
>> LUCAS FREITAS: Ternary operator is basically an if else statement, but
instead of having to have the two lines of checking an if and then the
else, you just have one line.
So basically if I want to give a string a value of high or low, and I
want to do a high if n is greater than 100 or low if it's less, what I can do
is this [INAUDIBLE]
the string is going to be, and then I ask the program is n greater than 100?
>> So that's why I have this expression in parentheses and then
the question mark.
I'm asking is it greater than 100?
If it is, it's going to be high.
If it's not, it's going to be low.
>> So basically what you think is you ask a question, so you put in parentheses
the condition.
Then you put the question mark because you're asking it.
And if this is true, it's going to be in the first value [INAUDIBLE] here.
If it's not, I'm going to put the value that is after the colon.
Does it make sense?
>> It's also something that is like a tool in C but you don't have to
specifically use it.
But CS people usually like that because it just saves lines.
>> LAUREN CARVALHO: It's very elegant.
It does a lot in just one statement.
>> AUDIENCE: So the percent sign is just whatever the results on
the previous line?
>> LAUREN CARVALHO: Oh, I'm sorry.
This percent sign is actually a placeholder for this s.
In a printf statement, you've got this string here, which is what's going to
print out, but you can't just put this s in there.
Otherwise it would print out the s literally.
So we put a percent here to keep this place held for whatever
comes after the comma.
>> So for a string, you've got percent s.
For an integer, percent i.
For a float, percent f.
You'll see those as we see more examples.
But maybe you should talk about the style guide for a little while, now
that it's up.
>> LUCAS FREITAS: If you go to cs50.net/style, you're going to see
the style guide.
For problem sets, your score for the problem set is composed of scope,
which basically means if you did all the parts of problem set or if you
attempted something.
The second thing is correctness, which means is your program doing what it
was supposed to be doing, or does it have bugs, or is it not doing what
it's supposed to do.
>> The third thing is design, which means are you implementing this well, or are
you using unnecessary statements?
So for example, if you want to repeat a number 10 times, are you writing a
for loop or are you just doing 10 times the same statement, just doing
copy and paste.
So design is for that.
>> And finally, you have style.
Style is just like judging if your code looks
readable and kind of pretty.
It just basically means is your code readable?
Would someone else who was looking are your code be able to understand what
it's doing?
Does it have comments and the like?
>> Let's talk about the style that you're supposed to use.
The first thing is comments.
Comments are very important for your code because sometimes you're doing
your program, and you know exactly what you want to do so you just start
coding, just type up a bunch of C. But if someone else is looking are you're
code, they're not going to be able to understand it because they don't know
the things that are in your head, like the pesudocode, so maybe they will
have a hard time to understand what the code is doing.
>> So comments are really useful to explain to someone who's looking at
your code what it's doing, and actually even for you,
they're very useful.
As you go to harder and hard Psets, you're going to see that the code gets
longer, and then comments are really important so you remember what each
part of your program does.
>> To put a comment, you can just do a slash slash and then write a comment.
And the whole line is part of a comment, and whatever you write down,
it's not going to be used for your code.
It's basically like the compiler is going to ignore that.
Does that make sense?
>> AUDIENCE: What do you do again to write a comment?
>> LUCAS FREITAS: This?
The slash slash?
Whenever you type slash slash, whatever is in that line is not going
to be considered to be C. Yeah?
>> AUDIENCE: So after the comment, you just enter and start a new line?
>> LUCAS FREITAS: Yeah, exactly.
>> LAUREN CARVALHO: The slash slash is for single line comments.
>> LUCAS FREITAS: For example, here we have--
let me zoom in--
for example, here I'm declaring a float, but maybe if you just put this
in your code, like you have no idea what this is doing, like actually
someone who's looking at this code is going to be, like, what the
heck is going on?
>> If you put a comment saying "convert Fahrenheit to Celsius, it makes much
more sense.
You're, like, OK, you're doing a conversion.
Does that make sense?
So it's just something that helps people ready your code and also make
you be able to organizing your code better.
>> The second thing is that you can do multi-line comments, so have one
comment for a bunch of lines.
Instead of having to put slash slash, the first line comments, slash slash,
the second line, and so on, you can use the slash star.
The way that you do it is that for the first line of the comment, you're
going to do a slash star star, so just like that.
Just like here we have the beginning of a program, you usually put who you
are, what this program does, and stuff like that.
It's kind of like a header for the program.
>> Again, it's not something that a compiler is going to care about, but
it's important for you to know what this program does or for other people
to know what your program is doing.
So you do slash star star, and then the last line you're going to do star
slash to be able to close that comment.
So basically for all the other lines, you can just write whatever, and this
is all going to be ignored.
>> And also, just as style, you see that each of the lines has a star, and all
the stars are aligned.
These are important things so that your code looks pretty.
Sounds good?
>> Now, conditions.
What David was saying that we should actually put in those slides, is that
when you have any kind of if statements or any kind of conditions,
you should use curly braces to show that this is what was going to happen
when you have the condition.
Technically, when you have a condition and then the next line is just one
line, you don't technically have to put the curly braces.
>> Basically the compiler would understand it anyway as implicit.
But for CS50, we ask you guys to put the curly braces just so that your
code is more organized and more readable.
Because that code gets longer if you don't have curly braces and such.
It gets really messy.
>> LAUREN CARVALHO: This is exactly what David just pointed out
in my previous slides.
It definitely works without the curly braces, but for stylistic reasons
we're going to ask that you do put those in.
>> And when you start running style50, which is kind of the check 50 for
style, it will remind you to do this.
>> LUCAS FREITAS: And another thing is remember to put the curly braces along
in each line.
So you do, for example, l's.
Go to the next line, open the curly brace, then you go to next line.
Indentation, which is four spaces.
Then you write whatever, then another line, and close the curly brace.
So be sure that both those curly braces are in different lines.
>> AUDIENCE: So if this code had like 10 else ifs, this should
be 10 sets of brackets?
>> LUCAS FREITAS: Yeah, for each of them.
Yeah.
So if, else if, and so on.
>> AUDIENCE: Can you not press Tab to indent?
>> LUCAS FREITAS: We're asking you guys to use four spaces.
Some text editors translate tab as four spaces automatically.
gedit doesn't, so you have to do space space space space.
This is also something that some programmers only do tabs, some
programmers only do four spaces, and some other people even do
two or three spaces.
It's just that here we're asking you guys to do four spaces.
>> AUDIENCE: There's almost definitely got to be a way for gedit to
automatically [INAUDIBLE].
Like no [INAUDIBLE] does that.
>> LUCAS FREITAS: Yeah.
I think you can go to Settings.
>> AUDIENCE: It already does all of that.
>> LUCAS FREITAS: It already does?
OK.
So just be sure that you have four spaces.
>> AUDIENCE: Do we have to put the curly brackets in for loops as well?
>> LUCAS FREITAS: Yes.
>> LAUREN CARVALHO: Whenever your encapsulating a block of code, just
please use the curly braces.
And before you think that we're just being sticklers about this for no good
reason, if you guys do go on to get tech internships at big companies,
they will give you a style guide, and they will tell you this is what your
code needs to look like.
And even though you learned to code like this, maybe in your internship
you'll have to do something completely different.
They do expect you to conform to their style.
>> LUCAS FREITAS: And one more thing is that do you notice how i have like x,
space, less, space, 0?
Whenever you have an operator--
it can be like plus, minus, times, less, greater, whatever operator--
you have to put a space on the left and the right of the operator.
>> LAUREN CARVALHO: Those are called binary operands.
Just make sure you always space out your binary operands.
>> LUCAS FREITAS: This is not a binary operand.
>> LAUREN CARVALHO: That is because it takes two.
>> LUCAS FREITAS: Oh, yeah.
>> OK, the switches.
First, you have to see that just like in an if or else statement, you have
to put the curly brace on its own line and under the curly brace here.
Notice that there is a single space after switch, just like in if and else
we had a single space between that and the parentheses.
So you have to put that.
>> And there's no space right after each of the parentheses or before the end
of the parentheses.
So you see that it's all together, and also like up here.
>> Yeah?
>> LAUREN CARVALHO: I think we need to move on.
>> LUCAS FREITAS: So this is part of the style guide that you have for CS50.
Be sure to go to cs50.net/style and take a look at this.
These are all very important for your style score.
>> Let me go back to the statement.
>> LAUREN CARVALHO: I'd rather just use style50.
We're just going to go straight to loops.
All right.
>> So let's get back to the content, now that we've gone over the style guide.
Loops, you've seen them in Scratch, and we're going to talk about them in
C.
>> In a for loop, there are three portions to the
declaration of this for loop.
You've got your initialization in which you set your variables.
You've got your condition, which are checking to see whether something is
true or false and whether the loop should continue.
And you've got your update, which is updating your variables.
>> Let's just take a quick look at some C code of this for loop in action.
What's going on is here in the initialization, we're setting i to 0.
In the condition we're checking whether i is less than 10.
And in the update we're incrementing i.
So what will result from this for loop?
>> LUCAS FREITAS: Actually, first of all, do you guys know what i++ is?
>> AUDIENCE: Increased by 1.
>> LUCAS FREITAS: Increased by 1, yes.
So this is the same thing as doing i equals i plus 1.
And notice that we don't have a space between the i and the ++.
>> So do you guys know what this will do?
Can someone tell me?
And maybe get some candy because of that?
>> AUDIENCE: [INAUDIBLE]
>> LUCAS FREITAS: OK.
[INAUDIBLE]
show everybody later [INAUDIBLE].
Yeah, it's going to show "This is CS50" 10 times.
>> LAUREN CARVALHO: I guess it was in the title of the slide.
>> LUCAS FREITAS: Oh, yeah.
>> [LAUGHTER]
>> LUCAS FREITAS: That helps.
>> LAUREN CARVALHO: So those are for loops.
We're going to see some more examples of for loops later on.
But let's actually take a look at the second type of loop you'll encounter
in C, and it's called the while loop.
>> In a while loop, you've just got a condition.
The condition is checked in the beginning, and if that condition
evaluates to true, then the code within the body of the while block
will execute.
And the code will continue to execute so long as that
condition remains true.
>> Here's an example of a while loop in C. We're going to set count to 10 to
begin with.
As long as count is greater than 0, we're going to keep printing out this
variable here.
We're going to decrement count and then check the condition again.
Print, decrement, check the condition again.
>> Here we have an example that pertains to your question earlier.
We had the percent s when we were printing out a string, and now that
we're printing out an int, we have a percent i as our placeholder instead.
The letters should all be pretty intuitive, s for string, i for int.
>> So if this code executes, you should count down from 10 to 0, printing out
each integer.
>> LUCAS FREITAS: And you can think of the percent as it feels like a blank
space, and then you're putting this variable here in that blank space.
So for example, here I will have just a blank, and then I'm putting the
count right there, the value of the count.
Does that make sense?
>> Could I just do like printf and then quotes, count?
Would that work?
>> AUDIENCE: No.
>> LUCAS FREITAS: No, because it will actually print the word "count"
instead of printing the value.
This is how you make it print the values toward the variable instead of
just the name of the variable.
Any questions?
>> AUDIENCE: Why do you use for as a opposed to while?
>> LAUREN CARVALHO: I would use a for loop if I know exactly how many
iterations I want my loop to run.
In this case, it doesn't matter because, I mean, I was just trying to
think of a really simple example for this slide.
But let's say that you want something to keep going while--
>> LUCAS FREITAS: For example, you're asking for input until the person
gives you the right input.
So like while the input is not OK, keep asking for it.
Or like while you're parents don't give you money, keep asking for it.
>> [LAUGHTER]
>> LAUREN CARVALHO: You don't know how many times you'll have to do it.
>> LUCAS FREITAS: Because you don't know how many times that's
going to keep going.
>> LAUREN CARVALHO: But in a for loop, you need to know how many times.
Yeah?
>> AUDIENCE: Just to clarify, the percent says this is something about to
happen, the i's for the integer, and the backslash n--
>> LAUREN CARVALHO: Means?
Who knows what the backslash n means?
New line, yeah.
Exactly.
>> LUCAS FREITAS: You can think of n as new line.
>> LAUREN CARVALHO: Nice.
>> AUDIENCE: What's that count dash dash [INAUDIBLE]?
>> LUCAS FREITAS: Count minus minus is the same thing as plus plus, but
instead of increasing, it's decreasing by 1.
>> LAUREN CARVALHO: Any more while loop questions?
No?
Oh, we were supposed to have skipped that.
>> This is a do while loop.
Very similar to a while loop except in this case we check the condition after
the code in the body of the loop executes once.
So we execute the code first no matter what.
Then we check the condition, execute it again if the condition is true.
>> You'll find yourself using this when you're asking for user input.
Because you're always going to have to ask once, and then depending on what
the user input is, you might have to reprompt and ask again and again if
the input was not what you expected it to be.
>> AUDIENCE: So it's like a form for people to fill out.
>> LAUREN CARVALHO: Yeah, exactly.
You'll find, I think, in your very first Pset, you're going to have to
use a do well loop to collect input from the user.
>> AUDIENCE: Depending on the situation, your while loops will tend to run more
often than while loops, like by one or something?
>> LAUREN CARVALHO: If it's the exact--
>> LUCAS FREITAS: They should always run the exact amount of times that you
want it to run.
Like we use do while because we really wanted to get the input at least once.
In while loops, sometimes you don't want to execute it at all if it's not
the case to execute.
So it depends on the design.
I mean, you can either use do while or while as long as your code does that
it's supposed to do.
>> LAUREN CARVALHO: But I do think the answer to your
question is probably yes.
Let's say the condition was false.
In one case it would run once, and another case it would run zero times.
So, yeah.
>> Any more questions?
Yes?
>> AUDIENCE: Can the loop condition be a for loop in terms of a variable other
than [INAUDIBLE].
>> LAUREN CARVALHO: Yeah, it can be anything.
Probably for the beginning Psets, you won't have to encounter that case, but
definitely it can be anything.
>> LUCAS FREITAS: I think we're done here.
>> LAUREN CARVALHO: Are we?
No?
>> LUCAS FREITAS: Loops?
Yeah.
>> LAUREN CARVALHO: We should not be.
Oh, we are done with loops.
OK.
>> Actually, because we've been talking so much, let's actually all navigate
over to study.cs50.net/conditions.
And you're going to navigate down to the problems widget.
Well, actually, while people are getting this set up, the CS50 Study is
going to be a tool that your TFs will use to prepare for sections and that
you guys can use if you miss sections.
It's going to contain a bunch of slides, some tips and tricks for each
topic, and some practice problems with solutions.
>> Why doesn't everyone just take a look at this problem right here.
>> I'm sorry, I should have reminded people to bring laptops today.
But if you don't have a laptop, that's OK.
You can just jot this down on a piece of paper or look over
your neighbor's shoulder.
>> Let's try and write a program that prompts the user to take a guess, the
average number of children per family unit in the United States.
That number is 2, by the way, as you can see from these examples.
>> We want to print out average number of children per family in the US, then we
want to what?
We want to collect input from the user.
And then depending on what the user's input is, you're going to print out
either this is correct or this is incorrect.
>> And the very first thing that you'll see, if you click the little arrow at
the bottom of the problem widget is a spot to jot down some pseudocode.
So you're not programming yet.
You're just getting your ideas in order.
>> Does anyone want to help Lucas come up with the right
pseudocode for this problem?
>> LUCAS FREITAS: What is the first thing that we should probably do here?
What do you guys think?
>> AUDIENCE: [INAUDIBLE]
>> LUCAS FREITAS: Yeah, OK.
So, print.
Just say the print this average number of people?
OK.
Print question, let's say.
After I print this question, what should I do?
>> AUDIENCE: Get input.
>> LUCAS FREITAS: Get input.
Anything else?
>> AUDIENCE: Condition.
>> LUCAS FREITAS: Condition.
>> LAUREN CARVALHO: What is that condition going to be?
>> AUDIENCE: The number of [INAUDIBLE].
>> LAUREN CARVALHO: Right.
>> LUCAS FREITAS: So what am I going to check in the condition?
>> AUDIENCE: If it equals 2.
>> LAUREN CARVALHO: If it equals 2.
Exactly.
>> LUCAS FREITAS: Is there anything else that I should do?
>> AUDIENCE: Count.
>> LUCAS FREITAS: So if it's equal to 2, let's say--
let me just write if input equals to 2, then what I'm going
to do is print correct.
And then else, I'm going to print incorrect.
Anything else?
No?
>> LAUREN CARVALHO: So you can click on the--
does everyone have this down?
>> LUCAS FREITAS: Does it make sense?
So now let's try to code this.
>> LAUREN CARVALHO: If you click the little arrow, it actually saves your
pseudocode, which is really exciting.
And you can make those into comments if you want just to remind yourself of
what to do.
>> LUCAS FREITAS: So for example, first I'm going to print the question, so
how am I going to do that, actually?
printf.
So printf, and the question is average number of children per
family in the US.
>> Then I'm going to get input from the user.
So how can I do that?
>> AUDIENCE: Get Int.
>> LAUREN CARVALHO: GetInt.
>> LUCAS FREITAS: OK.
So first declare, right?
int, let's call it n equals GetInt.
>> And now the condition.
So let me say that I'm going to here check answer.
So first, if input, which is n, equals to-- how am I going to write that?
>> AUDIENCE: Equal equal.
>> LUCAS FREITAS: Perfect.
So n.
And notice that I do a space, equals, equals, space, true.
And now what should I do?
Something very important for style.
The curly braces.
>> And then I'm going to print.
How do I print?
printf.
>> AUDIENCE: Could it be capitalized?
>> LUCAS FREITAS: No.
It's supposed to be all lowercase.
So that is correct.
>> Would this suffice for the printf statement, or am I missing anything?
>> AUDIENCE: New line.
>> LUCAS FREITAS: New line, yeah.
And now else.
>> AUDIENCE: Is there a space after the printf, or no?
>> LAUREN CARVALHO: We should check the style guide.
>> AUDIENCE: It shouldn't be.
>> LUCAS FREITAS: Shouldn't be, OK.
So else, printf, "That is incorrect."
>> LAUREN CARVALHO: Oops, your quotes.
>> LUCAS FREITAS: OK.
Anything else?
Does it look good?
>> LAUREN CARVALHO: Shall we try it?
>> LUCAS FREITAS: Yeah, let's run it.
So it's compiling, and it seems that it works.
Let's try two children.
That is correct.
And now let's run again, but now instead of putting two children, let's
say that I'm going to put like five.
That is incorrect.
So it seems that it works.
>> LAUREN CARVALHO: Why don't you show them what happens if you put something
weird, like some letters.
>> LUCAS FREITAS: Oh, yeah.
Let's also check that.
What if the user is trying to mess with your program, and they just
write, I don't know two.
It's going to say retry.
>> LAUREN CARVALHO: So GetInt is asking them to retry.
We didn't have to take care of that ourselves.
>> LUCAS FREITAS: Yeah.
So unless I actually put a number, it's going to keep asking me to retry.
>> AUDIENCE: How did you run it?
>> LUCAS FREITAS: Just click this star button.
>> LAUREN CARVALHO: You should go up.
There you go.
>> LUCAS FREITAS: The star button right there.
Yeah?
>> AUDIENCE: Does it prompt [INAUDIBLE] with decimal numbers too?
>> LAUREN CARVALHO: I believe it will.
>> LUCAS FREITAS: If it does what?
>> LAUREN CARVALHO: If they type in a decimal number, I think
it's going to prompt.
>> LUCAS FREITAS: Let's see.
>> LAUREN CARVALHO: Because it's not an int.
>> LUCAS FREITAS: Yeah.
>> LAUREN CARVALHO: So it it's not an int, Get Int will ask you to retry.
>> AUDIENCE: So where in that code would be the decimal place that prompts the
user that they have to enter an integer specifically?
>> LAUREN CARVALHO: GetInt is actually doing that for us.
>> AUDIENCE: No, I mean if we wanted to enter [INAUDIBLE]
>> LUCAS FREITAS: You can say like average number of children in the US.
I don't know.
We can say, for example, "Please input an integer."
>> LAUREN CARVALHO: An integer equal to the average number.
I mean, it's just how you word it, right?
>> LUCAS FREITAS: Yeah.
This is not part of actually coding.
It's more of like whatever you're going to want to
write in your program.
>> AUDIENCE: How would we have run it if it was actually in gedit [INAUDIBLE].
Instead of pressing play?
>> LUCAS FREITAS: We will first compile it.
We can do it by doing make and the name of the file.
And then to run it, we'll do dot slash and the name of the program.
>> AUDIENCE: I guess what I meant was if we wanted the program to respond to
their incorrect, invalid entry, you shouldn't put in--
>> LUCAS FREITAS: We will not be able to do that because the code this says
retry comes from Get Int which is a function that is already implemented
for us via the CS50 library, and since it's already implemented there, we
cannot just [INAUDIBLE].
>> LAUREN CARVALHO: If you don't use GetInt, then you can do that.
>> LUCAS FREITAS: Yeah.
But if you're using GetInt, you can't.
Any other questions?
>> AUDIENCE: Could you have gone with like GetFloat and then be able to put
it in a decimal?
>> LUCAS FREITAS: Yeah.
>> AUDIENCE: OK.
Then you just could input float a equals--
>> LAUREN CARVALHO: Yeah.
Exactly the same except a float instead of an int.
>> LUCAS FREITAS: Just put it here and then GetFloat there.
>> AUDIENCE: As you will see or have seen--
>> LAUREN CARVALHO: we have not gotten there yet.
>> AUDIENCE: --doing inequality with floats doesn't [INAUDIBLE]
>> AUDIENCE: What do the semicolons mean?
>> LAUREN CARVALHO: The semicolon just means that this statement is complete,
and we're going to go onto the next slide.
>> AUDIENCE: So it's necessary every time?
>> LAUREN CARVALHO: Yeah.
Well, OK, so not for these conditions, because they're
encapsulating something.
So basically if something is causing you to then create a curly brace
block, don't put a semicolon at the end.
>> LUCAS FREITAS: But everything else, yes.
When you finish a statement, put the semicolon.
And if you forget semicolons, the compiler's going to get angry at you,
and you're going to have to try to fix it.
>> LAUREN CARVALHO: If I put a semicolon here, that would stop this, and I
would never get to execute what's inside, by the way.
So it's one of those common bugs.
>> AUDIENCE: Always executing.
>> LAUREN CARVALHO: Yeah.
In this case, we would always execute what's inside.
>> AUDIENCE: Your style [INAUDIBLE] attach that for you, thought.
>> LAUREN CARVALHO: Oh, that's true.
>> AUDIENCE: [INAUDIBLE] single line thing [INAUDIBLE]
>> AUDIENCE: Yes?
>> AUDIENCE: Can you have multiple inputs in one line?
So like can you ask users to print two things?
>> LAUREN CARVALHO: Not using--
>> LUCAS FREITAS: No.
Let's say that I also want to get this integer m.
I have to do it in a different line.
>> AUDIENCE: If you were to run that, how [INAUDIBLE]?
It would print the prompt there [INAUDIBLE]
and then you'd enter one and--
>> LAUREN CARVALHO: Well, run it.
I mean, there's nothing stopping us from just running it and seeing what
would happen.
>> LUCAS FREITAS: I think it's going to complains at us
because we're not using--
>> LAUREN CARVALHO: Oops.
>> LUCAS FREITAS: It complained at us right now because we're not using this
variable, but if I did, for example, n is equals equals to, and m equals
equals to, then what will happen in this case?
What would this program do?
>> AUDIENCE: I'm just wondering how it would--
>> LAUREN CARVALHO: She just wants to know how it looks
like, I think, right?
So it took one of them, and then it took the second one.
>> I think we should move on, because we have one more
slide show to go before--
do you want to pull up math?
>> LUCAS FREITAS: Yeah, math.
>> LAUREN CARVALHO: Give me just one second.
>> There are a few different numerical variables in C. Numerical data in C
can be classified into different types, and these are the ones you'll
see the most often.
>> And int is a whole number.
You can't have any decimals.
In the Appliance, it takes 32 bits to store an int.
>> A float can have decimals.
It also takes 32 bits to store a float in the Appliance.
>> A double is a number with a decimal point that takes 64 bits to store it,
which means that you can just have greater precision because
you can have more--
what do you call it?
>> LUCAS FREITAS: Yeah, that's more.
>> LAUREN CARVALHO: More places after the decimal point.
And a long long is a whole number that takes 64 bits to
sore it in the Appliance.
>> LUCAS FREITAS: So it can be larger than ints can be.
>> LAUREN CARVALHO: The thing about data types in C is that they tell you what
kind of data you can store in your variable.
They tell you how you can use this variable in your program.
It's very important to just keep track of what data type you're using, and if
you do need to cast from a data type to another, make sure that you know
exactly what happens when that casting takes place.
And you'll see an example later on in these slides of a
mistake that can occur.
>> Let's take a look at some math in C and think really carefully, I guess,
about what's going on when this math is taking place.
In the very first line, what's happening when I declare x?
>> AUDIENCE: You're allowing x to be something?
>> LAUREN CARVALHO: Yeah.
Well, I'm really asking the operating system for 32 bits of memory, right?
In which I can store an int, and I'm going to call that int x.
>> LUCAS FREITAS: Basically going in memory and finding a place that has 32
bits and saying, hey, I want this space, and it's going to be called x.
So you're just pretty much finding a box in your memory to
store values for x.
>> LAUREN CARVALHO: And then I initialize x, which means that I just store the
value of 2 in that place, that chunk of memory, that we asked operating
system for.
And down here what we're doing is we're actually declaring and
initializing y in one line instead of in two lines like we did up here,
which is also a perfectly fine, We're adding 1 to x, and we're storing that
value in y.
>> LUCAS FREITAS: So basically what this line does, it finds now a box for y,
and then its value, the thing that you're going to put inside the box, is
going to be the value of x plus 1.
So it has this box for y.
It gets the value of x, looks here, and see that it's 2.
Put it there, plus 1, so it puts a 3 there.
Does that make sense?
So y would be 2 in this case.
>> LAUREN CARVALHO: All right, cool.
We saw some addition, so let's now think about division in C. Here I'm
declaring and initializing a float called answer and dividing 1 by 10,
and then I'm printing out the answer to two decimal places.
And note here, in my printf statement, when I want to print something to two
decimal places, %.2f is what I want to use.
So the point 2 is just saying I need two places after the
decimal printed out.
>> Now, what's going to happen if I run this code?
Does anyone have an idea?
What would print out?
>> LUCAS FREITAS: What are we expecting to get.
>> LAUREN CARVALHO: What do we want, first of all?
>> LUCAS FREITAS: [INAUDIBLE]
>> LAUREN CARVALHO: 0.1, yeah.
But what will print out?
>> AUDIENCE: [INAUDIBLE]
>> LAUREN CARVALHO: Well, no, actually.
This is what I was talking about why you need to keep very careful track of
your data types.
So, what data type is this?
Is an int, right?
There's no decimal place.
10 is also an int.
There's no decimal place.
>> If I divide an int by an int, I get an int which is not 0.1, because ints
can't have decimal places, but just 0.
Because an int just truncates off the decimal places.
>> LUCAS FREITAS: So whenever you do operations with ints, you're going to
get an int as the answer.
So basically, what the computer is doing, it's divides 1 by
10, and it gets 0.1.
But then it's like, wait, this is supposed to be an integer, so you just
ignore the 0.1, so it's now just 0.
>> LAUREN CARVALHO: So when I print this out, I get 0.00 rather than 0.10.
>> LUCAS FREITAS: Does that make sense?
>> LAUREN CARVALHO: That's kind of unfortunate, right?
>> LUCAS FREITAS: So how can we fix this?
>> LAUREN CARVALHO: Let's try and fix that.
Here's an example of where I'm actually casting the 1
and the 10 to floats.
So now, 1 is no longer an int.
1 is a float.
By the way, this is just the syntax for when you want to cast from one
data type to another.
You can just put the data type in parentheses and have that just precede
the value that you're casting.
>> So here, I'm dividing a float by a float to get a float answer.
And yes, this would in fact print 0.10.
Does that make sense?
>> LUCAS FREITAS: So type casting is just converting from one type to the other.
So float 1 is actually 1.000.
Not exactly this, but it's something with decimal places
that is actually 1.
Does that make sense?
Yeah?
>> AUDIENCE: What if you did float answer equals 1.0 divided by 10?
>> LAUREN CARVALHO: You're actually going to see that in the next slide.
That's actually our very next slide.
This is actually a nicer than type casting, I think.
So these two are floats just by nature of the fact that they
have decimal places.
Yes?
>> AUDIENCE: What if you did [INAUDIBLE]
divided by 10?
>> LAUREN CARVALHO: Actually a float divided by an int actually gives you a
float, so that would work fine.
>> Wait, does everyone understand this?
Because this is going to be a really big deal in Pset1, actually.
Yes?
>> AUDIENCE: Could you just quickly go over the red part?
>> LAUREN CARVALHO: The red part, yeah.
Lucas, do you want to?
>> LUCAS FREITAS: OK.
So remember that the percent is a placeholder.
It's like a blank space.
So let's try to understand everything that's going on here.
Percent means that it's a placeholder.
Then you have the f, which means that this placeholder is for a float.
The last thing that we have here is a .2, which means I want to have two
decimal places for the float.
>> If I just had %f, they it will put a bunch of decimal places.
So I can just put .2 or .1 or .5, or whatever, and it's going to put that
number of decimal places.
The only thing is that if you put like .100 or .1,000, it's not actually
going to put all those decimal places because it doesn't have enough memory
to have that precision.
>> Any other questions?
Yeah?
>> AUDIENCE: So float just allows you to go beyond integers?
>> LUCAS FREITAS: Yes.
>> LAUREN CARVALHO: Well, a float just allows you to have decimal places.
>> LUCAS FREITAS: It just means that you now can have decimal numbers.
>> AUDIENCE: Why would we ever use integers?
>> LUCAS FREITAS: Why would you use integers?
Maybe if you're counting from 0 to 10, you want to use actually an integer.
You don't need to use a float.
In some situations, you don't have to actually--
>> LAUREN CARVALHO: Maybe you don't want decimals.
>> LUCAS FREITAS: Maybe you don't want to use a float.
>> LAUREN CARVALHO: The other thing is, remember, both put a float and an int
take 32 bits to store them, and maybe you don't want to waste bits on the
decimal places.
>> LUCAS FREITAS: Yeah.
So like, just think about this.
You have 32 bits for both an int and a float.
For an int, all those 32 bits go to the integer part.
If you use a float with 32 bits, you have part of this memory being used
for the number, for the whole number, and the other part being used for the
decimal places.
So that means that you cannot get to very large numbers with floats because
the float's already using a lot of memory for the decimal places.
Yeah?
>> AUDIENCE: That first answer, what exactly is it doing?
Is it setting the value of float equal to that placeholder to that you can
recall it in the next line?
It says float answer.
>> LUCAS FREITAS: Yeah.
So, float answer means you're getting now a little bucket in the memory that
holds a float, and that's going to be called answer.
So that's what float answer does.
>> And then you say that inside of this, you're going to put
the result of this.
So you're going to do 1.0 divided by 10.0, you get 0.1, and then you put
that inside of answer.
>> LAUREN CARVALHO: So in one line, we're declaring and initializing this float
called answer.
>> LUCAS FREITAS: Interestingly, although this is just one line, it's doing
multiple operations.
It's just a way of C letting you use less code.
It's the same thing as the ternary operator, how you can just do an
[INAUDIBLE] in one line.
It's actually doing the same thing as doing float answer, semicolon, and
then answer equals this, but instead of making you do two lines for that,
it's like a shortcut.
Yeah?
>> AUDIENCE: So if you ran this, it would be 0.10?
>> LUCAS FREITAS: Yes.
Yeah?
>> AUDIENCE: Is answer actually code, or is it just the name?
>> LAUREN CARVALHO: This is actually code.
This is what we're going to name our variable.
>> AUDIENCE: So it's just a name [INAUDIBLE].
>> LAUREN CARVALHO: Yeah.
I could have said float x equals 1.0.
>> LUCAS FREITAS: The thing is that the name of the variable is
something that you use.
It's important for you.
For the actual program, the only thing that matters is the value that you
have inside of the bucket.
The reason why you have variables and names of variables is so you'd know
what you're dealing with, like what kind of data you're
accessing at a time.
But for the code itself, if you call it answer or question or anything,
pretty much, it doesn't matter.
It doesn't matter.
As long as you're consistent in every occurrence it and so you call it
something else.
Yeah?
>> AUDIENCE: Do the variables go beyond the function?
Like if you were to create a bracket [INAUDIBLE], could you just
[INAUDIBLE] answer again?
>> LAUREN CARVALHO: No.
>> AUDIENCE: [INAUDIBLE]
>> LAUREN CARVALHO: No.
That would be outside of the scope of this particular variable.
And actually, that's kind of interesting, because when we have
things like loops or these if conditions, if you declare a variable
within another set of curly braces, it really only exists
within those curly braces.
>> AUDIENCE: One last note about the [INAUDIBLE] floats.
Why would you ever want to use ints?
This is more of a side note than anything, but your computer is
significantly better at handling ints than it is at handling floats.
It's like an order of magnitude faster.
So if we just handled only floats, things would be a lot slower.
>> LAUREN CARVALHO: Actually, there seems to be a problem with floats.
>> LUCAS FREITAS: Kind of the reason for that, you can think, is how in binary
you can represent integers by putting 0's and 1's.
>> LAUREN CARVALHO: That's the next thing.
>> LUCAS FREITAS: OK.
Awesome.
>> LAUREN CARVALHO: Just hold that thought.
>> LUCAS FREITAS: OK, I'll tell you later.
>> LAUREN CARVALHO: Before we get to what Lucas was just saying, operator
precedence.
This is common sense.
You guys have done this enough.
So, just if something's in parentheses, it will happen first.
It's multiplication takes precedence over addition,
subtraction, things like that.
So actually, just does anyone want to tell me what number one actually
evaluates to?
>> AUDIENCE: 27.
>> LAUREN CARVALHO: 27?
Yeah.
>> LUCAS FREITAS: Let me give candies to people.
>> LAUREN CARVALHO: All right.
What about number two?
>> AUDIENCE: 22.
>> LAUREN CARVALHO: 22, yeah.
I don't know who got that.
Number three?
>> AUDIENCE: 10.
>> LAUREN CARVALHO: 10, yes.
Cool.
So modulo.
Lucas, do you want to explain modulo?
>> LUCAS FREITAS: Modulo is this operator that is actually very useful for a
bunch of problems.
If I do 55 modulo 10, what it does this let's say that I want to
divide 55 by 10.
What do I get?
It feels just like if I'm actually just doing the division, what is the
first time that I got the integer?
>> AUDIENCE: 5.
>> LUCAS FREITAS: 5.
And then the remainder is 5, right?
So it's like 5 times 10 plus 5.
Basically, what modulo does, it tries to divide this number by this one, and
it sees what is the remainder of the division.
>> LAUREN CARVALHO: So it just returns the remainder.
So number one is what?
5.
Number two?
>> AUDIENCE: 3.
>> LAUREN CARVALHO: 3.
Number three?
0.
And four is 1.
Exactly.
>> LUCAS FREITAS: Simple, right?
>> LAUREN CARVALHO: Why don't we skip this just because we don't really have
time, and people seem to understand.
So here's what Lucas was just talking about.
>> LUCAS FREITAS: Another reason why I would use ints instead of floats is
that floats are very imprecise.
And why are they imprecise?
David said that to represent an integer, for example, we use 0's and
1's, right?
And then you basically do the multiplication, do all those
operations to know what decimal number you have instead of the binary.
>> What happens now if I try to put decimal points?
Actually, for example, if I have the number like 3.33333?
It feels like 10 over 3.
How am I going to store all the 3's that I have after that?
It's really hard, right?
You cannot just tell the computer, hey, I have a 3, and
then you do that forever.
Actually the computer is not smart enough for this.
>> So basically all your computer does is that it tries to put as many digits as
it can up to the memory that you have.
So let's say that it's going to say 3.3333, and that's it.
So it's not able to actually express precisely what that float is.
>> LAUREN CARVALHO: We have a finite number of bits, which means that not
every decimal number is going to be able to be represented precisely.
And so in this example, if we are printing out 0.1, we're now printing
out 20 places.
>> LUCAS FREITAS: Just like I was telling you guys, you could put any number.
But if you just did the two places, you'd get 0.10, but if you put 20
places, it's going to show you a bunch of 's because this is what you can put
in the memory, right?
>> But right here, up to the 0, and then is just put it a
bunch of random numbers.
This is kind of like some--
>> LAUREN CARVALHO: This is because we're not able to represent this decimal
precisely with the number of bits we have.
>> LUCAS FREITAS: And then the computer is basically
giving you a trash number.
This is not actually the value of the number.
>> AUDIENCE: It's as close as it can get [INAUDIBLE]
using the [INAUDIBLE].
>> LAUREN CARVALHO: So the implication of this is that let's say that instead of
being 0.100014, whatever, what if it was 0.0999--
well, OK.
Let's pretend that the actual answer to this was 0.9999999.
That's very close to 1, but it's not exactly 1.
It's 0.99999.
>> That means that if I take that 0.999, and I cast it to an int, I get 0.
I don't get 1.
>> LUCAS FREITAS: Because remember, casting from float to int, you just
ignore all the decimal places.
You just ignore them.
There's no such thing as rounding up or running down when you typecast.
Actually, there is this function that does rounding, which is very
important, but you just think that when you do typecasting, it's just
going to delete everything.
So even if you have 0.99999, it's going to just give 0 if you typecast
[INAUDIBLE] an integer.
>> LAUREN CARVALHO: So just be really aware of floating point in precision,
especially when you start to use floats in Pset1.
But the walk through should warn you of that as well.
>> LUCAS FREITAS: And actually it's really hard, for example, if I did
float answer 1 equals 1.0 over 10, and then float answer 2
equals 10.0 over 100.0.
They are the same number, right?
0.1, right?
1 over 10 or 10 over 100.
But now if I try to see the equality, like if answer 1 is equal to answer 2,
maybe it's not actually going to be the same thing.
So floats are kind of like that.
They're like doing equality.