[ Silence ]
[ Noise ]
[ Music ]
>> This-- This is CS50.
Welcome back or welcome for the first time
to the end of week zero.
So today's opening act is courtesy of Nicole Bowman,
a student self-described last year is among those less
comfortable when she implemented that visualization
in a programming language called "Scratch"
into which we will dive as a group today.
But recall couple of invitations from Wednesday.
So this year is a representative puzzle
for the first ever CS50 puzzle day
to take place tomorrow at noon.
For those who only glimpse this earlier who happen struggling
with this sort of puzzle, I'll offer the following clue
if you focus on the very top line.
You might think to yourself 1, 2, 3, 4,
we there yet, 5, and so forth.
The other lines are in quite as straightforward
but if you actually fill in those blanks,
you'll come out with some word and that's represented
over the types of puzzles you'll be challenged to solve tomorrow.
So if of interest and you have not RSVP just yet,
do go to cs50.net/register to sign
up for the Facebook sponsored CS50 puzzle day.
It will take place at noon to 3 p.m.
in a building called Northwest Science which is right
across the street, just off the street from here.
You'll be greeted with pizza as well as seven puzzles,
either in printed or electronic form and once you do,
you and your team of friends of 2 or 3 or size 4 will be able
to go often to some corner,
opt into some conference room outside and figure
out the seven-letter clue that will lead you
to some prize on campus.
Among the prizes will be a Wii with the Mario cart bundle,
raffled off to any possible participant not just those
who win some 50 dollar, 25 dollar gift certificates
as well some mice [phonetic] as well
as some other giveaways during that.
And our friends from Facebook will be in attendance
if you would like to see what life is like their real world.
Also with of interest,
dinner tonight you are all cordially invited.
Do go to cs50.net/rsvp.
If you did not so already and you would like to join us,
the plan is to meet at 6:30 tonight at Maxwell-Dworkin
which is also just up the street.
If these names are unfamiliar to you,
just go to the course's home page, cs50.net,
and we have direct links to the addresses
so that you can find your way on campus.
So now those of you who are joining us for the first
and even those who are here on Wednesday, allow me to excerpt
from the syllabus something that is particularly important
to me and to the course.
So there's this excerpt there, what ultimately matters
in this course is not so much where you end up relative
to your classmates, but where you end up--
but where you in week 12, at the end of the course,
end up relative to yourself in week 0.
There's no predetermined distribution of grades,
there's no curve per se.
We very much do grade students in this course
in an individual basic whether doing a pass-fail
which certainly encouraged per the pink pass-fail
of change slips if you need them at any point
or if you take it for a letter grade.
And more over as we hit it on Wednesday,
we do take into account students [inaudible] backgrounds.
So once it comes time to section
and sectioning will start next week,
you needn't do anything just yet,
we'll announce it next week.
We'll have three different tracks of sections.
The track of section for those less comfortable,
those more comfortable, and those somewhere in between.
And when it comes time to section,
you kinda know what bucket you're in.
If you're in the less comfortable group,
odds are that's a label you'll immediately gravitate toward.
And similarly if you're particularly advance,
you're eager to those hacker editions,
you're probably among those come comfortable.
And if you're kinda of on the fence,
somewhere in between is a--
is a large portion of the class as well.
So realize that the course tries to keep everyone
on track not only for those without experience
but also keeping interested
and challenged those with more experience.
In terms of the support structure that awakes--
that awaits, realize that the first problems sets PDF was post
into the course's websites this morning
and it's due next Thursday.
But toward that end, there's this walkthrough,
every week for every problem
that we'll have a code walkthrough so to speak,
where about one of the teaching fellows will walk you
through the process of diving into that problem set.
They're not mandatory.
They are simply there, so that if you're one
of those students whether in this course or in general
who picks up a P set and is often--
I'm not really sure where to begin or how to tackle it, well,
that's exactly what these video tape walkthroughs will be.
We'll walk you through the spec itself.
We'll propose ideas, things to think about.
So realize that is the support structure
that would be taking place
and almost every Sunday whenever there's a P set of the week,
at 7 p.m. also
in Maxwell-Dworkin whose address can be found
on the course's website under problem sets.
Office hours too will commence this year coinciding
with Brain Break in four-- in three different dining halls,
simply check out the course's website for the schedule thereof
and you will see and you we will work out the kings
of [inaudible], our brand new fancy online system iPad based
systems [inaudible] coming up on Monday in [inaudible],
Tuesday in Leverett, and then Wednesday
and Thursday Lowell House.
So keep an eye on the course's website for details.
So rather than just tell you a little bit about 50 in terms
of pictures, what we thought we do is actually draw
on some voices of CS50 past from both former TFs,
former CAs as well as former students.
So if we could dim the light again,
allow me to convey some messages from your predecessors.
[ Noise ]
[ Beep Sound ]
>> My name is Jason, and I took CS50 in the fall.
I'm a freshman.
And I was sort of looking
for something I never studied before.
So-- And I knew it's gonna be fun but also a lot of work.
And I found all of those and more in CS50.
So I had a fantastic experience
and I'm pretty sure it changed my life
and changed what I was interested in.
So I'm here to encourage other people to take CS50 as well.
So hopefully they can as wonderful experience as I did.
>> So tell us how you got into the CS concentration.
>> I took CS in high school and I really liked it,
and I real like it all at Harvard,
so I'm happy with it, I guess.
>> Very cool!
What do you think about CS as a potential minor,
do you think it's a good option for lot people?
What would you sell some around that way?
>> I think it's really useful because CS is really liberating,
once you know how do just like a small script you can make
that [inaudible] people have to do or you can do, I don't know,
minor things just make your life easier, which is really nice.
So [inaudible] is good too.
>> [Inaudible], thank you so much.
There you have it, CS empowering.
>> So this is my final project that I developed last year.
Actually, was up in space for a majority the time taking a lot
of these photos which was the probably the biggest time
consuming aspect to this.
But it's pretty cool.
Apple got it lot of their ideas from me, so did Google Earth.
It's really, really unique, you know, can find your house,
even if you live in the middle of the ocean like over here.
Good place.
Also if you like wanna play--
let's play some chess, you can play chess.
It's a great little development too.
I learned a lot and thanks
to CS50 you can create things just like this.
[ Laughter ]
>> Oh, definitely, CS50 was a lot fun.
I remember when I was looking at the courses
like before I came here.
Like I knew it's gonna my math course and, you know, oh,
a lot of them I pick
out of having trying CS50 I did [inaudible] I was gonna take,
but after the first day of shopping, you know,
and then I entered, okay, this is for me.
>> [Inaudible] about CS50 at 4 a.m. in the morning.
>> I like it.
It's-- It really has a sense of creativity 'cause there's a lot
of creativity flowing around everywhere.
[ Laughter ]
>> Everybody just seems like really excited and it's already
like 4:30 and everyone is awake and working on their projects.
It's pretty cool.
[ Laughter ]
>> Like hackaton.
[ Simultaneous Talking ]
>> We had so much fun.
>> We were raging in the corner.
Yeah.
>> Music.
>> Eating all the candy.
>> Eating-- They're eating lots of candy,
that was disgusting, that was fun.
>> Yeah
>> So.
>> What do you think of the CS50 [inaudible]?
>> There're a lot of people here.
I think it's amazing that there're
so many really great projects that are going.
But it's definitely a challenging semester but I think
at the end-- at the end you can say
like I've gained some very tangible skills.
So definitely use the future.
>> I've really thought that has been one
of the most practical courses that I've taken at Harvard
and also lot of fun and I've really enjoyed learning how
to use the technology.
I'm really interested in medicine
as you can probably tell from this.
So I think I'm gonna go like to-- go and be a doctor.
But I think that being able to work with programming
and websites and computers is such a useful skill
and everybody should really know it.
>> Harvard President Faust, we just wanna ask you,
what you think of the [inaudible]?
>> Well, it was really fun.
First I was struck by how much energy there is in this room
and everyone so excited about their projects
and everyone else's projects.
But I was so struck by the ingenuity of the projects I saw.
Lost and found seems to be a shared theme
that obviously [inaudible] university why it's lost
and found.
So I-- and then I just saw an application which is
about debating across Harvard
where people can debate issues online and I thought there're
so many questions that it would be great
to just get people's responses on.
So I though about what a contribution
at the community that could be.
And then I just saw a lot of other very ingenious websites
about where to find libraries and different functions
in libraries and a variety of things.
So, what a great event.
[ Noise ]
[ Applause ]
>> So, even though the CS50 fair each year is the climax
of the course and the terminus of the course,
many students do go on with their projects
or with variants thereof to create their own startups,
to incubate their ideas with friends particularly now
that Harvard has its own J term.
And so those of you who are particularly interested
in pursuing some startup with your own or externally
or just generally interest in on entrepreneurship,
know that any number of resources exist that you'll come
across particularly toward terms and Hack Harvard for instance.
It's a relatively new student group on which some
of our own teaching fellows and CAs now participate
and this will be an opportunity and J term to come back
to campus early and immerse yourself with the team
of your friends and work on some project.
Maybe even your CS50 project and take it to the next level,
[inaudible] wonderful support structure surrounded 24/7
by peers doing something similar.
Know that the i3, the Harvard Collage Innovation Challenge
will come up.
And this is an opportunity to actually get potentially funding
for some idea that you might have and so we'll be sure
to announce the application deadline for an opportunity
like that when it arises.
Know too that Harvard and all of its schools is in a process
in building out a little something like this
across the river at HPS to be called the innovation lab
for students and faculty and staff from across Harvard.
This is meant to become a center of innovation,
a place where you can go to work with work spaces,
for invited guest, for food and such.
And so this will be debuting
in a few months once construction is complete
and the idea is to really bring people and ideas
from across campus particularly across schools to work
on all things innovative.
In particular if you're interested in J term
in joining some of those folks out in Silicon Valley
on a little field trip over J term,
know that there'll be an application opportunity
to just a couple of weeks to go with some 35 peers
out to Silicon Valley.
Meet with startup, see startups, talk to VCs, venture capitalist,
and talk with folks who live in breath this particular world.
And also closer to home, know that in a short term,
there are mailing lists and plenty of your peers,
an upper class man who discuss and explore these kinds
of things, the whipping through these URLs fairly quickly.
The slides are online so you can grab this later.
But in email list called the Harvard College Entrepreneurship
Form as well as the Harvard College Venture Partners exist
as well.
So in terms of CS50 today, we do have these pink forms
which are only necessary if you decide to switch grading status
between now and the 5th and Monday.
We also have copies of the same handouts from Wednesday,
one of which is the syllabus, the other of which is this,
the unofficial CS50 guide to computer science at Harvard.
The goal here is not to turn everyone in this room
into a computer science concentrate or a minor,
but to give you a sense of where else you can go.
And particular the last--
very last back page of this is a cheat sheet
of all the various directions in which you can go
after just this course alone.
And this brochure here is what was handed
to all 2,000 attendees of the CS50 fair in 2010
from which you saw excerpts just a moment ago.
And these we handout so that you can get a sense
in your own hands of the 500 some odd projects
that your predecessors implemented along
with their names, the descriptions
and additional details as well
as to what those folks were like.
So do explore that if this is of interest to you.
Now even now today, we're gonna dive into one of the core skills
of computer science, namely programming.
Bear in mind one that it's just tool the computer scientists
tend to use and folks from other domains tend to use
to solve problems on interest to themselves.
It is not programming a goal into itself,
but really a tool you can use expedite data analysis
as we heard one of our student-- our own student say,
to expedite just solving some real world problem
that you wanna do more efficiently or mechanically
than you could do, say by hand.
But toward that end of actually learning
to program it's perhaps useful
to understand why these same ideas now increasingly imply
in the real world?
So a lot of you, many of you will not go off
to take computer science minor or concentration
and maybe not even a second course in computer science,
but you're gonna go off at some point and some number of years
to the real world, to business, to politics and the like,
and presumably making decisions.
And if you watch the news, particularly the daily show,
you'll know that there's a whole lot of adults
out there making really bad uninformed decisions
particularly when it comes to technology, whether they're
in positions of power or just consumers
who are using technologies
without even understanding how they work,
or how they might threaten their own privacy and security.
One of themes we'll discuss term's end
when you introduce web programming
and how you actually program dynamic websites and tools
for the internet, we'll discuss issues of security,
but just to tease you now, I though I'd share
with you an example of something that literally crapped
up the day before our one of our lecture's last fall on inter--
on the web programming in the internet.
There's this tool with which from the whisperers,
some of you are already clearly familiar with already.
If not, you're about to be educated.
So number months ago, a security researcher presented
to the world a challenge that we as a society really need
to fix some flaws that have existed in the web,
in that protocol called "HTTP," which you've seen in front
of URLs now for years that have existed for ages.
Really since the beginning of a lot
of these technologies' adaption
but no one really took it seriously
because it required exploiting weaknesses in these technologies
and hacking certain things so to speak took more savvy than, say,
the typical population might have.
So this fellow wrote a plug in, an extension for firefox,
the browser with which you're probably familiar,
that makes it super easy to just log in to the Facebook account,
the GMail account, the Twitter account, the Yahoo account,
any number of big name accounts if you're
within wireless range of these people.
And when you run this tool,
it looks a little something like this.
This might be a browser window.
A typical Firefox window
and with this freely available tool installed,
you can see a button at top left that just says start capturing.
And so long as you're on a wireless Wi-Fi laptop
and you're surrounded by other people whether it's on campus
or in Starbucks or in an airport,
there's all of these bits.
All of this 0s and 1s going through the air
that pretty much all of our laptops are
on the honor system not to listen
to unless they're destined from my own computer's address.
Well, this tool simply turns itself into--
which definitely called "promiscuous mode"
where it listens to all of these 0s and 1s flowing
out the room whether or not they're belonging to me.
And what you see with this particular tool,
and the challenge this fellow laid
down is once you start running this, you see everyone
in the room or the wireless vicinity who's actually using
Facebook, or Google, or the like.
And with the simple double click on any one of these names bam,
in that browser window would be Ian Gallagher's [phonetic]
Facebook account or Eric's Google account or the like.
And the reason behind this derives from these--
the use of websites of cookies.
Something you're probably generally familiar with.
And we'll talk about in more detail towards term's end.
But these cookies are not often kept particularly secure.
A lot of websites do not have URLs that start with https,
where the S denotes secure.
And even those that do, only use it some of the time,
Facebook itself included.
In fact there was just a few months ago,
that Facebook finally started offering the option site-wide
to use HTTPS to ward off this threat.
But as recently as last fall, there was a good several months
where your best friend or your roommate might very well been
seeing everything you were doing inside of you Facebook account
or the like because it is incredibly easy
to steal cookies.
And that's a topic we'll discuss towards term's end.
And we'll also discuss defenses
against these kinds of mechanisms.
But if you're sort of wide eyed at this moment like,
my God it's that easy or it's this possible to do.
Well one of the goals of this course is again,
not just about computer science and not just about programming
but to empower you to then make educated decisions
and maybe you don't care that anyone around you could log
in to your Facebook profile 'cause you don't really use it
anyway, but at least that's a conscious decision
and not just an accidental one
because you don't understand how these technologies work.
>> Another exploit that came to light
or another security implication that came
to light a few months back too, related to Apple and the iPhone.
So Apple, if you actually read the
like 23-page document you have to agree to just to use iTunes
and listen to music, let alone use an iPhone,
it actually does say somewhere in there
that we actually log everywhere you're going.
And we store all of this information in iTunes
and we even upload this information to our servers.
Now as an aside, there're actually a lot of benefits
to all 500 of us walking around the world
with GPS transponders in our pockets.
If we have iPhones or androids or any
of these moderns smartphones
because then we can provide useful services to us
like what's near by me, Google Maps, these kinds of things.
So there's a compelling user case for this.
But what happens in this case a few months back,
was apple accidentally or unknowingly
or consciously was not encrypting the GPS coordinates
of everywhere you and in turn your phone had been.
And so this is minor worry [inaudible] 'cause
if anyone has physical access
to your computer whether it's a roommate or spouse or children
or parents, well, they could--
they could certainly just start poking around.
They could run this free tool
which is again it was another proof of concept
to demonstrate just how easy it is to do these things
and how scary it is scary it is if you don't understand.
Well, this will plot on the map absolutely everywhere you have
been because this file was on encrypted.
And so I actually did this on my phone out of curiosity.
I ran this free program on my own installation of iTunes
and what it generates for you is this nice map.
And these big blue dots represents proportional
to their size where I spent over that past year or two most
of my time and you can see I spent a lot of time
in the Northeast around Cambridge and such.
I apparently went to Texas and somewhere in Pennsylvania
as well as California.
The tiny little red dot at top left there, anyone had a hunch
as to why that one is so small relative to the others?
So was actually just a layover in an airplane.
When my phone actually got reception once we had a layover,
I got out in the terminal, viola, Apple knows where I am,
get back in the plane and they see me again ones I hit Texas
or the East Coast.
Well, if you zoom in on this it's one thing
that kinda see these big blotches,
most people in the world could guess I have spend a lot of time
in Cambridge but if you really dive in,
you just see how granular this is.
So this is the greater Boston area
and the dark splotch does indeed kind of overlap with Boston
and Cambridge and I apparently spent sometime in the suburbs
but not all that much.
But you can see now the concentration of my time.
I also happened to do a lot of work for a startup in New York
and so I take the train and truck back
and forth along the East Coast,
and sure enough, Apple new that too.
So at top right there, that's Boston up there,
at the bottom left is Manhattan,
in the middle there is Stanford Connecticut
where Amtrak lets off and we typically pick up a rental car.
And you can see that I've occasionally vacationed
on Cape Cod which is that little hook shape thing going
out into the Atlantic Ocean.
And so, it was cool on the one hand,
it was also a little worrisome, when I had no recollection
of being in Pennsylvania and apparently my phone had been.
And then finally, I figured it
out that I was just visiting some university folks there.
So this sort of stuff, these sorts of risks are all
around us, and so by semester's end, not only will you know how
to do things as those images and videos of your predecessors
to pick, you'll also understand these things.
So that even 50 is in fact the terminal course for you in CS,
you're better equipped in the real world just
to make smart decisions when it comes to your own phone,
let alone policy and business decisions moving forward.
So, with that said, I thought we would come back to this issue
of the program we wrote on Wednesday
that didn't really turn out so well.
There were clearly some opportunities for improvements.
And recall that the program that we wrote collectively as a class
or as the volunteer gave us his form was this five-step program.
And odds are, if you kind of reflected on the steps,
you yourself wrote on Wednesday,
well yours was probably relatively
as short somewhat cursory maybe even incomplete.
And granted if we gave you more than 60 or 120 seconds,
maybe you'd have come up with something a little more
fleshed out.
But the reality is there're probably a lot
of things you didn't in fact consider.
And they're representative of the type--
the types of things you need to consider
when commanding a computer to do something.
So for instance, and if Rob,
you could join me up here for a moment.
What's was one of the most obvious flaws in step one?
Take two slices bread from the loaf,
what is that clearly assuming as Rob
so professionally demonstrated yesterday?
>> It's a loaf with sliced bread.
>> So it's a loaf with sliced bread, right?
So with this-- okay, so here is the loaf and we were--
we told you're allowed to assume.
You have a jar of each of these things, a knife,
and a loaf of bread, take two slices of bread from the loaf.
You know, what is-- I mean,
as you saw as from our volunteers ripped
into to this thing because what does it mean to take two slices
from this loaf, right?
So what would be better?
What would be a marginal improvement if we wanna go back
and actually improve this with a better step one?
Okay, so open the package.
Well, let me just play kind of-- okay so open the package.
So now we do this and I turn the little twisty thing
and we can assume certain things otherwise this will very quickly
devolve into complete tedium.
So we know we can assume that I know how to open the bag.
But now we've kind of written slightly flawed program, right.
What if I make my sandwich and I then decide
to make another sandwich, what am I now assuming?
[ Inaudible Remarks ]
>> It's already open, right.
And I better say close the bag at the end of the program
or maybe I should say something like, "If bag is closed,
then opened bag, right."
I wanna actually make an intelligent decision
that takes us input, the state of my world to the state
of the loaf of bread and then decide if in only if it's closed
to open the bag, alright.
So notice what's Rob is doing here is gonna be a little
something called pseudocode.
And it will get us into the habit
of thinking more algorithmically, more carefully,
more procedurally which for today's purpose is can
be synonymous.
So he's doing this little thing of indentation just
to make clear that you only open the bag if the thing that's
above it and slightly to the left is actually true,
or is actually the case in reality.
Alright, so now if bag is close, I opened it, otherwise,
it's presumably just open.
Now, what do I do?
Let's start making the sandwich more properly.
Yeah?
[ Inaudible Remark ]
>> Okay.
[ Inaudible Remark ]
>> Okay, good.
So if loaf is sliced then take two slices.
And again, just to be a little nitpicky here.
You know, there's nothing saying I should be taking the bread
from the open end of the bag.
So there's another opportunity for improvement.
But I can kinda push back
on this one other else slice bread into--
slice loaf into slices, right.
Like we kinda need to do something before that, right?
Suppose that it is not sliced, what do I obviously need
to do first before slicing this thing?
So remove loaf from bag.
So we might do that.
But now, let's-- because this is pretty sliced, let's go back
to the other condition and let's start to record both of these.
So if loaf is already sliced, remove two slices from bag,
from open end, it is fairly precise, else.
So now let's introduce a fork in the road.
Be previously we did something if something was true,
but there was really no alternative.
This time it kinda makes sense to have an alternative.
So if loaf is already sliced, then remove two slices
from the open bag, else, what do I wanna do?
[ Inaudible Remark ]
>> Sorry.
[ Inaudible Remark ]
>> Okay, so get a knife at some points and still, else,
if it isn't sliced, what needs
to happen first before I start slicing this bread?
Yeah.
[ Inaudible Remarks ]
>> Remove bread from bag.
So I would actually slide all of the bread out
and then I could probably proceed
to use this part of the knife.
So now we're getting somewhere.
So we've introduced what we'll call conditions, or branches,
or decision points really in the real world.
So now what we-- what might we do here?
At this point in the story,
we have removed potentially the bread from the bag
and let's just put one more statement also indented cuts two
slices of bread from loaf.
Now here too, we could be nitpicky,
I could cut a slice that's this big
and be completely ridiculous unless you start
to make some more forward progress here.
But notice what Rob has done in pseudocode,
both of these lines removed and cut or indented
and this suggests that both of those things should now happen
if the decision that we just made if it isn't sliced is true.
So again, an example of logical flow here.
Well let's try to wrap this one up now.
So if we have at this point two slices of bread in hand,
now what we-- now what should we do?
Yeah.
>> Put them on the table.
Okay. It could be better, but it's alright.
So, you know, separately or some such adverb.
What's next?
[ Inaudible Remarks ]
>> Okay, open-- right.
And so this was the key point missing yesterday, right?
Open the jar of peanut butter.
That sort of something we took for granted.
If this is the first time using it,
here's another possible corner case so to speak.
And this is a piece of jargon we'll often use in programming.
Corner case is sort of a situation that, you know,
make sense that it might happen
but you didn't really think about it.
And even though it might only happen one
out of a hundred times, that one time it does happen, right,
you're not gonna be able to get any peanut butter
out of it unless you have another condition or branch.
So let's go ahead and say, open the jar of peanut butter;
if sealed, remove seal or something like that.
And so that gets me to the point of conditionally doing this.
So now I have the opened peanut butter, I have my two slices
of bread separately what's next?
[ Inaudible Remark ]
>> Pick up knife?
[ Inaudible Remark ]
>> Or put it-- sorry?
[ Inaudible Remark ]
>> Put knife in peanut butter?
[ Inaudible Remark ]
>> Scoop out peanut butter?
Right, this is very quickly gonna devolved
into yesterday, right.
Scoop out-- At least we got this part yesterday, right?
Scoop out two table spoons of peanut butter.
So let's go ahead and do that.
So, scoop out two tea spoons of peanut butter and then?
[ Inaudible Remark ]
>> Spread it on piece of bread.
But again, here too, you know this kind of reasonable.
Stupid but correct.
Or at least consistent with what you told me to do, so here too.
So again, we don't need to turn this into the complete drama
that we had yesterday.
>> But just notice all of the various opportunities there are
to make mistakes, all of these possible corner cases,
and so one of the things that's a little tougher,
a lot of people with programming initially is
that you just kinda want this computer to behave
as through it's a human just responding and interpreting
with their best human judgment, your command.
But the reality is that's not gonna happen and we're gonna see
that today with our own programs.
So thanks very much to Rob.
Very well done.
[ Applause ]
>> Let's see if we can now translate.
Well, again, we'll call pseudocode.
And to be clear pseudocode is something that akin to English.
It's not a programing language.
It's not really English.
It's some amalgam of the two and there's no one's standard.
You just kinda go with your instincts and write out words
that kind of communicate your idea.
But when you're actually commanding a computer,
you're actually gonna write something like this
and we saw this in a slightly different screen on Wednesday.
It was a little color coded but this is perhaps one
of the simplest programs you can write that a computer can run
in this language called C. So this is the language
in which we'll spend a lot
of our time transitioning ultimately the PHP and HTML
and CSS and SQL and various markup
and programming languages, but for now we have this.
And there's gonna be a lot of cryptic details here.
These include the angle brackets,
the parenthesis, the curly braces.
This is one of the challenges early
on for those less comfortable
and that there's all this crazy syntax.
And the frustrating thing about computers is that even
if you do something stupid and, you know, not even unreasonable
like leave off the semicolon 'cause you forgot
or you hit a colon or some other character instead,
the whole thing is gonna break whether it's 5 or 6 lines
or 5 million lines, a single flaw
like that can derail an entire program.
And know now that these are the kinds of silly things
that you do tend to trip up-- trip over early on.
But these are the kinds of things that you start
to spot spontaneously.
And so you'll find over the next 12 weeks that your--
the types of mistakes that you make become increasingly
sophisticated and that's a good thing
and they become increasingly interesting to solve
as you begin to speak this language
like you would of foreign language.
Unfortunately computers don't really speak this language.
This thing C is what we'll call a high level language
because it's pretty close to a human's conversation.
Alright, this is still English word, a few new things like int.
I'm not really sure what that is.
Integer it turns out, but it's still readable by a human.
And this wasn't always the case.
If you heard about things like punch cards in the like,
programming was not always so relatively human friendly
because at the end of the day computers still only understand
things like this.
So you might know just in generally
that computer somehow operate in 0s and 1s and that's useful
because 0s and 1s, very nicely map to electrical things, right?
Right now in sanders, the lights are on.
We could say that this huge theater is actually storing the
number 1.
If we killed all of the lights it might be storing the
number 0.
And this is why we have such a simple system, a simple alphabet
for computers because they're electrical.
You plug them into the wall.
They operate on batteries.
You have disability to either have electricity flowing or not
and that gives you these two binary.
"Bi" meaning two states
and so you can represent data, things in this way.
Now these are literally the 0s and 1s that are created.
When you start with the program like this,
you run it to a little tool called a compiler,
you get out 0s and 1s like this and these 0s
and 1s are just stored in the file.
Akin to any sort of MP3 or Microsoft Word file.
They're just stored in some file on a computer.
And when you double click it
or run some text-based commands these 0s
and 1s will be fed usually 8 at a time, 32 at a time,
64 at a time into that thing called the CPU
which you probably generally know is the brains
of your computer and that CPU then does something.
And in this case it might print to the screen, "Oh, hi, world."
Well now how do you actually get from all of these 0s
and 1s to useful data?
Well, it's actually relatively straightforward.
So I'm just gonna open up again a simple little text document
here just so we have something to type with and it turns
out that if I want to represent the number we humans know as 0
in binary, otherwise known as base 2.
Well, the world has decided that 0 for a computer means 0
to a human, so pretty simple.
Well, if we wanna do the number 1.
It's actually pretty simple.
Unfortunately, now we're out of digits, right?
The alphabet is only so expressive so now using only 0s
and 1s we have to somehow be able
to encode what we humans know is the number 2, otherwise,
this is gonna be the most useless computer ever
if the best it can do is count up to 1.
Surely we can do better.
So what can we do?
Well, much like in the real world
when you need another digit, you add 9 plus 2.
Well 9 plus 2, what do you do when you kind
of do that in grade school?
You put a-- You carry the 1, right?
So you get another column.
We can do the same thing here.
So to represent the number 2, why don't we do something
like 1-0, and the number 3, 1-1.
And now just to make things lined up prettily,
let's just assume that every number in the world uses 2 bits
and a bit is just a 0 or a 1.
Binary digit, bit.
So now using 0s and 1s I can represent what we humans think
of as 0 and 1 and 2 and 3,
and take a guess what's the number 4 gonna be
represented with?
Yeah, 1-0-0.
And now I can go back and clean this up.
To much like in the real world, you can have as many 0s
to the left of some number.
It makes no material difference, same thing with a computer here.
But in reality computers do use some fixed number of bits.
So anytime you store a number or a letter in a computer,
it's almost always using, say, 8 bits at a time or 16 or 32
or 64, generally, some multiple of 2.
There conventions that we'll see.
But this is precisely the same idea.
Now how if you were given a number like let's say 1-1-1.
How do you get from 1-1-1 to the decimal, the base 10 equivalent,
just the real world expression of that number?
Well, think of it how we do this in the real world.
If I just gonna-- let me just move my cursor over here.
If you have the number like 123 in the real world, well,
how do this series of digits, 1, 2, 3,
translate into conceptually what we know as 123?
Well, again, if you think back
to grade school this is ones column
and this is the tens column and this is the hundreds column,
and now let me just space this out so that they kind
of not line up nicely.
So how do you get from 1, 2,
3 to what we conceptually think of as 123?
Well, in the ones column we have 3 so that's 1 times 3.
In the tens column we have a 2, so that's 10 times 2.
In one hundreds column we have a 1 so that's 100 times 1.
Add all those up, 100 plus 20 plus 3.
Viola! You get this notion of 123.
So now let's go back to this challenge.
We kind of figured this out just by working our way up.
Let's now work our way backwards.
If we start with 1-1-1, how does the binary system work?
Well in the binary system, the system that computers use,
we actually start with, thankfully, the ones column.
But it's not the tens column that comes next,
it's the twos column and after that it's the fours column.
And so whereas in the real world of base 10 and decimal,
the columns-- the columns represent powers of 10,
in the binary world, binary again meaning two,
it's just powers of 2.
It's as simple as that.
So you have the ones column, the twos columns, the fours,
the eight, sixteen, thirty two, sixty four, and so forth.
And so now we have the ability to express numbers
in just the different way.
But the advantage is we don't need 0 and 1 and 2 and 3 and 4
and 5 and say, all of these-- we don't need 10 different digits.
All we need is to turn the power on or turn it off.
And by doing that rapidly in sequence, we can build
up some representation in electricity
with numbers like this.
So with that said, if I have a 1 in the fours column,
a 1 in the twos column, and the 1
in the ones column what does that now give me?
[ Inaudible Remarks ]
>> 7, right?
1, 4 plus 1, 2 plus 1, 1, that gives me 7.
And we can work our way up from there.
So just to round this out,
the reason that we often hear the term not so much bits
in the real world but bytes, megabytes, gigabytes, terabytes
and so forth is 'cause our single bit is actually pretty
useless certainly these days
and so somewhere along the way the world decided
that it's more useful to express units of measure in terms
of eight of these things so8 bits,
eight 0s and/or 1s is what we call a byte.
So if you have some crazy short like MP3 or music file
on your computer that was just a single byte,
obviously, unrealistic.
They're usually millions of bytes.
That would just mean there're 8 bits
to compose whatever song you're very briefly listening to.
And if it's 3 megabytes, that means you have 3 million bytes
or really 24 million bits
if you multiply the number of bytes by 8.
So when we talk about a bytes worth
of bits we have not just the ones, the twos,
the fours column, we have all the way
up to the one hundred twenty eights column there.
So now, a little something silly
like this might make some more sense.
[ Noise ]
[ Laughter ]
>> We got through it at least 1.
Excellent!
So if it's not clicking, come back to it later
but now perhaps even more comic books even will make sense.
But numbers alone are not all that useful, right?
The last-- You might use numbers in a computer
but more likely these days you're sending instant messages
or downloading photos or reading emails in the like.
So the next step up from just numbers is probably
something alphabetical.
The ability to express yourself as a human in English or Spanish
or whatever language, you need to be able
to represent other symbols using though at the end
of the day electricity.
That's on or off, 0s or 1s.
Well, thankfully the world years ago came up with a system
and this chart actually looks more overwhelming
that it needs to.
It's just because there's a whole lot of information.
>> Notice that you have the same two columns repeated again
and again.
Code and Char for character, code, char,
code, char, code, char.
So the way to read this is that at the top left,
it says the number 32 and that's a decimal number 32,
and that means that the space bar character
on the keyboard is implemented inside of a computer
by just storing the number 32.
How do you store the number 32?
Well you use some sequence of bits.
Now how do we get to this sequence of bits of 32?
Well, let me just again come to a place holder here.
If I've got the ones column, the twos, the fours, the eights,
the sixteens, the thirty-twos, I can kind of figure this out.
This is the ones column, this is the twos column, the fours,
eights, sixteen, thirty-two, wahla!
If you wanna represent that space bar character in something
like an e-mail or a Microsoft Word document,
all the computer stores is the bits 10000
and actually it probably uses at least a byte's worth.
So this is one, two, three, four, five, six,
so probably what it's really storing
for the spacebar character is eight total bits
but the first two are zeros.
Now, the spacebar is kind
of underwhelming 'cause you can't really even see it
but if you look back at this chart and fast-forward
to roughly the middle of the chart, what is the code,
the decimal number for the letter capital A?
Yeah, it's apparently 65 and if you scroll down to the rest
of the chart, the lower case A is represented with what?
97, right?
So let's do the 65.
Well, 65 is also pretty easy 'cause things line up nicely.
So this is the spacebar character which we'll represent
with this, even though that's a different character altogether.
The capital A then is gonna have again eight bits typically, one,
two, three, four, five, six, seven,
eight but this time I need the number 65.
So I have the ones column, the twos, the fours, the eights,
sixteens, thrity-twos.
Aha! 64, that's helpful but I need something else.
This now needs to become a one.
And so that's it.
When you're hitting the letter A on your keyboard
to do something completely simple like send and IM
or send an e-mail, what's really being stored in that e-mail
by your computer or in turn Gmail or the
like is some sequence of bits, 0100001 and what you seen
on the screen is the letter A. So,
how do these things actually get stored in reality?
Let's see if we can translate this idea of electricity
to something that's a little more visual.
Let me go ahead and open up a little demo here,
and we can dim the lights just a bit if you don't mind,
for this look inside of what's been inside your computer all
these years.
[ Background Music ]
>> The hard drive is where your PC stores most
of its permanent data.
To do that, the data travels from RAM along
with software signals that tell the hard drive how
to store that data.
The hard drive circuits translate those signals
into voltage fluctuations.
These in turn control the hard drive's moving parts,
some of the few moving parts left in the modern computer.
Some of the signals control a motor
which spins metal coated platters.
Your data is actually stored on these platters.
Other signals move the read/write heads to read
or write data on the platters.
This machinery is so precise
that a human hair couldn't even pass between the heads
and spinning platters, yet it all works at terrific speeds.
>> So if you've even wondered why dropping a laptop is
generally a bad idea, it's because one
of the few remaining mechanical parts inside of a laptop is one,
the hard drive and two, the fan.
And that fan, is actually the cheap, easily replaceable one.
This thing is a bad break because obviously all
of your data is on it.
But we're also getting to the point as in a side
where hard drives that spin like this are increasingly passe.
In fact, a lot of you with Macs or relatively new PCs have SSD,
solid state drives, and what's nice there is
that they're still electrical but they're indeed solid
and that there are no such moving parts,
which means you're much safer in tossing your laptop
across the room to let it land on you bed.
The only thing you really risk breaking then is maybe the
screen or the fan, but at least your data,
with much higher probability these days is going
to be in tact.
Though perhaps don't try that at home.
Let's dive in just a little deeper to now translate
that high level visual to the actual bits that we promised.
[ Background Music ]
>> Let's look at what we just saw in slow motion.
When a great pulse of electricity is sent
to the read/write head it flips on a tiny electromagnet
for a fraction of a second.
The magnet creates a field which changes the polarity of a tiny,
tiny portion of the metal particles
which coat each platter surface.
A pattern series of these tiny charged batteries
on the disk represents a single bit of data
in the binary number system used by computers.
Now, if the current is sent one way
through the read/write head the area is polarized
in one direction.
If the current is sent in the opposite direction,
the polarization is reversed.
How do you get data off the hard disk?
Just reverse the process.
So as the particles on the disk that get the current
in the read/write head moving put together millions
of these magnetized segments and you've got a file.
Now, the pieces of a single may be scattered all
over the drive's platters, kind of like the mess
of papers on your desk.
So a special extra file keeps track of where everything is.
Don't you wish you had something like that?
>> So to summarize, even though we began the notion of bits
with electricity, you can't always store things electrically
if there's no power and certainly most
of us unplug our laptops or desktops or just lose power once
in a while and then it would be nice if we not lose all
of these zeros and ones.
And so, thus was born magnetic storage
which again per the inside
of this hard drive stores tiny little magnetic particles
that have some poles, north pole, south pole,
and the idea is that you take that particle
and if you wanna represent a zero,
you sort of orient it this way
or if you instead you wanna represent in a one,
you flip it around so that it's south-north instead
of north-south.
And then-- there are yet other ways to represent these bits
but for now at this point in the story, we can now just take
for granted that there exists some technology
or some technologies with which to represent zeros and ones
and to covert high level things like C and as we're
about to see Scratch into the zeros and ones
that computers understand.
So the program with which we began today was called Scratch.
This is a graphical programming language developed
by MIT's media lab down the road the
and Nicole's program here is the result of her dragging
and dropping puzzle pieces into this middle palette here,
the so-called scripts area, such that anytime she wanted
to command one of these little characters to do something,
she had to tell it very precisely to move up,
to move down, to move left, to move right,
maybe to conditionally move in some direction, if too close
to the policeman, then move to the left else move to the right.
So you can implement interactions between all
of these various characters on the screen
and you can have sounds and costumes
and you can really have fun
with it once you understand the basics.
So let me go ahead and do this.
Let me go ahead and just create a new file with this.
This author is freely available and in problem set one,
both standard and hacker edition where you'll be challenged
to use this for the first time.
Let's just get a sense of this
and how we can now express ourselves using this very
simple, very low-barrier language, but you'll see
in just a minute with some more visual demos just how fancy your
programs can become.
So at top right here, we'll have what we'll call the stage.
So this is just a rectangular area in which Scratch,
the default sprite who looks like this cat here lives.
And the stage is where things can move up, down, left, right,
spin around, and so forth.
On the middle of the screen, we have the so-called scripts area
as this tab name suggests and this is where we're gonna drag
and drop these puzzle pieces and hopefully they'll interlock
in a nice graphical way that informs me, the programmer,
that indeed this is okay to connect them in this way.
The end result might not be what I intend but at least if I want
to do something conditionally just like Rob indented
with the spacebar when using a little text editor here,
we're gonna see puzzle pieces that look graphically like this,
that's an if condition, and the--
it has just enough space on the inside
so that you can fit another puzzle piece in there
and it only interlocks if it's appropriate in--
to actually put those two pieces together.
Well, what are all these pieces?
Well, the third and final feature of the program is the--
the palette here of all of your various puzzle pieces
and they're nicely color-coded into categories
like motion-related puzzle pieces, looks, sounds, controls,
sensing and so forth, and so initially you'll see
that you just don't really know where to look but you kinda know
as you'll probably find in the problem set, what you want
to achieve and as in the side,
the problem set is actually quite open-ended.
As you'll see, it's going to be make something of interest
to you with Scratch, and so you'll come up with an idea
and you'll then have to figure how now to express that.
So let's express the simplest of programs.
I'm gonna click the control palette,
which is where I have puzzle pieces with which
to really get things started and I'm gonna drag
and drop the very first one when green flag clicks.
Now this shape, notice, has a little curved edge on top
and that just kind of implies to you graphically
that you can't put anything on top but the little indentation
on the bottom is much like a real world puzzle piece.
Something can lock into that.
Well what do I want Scratch to perhaps do?
Let me go to the looks category and there's a whole bunch
of stuff here and I don't really care about all the intricacies
but I'm gonna go ahead and drag this one, say.
And now notice graphically if I move it close to it,
it kind of wants to snap together.
So if I let go of my mouse, it indeed snaps together
and I can make it say exactly what that other program might--
might say, "Oh, hi world!"
And now if I zoom out, nothing's happened just
yet because the way to start the program,
to double click its icons so to speak is, as you might infer,
by clicking this green flag.
So if I click the green flag, the program runs,
which means all those puzzle pieces start executing.
They start running top to bottom and as soon as it hits the one
that it says, say "Oh, hi world",
that's indeed what Scratch does.
>> Well, what is this an example of?
Well, we can start tossing some jargon
on this that's actually going to come back
in every single future language we explore.
So, this here is a self-contained program
that we ourselves just wrote.
But generally, anything that's purple
in Scratch is what we'll call a statement.
It just tells the Scratch program
or the Scratch character to do something.
You might have "Wait one second".
This would be another statement that does what it says.
You might have-- and then now it's getting interesting,
"Play sound meow."
So now we have the means
of expressing things audio [inaudible]
like Nicole did with her program.
And if we actually now wanna integrate these
into our programs, let's see what we might do.
I'm gonna go ahead and now open up rather
than writing all these programs from Scratch.
Another such example, all of which are posted online
so that can play yourself, and you can see
and infer pretty straight forwardly even
if you've never programmed a computer before what this
thing's gonna do.
It's apparently gonna say, "Oh, hi world,"
for a second and it's gonna wait.
It's gonna say it again, it's gonna wait,
it's gonna say it again, alright?
So let's see what happens.
Let's go up to the top right.
As in the side, you can also full screen these things
by clicking a certain icon.
So now if I wanna play this full screen,
we have a program that's three times as good
but just as underwhelming.
So let's see if we can make this a little more interesting.
Let me go ahead instead and open
up version three of this program.
And now, it's gonna be a little more interesting 'cause I'm
gonna make use of that other type of statement.
[ Cat Meowing ]
[ Laughter ]
>> Alright, so now, how did this happen?
Well, we just used a different statement.
Instead of doing the say, which was a visual one,
we use the audio one, play sound meow, and it just starts to flex
around with the various menus,
you can import other sounds as in the side.
You can go here, you can say import, we can then go
into things like animal and we can-- we hear crickets,
we can hear [noise] oh, I got to click on it.
[Noise] And so forth.
That has no academic bearing onto that.
So, let's see if we now can take things
to the next level 'cause this is not the most exciting
of programs.
Yes, we're commanding the computer to do something
but I'm not yet all that excited, so I need a way
to express more interesting forms of logic.
So it turns out once upon a time there was a man named Mr. Boole,
whose name is now attached to this notion,
a Boolean expression in programming, not just Scratch,
really any programming language out there is something
that is either true or false, yes or no,
on or off, one or zero.
So long as you have some kind of binary answer, you can express
that as a Boolean expression.
Now in Scratch, Boolean expressions have this diagonal
shape here and inside of that,
in this case is touching mouse pointer question mark.
So a Boolean expression is indeed some kind of question
that you can ask and it's either going to evaluate
to true or false, yes or no.
So in this case, you can ask Scratch to do something only
if it's touching the mouse pointer.
That is the little arrow that I move around with my mouse.
You might also do something like this, is the mouse down?
You might also say is some number on the left less
than some number on the right?
You might instead say is it--
the case that the thing on the left and the thing
on the right are both true.
So you can check two conditions at once.
Am I touching some sprite and am I also touching the mouse,
and together can you then build
up even more interesting expressions.
And there are not-- this is not an unfamiliar concept.
If you use for instance CS50's encore shopping tool
or really any, frankly, forum on the web,
you've often encountered check boxes
and radio buttons and so forth.
Well, how are we implementing something
like these filters one Harvard courses?
Well, we're essentially implementing this notion
of a Boolean expression.
If you check the box offered this year, that is only going
to return true for courses that are indeed offered
in fall 2011 or spring 2012.
So we too are embodying this idea
of a Boolean expression all throughout the source code
that implements Harvard courses
and asking ourselves before we show any search results,
yes or no, true or false, one or zero.
And it's the same idea.
It's not expressed graphically.
It's expressed in a language called PHP,
which we ourselves will use to our term's end.
The ideas are the same.
Now, Boolean expressions are alone--
are only yes or no questions.
You still need to act on them and go off in this direction
or this way, when you encounter some fork in the road.
So Scratch has puzzle pieces that looks like this
as I depicted earlier where if something is true then you wanna
do what's inside of this block.
But you can be fancier still
if something is true else you can do this,
and Rob did that in our example
of the slightly refined PBK example and you can actually
in Scratch stack these things.
So even though the space between the if
and the else there is actually pretty small
such that there's really no puzzle pieces that fit in there.
As we'll see in a moment as you drag puzzle pieces toward things
like this they will also grow in a similar way
to fit additional puzzle pieces.
So we can even do things like this.
If else if else, and now you can have a three-way intersection,
a three-way decision points in your program.
So let's now see what this allows us to do.
Let me go back to my examples of high.
Let me open the fourth such iteration,
and I can do something pretty stupid but at least
to demonstrate that this works if 1 is less than 2, play meow.
Meow! Works.
Meow! Okay, obviously, it's gonna keep working.
So we can do more interesting conditions.
So let's open a fifth iteration of this, and now you see
that the program can get more interesting especially
if the program you might start creating in your head is a game.
You probably don't want the game to be completely deterministic.
Every time you play it, the bad guys are
in the exact same place, makes it pretty easy to win, right?
Most any popular game has some kind of randomness built in
and computers can allow us what's called pseudorandomness.
They can come up with [inaudible] looks random
but it actually isn't but more on that in the future.
Scratch too can do something like pick a random number from 1
to 10 and you can then ask the questions if it's less than 6,
well, in other words, with 50 percent probability, go ahead
and play the sound meow.
So let's try this now.
I'll click the green flag, nothing, click it again.
Meow! Yes, again, nothing, nothing, nothing.
Meow! There we go.
If we do this in infinite number of times,
50 percent of them will indeed be on
and the other 50 percents will be off.
Now this too is making for very short programs.
And so in most any program we use today, there are some notion
of looping, doing something again
and again and again or forever.
Even the simplest thing on your computer like the clock
that tells you what time it is,
that's probably a program implemented with some kind
of loop, an infinite loop at that because you want it
to do something, keep updating the seconds every time
on every strike of the clock.
So you might wanna have loops that repeat infinitely,
some loops that repeat finitely but in short you wanna be able
to do something again and again.
And now our programs again can get progressively neither.
So in variant 6 here, notice I don't have to drag and drop
at [inaudible] all these puzzle pieces saying again and again
and again and again, do something.
I can express it much more succinctly with a loop.
And so here, I might play this particular program.
[Cat sound] This too will get annoying frankly quickly.
Your roommates will be thrilled this week.
But let's open something
that does a little something more dynamic.
This is version 7 of this.
So this is kind of interesting.
And now again, the programs are getting bigger and bigger.
And to be clear, this is exactly where Nicole started.
You'll see that we're only gonna equip you
with the basic concepts and fundamental skills here
and then it will be up to you to run with it.
So when green flag is clicked, do the following forever.
If I'm ever touching the mouse pointer, that is the cat,
play sound meow then wait 2 seconds.
So let's see how this works.
Let me hit play, nothing happens.
Now, notice the puzzle pieces do highlight in white
when they're actually running
so you can see your program running.
Now if I move my mouse toward the cat, [cat sound].
So it's kind of like petting the cat.
So that might be how we implement that.
Now we might go to say version 8 of this
and let's see how this cat behaves.
So this is sort of the opposite kind
of cat whereby [background cat sound] he meows and meow.
So again there's some loop in there.
This is not a cat you wanna touch.
[Growl] And then he does that.
Well, how does that actually get implemented?
Well we just need another fork in the road, right?
If touching mouse pointer, then play some lion sound, else,
play meow again and again and again.
Well, another very common constructing programming is this
notion of a variable, something that you take for granted
in algebra but in algebra, X and Y and Z are not very interesting
or expressive but they still store values.
In programming, you wanna store more interesting things not just
numbers but maybe sentences or user input,
photos, they've uploaded.
And so in Scratch, you can define what are called variables
not using just boring names like X, Y and Z but socks on feet
or the slices of bread left and so forth.
And you can check those values in your program.
So now we'll transition to a much more dynamic program also
in by a former student.
This one called Fruit-Craft RPG.
And in this Scratch program, which again written by a student
after just a couple of days,
runs a little something like this.
At top left, you see a graphical depiction
and this is just something he drag and drop into his program
and it shows you the contents of these variables.
Now notice that with my mouse,
I can now have things that are interactive.
I can walk around this guy's home and table.
I can follow the arrow out the door.
And now think about how this might be implemented.
It's many more script, it's many more puzzle pieces
but what's it doing?
>> There's probably a loop that says forever.
If down arrow pressed, then what does it do?
Well, there's another puzzle piece that says move
and you say 10 pixels, 10 dots on the screen.
And if you have another "if" condition that says,
"If up arrow pressed, go up or left
or right," the exact same ideas.
So now notice I'm outside my house.
I can go over to this orange.
And now notice at top left,
we see what's generally called an array.
An array is the simplest of this thing called a data structure,
a word I use on Wednesday.
But a data structure is generally like a bucket
where you can store not just one but any number of things,
in this case some fruit.
Let's go get this.
Let's go get this
on the question marks we're [inaudible].
So remember that you can check
if you're touching something else on the screen,
and sure enough, if I go close enough,
there's an "if' condition that says,
"If sprite touching question mark sign,
apparently it's gonna then say, got some fruit,
bring it to the fruit place."
So there are some built-in instructions.
Well, let's go get the rest of this.
And notice my inventory at top left change
and now I'm gonna enter the fruit place.
And now notice here, if touching door change the whole scenery.
And so the costume of the stage has just changed.
Now let's see if this guy has got any final words for us here
and then we'll introduce some actual sound.
Now that over there was the grand prize of this game.
So we need just a couple more tricks to express ourselves
and implement more sophisticated things.
So this notion of an array in Scratch looks a little something
like this where you can add something like a word
like orange or apple to a variable
in this case called "inventory."
But you can also do multiple things at once, right?
Thus far the only example we've seen involving multiple sprites
on this screen was this RPG game.
But here, we see more generally the notion of threads.
A computer program these days can do multiple things at once
and a slight white lie.
It can create the illusion of doing multiple things at once.
It's just so much faster than you, the human are,
you don't really notice that it's doing a little bit of this,
a little bit of this, a little bit of this and repeating
so fast that you don't realize
that actually it's not all happening at the same time.
But in Scratch we can implement this idea of multiple threads,
multiple scripts or multiple sprites executing
at the very same time rather than one after the other.
And what this allows us to do is write a program like this
where two different sprites somehow interact.
So now notice one last piece of the UI here, at bottom right,
you see icons for each
of the multiple sprites in your program.
If you don't want just a cat, you can have a bird,
you can have a piece of fruit, you can have a house,
any number of sprites and then you just add a graphic to them
and they look more interesting than just a simple orange cat.
So now notice I have the cat selected so these scripts
at top left are all of these scripts that belong
to that sprite, to that cat and that script,
those scripts will only control that particular cat.
Well now, if we click over to the bird here,
the bird has his own set of puzzle pieces
and now let's see what this game is gonna do.
One green flag clicked,
apparently the bird is gonna go to negative 150, 150.
So it turns out that stage, it's like a graphic in like Photoshop
or some program where you have all these little dots
and you just figure out by trial
and error what numbers represent what position on the screen.
Point in direction 45 degrees so at a certain angle.
And then forever,
if not touching the cat, do the following.
Move three steps and if you're on the edge, bounce.
Now the cat is gonna something a little different
so let's play this in parallel.
[ Noise ]
>> Right? So think about what the cat is doing.
If touching bird-- [growl], okay,
not so good for the bird in this case.
But there's now interaction
that you can see 'cause what was the cat doing?
Well, let's go back to his scripts and zoom in.
He too goes to some specific X, Y coordinate.
But then, point in direction random.
So we decided, let's at least have him point
into some direction so that every time we do this demo,
it's not the same pan demonstration.
There's a little something variant.
And then forever the cat does this.
If touching bird play the lion sound then stop, else,
and this time the "else" is implicit
which is perfectly fine too if you have an--
or rather it's not implicit,
rather the "if" condition is gonna execute
but the bottom two blocks will always execute point towards
bird, move one step.
So we can actually make the cat much better at this game
if we make the cat move say 10 steps at a time
by just changing that number.
And now we go back here and hit play.
[Growl] Okay, happens all that much more quickly.
So there's one last feature here.
And this in programming in general, not just Scratch,
it's called "event," whereby you can two sprites, 2 characters
on the screen actually somehow intercommunicate
and we can implement this age old game of Marco Polo
that you might have played in a pool whereby two humans need
to somehow communicate.
So here we have the boy, Marco, and he's highlighted down here.
And so at top left, we see
that Marco is gonna behave as follows.
Forever if the space key is pressed, he's going to say Marco
for 2 seconds and then he's gonna broadcast an event.
Thus far, we've seen ways where two sprites can interact
by checking where they are,
which we haven't seen one sprite send another sprite a message.
When we say, say Marco, that's just for our human benefit,
to see some little cartoon bubble on the screen.
If we want the boy to communicate
to the girl programmatically by sending some kind of 0
and 1 message, we have to do what's called broadcast an event
which is just a message that now other sprites can listen for.
So let's look at the girl.
The girl is actually defined quite simply not
with the green flag trick
but with this other starting point when I receive event.
So she's listening for Marco to send her this message,
this event called "event."
And when she receives it, she is gonna say Polo for 2 seconds.
So let's go ahead and see this in action.
Nothing happens at first but remember,
once I hit the space bar, I see this
and now the girl replies in turn.
So now we have the means
of conveying two different messages to people.
Well, these are just building blocks,
how can we take things up to the next level?
Well, let me go ahead and we could call one brave volunteer
to the stage and show you yet another example.
Only us in the front are playing along today I see.
Okay. How 'bout, let's say you,
right in front, easier to get to.
Come on up.
What's your name?
>> Trevor [phonetic].
>> Trevor.
Trevor, come on up.
You have to be comfortable appearing on camera
and also performing poorly on a game.
So here we have a little something also made
by one of our own alumni.
Come on up and I give you in a moments the culmination of some
of these fundamentals called Scratch, Scratch revolution.
[Laughter] And if Barry [phonetic] we could turn
up the sound a bit more.
>> Oh, great.
>> Here we have-- let me set the stage verbally.
Notice we got a whole bunch of variables
that this former student dragged into the stage
so that you could see the variable score,
the awesome score, the cool score, the good, the boo score.
Then there are these arrows and each
of these arrows is a sprite.
And notice he used some randomness
to have these things appear in different locations.
Here is just a very poor man's way
of implementing instructions.
You just kind of do it with the built-in text editor
in the program.
Press Spacebar to begin then you're gonna--
Trevor is gonna hit the arrow keys in time
with arrows to score points.
Watch out if your score gets negative, bad things are going
to happen, little like DDR, if familiar.
So if our volumes indeed cranked up,
we'll see how well you can do here.
So you got the instructions in mind?
>> I guess so.
>> Okay, arrow keys are right here, spacebar begins.
[ Music ]
[ Background Music ]
>> Now notice, it's gonna be some kind of timer
that realizes it's time to raise the bar.
[ Music ]
[ Laughter ]
[ Background Music ]
>> So a big hand for Trevor if we could here.
[Background applause] Very well done.
So unless we have the culmination
of all these various puzzle pieces--
but it doesn't even stop there.
In fact, for the hacker edition of this week's problem
that you'll be challenged not only to implement something
of interest to you
but to integrate a little something like this.
A sensor board which is just this little plastic board that's
got a whole bunch of metallic traces, wires connected to it
and it takes a whole bunch of inputs.
There's this little slider which is like a joystick.
There's this little connector here which allows you
to connect this to your Mac or PC.
There's a light sensor.
There are resistance sensors.
There is a sound and light sensors.
There're all sorts of sensors built
into this otherwise very primitive looking device.
And so if you think now about some of the toys you might have
or electronic devices, what's generally inside of them
if they respond to inputs like touch and sound and feel,
are these basic building blocks.
So for the hacker of the addition of the problem
that you'll be challenged to connect up a cable
to your own computer and not just take input from the user
at the keyboard but rather from this actual physical device.
Well, we thought we would share just one last example here also
by one of our former students.
This was to a song that you'll see takes advantage of loops
and conditions and sounds and animation
to give us this final song.
[ Music ]
[ Movie Clip ]
[ Background Music ]
>> That's it for today.
This is CS50 Problem Set [inaudible] on the website.
We will see you next week.
[ Music ]
[ Applause ]