Supersection 1, Less Comfortable


>>JASON HIRSCHHORN: Welcome. So just a few announcements
before we start. So sections, everyone should
have sectioned already. Sections normally run
from Sunday to Tuesday. So you guys should get your assignments. And then I think next week
you’ll be with your actual TFs. So if you have any questions, or
trouble, or you forgot to section. Some people do that. Just let us know. Shoot us an email at heads. >>Then office hours. We started office hours last week. Office hours every week. Monday is in Leverett 8:00 to 11:00. Tuesdays in the quad,
so Cabot 8:00 to 11:00. Wednesday, Mather 8:30 to 11:30. And Thursday Annenberg 8:00 to 11:00.>>So Scratch, a lot of people
don’t need office hours help for Scratch, which
is completely fine. If you do, that’s
absolutely fine as well. But in the future, the problem
sets, they get much harder. So this is going to be your friend. Definitely go to office hours. Plan on going to office hours. When you get to problem
set four, five, six, seven, you need to go to office hours. That’s where a lot the help
and the work gets done.>>I think last year, just for
example, on Thursday night– so I think the problem
sets were due on Friday. So Thursday night, we would have
200, 250 students at office hours. So definitely make use of these. These are your best friends. Like this is where if you’re
stuck on a problem set, this is where you’ll probably get help. So office hours. OK so those are announcements. Announcements are done, so let’s start.>>OK, the appliance. Did everybody download
the appliance yet? No. As in so so. I kind of downloaded the appliance. Right so the appliance is new
this year, so we’re probably going to have some bugs in it. So please download it
as early as possible. So problem set one is going to tell
you exactly how to download it. Getting started installing right here. It’s going to tell you how
to download the appliance.>>So definitely download the
appliance sooner rather than later. Instructions are in the P set spec. So if you wait until Wednesday
night, and then you have trouble, and you send us an email at
midnight on Wednesday night that you can’t download
the appliance, that’s not a legit reason for an extension. You need to do it now,
and you will have trouble. I tried to download it last night. I ran into a little bit of trouble. If you do run into trouble
installing, definitely hit retry, because I just hit retry a
couple times, and eventually it worked, which doesn’t make any
sense, but it does. So definitely retry, but if you
come up against a brick wall, shoot us an email at heads, and we’ll
be more than happy to help you guys. What’s up?>>STUDENT: Just a quick question. If they check style, there’s a part
that says we didn’t include this. JASON HIRSCHHORN: Not yet. STUDENT: It says execute it. How do you execute that to
make sure it’s in your system?>>JASON HIRSCHHORN: You’re
going to run a bash command. I’ll go over that later. Cool. So the appliance can be a
little bit intimidating, because you’re used to kind of
running in graphical user interface. So Scratch was a graphical user
interface, so what do I mean by that? What I mean is that
when you’re programming, you’re basically using blocks of code. You can see the code
and stuff like that.>>The appliance, you’re going to be doing
things in a command line environment. And so for the rest of
your programming lives, you’re going to be doing things
in the command line environment. So it’s good to jump in early
and get some experience. So let’s do it. So that’s Rob. >>OK, so let’s just jump
into command line stuff. So this is the appliance. Can anybody tell me why
we use an appliance? What is the appliance? Does anybody know? What’s up?>>STUDENT: It’s just a virtual machine
that you run your machine so that you can get over the OS
disagreements between Mac and PC. JASON HIRSCHHORN: Perfect. STUDENT: It’s a universal template. JASON HIRSCHHORN: Yeah, it’s
like a universal template. That’s perfect. So yeah, it’s a virtual machine. So it’s basically an operating system. This is an operating system. This is equivalent to your
Mac, or you PC, or your Linux. This is the exact same. And so you can do similar stuff. If you wanted to, you could
go online for example. You can’t really see it, but
right here, Google Chrome. You can go online if you want to. This is an operating system. And the reason we do that is because
it’s much easier when we’re handing out instructions and everything. If it’s just a uniform environment,
it’s much easier for us, and it’s much easier for you. You’re not going to run into
it run into any idiosyncrasies when you’re programming. We know exactly what
you’re going to run into. So the appliance is here.>>So when you’re starting
programming, you’re going to go down to
the left hand corner. There’s a little box. You’re going to click it. This is your terminal window. So this is where you’re going
to be a lot of the semester. So let me zoom in a little bit. >>OK, so getting around a terminal
window is a little bit different. First, there’s no icons. I can’t click anything. There’s nothing to click. So you have to figure out a way to
figure out where you are, what’s here, and how to move around.>>So the two most useful commands probably
are ls– so what do you think ls does?>>STUDENT: List. JASON HIRSCHHORN: Lists, yeah. It just lists algorithms
within the directory. And then cd. So let’s say I wanted to
cd CS50, what did that do? Yeah?>>STUDENT: Changes your directory. JASON HIRSCHHORN: Perfect, yeah. It just changes the directory. So let’s go back. So how do I go back? So let’s say I wanted to go
back to the previous directory. Yeah sure.>>STUDENT: I think you write
cd just without anything. JASON HIRSCHHORN: You can do that. So perfect. So if I write cd, this is
actually going to pop me back up to the home directory. So you see this tilde,
that’s going to pop me way back up to where the tilde is. So it’s going to pop me up
back to the home directory. But let’s say I did something like this. Let’s say cd CS50. ls again. I’ve got other stuff. cd super section. cd file, so I’m going deep. And then let’s say I want to
just pop back up to the top. cd. Let’s say I don’t want to do that. Let’s say I just want to pop back up to
the directory that was right above me. How do I do that? So let’s say cd CS50. cd supersection. So let’s I’m here. Let’s say I just want
to pop up one time. How do I do that? Yeah, what’s up?>>STUDENT: You just type
cd supersection again.>>JASON HIRSCHHORN: You could do that. Well I’m in supersection.>>STUDENT: Oh, the one before.>>JASON HIRSCHHORN: Yeah,
so you could do that. You do cd ~CS50. So if you look here, this
is your path right here. That’s your path. So you could seriously
just go cd ~/CS50. Boom, you’re there. What’s an easier way to do it though? That was perfectly correct, but
what’s a slightly easier way to do it, because a lot
of times you’re going to be jumping in directories,
jumping out of directories. So let’s go back. cd supersection. So let’s say I want to
get back real quick. You can do something like cd dot dot. That’s going to pop
you back up real quick. So cd supersection. cd files. Let’s say I’m here. By the way, clear is good, because my
window was getting a little bit messy. So ls, so I don’t want to
be here anymore. cd dot dot. Take me one up. Cd dot dot takes me one up. Cd dot dot and back to my home. >>OK, so cd, ls. Probably the most important
things, because you’re going to need to know where you are. ls is going to tell you
where you are, and cd is how you’re going to jump around.>>You can also do some other cool stuff. So for example ls. ls is just going to show you
the directories in your file. It’s also going to show you the files. But it’s just going to
show you what’s there.>>If you wanted something
a little bit more cool, and you’re going to actually do
this in a couple weeks. ls dash l. That prints out a
little more information. Can anybody guess on the left hand
side the drwx, rwx and stuff like that. Does anybody have an idea
what that might mean?>>STUDENT: Permissions.>>JASON HIRSCHHORN: Yeah, perfect. Permissions. So you’re going to
have to deal with this when you do web programming later on. If you’ve ever gone online,
and you’ve clicked on an image or clicked on a field,
and it said permission– like permission not allowed, or like
permission not granted or whatever, that’s because when
they’re programming, they haven’t set these permissions
to let you do that. So where that comes from. >>OK, so that’s cool. So we jumped around. We can look at stuff in our directory. But how do we make stuff? I’ve got directories here. That’s awesome. How do I make a new directory? Say I want to make a new
directory for right now. You can do something like this, mkdir. So make directory. So what do you want to
call this directory? STUDENT: Awesome. JASON HIRSCHHORN: Awesome. I love it. Make directory awesome. And there you go. So we need a list. You have a new directory. And you can tell it’s a
directory in your appliance because it’s got this color, right? So it’s like a bluish
purple or whatever. So let’s change. Let’s change directory. Let’s go into directory–
yeah, what’s up? STUDENT: Is there a list of
all these [INAUDIBLE] online? JASON HIRSCHHORN: Yeah, I’ll put
them up on– we’ll have slides. It’ll be a PDF. We’ll put them up for everybody. Yeah, sorry it’s a lot of commands. if you want me to slow down, or
go back, or anything like that, definitely let me know. It’s a lot to absorb right away. What’s up? STUDENT: Can I ask a
potentially stupid question? JASON HIRSCHHORN: There’s
no stupid questions. STUDENT: There are. A directory is for just looking
at information where you store it.>>JASON HIRSCHHORN: Yeah.>>STUDENT: Is that essentially
what you’re doing? So when we make something,
hello world or whatever, it’s stored in the directory, yes?>>JASON HIRSCHHORN: Yeah.>>STUDENT: Is that true for everything. Everything is stored in the directory.>>JASON HIRSCHHORN: Yeah. Whenever you make it,
whatever directory you’re in, it’s going to basically
store it in that directory. STUDENT: And when we’re calling it,
we’re calling it from the directory.>>JASON HIRSCHHORN: Yeah, so
the directory is where it es. So it’s kind of like if you have
a new folder on your desktop and then you write a text
file, put it in that folder, that file is in that folder. Does that make sense?>>STUDENT: Yeah.>>JASON HIRSCHHORN: Yeah, what’s up?>>STUDENT: Can I just ask you to
make it a little bit bigger?>>JASON HIRSCHHORN: Oh, yeah. Sorry. The projector is so bad. We had so much trouble
with this earlier. Is that better? Is that too big?>>STUDENT: No, no it’s not.>>JASON HIRSCHHORN: No, it’s not. Is it too small? What’s up?>>STUDENT: I’m sorry, what’s
the command for clearing?>>JASON HIRSCHHORN: Oh, clear. If you want to clear, just
type clear. clear enter. On the last appliance,
what it basically does, it doesn’t clear your information,
it just kind of shift everything down one screen. So if you typed clear– for
example let’s say I’m scrolling up. I can scroll up. This is everything I did. But if I type clear, and then
I scroll up, here’s my stuff. So it’s not erasing it, it’s just
basically giving you a new frame. >>STUDENT: How do you
[INAUDIBLE] a directory?>>JASON HIRSCHHORN: We’ll get there.>>[LAUGHTER] Slow your roll. All right. OK, so so we’re in a
directory right now. So David, I think in lecture
he did some cool stuff. So let’s write a program. So you can do that a
number of different ways. You can use any kind of text editor. You can use Nano, you
can use Vim, Emacs. Those are slightly more complicated. Or you can use something in
here that’s called gedit. Sorry. gedit is not happy. So gedit is basically
just a text editor. Just like I text edit word
processor or something like that. So it’s got a nice
interface just like this. So you do something like that. So actually let me give it
name because I never did that. >>So what do you want to
name our first program? It’s just going to say hi.>>STUDENT: [INAUDIBLE]. >>JASON HIRSCHHORN: You lack imagination. OK, gedit. Hi. I’m going to do .txt. OK, so here it is. So let’s write a program. So I think you saw this in lecture. You need a main function. Boop. So this is just a main function. Bigger? Yeah, sorry. OK so you need a main function. I think I’m jumping
the gun a little bit, but it’s never bad to get
used to this kind of stuff.>>So a main function,
every C program you’re going to write for the rest of this
course will have a main function. Does anybody know why? What’s up?>>STUDENT: Start.>>JASON HIRSCHHORN: Exactly, so it
tells your program where to start. So if you don’t have a main function,
and you compile this and stuff, it’s not going to know were to start. Main is always where it starts. So you have a main function, and then
does anybody remember how to print?>>STUDENT: printf. JASON HIRSCHHORN: Yeah, printf. printf hi. OK, now I have set you guys up
to fail, but will this compile.>>STUDENT: No.>>JASON HIRSCHHORN: Why? There’s multiple reasons why. Sure.>>STUDENT: It doesn’t have
the standard [INAUDIBLE].>>JASON HIRSCHHORN: Perfect. So what’s this called up here?>>STUDENT: [INAUDIBLE]>>JASON HIRSCHHORN: It’s
a header file, right? It’s a header file. And you’re exactly right. Both of you are right. So to include that, I just do pound
include standard input output .h. Am I good to go now? Would this compile?>>STUDENT: No. JASON HIRSCHHORN: Why not? STUDENT: It’s a [INAUDIBLE] text. JASON HIRSCHHORN: Yeah, awesome. So I just made it. So I saved it, but gedit hi.txt. So now if I do ls, here it is. Right here. Oh, it’s not in awesome. I must have gone out. Oh, perfect. How do we put this in awesome? So I don’t want it here in my home
directory with everything else. I want to put it in the folder. How do I do that? >>Not too hard. Move. So we’ve already learned ls, list. cd to change directories. cd dot dot to bump out of directories. And now we’re going to
start moving around files. So to move just move hi.txt, and
I’m going to put it in awesome. >>Just be careful. Whenever you’re writing in the
command line, everything matters. So uppercase matters. If I had done lowercase a, it’s
not going to know what’s going on. So everything matters
to be very specific.>>So run that ls. It’s no longer here. Let’s change directory into awesome. ls. Boom, it’s there. And somebody mentioned why
this would not compile. Why is not going to compile again? Yeah?>>STUDENT: It’s not in a .c.>>JASON HIRSCHHORN: Perfect. It’s not a .c, s so how am I
going to rename it to a .c? What’s an easy way to do it? Let’s use the command we just did. So we’re just going to
move hi.txt to hi.c. ls hi.c. And then just for fun, let’s
just jump into this real quick. hi.c. You’ll notice it’s nice and colorful. So these are just kind of helpful
colors when you’re programming. So the int and the void,
those are data types. We’ll go over those in a second. Hi is a string. But useful colors. So if you ever accidentally
choose the wrong extension, you should have these colors. What’s up?>>STUDENT: How can I copy into a
file [INAUDIBLE] into another file? JASON HIRSCHHORN: Oh, I’m
going to show you that too. So copy remove are on the dock. Let’s make this. Make sure it works. Make hi. Works, run it. So ./hi awesome. That is your first program. Let me make it bigger. So a really good– Yeah, what’s up? STUDENT: Why would it be
that when I take gedit, it doesn’t give me another
line for gedit hi.txt? It just puts the cursor to a line. STUDENT: Yeah. The [INAUDIBLE]. >>JASON HIRSCHHORN: Wait,
so say that again.>>STUDENT: When I type gedit, the
first time, the first line– JASON HIRSCHHORN: Up
there at the very top? STUDENT: Has no J Harvard
appliance, and the cursor is just all the way to the left. JASON HIRSCHHORN: All
the way to the left? STUDENT: Yeah. Does anyone else have that?>>JASON HIRSCHHORN: Oh, well J
Harvard is just like the username. So that’s just the standard username.>>STUDENT: It stops responding. JASON HIRSCHHORN: Oh,
it stops responding. OK, so what you have to do is
you have to close out of gedit. So close out of gedit,
and it should return. So is gedit open on your
appliance right now? If you exit out of it, it should
return and you should be good to go. Did that work? >>STUDENT: So did you use
the gedit command to open– JASON HIRSCHHORN: No, the first
gedit command was a mistake. The first gedit command,
I to do gedit hi.txt. So I give it a name, so gedit hi.txt. hi.txt is the name of the file. The first one was just
a mistake, and then I quickly closed out of the program. So the question was
if I do gedit, I’m not going to be able to really do
anything else in my command line until I close that gedit, so I
think that’s what they ran into. So close the gedit. It’ll return out of that
program, and then you’ll be able to actually type
in the command line again. Is that good? Dope. So just make sure to close
it if you run into trouble. >>OK, so a really good question
a second ago was copying. So now I have hi.c. But let’s say I want to change it. I want it to be slightly better. I want to say hi class. But I want to keep this template. I don’t have to rewrite
the entire program again. I want to keep the template. How do I copy? >>Easy. So cp hi.c newhi.c. Now I have two. And then I can open up newhi.c,
and instead of just saying hi, I’ll say hi class! So I’ll go back. Exit out. Go back to my command line. ls make newhi. Run it. There. It’s better, much better. So if you want to copy something,
easy as that. cp is copy. >>So we’ve gone over moving,
making directories, copying. Let’s see what else should I go over? Oh, yeah. Let’s go over this. So this was awesome. Let’s say I wanted to– oh wait. I’m going to go back into awesome. >>Let’s say I don’t want
these files right here. Let’s say I want them in a new
directory like in directory awesome. I want to start organizing,
so now I’m going to have a new directory called hi. It’s got all the programs
I write called hi. So how do we do that? >>STUDENT: Change directory.>>JASON HIRSCHHORN: Yeah. hi. Boop. mkdir hi_dir. It complained because I
already had a hi in here. So now I have a hi directory. So now I want to move everything,
all the programs I wrote, I want to move them
into the hi directory. Just clean stuff up. How do I do that?>>STUDENT: [INAUDIBLE].>>JASON HIRSCHHORN: Moving it, right? So let’s move. Yeah?>>STUDENT: What’s the difference between
hi and hi.c that we already have. JASON HIRSCHHORN: Oh,
hi.c is the actual. So that’s the file that you wrote. So if you were going to
open it up in gedit or Nano, that’s what you’re typing in. When you compile it, then
you get an executable file. That’s hi. So if you opened up hi, that’s going
to be a bunch of gibberish to you. It’s basically going to be
bunch of computer instructions. What’s up?>>STUDENT: What do all the colors mean? The blue is probably directory. What’s yellow?>>JASON HIRSCHHORN: Those
are the executable files. So when I did make hi.c, it
gave me– so I can do it. Jumping the gun. So remove hi yes. So then let’s do ls. I don’t have hi anymore. So when you make, that’s when you
take the program that you wrote and you make a executable file. So make hi.c– well, make hi. I think he went over this in lecture.>>These are bunch of
compiler instructions. So instead of having to actually
write clang with all these. These are flags. You just write make. So this makes the executable files. So when you do ls, now you have hi. So this is what you’re
going to actually run. And so you run that by doing
./hi, and it runs your program. But if I try to do that with
hi.c, no, it’s not going to work. Yeah, does that make sense?>>STUDENT: Mm-hm.>>JASON HIRSCHHORN: Cool. Wait, I wanted to say something else. Just real quick, in case
anyone is interested, when you jump out of a
directory, it’s the dot dot. Does anybody have an idea of what
maybe just the singular dot means? STUDENT: [INAUDIBLE]. JASON HIRSCHHORN: It’s
actually the current directory. So it’s your current directory. So I’m here. I’m in awesome. If I do cd dot, it doesn’t do anything. It takes me to my current directory. So a little redundant, but you have
to do that, so ./ your program name, in order to run your
program, you have to do that. OK. >>STUDENT: If you do cd dot
dot dot, however many dots will it just take you up
that many directories?>>JASON HIRSCHHORN: You mean
like cd dot dot dot dot? STUDENT: Yeah. JASON HIRSCHHORN: No. So actually Remember I did ls -l? If I do something else, I can ls -a. So what this says is -a
shows you everything. So it shows you things that are
hidden and things that aren’t hidden. So if you notice that when I did
just ls, I just saw that stuff, the stuff I made. If I do -a, it’s going
to show me everything. And so current directory,
directory above it. Does that make sense? Yeah?>>STUDENT: But those aren’t
actually in the directory. The current directory isn’t in itself,
so why show those with dots inside?>>JASON HIRSCHHORN: You mean it’s
like it’s not in the directory.>>STUDENT: Right. So we have a list of things
that are in the directory.>>JASON HIRSCHHORN: I think
it is in the directory. Like -a shows everything, even if
it’s hidden inside the directory. So I think it’s just a route to the
current directory if that makes sense. It’s a little recursive. It’s like Inception. What’s up?>>STUDENT: So you said
-a shows [INAUDIBLE]? JASON HIRSCHHORN: -a, yeah. >>STUDENT: What makes it [INAUDIBLE]? JASON HIRSCHHORN: Well, for example,
let’s go back up to the top. It’s usually stuff you shouldn’t mess
with and you don’t need to worry about. So this is kind of the home. If I do -a, I get tons of stuff. All that stuff. And you don’t need to mess
with any of that stuff. Definitely don’t start going
in here and just remove stuff.>>[LAUGHTER]>>Yeah, this was a little bit more
comfy stuff, but it’s good to know. I think it’s good to know. But if you never want to type -a
in the entirety of this course, just forget it. What’s up?>>STUDENT: What is the ls -l? JASON HIRSCHHORN: That
shows you permissions. Basically you use that
for permissions, so -l. It shows you a little
bit more information, but again, this is a little
bit more comfy stuff. These are just permissions. So the permissions of the folders. So just for example, let’s say I made a
folder, so the d represents directory. So those are directories. And then read, write, execute. So those are permissions for
I think user, group, world. Totally going too far. You’re going to go in this like
weeks from now, but just so you know, those are permissions. What’s up? STUDENT: Can you clear rather than
just skip to the next thing, can you– JASON HIRSCHHORN: You
mean actually delete? I don’t know. I never do that. Why would you want to do that? Why would you do that? I like it. You live on the edge. You’re like I don’t want any of this. That’s intense. STUDENT: [INAUDIBLE]. JASON HIRSCHHORN: Yeah, we can do that. Sorry, messing with the size is
totally freaking me out right now. Let’s go up. Yeah, so now I can’t go up anymore. So everything I taught
you is gone forever. Thank you. We made our directories. We made our files and stuff like
that, so we still have all that stuff. OK, I’ll go fast. >>So real quick. So let’s say I’m in awesome. Let’s say I wanted to move
everything to the new directory because I just want
to organize it, right? So how would I do that. Move. We’re going to use move again. So move hi.c. And what you can do is
you start listing stuff, and the last thing you list is
where it’s going to move it. So move hi.c, hi newhi newhi.c, hi_dir. And so it moved everything into
the last thing you mentioned. So then change directory, hi_dir ls. Everything is in there. So it’s nice and more organized. >>OK, let’s say I hate
my original hi program. I want to get rid of it. How do I get rid of it? What’s up?>>STUDENT: [INAUDIBLE]. JASON HIRSCHHORN: Let’s say I just
want to remove the program, so just hi. No, you’re right. So it’s rm, but you do rm hi. It’s going to give you a little warning. It’s going to say are you sure you
want to delete this, and yes I do. If you don’t want to
be prompted like that because you don’t have time
for warnings, remove -f. This is another flag. It’s like the -l. It’s like the -a. These are just flags that
you’re giving to these commands. So remove -f. -f f means force. So force it. I do not want to be prompted. So remove -f. Let’s do hi.c. Just got rid of it. It didn’t tell me. >>But let’s say instead I’m
done with this directory. I want to get rid of directory. So I do remove hi_dir. No, that doesn’t work. No, so it won’t let
me remove a directory. Does anybody have an idea?>>STUDENT: Is it remove dir? JASON HIRSCHHORN: So remove like this? >>STUDENT: No. Just the [INAUDIBLE]. JASON HIRSCHHORN: No,
right now there’s no hi. There’s just hi_dir. So this is the directory. So actually what you want to do
is– oh, do you have an idea? I see like a half hand.>>STUDENT: [INAUDIBLE]. JASON HIRSCHHORN: OK. >>STUDENT: [INAUDIBLE].>>JASON HIRSCHHORN: OK. So if I want to remove
a directory, remove -r. That’s recursive. So remove that directory recursive. So I want you to go into that directory,
remove everything in the directory, and then get rid of the directory. And then I’m going to
f as well, because I don’t want it to prompt me with warnings
after it removes every single file. So remove -rf hi_dir. Boom, it’s gone. Just be careful where you use that. I’m going to use it
here too. -rf awesome. It’s gone. Be careful where you use that. If you use that here and type something
like this, don’t ever do that. That will delete everything on your
appliance without ever warning you. And I’ve done that. Grading problem sets at 4:00 AM, I was
trying remove problem set directories, and I did this. And I was like oh my god. Don’t do– I tried to cancel it. You can cancel. Command C just cancels everything,
but I already deleted like 75%. So I ruined it. And people do that. Everyone does this. Don’t do this though this semester. It sucks. I’m nervous having it here. I’m going to delete it.>>[LAUGHTER] Can you imagine? OK, questions. Yes?>>STUDENT: So if we do everything
you’re doing here, is this the same as just going into actually
file save as [INAUDIBLE]. JASON HIRSCHHORN: Oh,
you mean through gedit. STUDENT: Yeah. JASON HIRSCHHORN: Yeah it is. gedit is useful. So gedit is very useful. In this class, you can do a
lot of things through gedit. You can save. You can rename. You can move files and stuff like that,
but just truthfully when you go to 50– well you are in 50– 51, if you go
into 61, this is going to be your life. This is the command line. This is where you will program
for the rest of your life, so definitely use gedit if you want. Definitely a good way to start. For the first problem sets, definitely
use, but every once in a while try to get more used more comfortable
with command line arguments. But you can, definitely. What’s up?>>STUDENT: So when we want to delete
one of the files we’ve made, there are two ways. [INAUDIBLE]. Is there a way when you’re
deleting a directory to ask for prompt so you
don’t end up deleting it? JASON HIRSCHHORN: rm -r. So don’t force it. So rm -r. It’s going to go through recursively,
delete everything, prompt you after everything, which
will drive you nuts. Like I almost exclusively do rm
-f and rm -rf, just constantly. I’m like I don’t have time. I’m a busy man. All right, what’s up? Yeah.>>STUDENT: Is there an undo, or a
trash bin, or anything like that? JASON HIRSCHHORN: No. [LAUGHTER] I could go back and get my
appliance that I just deleted, no. No, no, no. If you do rm, it should be gone. What’s up?>>STUDENT: So you can
[INAUDIBLE] together. JASON HIRSCHHORN: Yeah, just like that. So for example, you saw the rm -rf. Let’s say I want to
do ls -la, it shows me everything, even hidden
things with their permissions. >>STUDENT: So then r, the -r
flag, if we don’t have that, we can’t delete a directory, right?>>JASON HIRSCHHORN: Right.>>STUDENT: So that says that we want
to do something with the directory? Is that what that means?>>JASON HIRSCHHORN: -r means recursively. So a directory is going to
have files in it, right? So what you want to do is you
want to go into that directory, delete everything, all the
files in that directory, then pop out and delete
the actual directory. So the -r means recursive, do it
all recursively if that makes sense. Yeah.>>STUDENT: So directories have
[INAUDIBLE] and just delete it. Or do you still–>>JASON HIRSCHHORN: Let’s try. So mkdir empty. Remove empty. No.>>STUDENT: No, so you still need the–>>JASON HIRSCHHORN: Yeah, always. So -r, -rf, empty. Awesome. Anymore questions? I think that is your crash
course on command line stuff. Any questions, because we’re going to
jump over to data types, and loops, and all that stuff? What’s up?>>STUDENT: I don’t have CS50
showing as one of my directories. Is that normal?>>JASON HIRSCHHORN: Oh, yeah. That’s mine. Yeah, that’s where my
solutions to your stuff is.>>[LAUGHTER]>>So yeah, that’s absolutely normal. I think you’ll probably
just have these four. I think that might be normal. Yeah. OK, any more questions? I know that was super fast. What’s up?>>STUDENT: I saw the blue green color. What is that?>>JASON HIRSCHHORN: That means directory. STUDENT: And this blue?>>JASON HIRSCHHORN: Oh,
you mean this blue green?>>STUDENT: No.>>JASON HIRSCHHORN: This blue green?>>STUDENT: No. When you listed– JASON HIRSCHHORN: Oh. Like this?>>STUDENT: Yes, the icons.>>JASON HIRSCHHORN: Oh, this. I don’t know. What does that mean? So it seems to be
pointing to a directory.>>STUDENT: It almost looks like an alias.>>JASON HIRSCHHORN: It might be. You don’t need to know that. Anymore questions? Awesome. If you have any questions,
I know that was a lot. A lot of this information
will be online. I’ll put it online for you guys. Section B will be online as well. Or if you have questions,
just come up and ask me. Here you go. So now Sharon and
Hannah will go over some of the logic behind problem set one. >>SHARON: Oh my god. Troubles.>>HANNAH: In the meantime, I’m Hanna. This is Sharon. We are both CS50 TFs, and
we’re going to give you a little intro on a couple important
things for problem set one. Everything from data types
to conditionals to for loops. Loops in general.>>SHARON: And in the end, we
will take a look at P set one, and what you have to know for it. So let’s start with data types. You guys should be familiar with the
ones that are highlighted in blue. So we can start with ints. ints are integers, so one, two,
three, four. floats, floating points. Like integers with decimals,
so 5.2 or even 5.0. Chars are characters like A, B, C.
And a string, you guys all know. Like CS50 or hello and world. >>Bool is a Boolean, so we have
true and false as our Boolean. So if something computes to truth–
5 equals 5, that computes to true, and if we have 5 equals 4, that’s
false, so that computes to false. And here we have the associated
size of all of these data types. And you will have to know
this for your quizzes, so this might be helpful to
remember, but we’ll post these. Actually they’re already posted. So you should be familiar with
the ones highlighted in blue. >>JASON HIRSCHHORN: Real
quick, just in case you want to know the difference
between a char and a string. So when you’re writing stuff,
whenever you have something like “hi,” that’s going to be a string. So if there are two
quotes, it’s a string. But if I were to an “h”
like that, that’s a string. But if I only have one
quote, that’s a char. And so what’s the
difference you might ask. Well, the difference is
this is one byte of memory. This is 4 bytes of memory. >>SHARON: float? Describe it again? OK, it’s kind of like a number
that has a decimal point with it, so 5.2 or even 5.0 is a float
versus just 5 is an int. STUDENT: [INAUDIBLE]. >>SHARON: Oh, it can have many
decimal points, so 5.675 to 3.1415. Yes?>>STUDENT: Do we need to know
all the sizes for this? SHARON: Yes. STUDENT: [INAUDIBLE]. >>SHARON: You don’t worry about
the white ones just yet.>>HANNAH: The notes are
posted at CS50.net/sections. STUDENT: Just out of
curiosity, is there a utility to knowing the size of the files you’re
calling so that we should know it? >>SHARON: So these are
the data types, but yes. So in this case, which one would
take up more memory or more space?>>JASON HIRSCHHORN: So
for a concrete example, in I think problem set
four or five, we’re going to give you a bunch
of data, and you basically have to traverse that data. So it’s useful to know that
if you have so many integers, that’s going to be what. So let’s say I gave you ten
integers, what’s the size of that? STUDENT: 40. JASON HIRSCHHORN: 40 bytes, right. So you might need to know that
OK, I need to jump 40 bytes. So let’s say instead it’s 40 chars,
then– let’s say I gave you 10 chars, then you know, OK, I only
need to jump 10 bytes. So it’s very useful to know the
size of the actual data types, because a lot times you’ll
be jumping around the data, so you need to know how far to jump. >>STUDENT: What’s the difference
between the quotes and one quote?>>JASON HIRSCHHORN: OK,
two quotes is a string. One quote is a char.>>STUDENT: Like operationally,
what’s the difference? They both say hi. JASON HIRSCHHORN: Oh, sorry. This is just h. Sorry, this is not clear. >>STUDENT: So the top one– JASON HIRSCHHORN: This
is a string, yeah. So it’s two characters, right? String, hi. But let’s say I just
had a single character, if I put two quotes around
just h, that’s a string. This is a string h. Does that make sense? So this is four bytes. But let’s say I got rid
of that an only put one. One quote. This is now a char, a char h. So exact same thing,
different data type. This is only one byte, whereas
the string was four bytes. Yeah. STUDENT: So if you were trying
to store a word like David, you would want to use a string because
regardless of how long the string is, it will only take four bytes
unless it’s a long long or a double, in which case– so if it
extends beyond four characters, then you want to make it into string. Is that the general logic behind. JASON HIRSCHHORN: If it extends
beyond one character, it’s a string. >>STUDENT: So there’s no [INAUDIBLE]
to storing a four letter word as four individual or three letter
word as three individual characters because that will only be three bytes,
as opposed to the three part string. JASON HIRSCHHORN: You could do that. You could do that, but I don’t
think that’s really worthwhile, because you’re only saving one byte. You know what I’m saying? In the big scheme of things,
that won’t really matter. But for example, printf, if
you’re printing f and you have three chars you can print
that out– let’s say you C-A-T, you can print out cat just by
doing one char one char one char, or can you print out string cat. It’s the exact same thing. So you could do it like that, but–>>STUDENT: It’s not going
to save you that much.>>JASON HIRSCHHORN: Yeah. The headache that it would
induce is not worth it. Yeah?>>STUDENT: For example,
you had the h with one versus two, the single or double quotes. Why would you want to save
a single letter as a string. What purpose does that ever serve? JASON HIRSCHHORN: I don’t know. I mean, it really
depends on the program. For example, I think later on
you’ll get into command line stuff. So for example, instead of me
asking you for an integer– sorry, can I speak into you? HANNAH: Yes. >>JASON HIRSCHHORN: So
for example, instead of me asking for something
with get end, which I think you might have seen a lecture,
there might be a command line. You just type it in at the command line. It would be like -/hello4 or
something, or helloh h, whatever. You do it at the command line
instead of being asked for it, that command line is always a
string, whether it’s a four or an h, that is always a string. So that’s one example of when you might
have a singular letter or a singular number represented as a string. SHARON: All right. And then here are some basic operators. Hopefully you’re familiar with
the first four just in general. So there’s adding, subtracting,
multiplying, and dividing, and make sure you use
the right keystrokes. >>And then there’s also modulo, which some
of you might not be very familiar with. And what modulo does is that
if we take this example 4 % 2, it takes the remainder of
what that does in division. So 4 divided by 2 is 2,
and there’s no remainder. 4 divide by 3 is 1 and
1/3, and so remainder 1. So it computes to one. And then 4 % 5 is the fraction
4/5, and the remainder is 4. Does that make sense? OK, cool. And we follow PEMDAS here too. >>So some Boolean expressions. So you guys have seen equals equals
to compare, say, two numbers. So if 5 equals equals 5,
that computes to true. Make sure you have only one equals sign. For not equals, it’s
bang or exclamation.>>HANNAH: Two equal signs
when you do equality.>>SHARON: Oh, yeah. Make sure you don’t
have one equals sign. And then for not equals,
it’s bang equals. And then you can look through
for less than, greater than. And then we have logical and
and logical or right here. And what that does is, if
you remember in Scratch when you had that block
that said and, and then you could fit two different pieces
there, that’s what this and does. And make sure you do two ampersands. And this key you can find towards the
right, right under your Delete button. Question?>>STUDENT: What was the [INAUDIBLE]? >>SHARON: OK, so if you only do
one, you’re comparing bits. So if you remember what bits
are, they’re 0’s and 1’s, and so that’s comparing something else. So we’re going to focus on this for now.>>JASON HIRSCHHORN: So one equals
is like assignment, right? So like int x equals 4. You’re saying that variable x equals 4. So 1 equals is assignment,
but the ands and the pipes or the bars, just like Sharon
said, they’re bitwise operators. You don’t need to worry about that. >>STUDENT: When do you use logical
and and logical or again? I just forgot when you do that.>>HANNAH: Sure, so if you ever want
to check if two things are true, for example, let’s say I want to check
that a number is between 5 and 15, I would say make sure that
the number is greater than 5. So let’s say if x is greater
than 5 and less than 15. So in order for that whole
statement to evaluate to true, you need both of the kind of sub
statements to evaluate the true. With or you only need
one of the two or both. STUDENT: Great, thank you. >>SHARON: And also in Scratch, I’m
sure in a lot of your projects you had if touching the edge or if
touching another sprite then bounce or something. HANNAH: All right, so why do we
care about all of these Booleans? We have these structures that you’ve
seen in Scratch called conditionals. And conditionals are anything of the
form if some condition or some Boolean is true, then do the code
between these curly braces. So you can see on the right
here is the Scratch block. You have this if then, and anything
that goes into that little shape that looks like this– i
don’t know what you’d call that– that’s going to
be the Boolean or condition.>>So again, a Boolean or
condition is anything that either evaluate to true or false. And again, you can combine Booleans. You can have, again, x is greater
than 5 and x is less than 15. Or you can just have one
of those x is less than 5.>>OK, so in C It’s on the left. It’s just the keyword if. In parentheses, the
condition or the Boolean. And then the code in between
those two curly braces will only execute, will only run if
that condition or that Boolean is true. Does that make sense? Awesome. OK. And then as you may
have seen in Scratch, we can also add on an
else, which is basically anything between the
curly braces under else will only execute if
the condition is false. Make sense? Any questions on these two? Awesome. >>Cool. So here’s a quick example. Let’s say we want to
determine, based on the time, whether we should say good
morning or good evening. I’ll say if it’s before 12:00 noon,
we’re going to say good morning, otherwise we’re going
to say good evening. And when I said that otherwise,
that’s equivalent in C to this else. So we’re going to check if the
military time is less than 12:00, say good morning. Otherwise say good evening.>>STUDENT: So we’d actually get that input
for military time with the get string. HANNAH: Right. Did Somewhere else we’d actually
have to provide that information. Right here it has no value. We never even declared it. I assume that somewhere above
this program I declared the time, or asked the time, or–
anything else with this example? Awesome. >>OK, now we have in addition
to that simple if else format, we have two other different structures. So first we have the switch
statement, and here’s the general form of a switch statement. You have the keyword switch the
same way you have the keyword if. And then an input, in this case n. So this can only work with integers. So that input n has
to be an integer, OK? And we’re going to
follow one of these cases depending on what that value n is.>>So in this case, you first compare
is n equal to constant one, if it is, do everything
kind of indented over here. I can point to it because I’m too short. If it’s not equal to constant one
and is instead equal to constant two, we’re going to follow that second block,
and we can do this for as many times as we want, and then that default is if
it didn’t match any of the above cases, execute that code. Any questions here? This one is a little more complicated. Yeah?>>STUDENT: Break signifying what?>>HANNAH: Sure, so once we enter
that block, if we find, let’s say, our n is in fact equal to constant one,
we enter that block after the colon. We do whatever is on
that line of commenting, and then we break, meaning we get out
of this switch statement entirely. STUDENT: OK. >>HANNAH: Mm-hm?>>STUDENT: Is the indentation
necessary, or do you need [INAUDIBLE]? HANNAH: Sure. So, as always, indentation
is not strictly necessary. The computer doesn’t
care whether you indent. We, as people who are
reading your code, do care. So it’s a lot easier to
look up at the screen and see, oh, I know exactly what
happens when n is equal to constant one. If I had random indents, I
wouldn’t be able to tell so easily. Yeah?>>STUDENT: So if wanted a user to
choose one of the options that we have on the list, would
we use, I guess, [INAUDIBLE], but get an integer from
them and put that into n? HANNAH: Exactly right. So let’s say we wanted
to check what integer. We said oh, enter an integer
between 0 and 5, let’s say. We could ask for that
n, ask for that value, and then have each of those cases. And again, this is something we could
very easily do with if statements, right? We could have if equal to case
one, if equal to case two, if equal to case three,
so on and so forth. This is a little bit faster
and a little cleaner. It’s kind of just a nice structure now. STUDENT: Faster as in writing? Or does it run the
computer a little faster?>>HANNAH: It’s runs a little faster. Yeah?>>STUDENT: So the double
dash is just comment or–>>HANNAH: Oh, I’m so sorry. I didn’t point that out. OK, if you have that //, is a comment. Just // is a comment. So any time the computer
sees that, it’s going to say OK, anything that follows
this, I’m just going to ignore. I’m going to pretend you
never even wrote that.>>STUDENT: So this [INAUDIBLE].>>HANNAH: This does absolutely nothing. But if I wrote something like on the
first comment I wrote instead printf say congratulations you entered
constant one, that would be something. Yeah?>>STUDENT: So in real life, the
case would just simply be a number and then [INAUDIBLE]
would be another integer.>>HANNAH: Exactly.>>STUDENT: And do you need the ellipses?>>HANNAH: Sorry?>>STUDENT: Do you need that?>>HANNAH: Oh, no. I’m sorry. That was just to indicate that you can
go on for as many cases as you want. Let’s do a concrete example that
might make things a little more clear. OK, so let’s say I say,
OK, give me an integer n that represents a class number,
specifically a computer science class number. So if you give me 50,
I’m going to say great. CS50 is an introduction to computer
science, and then I’m going to break. So that means I jump out of
this whole switch statement, so now I’m done running the code, OK?>>If you gave me instead 51, I’d
print the second statement. And then if you gave me some
number that wasn’t 50 or 51, I’m going to say sorry, I’m
not familiar with that class. Yeah?>>STUDENT: You don’t have break.>>HANNAH: I’m sorry, I don’t have break?>>STUDENT: What if.>>HANNAH: Oh, what if
you don’t have break? Excellent question. So what would happen is you would go in,
and you would check am I equal to 50? And let’s say, yes, you were equal
to 50, you’d print the statement. And then you would continue executing,
so you would say am I equal to 51? And you would go on and go
through every case like that? Yes?>>STUDENT: Is the default line
analogous to saying else? HANNAH: Exactly. Very good. It’s like the kind of catch all.>>STUDENT: So if you didn’t have break
and then one of the case statements was true, and then it
said increase n by 1, then it would automatically
make the next one. It would check case 51, and
then display that as well? HANNAH: Yeah, I think that would work. So you could kind of get messy, so
a break is a good thing to have. Yeah? STUDENT: Without break
would it do the default? HANNAH: That is a good question. JASON HIRSCHHORN: I think it just runs. So if you don’t have break– so
let’s say I did 50 and it checked 50, and it would print that out. Print out CS50 as introduction
to computer science. You don’t have break, it should go
and keep going until it hits a break. So if there’s no break,
it’s going to keep going. It’s going to print everything else out.>>HANNAH: So I guess that would
be including the default.>>JASON HIRSCHHORN: Yeah.>>HANNAH: Good question. Yeah? STUDENT: Is case considered a function? HANNAH: I’m sorry? STUDENT: Is case considered a function? HANNAH: Is case considered a function. I would hesitate to say that.>>JASON HIRSCHHORN: So normally
when you see functions they’ll be in curly braces. So for example, when you look
at your code, for example, main. It was main open paren,
then void, close paren. Functions– they’re basically
always you’ll see parentheses. So case if case doesn’t
have any parentheses, that’s your clue that
that’s not a function. But it’s not. Yeah?>>STUDENT: So then does that
make switch a function?>>HANNAH: Does that makes
switch a function.>>STUDENT: In the sense
that you put something in, it gives you one of the cases. JASON HIRSCHHORN: I
wouldn’t call it a function. Not everything with
parens is a function. I mean that’s a clue
that it is a function. I wouldn’t say switch is a
function because it’s not really returning anything. You’ll get into that more. >>HANNAH: Yeah, just think
of it as an if structure. STUDENT: Can you nest
this in an if structure? HANNAH: Yeah, you nest
pretty much anything. >>STUDENT: Barry said the quotes
are sort of like the same as else. So can you replace the quotes with else? >>HANNAH: Not in this particular
structure because the switch statement is expecting the word default. The computer knows that default
means something special, that means the catch all. Anything else on this. We have one more to get through. Yeah?>>STUDENT: [INAUDIBLE]. HANNAH: Difference between
using a semicolon and a colon. So a semicolon is always to tell the
computer I am done with this line. You can go ahead and execute it. This is a complete line. A colon is going to, in this case,
bring you into a particular block. So semicolons are
always use to end lines. Colons are used for a
variety of other cases.>>STUDENT: Is this equivalent to
just saying if n equals 50, then blah blah blah. HANNAH: Mm-hm. STUDENT: It’s just the same [INAUDIBLE]. HANNAH: Exactly. And we said it runs a little bit faster. If we did not have the break,
it would be like if, if, if. If we do have the break, it’s
like if else if else if else if. Cool?>>JASON HIRSCHHORN: So when
coding, you can just use if else, but I think last year on the quiz we
had them write a switch statement, so just in case. HANNAH: But definitely nothing wrong. We’re not expecting you to worry
about performance crazy things. Just if else is totally fine. These are just good
things to be aware of.>>And here’s our last one that kind of
falls under this conditional category. We have something of the
general form condition ?, and then a little piece of code that
happens if the condition is true. And then colon, a little
piece of code that executes if the condition
is false, semicolon. We’re done with that line. So syntactically it’s a little bit ugly. We’re going to go through an
example I think that’s most clear. So we want to assign
the string professor to one of these two values, either
David Malan or not David Malan, OK? So you should be familiar
with the string, professor=. We’re going to assign a string
to the variable called professor. >>Now we want to check a
particular condition. In this case, our condition
is class_num==50. And now might be a
good time to point out when we have string professor
=, that’s one equals sign. That’s assignment. Whereas in class_num==
50, that’s two equal signs. That’s a quality check. So we’re going to say is the
class number equal to 50? If so, assign Professor David Malan. If not, assign professor
to not David Malan. Any questions there? Again, this is just something
that’s good to know. You could do this with if else. A good practice problem
might be to, when you go home, right the same exact
conditional in an if else form, because you can do that. Any questions here? All right, I think we’re
going to go on to loops. Awesome. >>SHARON: OK, let’s talk
about while loops. So first on the left you see here–
OK that says while, while (condition). And then you do this–
can we change this?>>JASON HIRSCHHORN: It’s cut off. >>SHARON: OK, and then there
are also curly braces. So pretend that curly
braces are there above and below the do this again and again. So if the condition in those
parentheses evaluates to true, then you should keep doing
whatever is in the while loop.>>So for example, we could
do a practice problem. I’m wearing two earrings
right now, so let’s say if I’m wearing at least one
hearing, clap your hands. So if we’re going to go
through this, we’re going–>>[CLAPPING]>>Right? And I’m wearing one, and– OK. Yeah. I’m not wearing earrings
anymore, so no more. OK, so then that’s when you would stop. And this you could say is
equivalent to almost variables. Let’s say you have a
variable number of earrings. So while number of earrings is greater
than or equal to 1, clap hands. And then after clap
hands, decrement earrings. Do like earrings minus 1. So decrement the number of earrings, and
then you will go through the while loop twice.>>And if the condition is always
true– so if, let’s say, 2==2, and 2 is always equal to 2, right? Then you would always
do something in there, and that’s equivalent to almost the
forever loop that we had in Scratch. Yes?>>STUDENT: Are the two sides just
different ways of writing a while loop?>>SHARON: So we’re focused
on this one first. And then so let’s compare
it with a do while loop. So they’re slightly different. OK, so let’s say the condition is still
while I’m wearing at least one earring, and I’m not wearing
any earrings right now. And let’s say do and still clap while
I’m wearing at least one earring. What should happen?>>[CLAPS]>>Uh oh, guys. OK, so you’re supposed to clap
once because basically you go through the first part of the code. You do it no matter what, and
then you see the while condition, and you go back into
that loop if it’s true. Does that makes sense?>>STUDENT: You always
do it the first time. SHARON: You always do it the
first time regardless of whether or not that condition is true or not. JASON HIRSCHHORN: When do
you think you could use this? When does it make sense to use this? HANNAH: Yeah? STUDENT: [INAUDIBLE]. JASON HIRSCHHORN: Right. HANNAH: Very good. JASON HIRSCHHORN: So
when you prompt a user, you’re going to always want
to prompt a user one time. You’re always going to want
to prompt the user one time. So instead of putting
in a while loop, you put in do while loop, because you’re
always going to do it one time. If they give you the
correct answer, you’re done. If they don’t, then you re-prompt them. >>SHARON: All right, for loops. So in Scratch, we had repeat blocks. So we wanted to repeat something,
let’s say, seven times. So we just said repeat seven, and
say I’m here to help you Snow White! In C, we have for loops if we
want to go through something a specific number of times. Let’s say if we initialize
the variable dwarves and make sure– so the first block
right there before the first semicolon, we initialize our variable
to, and we set it to 0. And our variable there
is an integer, int. And the variable name is
dwarves, and we set dwarves to 0.>>And the second part between the
two semicolons is our condition. And so as long as dwarves
is less than seven, we’ll keep going through this for loop. And then the last part is what do
we do at the end of this for loop? dwarves ++, and that means we
increment dwarves by one every time.>>So what’s going to happen here? So first we’re going to go through. We have dwarves as 0,
and then we’re going to print I’m here to
help you Snow White! And then dwarves is going to
increase because we said dwarves ++. Dwarves is going to be 1. And then we compare– dwarves is 1. We compare it to is dwarves less than 7? Yes. I’m going to go through this again. I’m here to help you Snow White! And then dwarves becomes
2, and then we compare it. Is 2 less that 7? Yes. We’re going to keep going through. And we’re going to go
through this seven times. >>So in the end, we’re going to
have dwarves=0 print out I’m here to help you Snow White! dwarves equals 1, 2, 3, 4, 5, and 6. Remember we index at 0. So we start with 0. Yes?>>STUDENT: So this is
different than the do because this doesn’t print
out initially at first. So you could continue the same thing. Could you do this also with the do loop? Like the same process. The condition is dwarves
less than 7 or whatever. Response is less than 7. SHARON: So you could technically. So if we go back to– your talking
about the while loop, right? So the do while loop
is slightly different because we guarantee at least
once that we’ll go through it, so that’s the biggest difference. But with the while loop we could
say while dwarves is less than 7, do this, and then
increment dwarves by one. And then we have to initialize
dwarves before this whole statement that it equals 0. So yeah, we could do that with that. Mm-hm?>>STUDENT: Can you go back to [INAUDIBLE]? SHARON: For loop? STUDENT: Yeah, so with the dwarves ++,
seems like that’s what you do after you’ve ran some other means. SHARON: Correct. STUDENT: Could you just
not have that and put it– SHARON: You could. STUDENT: So is [INAUDIBLE] right after
print out in the next line underneath. SHARON: Yes, you could put it there. But then you would
just leave that empty.>>JASON HIRSCHHORN: You still
need the semi colon though.>>SHARON: It looks a little awkward,
but you could technically do that. Technically. Please don’t. Yes?>>STUDENT: Are there any
alternatives to ++? Is there anything else [INAUDIBLE]? >>SHARON: Technically it it’s dwarves. One equals sign we’re going
to set it to dwarves plus 1. So technically that’s
what dwarves ++ means. Does that make sense?>>STUDENT: Yeah, but are
there any alternatives? Like if you ever–>>SHARON: Yeah, you could do dwarves – -.>>STUDENT: [INAUDIBLE].>>SHARON: Yeah, you could
do a host of things. HANNAH: And you could increment by 2. You could increment by 3. Anything that’s going to be changing,
it will eventually make the condition.>>STUDENT: So if you wanted to increment
by 2, how would you write that? >>HANNAH: You could either write dwarves–
you could write this whole thing out. dwarves=dwarves + 2. Or a slightly shorthand. I’m just going to write + equals 2.>>STUDENT: OK, and you’d write that
right where dwarves ++ is there. HANNAH: Exactly. SHARON: Correct. There was another question? Yeah?>>STUDENT: [INAUDIBLE] dwarves ++? SHARON: So you don’t– JASON HIRSCHHORN: Yeah, don’t need
it there. [INAUDIBLE] very end. So you do [INAUDIBLE]
ones because you’re kind of splitting up the initialization,
condition, and the change. At the very end, you don’t need it. SHARON: Also notice you
don’t need the semicolon after the whole entire for loop. >>STUDENT: How would you start with
an initial number of negative, for example?>>SHARON: You can initialize
dwarves equal to negative 2.>>STUDENT: Do you just do dash 2?>>SHARON: Yes, so the
negative sign, dash 2. >>STUDENT: Is it [INAUDIBLE] to
initialize as dwarves [INAUDIBLE]?>>SHARON: Yes. So if we had just initialized dwarves
earlier, just do int dwarves semicolon, and then there we can do
dwarves set that equal to 0.>>STUDENT: Could we do it earlier in
the program saying int dwarves=0, and then just–>>SHARON: Just not have– so
there would be a space again, but you would still need a semicolon. Yeah. Mm-hm? STUDENT: Does this code say what
the value of dwarves is afterward. If you ask it after all of this is
done to print the value of dwarves. SHARON: You could ask it to do that. STUDENT: OK. SHARON: But you would
have to ask it to do that. It doesn’t do it for you. I’m scared to put my earrings back in. Do it at the end for the applause. Just kidding.>>JASON HIRSCHHORN: So that’s the same. So initialize at the top. Condition, print, change. So this is exactly the same as
that if anybody can see that. STUDENT: Why don’t you have the
semicolon on the first line? SHARON: Where? STUDENT: After the first line. SHARON: After the 4 in the parentheses? Because we want to go
through into this loop. And if you take a look at loops in
general, they don’t have semicolons. It’s not practical. Are we good? One more question, two more. Yeah? STUDENT: This might be
a little bit advanced, but do dwarves have a
different [INAUDIBLE] from a variable [INAUDIBLE]. SHARON: Very good. STUDENT: [INAUDIBLE]. >>SHARON: Yes, it is. What? JASON HIRSCHHORN: Yeah, so it does. So normally the scope of
something is in the curly braces. So scope is always going
to be in the curly braces, but that wouldn’t make
much sense for the for loop because we initialized
dwarves in that curly brace. So normally the scope of dwarves, that
variable wouldn’t extend past that. This is a special case though. So you initialize it
within those parentheses, and then you have the scope later on. So special case. They should not have that, and
that was– That make sense?>>STUDENT: No.>>JASON HIRSCHHORN: I can go over it. So what are you still confused about?>>STUDENT: As in can you use dwarves– you
can obviously use dwarves [INAUDIBLE] outside the formula too?>>JASON HIRSCHHORN: No, no. So the scope of it is only inside the
curly braces for the for loop, yeah. SHARON: But if you initialize
dwarves outside of the for loop, then you can use it elsewhere
There’s one more question? No? OK.>>So we talked a little bit about nesting
for loops or nesting conditionals, if statements. So here’s an example of where
we could nest a for loop. Let’s say we were printing
this table of x’s right here. We might want to– first if
we just don’t look at the code and we just think about it, we
want to go through every row and print each column, right? Does that make sense? OK. >>So here we’re going through
every row for basically each row, and there are three rows. And then within each row
for each column, and there are four columns, print an x. so. When row is 0 and column
is 0, we print this x. And then we keep going
through the column loop. Row is still 0, but column is one. And then column is 2,
and then column is 3. And then we exit out of that
loop because then column is no longer less than 4. And then we print a new line,
and we go to the new line. And then we go through the next
row, and row gets incremented, and we go through that again. Does that make sense? Yes?>>STUDENT: So to nest [INAUDIBLE] is
just putting it inside of the for loop?>>SHARON: So nesting means that we
have a for loop within a for loop, like Inception.>>STUDENT: You don’t need a
special rotation or anything? You just stick it right inside of it? SHARON: Correct. Yes?>>STUDENT: Might be [INAUDIBLE], but
it looks like there’s an extra space between all the x’s. I don’t know if that’s– If that
were to actually do the program, would it do something like that?>>SHARON: So no. That was pressing Enter. That was bad on our part. Apologize.>>HANNAH: How would you change this
program if you did one extra line?>>STUDENT: [INAUDIBLE]. SHARON: Good job. STUDENT: You can also print the same
thing by switching around the column, right?>>SHARON: Hm?>>STUDENT: So to print your little
xxx, the first line can say print, and instead of saying
row, it says column.>>SHARON: So you could change
the variable names to do that. Is that what you’re saying?>>STUDENT: No, I’m just
saying that– so you’re printing rows first and
then printing columns. Can you also print columns first and
then rows to get the same diagram? SHARON: You could, but
then you would have to be careful of where
you put the new line. And how would you jump back? JASON HIRSCHHORN: If
you just switched it and the values were different,
instead of having four– what is it? Three by four. I You would have four
rows and three columns. Does that make sense?>>STUDENT: Yeah.>>JASON HIRSCHHORN: So
yeah, you could do that. Yeah, definitely. SHARON: But that would be literally just
switching the names of the variables. Good? OK. All right, P set one. OK, so the first part of it
you will be doing this, Mario. Just kidding. It’s more like this. And so when we just looked at the
nested for loops, making that table, think about how you might be able to
print out these hashtags in this way. And then right here how would you
print this right here, this open space.>>STUDENT: [INAUDIBLE]. SHARON: Yeah, you just print a space. OK, so just think about that. HANNAH: And then the same
part of the problem set is a program called
greedy.c, so that you’re going to want to think
about conditionals and making sure that you
can make proper change. And one little warning we have for you
is be careful of floating point values. If that means absolutely nothing to you,
it’ll be covered in lecture this week and also in [INAUDIBLE]
walk through, which you guys will learn to
love on the problem sets.>>One thing that I really suggest,
especially with Mario.c, when you’re doing the problem
set, if you get stuck, start by doing it on paper. Write it out and actually sit there
and pretend to be the computer, and go through– say
I were the computer, how would I follow
this for loop through? How would my variables
in the for loop change? So doing it on paper
makes it 10 times easier when you go to sit down at the computer. So just my little plug.>>SHARON: And also don’t think that you
have to code everything all at once. Make sure you take an iterative process. Do a little bit, print
it out, see what happens. Sometimes it’s a little trial and error. And come to office hours. Super fun. JASON HIRSCHHORN: So any questions? All right guys, that
was your first section. HANNAH: Thanks for coming. JASON HIRSCHHORN: Thanks for come. SHARON: Thank you. [APPLAUSE]

15 Comments

Add a Comment

Your email address will not be published. Required fields are marked *