>> [MUSIC PLAYING]
JASON HIRSCHHORN: OK, everyone.
Welcome to Section.
This is CS50, our very first super section.
As you all know, next week regular sections will start,
but this week we're all together.
>> There is myself.
My name is Jason.
I'm a lecturer in Computer Science.
We have Andi, who's the head TA of CS50.
And Scaz, Professor Brian Scassellati, he's a professor in Computer Science.
>> We are the CS50 heads at Yale.
You can always email us heads@cs50.yale.edu.
We will be at lectures.
We'll be at office hours.
If there's ever anything we can do for you, anything you need,
feel free to reach out to us.
>> So what are we going to do today?
Or before then, what's coming up this week?
So office hours are Monday, Tuesday, Wednesday, and Thursday, 8:00 to 11:00.
There's a schedule on the website.
>> Sections, like I mentioned, are starting next week.
And you'll find out this week what time your section is, what day it is,
and who your TA is.
>> Problem Set 1 is due at Thursday at noon, Friday at noon with a late day.
Yes, you have nine late days.
And there are nine problem sets.
And can only use one late day per problem set.
Yes, in effect, all problem sets are due Friday at noon.
That is all.
All that details are on the syllabus.
>> Every Friday, we have lunches.
We like to make this large course feel a bit smaller.
So you're welcome to RSVP.
Free lunch with fellow classmates and staff.
cs50.yale.edu/rsvp.
>> All of these things that I have on the screen,
and more, you can find cs50.yale.edu or cs50.harvard.edu, if you go to Harvard
and are watching this online.
>> Also on those websites, there are a ton of resources for you.
Every lecture not only has the video of the lecture, but also notes.
Somebody will watch the lecture and take notes for you.
So you can just pay attention during lectures,
or you can use their notes/our notes to supplement your own notes.
>> There are slides online.
The source code-- everything David goes over in lecture,
or Scaz goes over lecture, that source code is available online
as well, as like I, mentioned videos.
>> Sections, likewise, will all be filmed.
All of those slides will be available.
All of that code will be available for you.
There are shorts on the website where CS50 staff members spend five to 10
minutes going through topics for you.
Some of those you've already encountered in problem sets.
>> Office hours, as I mentioned, Mondays through Thursdays, 8:00 to 11:00.
CS50 Study is a fantastic resource.
There are practice problems, sample slides, known bugs
that students like to run into, for essentially every topic we
will cover in this course.
>> And finally, on this slide at least, Reference50
which gives you information on all of the C functions
you could ever possibly hope to use and many, many more.
>> Again, that dot dot dot at the bottom of the screen
is to indicate that there's even more resources on those two websites:
cs50.yale.edu, cs50.harvard.edu.
>> So, what are we doing today?
Well, first I'm going to give you look at the resources.
Already did that one.
Look at us.
>> Now, we're going to go over data types and operators in C.
Then Andi will come up here and go over libraries in C, Printf(),
a function with which you are already familiar or will quickly become very
familiar.
And you'll also go over conditionals and loops.
>> And then finally, Scaz will go over the CS50 ID.
If you have not dove in already in Problem Set 1,
as well as how to use the terminal and useful commands
and then give you some demos and practice coding in the CS50 ID.
>> Before I jump in to this content on this side,
does anybody have any questions so far?
Great.
OK.
Changing gears.
>> [CHANGING GEARS]
>> That was me shifting gears in a manual car.
So data types in C, you cannot just have a variable x and have it be on line 10,
an integer, maybe the number 1, on line 20, a decimal number 10.5,
and then a couple lines later a string, I love CS50.
>> In C, variables can only be one thing, and one thing only.
And so you have to give them types.
And on this board, we have a list of types.
There are chars, which stand for characters.
So that's one letter A, B, C, D. That can also be a space,
or that can also be a new line character.
>> There are integers, which are just numbers, whole numbers-- or integers
rather-- but no decimals.
>> For decimals, we have floating point numbers.
Floating point numbers are numbers with decimals.
>> Then there are longs, which are essentially longer integers.
You'll notice that a long is 8 whereas an int is 4.
I'll get to that in a second.
That's because longs can store even more integers than an int can store.
Double is a long float.
>> And finally, we have string, which is a type
that you have probably used before.
If you include hashtag #includeCS50.h in your source file,
then you can use a string type.
It's not actually built into C.
>> There are a couple other types as well.
But these are the principal ones with which you will use and encounter.
C as, again I mentioned, every variable-- it
can only be one type and one type only.
That's because C is a statically typed language, as opposed
to dynamically typed languages where if you create a variable
you can change what's stored in it as you go on in your program.
Over here on the right hand side, I have the different sizes
of these types of data in bytes.
So a character is 1 byte.
That's 8 bits.
And that means because 8 bits there's eight 0s and 1s.
We saw those demos in the first week with that binary bulbs.
>> With 8 bits or 1 byte, we can represent numbers from 0 to 255.
Right.
If all 8 bits are 0 that's 0.
If the first bit is 1, that's the number one, and so on, all the way up to 255.
And that's why for characters you can have essentially up to 255 of them,
but that's plenty to cover all the characters we need to use.
>> For integers, you can have 0 and then you have 2 to the 32 minus 1.
Those are how many options you have for integers.
>> For a long, you have 0 to 2 to the 64 minus 1.
So you have many, many, more options for longs.
>> Strings, that's a question mark because that's a cliffhanger for later.
Yeah.
I could tell everybody's on the edge of their seat,
like what's that question mark?
It's a cliffhanger.
We will eventually cover the size of strings and talk more about strings.
Because string is actually one of the training wheels
we put on in this course at the beginning
and then take off later on, because strings in C
are actually pointers to character arrays.
But again, that's a cliffhanger for later.
Before I move on any questions, comments, concerns about this slide?
>> OK.
I must be a fantastic explainer.
Also when you're programming, you're going to use these operators.
Really simple signs that can do simple operations, with which you're probably
already very familiar.
>> For example, on the right hand side, we see add.
To add, you just do a Plus sign.
And so you might have two variables x and y.
You can do x + y in your code.
Maybe you want to-- say you have the number 7 and 3,
you want to store that in a variable called sum.
You can do int for the type, space sum, the name of the variable, = 7 + 3;
>> What would that store in sum?
Anyone?
If I had in sum = 7 + 3.
What would be stored in sum?
You can just shout it out.
AUDIENCE: 10.
>> JASON HIRSCHHORN: 10!
That's exactly right.
What about in sum = 7 - 3, I just use that dash sign.
What's going to be stored?
AUDIENCE: 4.
JASON HIRSCHHORN: 4.
Sum is probably the wrong name for that variable, but 4 would be stored.
>> To multiply in C, use the little Star character, you don't use an x.
To divide, use a Forward Slash not the division symbol.
And to modulo, use the Percent symbol.
>> So let's say I want to see int z = 7 % 3, what would be stored in z?
So, that's essentially asking what does modulo do?
Does anybody know what modulo does?
Yeah.
y 4.
SPEAKER 1: It's the remainder.
JASON HIRSCHHORN: It is the remainder when you divide.
So 7 divided by 3 is 2 remainder 1.
So 1 would be stored.
>> You did subtraction, but that's how it works.
That's what the modulo operator does.
It takes a number divides it by another number and returns to you
the remainder.
So again, 7 % 3 gives you 1, because 7 divided by 3 is 2 remainder 1,
and returns the remainder.
>> What about let's go back up one step to that division sign.
If I did 7 / divided by 3 does anybody know what that would return?
>> SPEAKER 2: 2.
>> JASON HIRSCHHORN: Why 2 and not 2.333?
>> SPEAKER 2: I think it cuts off after the whole number.
JASON HIRSCHHORN: So that's exactly right.
In C, if you do division of two integers 7 divided by 3,
it gets the answer which in this case 2.3333 forever
and it finds that decimal point and chops everything off after the decimal
and it just returns you 2.
>> So if I did 8 divided by 3.
That actually, we know that to return 2.666,
but it chops everything off at the decimal, truncates it all,
and just returns to you 2.
So 6 divided by 3, 7 divided by 3, 8 divided by 3,
are all going to return to you 2.
>> Does anybody know, or have a guess, as to how I could actually get
the full answer if I want that decimal?
Yeah.
Go ahead.
SPEAKER 3: Use a float?
JASON HIRSCHHORN: What do you mean?
SPEAKER 3: Because you said the float is for numbers [INAUDIBLE]
JASON HIRSCHHORN: Right.
So, that's exactly right.
7 is an integer.
But if I wanted to turn that into a floating point number,
I would have to store that as 7.0 because the computer
is really stupid-- we saw that with the PB and J example--
it will only do exactly what you tell it.
>> So if you write 7, it think that's an integer.
If you write 7.0, even though we know those things are equivalent,
it treats that like a floating point number.
So if you did 7.0 divided by 3, or divided by 3.0,
it would say, OK, now we're dealing with floats.
I will return to you 2.333333 forever.
But not really forever, because as we saw also in lecture,
these floating point numbers aren't exactly precise.
>> So if you do want that decimal, or a part of that decimal,
then you have to use-- one of them has to be a floating point number
and the computer has to understand that this is a floating point you're
dealing with, not an integer.
Any questions on that table on the right hand side, so far?
Or your left hand side, your left, your left hand side.
Yeah.
SPEAKER 4: Right.
For just like a regular integers, you would
write-- would you have to write float?
>> JASON HIRSCHHORN: Yeah so.
Well, if you want to create a variable that's a float,
you need to say float z equals something.
>> SPEAKER 4: OK.
>> JASON HIRSCHHORN: But if I wanted to do 7 divided by 3 and get that decimal,
I would do float z = 7.0 / by 3.0; and that would give me the floating point
number.
>> SPEAKER 4: OK.
>> JASON HIRSCHHORN: If I did something like int z = 7 / by 3,
that would give me an integer, because those are all integers.
Does that make sense?
>> SPEAKER 4: Yes.
>> JASON HIRSCHHORN: OK.
Great.
Any other questions about that table?
Really?
I'm so excited.
OK.
>> So some other things you will use, oftentimes in conditions or loops,
are these types of operators-- these types of Boolean expressions.
And as we learned, == is what you use to check if two things are equal.
>> So here I'm checking if x == y, let's again assume that x is 7 and y is 3.
So if I wrote 7 == 3, what would that return to me?
Is that true or false?
>> AUDIENCE: False.
JASON HIRSCHHORN: OK.
I feel like everybody can get this one.
So everybody, what would that return?
AUDIENCE: False.
JASON HIRSCHHORN: False.
Great.
7 does not equal 3.
So 7 == 3 would return false.
You do have a not equal sign, so if I checked 7 != 3, what would that return?
AUDIENCE: True.
JASON HIRSCHHORN: True.
Excellent.
Somebody was very emphatic in the back and appreciate that.
Then you have less than operator, less than or equal to operator,
greater than operator, greater than or equal to operator.
>> So one more sanity check.
If I had 7 is greater than or equal to 3.
What would that return?
>> AUDIENCE: True.
JASON HIRSCHHORN: True.
Yes.
That back room, back side of the room, fantastic.
You can combine these expressions if you like, as well,
with a logical AND which is && or a logical OR which is ||
||.
And so now you can test two things together-- Is that funny?
Why is that funny?
>> So if I wanted, I can say is 7 greater than 3 AND 2 is less than 4?
Well, if 7 is greater than 3, that's true.
2 is less than 4, that's true.
So this whole thing would return true.
>> If I tested 7 is greater than 3 and-- I'm just picking random numbers here--
4 is less than 2, well, that's false.
So true and false make false.
And you can go on and you combine as many conditions together as you'd like.
>> Does anybody have any questions, comments, concerns so far?
And I see some of you taking pictures of me in the screen, which I appreciate.
Hopefully, it's not Snapchat.
Hopefully, it's for your notes.
But all of these are going to be available online.
So you don't have to take pictures of this.
Like I mentioned, everything will be available online for you.
OK.
I'm about to get offstage, so does anybody
want to say anything before that happens?
Questions?
>> [INTERPOSING VOICES]
>> JASON HIRSCHHORN: Oh, stop.
You guys are too nice.
OK.
I'm going to tag out.
Let's go.
>> ANDI PENG: We're going to do an awkward mic change now.
>> JASON HIRSCHHORN: I'm going to take this off.
ANDI PENG: Appreciate the support, guys.
Can you hear me?
Is that good?
Perfect.
Beautiful.
Let me just tuck that in.
OK.
So, I'm about to do like a giant information dump on you guys right now.
And no worries whatsoever if you're not following every little line of what
I'm about to show you.
As Jason said, everything is completely online.
Just we're going to try to introduce everyone to the concepts covered
in some of these slides.
So just follow along.
No worries if you don't understand everything; however, if at any point
you feel lost, raise your hand, we'll stop, no worries.
Cool.
>> So I think something that David has already kind of mentioned in lecture
and Jason has kind of alluded to today is what a library is.
So in programming, we have these things called libraries.
Where, essentially, all they are is just a set
of functions, that is essentially just code that's
already been written by somebody else that we can all
use within our programs.
>> So how does a normal library work, right?
You enter the library door.
You have all these books you can pull out
and you can access information within those books.
Same thing in programming.
>> You have a library that has already been written by other people.
And the way that you can, as programmer or a student,
can gain access to that library is through hashtag #include.
>> For example, in C we have three C-- the most
commonly used three C standard libraries-- the input/output library,
the string library, and the math library.
>> So within your first couple problem sets,
those will be the three that you're primarily using.
So as David already explained in lecture,
the standard I/O library, or the standard input/output library
does exactly that.
It allows you, in your code, to input anything from the user and output that
to the screen, essentially printing it to the screen.
>> And so any time you use any functions, for example the function printf() which
we're going to go over the next slide, make sure to include the standard I/O
library or else you're not going to be able to use the printf() function.
Because you, as the programmer never actually wrote that function,
you're just using somebody else's code within your own code.
>> Does that make sense everybody?
Cool.
>> And then we essentially get onto the string.h library and the math.h
library, .h just signifies library, other libraries that you'll eventually
be using within your code.
>> However, for purposes of this library, we also
have-- of this class-- we also have what is called the CS50 library,
cs50.h, where we, for your perusal have created
many important and useful functions.
So that you as a programmer can piggyback off
what we've already written so that this function's available to you
for your use.
>> I'll be covering a couple of the functions commonly used,
but know that these are all googleable online.
Feel free, pull up the CS50 library and then you have all the functions there.
Yeah.
>> SPEAKER 5: So if you weren't using the software that
is provided to us by the class, does that
mean we'd have to download and find this header file ourselves and tell
the computer where it is?
>> ANDI PENG: Great question.
Exactly.
So it's like if you're not physically present at a library,
there's no way for you to actually go and access it.
So the same thing with programming in C, you
have to make sure that you're using our appliance because the library's already
been incorporated into it.
And then when you hashtag #include, the library's there.
Yeah.
Good question.
Everyone good?
Cool.
All right.
So we're going to hit, essentially, what is
the first function that we as programmers are
going to be using within our code.
This is called the printf() function.
So, printf() function, as I've already said in the last slide,
is included in the standard I/O, standard input/output, library.
>> So make sure whatever you're using-- oh, let me grab the laser pointer--
whenever you're using the printf() function,
you have to include the standard I/O library or else, when you compile,
you're going to get an error because the computer will be like,
oh I don't know where printf() is, you're not telling me where printf()
is.
Well printf() is included in the standard I/O, so when you use printf(),
make sure to have that line above your code.
>> So for example, printf() here is a function.
All it does is print out something inside the parentheses to the user.
Would anybody like to take a stab at what this line of code right
here is going to print out?
Hence, the answer's on the screen.
Anyone?
>> VERDI: The bottom.
>> ANDI PENG: Verdi, why don't you go ahead and say the whole statement?
>> VERDI: Hello, my name is Andi, and I am 20 years old.
ANDI PENG: Ah, lovely.
So in this line we're going to print out, Hello, my name is Andi,
and I am 20 years old, which is in fact a true statement.
>> Syntax.
So couple of syntactic issues that you guys want to make sure you know.
Quotes.
Double quotes tell the computer that everything inside
is going to be printed.
>> Everything inside can just be exactly how
you type it, except for commonly at the end,
we're going to want to include a hash-- oh sorry-- a dash n.
So a dash n.
>> Does anyone want to take a guess at what that does to our code?
Yeah.
SPEAKER 6: Puts it on a new line.
ANDI PENG: Exactly.
So all this does is allow whatever happens
after this, after this statement, to be printed to a new line.
So that when you're writing your code you don't randomly have things
tacked on to the end that you don't want.
That everything gets neatly printed one line
and then we start on the following line.
>> Does anyone remember what that semicolon does when we're coding?
Yeah.
>> SPEAKER 7: Statement.
>> ANDI PENG: Sorry?
>> SPEAKER 7: Does it end the statement?
>> ANDI PENG: Yeah.
So in C, or any programming language, the semicolon
denotes the end of a programming line.
So for example, in English we use period to say, oh this is the end a sentence.
In programming, same thing, we have a semicolon to denote the end of a line.
Oftentimes, when you're starting to program,
you'll realize you forget to add a semicolon
and then you try to run your code and it doesn't work.
And you're like, Ah, I don't know why, it should be working.
>> Chances are you probably forgot a semicolon or a bracket or something
somewhere.
So, that's important to remember.
Cool.
All right, show of hands, how many people here have ever
taken AP Computer Science or programmed in Java before, ever?
OK.
Lovely.
That will not be applicable then, but in Java, you
have something called System.out.println which does not exist in C.
>> So in C, whenever you want to add in variables
into anything you want to print out, there's
a specific syntax we're going to use.
That's called the placeholding-- essentially
we add placeholders in place of the integer or the variable
that we want to print.
>> So as you guys can see we've included a new library header file here.
The CS50 library.
And contained within that CS50 library is a common function we'll be using
in our course called GetInt().
>> Does anybody want to take a stab at what GetInt() may possibly be doing.
>> AUDIENCE: [INAUDIBLE]
>> ANDI PENG: Sorry.
Couldn't hear you.
Maddie, anyone.
MADDIE: Oh, it prompts you for an integer.
ANDI PENG: Exactly.
So this function, another function that's already
been written by somebody else that we can call upon now.
All it is is prompt the you, the user, to input whatever you want as the code
is running and it stores whatever-- in this case we're GetInt()-ing,
so that means we're obtaining an integer.
>> And we're going to store that in another integer
that we've just made called age.
That make sense to everybody?
Cool.
>> So now that we've stored this integer, that we've prompted from the user,
in this variable, we've created of type int called age,
we can go ahead and place that in our printf() function.
So the syntax usually for printf() is that wherever in your actual physical
line you want to include that integer, you do that symbol right there,
the percent symbol, with the type of variable that you want.
>> So in this case age is an integer.
So you're going to include %i because it's an integer.
And then after your statement, you're going
to do comma and the name of the variable.
>> So here, we're going to print out, Hello, my name is Andi,
and I am blank years old.
Hashta-- or, sorry-- , age with whatever I input.
So if I were to input 20 for my GetInt() here,
it would print out the exact same thing.
But, if I wanted to input something else, like perhaps 40 or 45,
then you would see that reflected accordingly in the code.
>> So this is an example of something in which
it prints out and looks to you as if it's the same thing,
but underneath the hood of the computer there's actually
very different things happening.
Cool.
All right.
So what if we want multiple variables?
Pretty easy.
Same thing.
We also have a new function here called GetString(),
also included within the CS50 library, that all it does is prompt the user
for a string, which is just a set of characters,
so like a sentence or like a name.
>> So in this case, we would have two placeholders %s for string and %i
for integer.
And we're going to follow that by the two variables
we want included, in the order that they appear in the sentence.
So for example, my name is blank, I want a name there,
so I'm going to have the name first.
And then afterwards, I want age, want to have age second.
And so if I wanted to input, Hello, my name's Andi, and I am 20 years old.
If I inputted Andi and 20, the exact same thing would print;
however, now we've got two stored variables of name as well as int.
Yeah.
>> SPEAKER 8: Would it be able to run if you switched
name and age at the end of that?
>> ANDI PENG: Yeah.
That's really good question.
So long story short, no.
Because name and age-- what type of variable is name?
>> SPEAKER 8: String.
>> ANDI PENG: And what type of variable is age?
>> SPEAKER 8: Integer.
ANDI PENG: So here we have a placeholder for string and integer, right?
So if you were to switch these, the computer's not going to know.
It's going to check for a string, and if you try to give it an int,
it's going to be like wait I'm confused, you
told me I should be allotting memory for an int.
And right here, when it expects an integer
and you give it a name and a string instead,
it's also going to be very confusing, it won't run exactly the way you need it.
So here, naming and syntax is very important for running code.
Everyone good?
Yeah.
Maddie.
>> MADDIE: Here, I know we've looked at examples in class, where they ask
what is your age, what is your name.
Here, would that be it doesn't-- if we were to run this code,
it wouldn't ask for that?
But you would just input two numbers and then it would run like that?
>> ANDI PENG: Yeah, exactly.
So if you wanted for it to display, please enter your age,
you can just add a printf() function that says, Please enter your age,
above it.
That's a good question.
Yeah.
>> SPEAKER 9: So, would already be included in GetInt() [INAUDIBLE].
ANDI PENG: No, it actually is not.
So all this does is just prompts an empty screen
to the user to input something.
If you want it, if you want it to print out something that tells the user like,
please give me your age, then you would have to printf() that yourself.
Because everyone uses this function for different things,
you could be storing age, you could be storing address,
you could be storing phone numbers.
And so it's really up to your guys' individual uses
for what you want it to say.
Yeah.
>> SPEAKER 10: So, just to clarify, where do you input the name and the age
so that it shows up in place of %s?
>> ANDI PENG: As in the user?
>> SPEAKER 10: Yes, like how do I make it-- where do I put Andi,
and where do I put 20?
>> ANDI PENG: Yeah.
So if you were to actually run this code,
I'm not actually running the code, this is just here right now.
If I were to run the code, I would compile the code, make the file,
run the code, and then there would just be two spaces for me to input it.
Yeah.
You guys will see when you play around with the code yourself.
>> All right we're going to move into the next section of what
we'll be covering today.
We're going to go over what conditional statements are.
So if you guys remember and recall from lecture,
conditional statements, all they are are a set
of instructions to the computer in which if a certain condition is true,
you execute the code inside of that condition.
>> So in Scratch-- essentially the big theme from today
is that you guys have already all seen the logic behind everything
that we're covering.
And all we're doing is translating something
that was very intuitive in Scratch, and hard coding it
into the syntax we'll be using for the class which is C.
So logically, all this block was is that piece of code right there.
Yeah.
OK.
>> We also get into an if...else statement which is just an added layer
of complexity to the if statement where the computer takes a look at this
and sees, if this condition is true, do whatever's inside these two brackets,
else-- so kind of like the default if it doesn't meet the condition-- do this.
It's like a fork in the road.
If it's raining outside, I put on a rain jacket, else anything
else I don't put on a rain jacket.
>> Does that logic make sense to everybody?
Cool.
All right.
So like a hard example of this that we would see in C
is if I wanted to create a variable called homework hours.
And if homework hours is less than five, I say Life is great.
It's wonderful.
However, say The struggle is real-- which is what we all on this Monday
afternoon up Science Hill are probably doing right now--
AUDIENCE: [LAUGHING]
ANDI PENG: They way we would, thank you for that.
The way we would hardcode this in C is if-- let's assume
we already have a variable of type int called homework hours right here.
If homework hours is less than five printf(), Life is great.
Remember keep /n because you want a new line after.
Else print, The struggle is real.
>> Does everyone understand how I transitioned from this block
into this block of code?
Cool.
All right.
>> So now we're going to take a look at multiple If statements altogether.
So let's assume the purpose of this program
was we prompt the user for a grade.
We prompt using GetInt() for a grade, and they input a value,
and you want to display what type of grade they got.
>> So if I were to design a program, I mean typically in all of our eyes,
90-100 is an A, 80-90 is a B, and so forth and so on.
What is wrong with this piece of code that it's not
doing what I want it to do.
Yeah.
>> SPEAKER 11: They have lower limits, but they don't have upper limits.
>> ANDI PENG: Exactly.
Did everybody hear what she said?
There's going to be upper limits, but no lower limits.
Sorry, other way around, lower limits, no upper limits.
So would you like to take a stab at saying
what would be printed on this screen if I were to run this code.
SPEAKER 11: An error?
ANDI PENG: An error?
Great guess, not quite right.
Does anybody have another stab?
Yeah, Aaron.
>> AARON: If you put in something greater than 90,
it would show all the grades you got.
It would show you got an A, you got a B, you got a C.
>> ANDI PENG: Yeah.
That's exactly right.
So, that'd be wonderful.
However, they are mutually exclusive, I think.
If I were to run this piece of code.
And then let's just say, I inputted the grade of 95.
So 95 is now stored in the int called grade.
>> And so C is a language that runs up top to bottom,
so it's going to always run up to bottom.
So it's going to come here, read if grade is greater than or equal to 90,
printf() you got an A. Great, I have a 95, that's greater than 90.
It's going to print, I got an A. It's going
to take a look at this if, it's going say, well 95 is also greater than 80,
it's going to print you also got a got a B exclamation mark, and so on and so
forth.
>> So as we can all see, this is a common bug
that may occur when we're writing code to look out for.
Anyone, any questions on why that was happening?
Great.
OK.
>> So how do we fix this, is obviously the logical next question.
Well, we have these beautiful things called if else if else
if else statements.
So you can see, if you wanted to change that problem,
you wanted to make each of the conditions mutually exclusive,
you would add an else...if statement.
>> And these of course-- think of them as ladders, or like rungs on the ladder--
you can add as many of these as you want for as many conditions as you want.
So here, if I inputted grade to be 95.
If grade is greater than 95, printf(), I got an A. Great.
It's going to see an else...if and it's going to know, Oh no,
I already executed the first else.
I know-- or the first if-- I know that I don't have to look at any of these
because one of them has already been true.
So it's going to run from top down.
As soon as the first one is true, then it's going to skip over all the rest
of the else...ifs.
Does that make sense?
So, in this way you have different levels of checking
and as soon as one of them is false, the rest are also, they won't even check.
Cool.
All right.
>> So this is an example of a different sort of conditional statement
we see less often, but we'll see them and use them.
And they're often more efficient for certain cases.
So we have what's called a switch statement.
So before we covered what's an else statement, an if...else statement.
Here, we have what are called switch statements.
>> So when do we use switch statements is the key?
So in a switch statement, you usually, typically, actually you
can only input integers for your variable that you're checking.
So if I wanted to check to see if a certain number-- for example
a grade, if I got a 90.
I want it to see if that's an A, B, or C. I could have a case here.
However, the case has to be another constant.
>> So in this sense, the switch statement can only
check for equality of two numbers.
It doesn't check for anything else.
So that's something to be very careful of when you're using this.
So here, if I wanted to check to see if my grade of 90 is equal to 90 or 80
or 70 or 60, and then print the corresponding grade,
I'd be able to write that in a switch statement.
>> So it's going to come here, check is this integer equal to this constant?
If not, it's going to skip.
Is it equal to constant two, and so on and so forth,
until you hit the default, if none of them are equal.
As soon as one of them is set to be equal,
it's going to do this line of code and break.
Which means it's going to hit that run, break, and just completely skip
to the bottom of the code.
So in that sense the kind of functions like an if else if else if statement.
>> So here's a concrete example for you guys.
So let's assume that I wanted to create a variable called year founded.
And I want to prompt the user to input the year that their school was founded.
So I can create a switch statement here.
And let's just say, I input 1636.
>> This code here is going to see switch year founded which equals 1636.
It's going to see case 1636, oh those are equal,
printf() Shouldn't you be at that school up north?
Because we assume they don't go here if they go to Harvard,
break and skip to the end.
>> If I were to input 1701, which I assume all of us would input,
it would skip this case, come down to case two which is 1701,
and print Welcome to Yale!
Break, skip to the end.
Else you probably are taking this course online in which case-- awesome welcome
to Yale-- it's going to go to the default print, Hello Internet!
And break.
Yeah.
>> SPEAKER 12: Can you use else instead of default there?
>> ANDI PENG: No, because the way that this whole function
switch is built, the syntax you need to use is case case default.
It's like the if else if, this one it's case case default. Yeah.
SPEAKER 13: You maybe already said this, but can you have more than two cases?
ANDI PENG: Yeah, you could have as many cases as you want.
Think of it as just like infinitely adding on.
>> SPEAKER 14: If you switched 1701 with 1636,
it doesn't really make a difference right?
It's just going to be checking for it.
>> ANDI PENG: That's a really good question.
And we'll touch upon this later, but just
know that's the switch statement is infinitely-- it's
more efficient than it and if else if because it operates
using a different type of function that allows you, to essentially, to just
jump straight to the case you need to be at.
Yeah.
SPEAKER 14: Cool.
Thanks.
ANDI PENG: Yeah
>> SPEAKER 14: And you couldn't do cases like and greater than.
ANDI PENG: No.
So, that's what's limiting about the switch statement is that you have
to have constants only, only integers.
Yeah.
OK.
>> So this is something that you guys will encounter less often of,
but I just wanted to introduce it in case.
We have here what's called a ternary operator.
Where essentially, it's just like an if statement compiled into one line.
Here, I'll go onto the next line, the next page, because it's easier to see.
>> So we've all seen this right?
This is pretty easy to follow.
If I wanted to make a variable called string named s, if a certain number
I give it is less than 100, I want to assign low to the string,
else I want to assign high.
>> This here is doing the exact same thing those eight lines of code are doing.
So here I want to create a variable string.
And this is the condition I'm checking, if a number is less than 100,
then you assign the value of low, else assign the value of high.
>> These slides will be online, no worries if you guys don't get this down.
This is just a simpler way, a more efficient way of writing code.
OK.
>> So now we're going to enter what, for most people
are like a very, very confusing thing to think about at first.
The loop section.
So today, we're going to talk about three types of loops.
We're going to start with a while loop, then talk about a do while loop,
and then talk about for loops.
>> So essentially, for the more visual learners out there,
we have, essentially, a graphical depiction of what a while loop does.
So in a programming, you would begin and enter the loop at a certain point.
You check a condition.
And if the condition is true, you execute the code inside.
And you come back around and you check.
If it's still true you keep running this code around and around in a loop.
>> However, the second that the condition becomes false,
you're going to break and end the loop.
And this is essentially the syntax you're going to use.
While a certain condition is true do this, if it's not true,
you're going to skip to the end and move forward with your program.
OK.
>> Does anyone have an example on what may potentially happen
if I try to run this piece of code?
By the way, SAJ-- that's Scaz, Andi, Jason-- we'll sign off our emails,
Love, SAJ.
That's us.
OK.
>> Anyone have an example, or have an idea on what this would print?
Kind of a trick question.
So here, remember the condition we're checking for is while true.
So while this is true, it's going to print, I love SAJ!
Is there any point in which we'd change this to anything otherwise?
No, right?
>> So in here, we have encountered what's probably
going to be bugging a lot of your programs, the infinite loop.
You'll find that if you run this piece of code,
it's just going to keep printing, I love SAJ!
While we appreciate the support, we don't want your computers
to crash because you keep printing I love SAJ!
>> So please, please, avoid the infinite loop
because it's never going to evaluate to false
and you're never going to leave the loop.
And you're going to be sucked in forever.
Cool.
OK.
The second type of loop we'll talk about today
is the do while loop, do while loop.
And it's called a do while loop because you have a do and a while.
So this is pretty similar to a while loop, but a little bit different.
Here, you're going to do whatever's inside of this
while a certain condition is true.
So if I were C and the computer I'm running down this piece of code,
I take a look at the top.
I go to C, I say do this thing.
And then I check, while this is true, I have to repeat it.
But while this is false, then I move forward
and I never go back to that loop again.
>> Can anybody take a stab at what the difference
between this loop and the one we just looked at was practically.
Yeah.
SPEAKER 15: The condition comes after instead of before?
ANDI PENG: Exactly.
So she said the condition comes after, not before.
Ultimately, the difference between this and the while loop
is that you're just going to do whatever's inside this regardless of
whether or not your condition is true, and then check the condition.
So in this case, you're always-- in this way,
you're always making sure whatever's inside runs at least
once before checking to see if you want it to run again.
>> And here is an example of when we would use it.
So for example, if I wanted to have a variable of type int named age,
and I want to prompt the user for their age,
I'm going to do printf() What is your age?
age = GetInt(), which is prompting the user.
>> And some people will be really annoying.
And you don't want bugs in your program of somebody inputting like,
oh I'm negative 4 years old or whatever.
In which case if they do that, this evaluates
to true, which means that I'm going to have to keep going back and doing this.
So this is going to keep re-prompting the user
to give you like a real age number and it's
going to keep going back and redoing it until they give you a real age
greater than one, or not zero.
>> So hint, hint.
This will be very, very useful for one of your PSet problems.
Yeah.
>> SPEAKER 16: Whoops, sorry.
>> ANDI PENG: Yep.
>> SPEAKER 16: Are there, not to be an asshole, but--
ANDI PENG: No worries.
SPEAKER 16: --are there different rules here,
or did you just forget to put the quotation?
>> ANDI PENG: Oh Yeah.
Sorry, that's totally my bad.
That was definitely supposed to be a quotation.
Good catch.
That would have not run.
OK.
>> So the last type of loop we'll talk about and, ultimately, kind of the most
complex is the for loop.
Don't worry if you don't know what that means.
It's pretty confusing at first.
We'll go over an example.
>> All that happens in a for loop is that you have three statements
that you're going to include.
So for a certain thing, you're going to initialize a variable.
You're going to add the condition to which this loop will keep running.
And then, at the end of the loop, you're going to update it.
You can update the variable that you want to keep track of.
So we typically use for loops for when we
want to run a loop for a certain amount of times
and we already know, Oh I want this loop to execute like 10 times,
then you do-- I'll go over an example on the next page.
>> So here for example, in Scratch, if you wanted something to repeat 10 times,
all you had to say was, repeat 10 times I love SAJ!
which is a more acceptable show of support for us
rather than the infinite loop.
Here, how you would transition to C and write
that is for int-- I'm going to create or declare a variable of type int named i.
I'm going to initialize it to 0, so i = 0;
and this is going to be my condition.
So i is less than 10.
And then at the end-- the last statement you're
going to have is the update of what happens to the variable i
at the end of your for loop.
So it's kind of confusing, because different parts of this line
are happening at different types of the loop.
But I'll go over a pseudocode example of that
and maybe explain this just a bit better.
>> So here.
That's the loop we just saw.
Essentially in pseudocode, what is happening in this program,
is first I'm creating i, initializing it to 0.
I'm checking to see if i is less than 10, in which case
the first time it is because 0 is less than 10.
Thus the loop is going to run.
And then I'm going to print this line.
And then at the end of this line, right here, I'm going to do increment i, i++,
all that means is incrementing it by one.
>> So i is now 1.
Because it was once 0, if I increment it's, it's now 1.
And then I'm going to go back to the beginning of the loop
and I check the condition.
Is the condition still true?
Yes, 1 is still less than 10.
So it's going to print this again, go and then increment i, and check
the condition continuously, continuously,
until you eventually get to the point where i is 10.
>> You're going to print this 10 times and then i is going to equal 10.
You're going to check the condition.
Is 10 less than 10?
No, that is false.
Thus, this loop is not going to run, it's going to break,
and you're going to continue on with your code.
>> So as you guys can see, this is a really great example
of a loop you can program in that runs for a specified amount of times.
Every one clear?
Yeah.
>> SPEAKER 17: How about increment exponentially, is it different coding?
>> ANDI PENG: You can-- we'll go over this in the next slide.
Good question.
Is anyone-- before I move on-- anyone at all confused,
because this is a really tough concept.
No worries, if you're-- OK.
Cool.
>> All right.
Just a general slide.
This while loop is doing the exact same thing the for loop was.
It's just written differently.
You guys can peruse the slides at your convenience later on.
But just know that there's multiple ways of writing the same thing
to happen with different loops.
OK.
>> So, now we get in the question of what if we have a loop inside of a loop.
We're getting into real Inception type stuff here.
When you want to do things multiple times inside of other things
that do things multiple times, you want what's called a nested for loop.
For those of you who first see this and get very confused, all we're doing here
is having a for loop where we have a variable of row.
But inside of it, we also have another for loop of a variable called column.
And I highly suggest all of you who are confused to first keep
track-- draw this out, draw this out.
Don't try to just reason through it, draw it out.
>> In your head, on this piece of paper, or whatever, write row,
keep track of what row is equal to.
Write column, keep track of what column is equal to.
And keep track of what is printing out with every iteration.
Every iteration of this loop, every iteration of that larger loop,
just keep following the logic.
And I guarantee you, you'll love what you see,
because it's also very applicable for your problem sets.
Cool.
All right.
So the most important thing that you guys are probably all
thinking about right now, are your Problem Set 1s,
which are due Thursday/Friday.
>> In your water.c program, hint you're going to have
to prompt the user for an input.
Within your mario.c program you're going to have
to use a nested for loop, which is a for loop inside of a for loop,
to print a block of pyramid, essentially like what Mario has to jump through.
And then inside your greedy-- or perhaps Making Change, if any of you guys
have ever heard of that-- you're going to have to be very
careful of first floating point values.
Remember floating decimals and integers are not the same thing.
Keep track of which one is which.
And you're going to use conditional statements, as well.
>> All right, last thing.
I've got a couple minutes left.
Style.
So this is something that doesn't actually effect the efficiency,
or the actual running of your code.
However, it effects us as your graders, as your readers.
It effects yourself, if you're trying to find a problem.
It effects the readability of your code.
>> So style, like when you're trying to style an essay for English, if you
didn't have paragraphs, you have everything kind
of jumbled together on one line, it makes it really difficult for anybody
to read your essay, even if your points are logically sound.
Same thing in programming.
You can have horribly obscure code which Scaz will cover,
and it can still run and function.
But for us, as your lovely TAs, who will be reading and evaluating your PSets,
that's not very nice.
>> So please, for the sake of us and yourself,
when you're trying to fix a problem in your code,
and you're trying to read your own code, make
sure you follow some conventions that we're going to go over.
>> So first.
Give your variables meaningful names.
If you want to store an integer called age, please name it age.
Don't name it height.
When you're trying to store an age in height,
it makes everything very confusing for us.
We don't like to be confused.
You don't like to be confused.
No one likes to be confused.
If you're going to create something, name it something meaningful.
>> However, in for loop, single character variables are usually fine.
And in for loop, if you want just i, and j, k, feel free to just do that.
>> Consistent initialization.
So what does that mean?
That means technically, theoretically, you can initiate and create
multiple variables on the same line.
So for example, I can create an integer called scaz_age,
and integer called andi_age = 20, and an integer called jason_age
on the same line.
And I can also assign only one of them and not the others to values.
We ask you please don't do that.
Because here you've essentially created three variables, but only one of them
actually has a value.
And then when we're trying to read your code,
or if you're trying to fix a problem in your code,
it's very confusing to follow.
So just for your readability, for our readability, don't do that.
>> Consistent curly braces.
Some people like to put their curly braces in different places.
It doesn't really matter.
Just make sure you're consistent in your own code on where you like to put them.
>> Consistent spacing.
If you put a space after a for loop, always do that.
Don't just like kind of do it at some places, don't do it in others.
Just be consistent.
>> Secondly, if anybody would like to peruse the CS50 Style Guide,
we officially have a style guide that tells you all of these conventions,
plus more.
It's online.
It's like cs50.net/style or something like that.
You can google it.
>> Consistency is key.
So don't worry what other people are doing,
just make sure that you are consistent within your own code.
Anyone have any questions about that?
Yeah.
>> SPEAKER 18: So the proper thing to do with initialization is just
have them all in a separate line, is that what you're saying?
>> ANDI PENG: So I rarely have this happen.
But if you wanted to, if you wanted to be like saving space in your code,
or whatever, you can do this.
We ask that you just don't initialize one thing and not the others.
So if you want to do int scaz_age, int andi_age, int jason_age, that's fine.
Just don't initialize one and not the others is all.
>> Questions?
All right.
I'm going to pass off the microphone, and the laser pointer,
and the baton to Scaz at this point.
This is awkward.
Here it is.
>> BRIAN SCASSELLATI: Thank you.
How's that for sound?
Sound is good? .
Excellent.
OK.
So, hi everyone.
>> I'm going to try to work through a practical example with you.
And we're going to use CS50's development environment, what's called
the Integrated Development Environment.
And you've seen this demonstrated in lecture.
And in Problem Set 1, you're going to have an opportunity
to use it, and play around with it, and get accustomed to it,
because we're going to use it through the rest of the semester.
>> So in this IDE, you have what looks like a very traditional file browser
over on one side.
You've got a portion up top where you're going
to see your source code, your Cfile, the code that you write.
And down bottom, you'll have a terminal window
which you'll be using to both compile your program
and to run or execute your program.
OK.
>> So just to give us a little bit of foundation, in that terminal window,
you're going to be using a set of commands
that are standard commands throughout most of Unix or Linux systems.
And so if you've ever used any form of Unix, Ubuntu,
or any of the other flavors, these are going to look familiar.
>> If you haven't, don't worry.
There's nothing complicated about them.
They're just using a different syntax, a different naming
convention than you've seen before.
>> So to list out the files within a particular directory,
they're going to use a command called ls, or list.
If you want to see everything with all the details,
you'll use a command line argument with ls -l.
And that'll show you everything in more detail, including
the permissions for a file.
>> To change directory, you'll use the cd command.
And you'll be able to change directory both to go to your home directory.
That's just cd all by itself, cd with two
dots will return you up one level to your previous directory.
And you can also cd to a subdirectory by typing in cd
and the name of that subdirectory.
>> You can also create new directories.
And we're going to walk through this in just a minute.
But just to put everything on the screen.
So that you can see them.
>> You'll also have the ability to manipulate files directly
from the command line.
You'll be able to copy them, to move them, or to remove them,
that is, effectively, to delete them.
>> The CS50 IDE gives you the full power of command line arguments.
And that means you can also do highly dangerous things.
OK.
For example, you can remove, or that is delete,
a file without asking for a confirmation.
And you can even remove recursively-- that's
the dash r flag-- an entire subdirectory and all of its contents.
OK.
They're listed in red because you should think "Danger" every time
you see those things.
OK.
All right.
>> Now finally, the things that are going to be really valuable to you,
are there are a few good tricks to know as you're navigating
through this terminal window.
First, you can clear the screen at any time by just typing in clear.
And you're going to see me do that quite often.
You can also just display the text of a file
by typing more and then the file name.
You'll then be able to scroll back and forth with that just with the spacebar
and arrow keys.
>> If you have, as we did today in lecture, a program that is running continuously
in infinite loop, you can stop that program
from executing by typing in control, that is holding down Control-C.
And you may have to do this multiple times.
The computer gets far ahead of you.
And you sometimes need to give it a couple of tries
before it will actually come through.
>> You'll also be able to sort through the commands
that you just typed using the up key, arrow key, and then the down arrow key.
And what's most useful is instead of typing out long file names,
you'll be able to the use Tab to autocomplete a few instructions.
Now we're going to demonstrate all of those in just a second.
So if you don't remember them, don't worry.
These are things that you'll pick up and use as we go along.
OK.
>> So in C-- unlike in Scratch-- C is a compiled language.
That means we're going to take a source file-- that's the text that you write,
the commands that you write, the printf() statements, the loops,
everything else-- and we're going to take that file and hand it off
to a program called the compiler.
>> The compiler will then take that text that you've written
and translate it into the binary instructions
that your computer is actually going use.
That's called the object or the executable file.
If you look at this file, you're going to see the code that you've written.
If you look at this file, you're going to see a random sequence of characters
that make no sense whatsoever.
That's because this is the binary.
It's not meant for you to be reading.
>> However, any time you want to run something, what you're going
to be running is this object file.
So when we work with these files, we'll write a file in C.
We'll then compile it, using a command like make which will invoke
the compiler clang for the C language.
And that will produce an object file, like a out, or in this case,
the name, my file, that I've put in.
All right.
>> So let's actually try this.
So I came up with an example of what I wanted to try.
And one of the things that fascinates me is animation.
So we're going to try to do a little bit of animation
using just ASCII characters.
Characters we can print out easily now.
>> So here is my best attempt at creating for you
the animation of a bunny running through the tall grass.
There he is.
OK.
So he's not running yet, but he's standing there in the tall grass.
>> Now if I were an animator, in the old school
version of animation, what I would do is I would produce
a picture of this bunny in the grass.
And then I would produce another picture--
another what they called cell-- that had the bunny only slightly moved.
And then a third one that had the bunny moved a little bit further.
>> And I would produce an entire sequence of these cells.
Some where the bunny is over onto the left hand side.
And then moves slowly, one by one toward the middle.
And then from the middle over to the right.
And if I were then, really lucky, I could put it together
and I could animate them.
And there's my bunny running through the grass.
>> That's my great PowerPoint trick for the day.
OK.
So this is as good as it gets.
OK.
So, here one more time, here is our bunny running through the grass.
>> SPEAKER 19: Again.
BRIAN SCASSELLATI: One more time, all right.
There's your bunny.
OK.
>> So today what we're going to do is we're going
to try to automate the process of producing these cells.
We won't quite get to the point of being able to put them all together.
But, we're going to try to automate the process of generating this sequence.
>> And this is much of what animation today is like.
That is, you don't do things necessarily by drawing everything by hand.
We use a computer to automate parts of that process.
OK.
>> So let me switch over now to our CS50 IDE.
And I have created for us-- and let me zoom in here a little bit-- I've
created for us a starting point.
>> Every time that we ask you to sit down and write a piece of code,
what we're really asking you to do is we're asking to solve a problem.
And the way that you should think about doing that
is by starting with some simple part of that solution.
And then build out from that part.
And that's what we're going to do today.
>> So rather than trying all at once to write the entire bunch of code that's
going to produce those 10 animation cells,
we're going to start instead with one piece that works.
And then we'll build a little bit around that
and a little more and a little more.
>> Now the good thing about solving problems this way
is that it will allow you to start always with something that you know
works and introduce one gradual change.
And that's a great way to learn how to code,
because each time you make a change, you see what impact it has.
OK.
>> So here's our starting point.
OK.
So at the beginning of my file, I've hash #included stdio.h.
That's so that I can get the printf() function to work.
>> I then I have my main function.
And this still looks a little arcane or obscure to some of you.
That's OK.
All it says is that the main function takes
no arguments-- void means nothing in C. And it
returns by convention an integer.
Main always returns an integer, usually a code saying things
went well or didn't go well.
OK.
But main has to have that form for us right now.
>> I've then put in three lines of code.
And along with each line of code, I've put a comment.
Now one of the things that we will insist that you do,
and it's such an important programming practice,
is to always comment your code.
Always write down in an English comment something
that you think the code is supposed to do.
That way, later on when you come back to it, you can look at it and you can say,
oh, I remember what I was trying to do with this.
Or when a TA sits down with you to try to help you at office hours,
they can look at this and go, I see what you were trying to do,
but instead this is what's really happening.
OK.
>> So I've got three parts to my code, I'm going to first of all print
out some dots at the start.
I'll then print out my extremely fancy bunny.
And then some bit of dots at the end.
>> And these three print statements should look familiar to you at this point.
All that I'm doing in each of them is I'm
printing out a sequence of characters.
There's no variables involved.
Everything is just flat.
OK.
>> So if I go down now to my terminal-- let's see if I can get this back out--
and I'm going to type clear again.
>> SPEAKER 20: Do we use the double slash to comment?
BRIAN SCASSELLATI: Can use the double-- yes.
There's multiple ways to leave comments in C. One
way is to use the double slash.
The other is to use a slash and a star and then close with a star and a slash.
All right.
>> First of all, I'm going to start navigating around here.
So if I go to my home directory, I've changed directories there,
I'm going to look and see what's in that directory, ls, list out.
I'm going to see that I've got two subdirectories.
Let's make this a little bigger here so that we can all see it.
>> I can see that I've got two subdirectories.
I'll change directory to go into workspace.
And I'm going to only type out part of it, and then just hit Tab.
And it'll complete the rest for me.
Fancy.
>> I'll look and see in workspace.
And right now, I'm working on the SuperSection
that we're teaching right now.
So I'll go into that directory.
And finally, look and see.
And I've got that file bunny.c.
>> All right so let me clear once more.
And I'm going to now-- again I'm still staying in that directory
and it's telling me I'm in that SuperSection directory.
I'm going to go ahead and make my program bunny.
And that command, make bunny, while sounding a little bit odd,
also invokes the clang compiler.
>> And it's produced for me an output that is
an executable funny-- an executable file called bunny.
OK.
I can then, and this sounds even worse, execute bunny.
OK.
And let's see what it does.
OK.
That's a little bit of what I was expecting.
I've got my bunny picture in there, but I kind of wanted it all by itself.
What did I miss?
>> SPEAKER 21: Slash l or slash n.
BRIAN SCASSELLATI: Slash n.
OK.
So let's go back out here.
And I'll get out of that.
And I'll go back into this one.
And let's take a look now from my main function here.
>> So what should I do?
I want to end the line.
So I'll put in a comment.
I'll put in a printf().
And what do I have to put in?
/n.
OK.
What do I have to end it with?
Semicolon.
All right.
>> Now, one of the really important things is make sure
every time you make a change in your code, that you save it.
If you haven't saved your code, you're going to notice a little star up there.
And that star says you haven't saved this code.
>> If I compile it right now, it's not going to reflect any of those changes,
because the compiler looks at the file that's on the disk, not the file that's
open in your editor.
All right.
>> So let's save it and then we'll go right on down here, come back out.
Come down to my terminal.
And let's clear the space again.
>> And we can go ahead and one more time make our bunny program.
And execute the bunny.
That didn't work either.
Wrong slash.
So if you look at what I've got, I put a /n in there, but I had the wrong slash.
>> Everything that your computer does is very explicit.
OK?
One little mistake of punctuation, and suddenly you don't get what you want.
All right.
So let's zoom back out again.
We'll go back.
Well make that very quick repair.
We'll put the right slash in.
We'll save it.
We'll zoom back in.
>> For some reason, that's not being happy, but let's go ahead
and we'll go back to the terminal here.
Clear it up.
We'll zoom in.
And one more time, we'll make bunny.
And now sure enough, it works.
Hooray.
OK.
>> So let's try to make this a little more general.
Let's see if instead of just printing one particular frame,
let's see if we can make this so that we can get all 10
of those animated frames that we wanted to have.
So again, let's take this a step at a time.
>> Let's first generalize it, not so that I do all the frames,
but so I do any one frame that I might want.
So what's the difference between the frames?
Is the bunny the same?
AUDIENCE: Yes.
BRIAN SCASSELLATI: Yeah.
What's the difference?
>> AUDIENCE: Position.
BRIAN SCASSELLATI: Its position, right?
And how do I control its position?
How many dots I'm putting at the beginning
and how many dots I'm putting at the end.
So I had five at the beginning and five at the end.
>> Let's replace that five with a for loop.
OK.
And I'm going to create a for loop now that's going to say,
I'm going to print some number of dots at the beginning.
I'm going to use a variable.
Let's say, how about i as the counter in my loop.
And I'm going to declare it up top.
>> And then in the for loop I need to do three things.
The first thing I need to do is I need to initialize i.
What should I initialize it to start to be?
0.
OK.
>> Then I need to say, what's the termination condition?
When should I stop?
Well how many dots do you want to print on this one?
>> AUDIENCE: Five.
BRIAN SCASSELLATI: Five again?
How about let's do something different, we did five.
Let's show that it's different.
SPEAKER 22: Two.
BRIAN SCASSELLATI: Two.
OK.
So if I want two dots, what should I put here?
>> AUDIENCE: Three.
>> BRIAN SCASSELLATI: Three.
OK.
How many times is that going to go through?
That's going to go through three times, 0, 1, and 2, Right?
All right, let's go back down to two.
Now we'll get two dots.
And what do I want to do each time I go through the loop?
What has to change each time I go through?
>> SPEAKER 23: Add a dot.
>> BRIAN SCASSELLATI: I have to keep going.
I'm going to add a dot.
I'm going to print a dot, each time through the loop.
But how am I keeping track of how many times I've been through the loop?
I'm using i, that variable, that counter.
>> So every time through, I'm going to increment the counter by one.
Now, that's the same for me as saying i = i + 1.
That's OK.
I could do it that-- I like the shorthand, so I'm going to say i++.
OK.
>> Let's do the same thing down here at the bottom.
Only I kind of did that one.
I'm going to let you guys do this one completely.
All right.
>> So what should I write here?
Here's my for loop.
I'm going to do a printf() and I'm going to make it so that I only print one dot
on that bottom.
>> What should I write inside this for loop now?
Well, first of all what variables should I use?
>> SPEAKER 24: j.
>> BRIAN SCASSELLATI: I could use j.
Can I use the same one?
Can I use i again?
Yeah.
That's OK, because the Is that I'm using up here,
I don't need them again when I get down to this point.
>> So what should I initialize i to?
SPEAKER 25: 10.
BRIAN SCASSELLATI: 0.
What should I check?
How many dots do I need now at the end if I've got two dots at the beginning?
I need eight at the end, so what should I check, i less than--
>> AUDIENCE: Seven, eight, nine.
BRIAN SCASSELLATI: I heard seven.
I heard eight.
And I heard nine.
OK.
So we're all in the right ball-- Jason says 10.
OK.
>> If I needed two dots for the first one, how many do
I-- and I need eight dots for the last one-- I put a two up above,
what should I put down below?
>> AUDIENCE: Eight.
>> BRIAN SCASSELLATI: Eight.
Because that's going to count zero through seven.
And that's eight times through the loop.
OK.
And what do I have to do at the end?
>> AUDIENCE: i++.
>> BRIAN SCASSELLATI: i++.
All right.
So that's looking pretty good there.
Let's try it and let's see what it does.
OK.
So we're going to save it.
Nice and saved.
>> We'll zoom back out.
We'll try here in the terminal.
We'll zoom in.
Oops.
We'll, one more time, make our bunny program.
And go ahead and execute bunny.
And there it is.
>> So there's our bunny.
Where it has two dots at the beginning and eight dots out at the end.
Everybody still with me?
OK.
>> So we built it up.
We built one bunny, one particular frame.
Now we've been able to generalize that to build
more, different kinds of frames.
>> Now let's go ahead, and have it generate not just one frame,
but let's generate 10 frames, where we slowly
make the bunny move all the way across the field.
All right.
>> Let's go back.
And we'll try now.
So what do I really need to change here?
What do I need to change?
>> SPEAKER 26: You first need to change the number of dots maximum
at the beginning.
Because if we're doing 10 dots, it's going to need to up to scale.
BRIAN SCASSELLATI: Yeah.
So right now I have it sort of hardwired to always do two dots at the beginning
and always do eight dots at the end.
>> I want to build another loop, right?
Because I don't want to build just one bunny picture,
I want to build 10 bunny pictures.
So I need to build another loop, and as I go through that loop,
I want to change how many dots I print at the beginning
and how many dots I print at the end, based on which cycle through the loop
I'm in.
All right.
>> So let's get another counter.
Somebody's before said j, so we'll make another j.
And now, we're going to build another for loop.
>> What goes inside that loop?
This stuff has to go inside the loop, right?
Does the bunny have to go inside the loop?
Do I need a bunny in each of those 10 frames?
>> AUDIENCE: Uh-huh.
BRIAN SCASSELLATI: Yeah.
I want a bunny in each of the 10 frames, right?
How about the dots at the end, do I need that?
OK.
>> So I'm going to indent all of them.
I'm going to highlight all of this, and I'm going to hit Tab.
And that's going to push them all over a little bit, so that it's easy for me
to see what's in the loop.
And then I'll end it.
Let's say.
OK?
>> Now, in this loop that I'm building-- whoops,
make that so you can see-- I've got my counter j.
I'll started it at 0.
How many times do I want to go through this loop?
AUDIENCE: 10 times.
BRIAN SCASSELLATI: 10 times.
So what number should I put here?
>> AUDIENCE: 9, 10.
>> BRIAN SCASSELLATI: 9, 10, somebody's got to say 11, right?
I wanted two dots before and put i less than 2.
I wanted eight dots, I put i less than 8.
Now I want to go through 10 times, so I put j less than--
>> AUDIENCE: 10.
>> BRIAN SCASSELLATI: 10.
There we go.
And what do I do at the end to j?
++, increment it.
OK.
Now, here's the tricky part, what's going to happen right now if I do this?
Am I going to print 10 frames?
SPEAKER 27: I think they'll all be the same.
BRIAN SCASSELLATI: They'll all be the same, right?
Because all of them are still going to put two dots at the beginning.
But I don't want them all to have two dots the beginning.
How many dots do I want at the beginning?
>> AUDIENCE: Changing.
>> BRIAN SCASSELLATI: I want it to change, right?
So what do I have here that's changing each time the loop goes through?
>> AUDIENCE: Number of dots, j.
BRIAN SCASSELLATI: j, the number of dots.
So I can change this to be j.
The first time through the loop, what's that going to be?
What do I set j to at first?
>> AUDIENCE: 0.
>> BRIAN SCASSELLATI: So how many times am I going to do this?
0.
The second time through the loop, j's going to be 1 because I increment it.
How many dots am I going to print?
1.
The third time through the loop, how many dots am I going to print?
>> AUDIENCE: Three.
BRIAN SCASSELLATI: j is going to be 3.
How many dots am I going to print?
Oh, sorry, j's going to be 2.
How many dots am I going to print?
>> AUDIENCE: 2.
>> BRIAN SCASSELLATI: 2.
OK, so I'm going to keep incrementing that as we go along.
How about down below?
What goes down here?
I don't want 8 always at the end anymore?
>> SPEAKER 28: 10.
BRIAN SCASSELLATI: I want 10 dots?
I want it to change, too.
So how do I want it to change?
>> AUDIENCE: [INAUDIBLE].
BRIAN SCASSELLATI: Well, if I have five dots at the beginning,
how many dot's do I get at the end?
>> AUDIENCE: Five.
>> BRIAN SCASSELLATI: If I have six dots at the beginning,
how many do I get at the end?
AUDIENCE: Four.
BRIAN SCASSELLATI: If I got seven dots at the beginning,
how many do I get at the end?
AUDIENCE: Three.
BRIAN SCASSELLATI: If I've got j dots at the beginning,
how many do I get at the end?
10-j.
OK.
So let's try that out.
>> So I'm going to save our bunny program.
Once again we'll zoom out.
We'll go down to our terminal.
We'll clear it.
And zoom in.
We'll make our bunny program, again.
And we'll execute it.
Uh-oh.
>> Hold on, let's zoom out.
Did I get 10 frames?
Well, how many bunnies do I see up there?
1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
I got 10 frames.
Are they all the same?
AUDIENCE: Yeah.
No.
>> BRIAN SCASSELLATI: No.
That first one, the bunny is far on the left.
And over here, the bunny is far on the
SPEAKER 29: Right.
BRIAN SCASSELLATI: So, what did I forget to do?
SPEAKER 30: Start a new line.
BRIAN SCASSELLATI: I forgot to start a new line, again.
Same mistake I made before.
All right, let's go fix that.
Now, I've still got the code in there to make a new line.
Why didn't it work?
SPEAKER 31: It's not inside the loop.
BRIAN SCASSELLATI: Oh, it's not inside the loop.
That's right.
It's sitting outside here.
And the for loop is everything that's inside that text.
>> So I'm going to move this inside the for loop.
And I'll Tab in front of it to show that it's in the right place.
And now, we'll save it again.
We'll zoom out.
We'll switch over to our terminal.
Zoom in.
We'll remake our bunny program.
And now, now we've got our 10 frames.
>> [CLAPPING]
>> BRIAN SCASSELLATI: OK.
So.
Here is our nested for loops.
We were able to generate in the inner loop how many dots I wanted
to print at the beginning, at the end.
And the outer loop controlled how many frames I was building.
We started with one little kernel of the problem,
and we built out from that point.
All right.
>> Let's do one more step.
You ready?
There's one thing in here where we've actually kind of got
more complexity than we need.
Let's take a look.
So in our bunny program, if I zoom out here,
I actually do some of the same thing over and over again.
What do I do that's sort of the same thing repeated twice?
>> SPEAKER 32: Print the dots.
>> BRIAN SCASSELLATI: Yeah I print those dots twice.
Really, I should have this comment down here.
I print some dots at the beginning, right up here.
And then I print some dots at the end.
And I do kind of exactly the same thing.
What we're going to start working on in the next few weeks
is being able to take those blocks of code that we use over and over again,
and through a process called abstraction,
we're going to pull them out and write them once so that we can then
reuse them over and over again.
So let's try that.
Ready?
>> We're going to take this block of code.
And I'm going to take it out of there.
And I'm going to define-- down at the bottom,
I'm going to write a new function.
It's not going to return anything.
And I'm going to call it printDots.
>> It's going to take one argument, an integer that
says howManyDots I should print.
And now instead of printing j dots, I'll print tell howManyDots I should print.
>> And there's one little problem here.
Anybody know what it is?
What do I have that's listed in here that's not defined?
>> AUDIENCE: [INAUDIBLE]
>> BRIAN SCASSELLATI: Well how many dots is defined right up
here, but I'm using that variable i.
So I'm going to take that variable i and I'm going to define it down in here
instead.
>> So now it's going to stay-- oops, got the caps lock on somehow-- I'm
going to keep i down in here.
>> So now here's my little function or sub-routine, and it says,
how many dots am I going to print?
And it'll go through this loop and print them over and over again.
>> I can then modify my program up here.
And what did I call that function?
printDots.
So I'll call printDots.
How many dots do I want to print the first time before the bunny?
AUDIENCE: j.
BRIAN SCASSELLATI: j.
How many dots do I want to print at the end, after the bunny?
10-j.
>> And there's one thing I'm missing.
As you saw in lecture today, we're going to declare printDots
up above to give the prototype.
OK.
>> So what I've done is I've tried to isolate that reused part of code
that I've done over and over again.
And I've tried to pull it out so that all of that is contained in one place.
That way, if I have a mistake somewhere, I only have to fix it in one spot.
All right.
>> So let's save it.
And let's make sure it works.
So let's go out.
We'll go again to our terminal.
We'll zoom in.
We'll make that bunny process.
>> Oh.
And it's given me a warning.
What is it telling me?
Well in any of these times, you always want
to scroll up to the very first error-- now on this one, I've only got one.
>> It tells me in bunny.c, on line 8, column 9, there's a problem.
It says, you've declared this variable i and you haven't used it.
>> Now normally, that's not the worst error.
That's an easy one to fix.
And in fact, we can go back in.
We can go back to bunny.
And in this case, all we have to do is get rid of i,
because we're not using i within main anymore.
We're just using it within our sub-routine.
>> So let's save that.
We'll go back.
And zoom in.
We'll one more time make bunny.
And there again are our 10 frames.
OK.
>> Any time you're given a procedure-- Yeah.
SPEAKER 33: I have a-- I'm confused.
Can you go back to the code?
BRIAN SCASSELLATI: Yep.
>> SPEAKER 33: So, when you wrote your prototype, the argument you
had it named was called howMany?
But, below--
>> BRIAN SCASSELLATI: Oh yeah.
>> SPEAKER 33: --called them something different, I don't understand.
>> BRIAN SCASSELLATI: Why are they different.
So that's a great question.
So the question was, up here, I wrote howMany,
and down below, I wrote howManyDots.
>> The reason is that up in your prototype, it's actually not paying attention
to the names that you're putting in.
All it really cares about is that it's an integer.
It wants to know the form of what you're putting in.
>> Now stylistically-- oops-- what I should do, is I like to make these match.
OK.
Because that'll be keep it easier for me to remember.
But that was my mistake Yeah.
>> SPEAKER 34: And so for the prototype to work, just
writing that line, writing the prototype,
allows that function that comes right below it to go to the end
and retrieve what that means?
>> BRIAN SCASSELLATI: What it means is when the compiler goes through,
it goes from the top of your code to the bottom.
And what this prototype is, is it's basically a promise.
>> It says, there's going to be a function defined somewhere.
It's going to be called printDots.
And it's going to take one argument that's going to be an integer
and it's going to return nothing, void type.
OK.
I promise you it's going to be defined somewhere down the road.
But any time you see that, as you go down
through the rest of my main function, I want
you to treat that as a function that takes one integer input.
>> And so when the compiler goes down through this, it sees that promise.
And when it gets down, keeps going, keeps going,
it finds the first time printDots is mentioned.
And it says, Oh, you're giving me this j. j's an integer.
>> Well you promised me that would be an integer and that's right.
That's OK.
And then finally, down at the very bottom,
before I get to the end of my file, I make good on my promise
and I define it.
OK?
>> SPEAKER 35: So, it saves-- the program will save blank spaces
that it goes back and fills at the end?
>> BRIAN SCASSELLATI: It's not about the memory allocation.
It's actually just about what type are you expecting to see.
Should this thing have one argument, or five arguments?
Should have it integers be given to it, or strings?
That's all it's looking to do is to check,
are you giving me the right kind of argument.
OK?
All right.
>> Let me leave you with one other bit to look at.
ASCII art is not the great model of animation as it is today.
But some people of course take things and they push it to their extremes.
>> This is, as David demonstrated in lecture, a piece of code
that you should under no circumstances try to duplicate yourselves.
Because it is terrible stylistically.
In fact, it is designed to be as difficult as possible to read.
OK.
>> So, to do again, let's zoom in here.
I'm going to go now, change directory.
I'm going to go up one level, back to my workspace.
I'll then change directory into this other directory that I've created.
>> And this is the International Obfuscated C Programming Contest.
Obfuscated means as hard as possible to understand.
So please, don't be scared if you look at this and go, I can't read that.
That's the point of it.
OK?
>> But, we have this wonderful program that I'm
going to look at by just typing more.
And let's see if I can zoom out just a bit, so you can see it
>> And this is the program.
It's named endo.c.
And it looks like this bucket with kind of the letters F-L-U-I-D in it.
>> This is actually a program.
It's written in the most obscure way that the authors could possibly
write it.
But it's a wonderful little piece that generates a model of fluid dynamics.
>> And so we're going to give that model an input that
looks like this, of a staired set of containers
with some fluid, some liquid, up at the top.
And we'll have it simulate what's happening to it.
>> So I'll call that function, endo.
And I'll give it that input file that I've had.
And there is our epitome of ASCII art.
A full fluid dynamic simulator running in just a few lines of code.
>> Now what's actually really amazing about this little program
is that I want to stop it now, so I'm going to use what command?
>> AUDIENCE: Control-C.
>> BRIAN SCASSELLATI: Control-C. OK.
So I'll use Control-C. C That'll stop it.
I'll clear again.
And now I'm going to invoke the program, execute the program,
using itself as input.
>> And we'll see the fluid dynamic simulation of it melting.
OK.
Please don't let that program be the thing that confuses you.
It's just so that we can end on something cool and interesting.
OK.
>> Best of luck with your problem sets.
We'll be happy to answer questions after class.
Thanks much, guys.