text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
language from our friends
at MIT's Media Lab. | 25.44 | 1.92 |
>> And with Scratch, did we explore
ideas like functions, and conditions, | 27.36 | 4.37 |
and loops, and variables, and even
events, and threads, and more. | 31.73 | 3.48 |
And today, we're going to
continue using those ideas, | 35.21 | 2.67 |
and really taking them for
granted, but translate them | 37.88 | 2.75 |
to another language known as C. Now,
C is a more traditional language. | 40.63 | 3.59 |
It's a lower level
language, if you will. | 44.22 | 1.8 |
>> It's purely textual. | 46.02 | 1.28 |
And so at first glance, it's
all going to look rather cryptic | 47.3 | 2.61 |
if you've never programmed before. | 49.91 | 1.52 |
We're going to have
semi-colons, and parentheses, | 51.43 | 2.1 |
and curly braces, and more. | 53.53 | 1.62 |
But realize that even
though the syntax is | 55.15 | 2.09 |
about to look a little unfamiliar
to most of you, see past that. | 57.24 | 3.36 |
And try to see the ideas
that are, indeed, familiar, | 60.6 | 2.62 |
because here in week one what
we'll begin to do is to compare, | 63.22 | 3.53 |
initially, Scratch versus C. | 66.75 | 2.23 |
>> So, for instance, recall that when we
implemented the first of our programs | 68.98 | 3.37 |
last time, we had a block that looked
a little something like this-- when | 72.35 | 3.87 |
green flag clicked, and then we had
one or more puzzle pieces below it, | 76.22 | 3.77 |
in this case, say, hello world. | 79.99 | 2.16 |
So, indeed, in Scratch,
when I click that green flag | 82.15 | 2.72 |
to run my program, so
to speak, these are | 84.87 | 2.52 |
the blocks that get executed, or run. | 87.39 | 2.13 |
And, specifically, Scratch
said, hello, world. | 89.52 | 2.71 |
>> Now, I could have specified
different words here. | 92.23 | 3.147 |
But we'll see that, indeed, many
of these blocks-- and indeed, | 95.377 | 2.583 |
in C many functions-- can be
parametrized or customized | 97.96 | 3.92 |
to do different things. | 101.88 | 1.27 |
In fact, in C if we
want to convert, now, | 103.15 | 2.37 |
this Scratch program
to this other language, | 105.52 | 2.047 |
we're going to write a
little something like this. | 107.567 | 2.083 |
>> Granted, there is some unfamiliar
syntax there most likely, int, | 109.65 | 2.89 |
and parentheses, and void. | 112.54 | 1.84 |
But printf-- even though you would
think it would just be print. | 114.38 | 3.36 |
But print means print
formatted, as we'll soon see. | 117.74 | 2.38 |
This literally will print
to the screen whatever | 120.12 | 2.02 |
is inside of those parentheses, which
of course in this case is, hello world. | 122.14 | 3.85 |
>> But you'll notice some other
syntax, some double quotes, | 125.99 | 3.3 |
that the parentheses at the end,
the semi-colon and the like. | 129.29 | 2.6 |
So there's a bit of overhead,
so to speak, both cognitively | 131.89 | 3.137 |
and syntactically, that we're going
to have to remember before long. | 135.027 | 2.833 |
But realize that with practice,
this will start to jump out at you. | 137.86 | 2.86 |
>> In fact, let's focus on that one
function specifically-- in this case, | 140.72 | 4.2 |
say hello world. | 144.92 | 1.37 |
So say is the function. | 146.29 | 1.27 |
Hello world is its parameter,
or argument, its customisation. | 147.56 | 3.76 |
>> And the equivalence in C is just
going to be this one line here, | 151.32 | 3 |
where printf is equivalent to, say,
the double quoted string, hello | 154.32 | 4.39 |
world is equivalent, of course,
to what's in the white box there. | 158.71 | 2.76 |
And the backslash n, though a little
strange and absent from Scratch, | 161.47 | 4.21 |
simply is going to have the effect we'll
see in a computer, like my Mac or a PC, | 165.68 | 3.7 |
of just moving the
cursor to the next line. | 169.38 | 2.28 |
It's like hitting
Enter on your keyboard. | 171.66 | 2.31 |
>> So we'll see that again before long. | 173.97 | 1.61 |
But first, let's take a look at this
other example in the case of loops. | 175.58 | 3.06 |
We had this forever loop last time,
which was a series of puzzle pieces | 178.64 | 4.19 |
that did something literally
forever-- in this case, | 182.83 | 2.66 |
say, hello world, hello world,
hello world, hello world. | 185.49 | 2.87 |
So it's an infinite loop by design. | 188.36 | 1.99 |
>> In C, if we want to implement this
same idea, we might simply do this. | 190.35 | 4.23 |
While true, printf hello world-- now
while, just semantically, kind of | 194.58 | 4.99 |
conjures up the idea of doing
something again, and again, and again, | 199.57 | 3.52 |
and for how long? | 203.09 | 0.89 |
Well, true-- recall that
true is just on or one. | 203.98 | 4.01 |
>> And true is, of course, always true. | 207.99 | 2.67 |
So it's kind of a meaningless
statement just to say true. | 210.66 | 2.4 |
But indeed, this is deliberate,
because if true is just always true, | 213.06 | 3.83 |
than while true just implies,
if a little indirectly, | 216.89 | 3.96 |
that the following lines of code
in between those curly braces | 220.85 | 3.22 |
should just execute again, and again,
and again, and never actually stop. | 224.07 | 4.25 |
>> But if you do want your
loop to stop, as we | 228.32 | 1.91 |
did last time with something like
this, repeat the following 50 times, | 230.23 | 4.27 |
in C we can do the same with what's
called a for loop-- the keyword | 234.5 | 3.2 |
not being while, but for. | 237.7 | 1.63 |
And then we have some new syntax here,
with int i equals 0, i less than 50, | 239.33 | 3.96 |
i++. | 243.29 | 0.59 |
And we'll come back to that. | 243.88 | 1.55 |
But this is simply how we would
translate the set of Scratch blocks | 245.43 | 4.23 |
to a set of C lines of code. | 249.66 | 3.419 |
>> Meanwhile, consider variables. | 253.079 | 1.371 |
And, in fact, we just
saw one a moment ago. | 254.45 | 2.09 |
And in the case of Scratch, if we
wanted to declare a variable called i | 256.54 | 4.68 |
for i being integer, just a number,
and we want to set it to some value, | 261.22 | 3.37 |
we would use this orange
block here-- set i to 0. | 264.59 | 3.82 |
>> And we'll see today and
beyond, just like last week, | 268.41 | 2.39 |
programmers do almost always
start counting from zero, really | 270.8 | 3.05 |
by convention. | 273.85 | 1.1 |
But also because recall from
our discussion of binary, | 274.95 | 2.3 |
the smallest number you can
represent with any number of bits | 277.25 | 2.74 |
is just going to be 0 itself. | 279.99 | 1.65 |
And so we'll generally start
initializing even our variables to 0. | 281.64 | 3.55 |
>> And in C to do the same,
we're going to say int | 285.19 | 2.52 |
for integer, i just by convention. | 287.71 | 2.4 |
I could have called this variable
anything I want, just like in Scratch. | 290.11 | 3.28 |
And then equals 0 just assigns
the value 0 from the right | 293.39 | 4.38 |
and puts it into the variable, or the
storage container there, on the left. | 297.77 | 3.549 |
And the semi-colon as we'll see-- and
we've seen a few of these already-- | 301.319 | 3.041 |
just means end of thought. | 304.36 | 2.17 |
Proceed to do something else
on the lines that follow. | 306.53 | 2.9 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.