text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
>> Now, what about Boolean expressions? | 309.43 | 1.9 |
Recall that in Scratch,
these were expressions | 311.33 | 2.99 |
that are either true
or false-- questions, | 314.32 | 2.42 |
really, that are either true or false. | 316.74 | 2.17 |
So in the case of Scratch, we might
ask a simple question like this, | 318.91 | 3.05 |
is i less than 50? | 321.96 | 2.626 |
So i, again, is an integer. | 324.586 | 1.124 |
Maybe we're using it
in a Scratch program | 325.71 | 1.5 |
to keep track of a score
or something like that. | 327.21 | 2.1 |
So this syntax here in Scratch
just means, is i less than 50? | 329.31 | 4.5 |
Well, thankfully, something is
simple in C. And to translate, | 333.81 | 3.52 |
this we would simply say i less
than 50, using the familiar key | 337.33 | 4.45 |
on your keyboard. | 341.78 | 1.07 |
>> Meanwhile, if you wanted to
say something more general, | 342.85 | 2.291 |
like, well, is x less than y where each
of x and y are themselves variables? | 345.141 | 4.749 |
We can do the same thing
in C, so long as we've | 349.89 | 2.39 |
created these variables already. | 352.28 | 1.662 |
And we'll see how to
do that before long. | 353.942 | 1.708 |
We would simply say x less than y. | 355.65 | 2.94 |
>> So you're starting to
see some similarities. | 358.59 | 1.94 |
And those folks who made
Scratch were certainly | 360.53 | 2.96 |
inspired by some of these basic ideas. | 363.49 | 1.76 |
And you'll see this kind of
syntax in many languages-- | 365.25 | 5.1 |
not just Scratch, not
just C, but Python, | 370.35 | 1.81 |
and JavaScript, and
other languages still. | 372.16 | 2.63 |
>> Let's consider another construct
from C, the notion of a condition, | 374.79 | 3.48 |
doing something conditionally. | 378.27 | 2.1 |
If something is true, do this. | 380.37 | 2.35 |
If something else is true, do that. | 382.72 | 1.737 |
It's sort of the programming
equivalent of a fork in the road. | 384.457 | 2.583 |
Maybe it's a two-way fork,
a three-way fork, or more. | 387.04 | 2.69 |
And in Scratch, we might have
seen something like this. | 389.73 | 3.07 |
>> So this one's a big one. | 392.8 | 1.21 |
But consider the relative
simplicity of the logic. | 394.01 | 2.74 |
If x is less than y, then say x is less
than y, else if x is greater than y, | 396.75 | 7.26 |
then say x is greater than y. | 404.01 | 2.22 |
And then, logically, if
you think back to Scratch | 406.23 | 2.07 |
or just your own human intuition,
well, if x is not greater than y, and x | 408.3 | 4.31 |
is not less than y, then of course
x is going to be equal to y. | 412.61 | 4.39 |
So in this case, by nesting
those Scratch blocks, | 417 | 2.69 |
can we achieve a three
way fork in the road? | 419.69 | 2.89 |
>> Meanwhile, if we want to
do that in C, it arguably | 422.58 | 2.4 |
looks a little simpler-- at least
once you get familiar with the syntax. | 424.98 | 3.44 |
If x is less than y,
printf x is less than y. | 428.42 | 3.63 |
Else if x is greater than y,
printf x is greater than y. | 432.05 | 4.09 |
Else printf x is equal to y-- and,
again, with those backslash ends just | 436.14 | 5.07 |
for those new lines so that if you
actually ran this kind of program | 441.21 | 2.95 |
it would just move
your cursor ultimately | 444.16 | 1.78 |
to the next line of the screen. | 445.94 | 2.16 |
>> Now, meanwhile Scratch had other
more sophisticated features, only | 448.1 | 3.17 |
some of which we're going to
initially move over to the world of C. | 451.27 | 3.05 |
And one of them was
called a list in Scratch. | 454.32 | 2.69 |
And this was a special
type of variable that | 457.01 | 2.09 |
allowed you to store multiple things
in it back, to back, to back, to back. | 459.1 | 3.74 |
>> In C, it doesn't have
lists, per se, but something | 462.84 | 2.7 |
that are more generally
called arrays, although we'll | 465.54 | 2.55 |
come back later this semester
to looking at something | 468.09 | 2.5 |
called a list, or really a linked list. | 470.59 | 2.19 |
But for now, the closest
equivalent in C for us | 472.78 | 2.73 |
is going to be something
called an array. | 475.51 | 1.835 |
And an array is simply a
special type of variable | 477.345 | 2.395 |
that allows you to store data
back, to back, to back, to back. | 479.74 | 3.42 |
>> And, indeed, in Scratch,
if we wanted to access | 483.16 | 2.68 |
the first element of an array or
a list-- and I'm going to call it, | 485.84 | 3.19 |
by convention, argv, argument
vector, but more on that before long. | 489.03 | 4.57 |
If I want to get at the first element
of argv, in the world of Scratch | 493.6 | 3.49 |
you actually do typically
start counting from 1. | 497.09 | 3.84 |
>> And so I might get item 1 of argv. | 500.93 | 1.92 |
That's just how MIT implemented
the notion of lists. | 502.85 | 3.46 |
But in C, I'm going to
more simply just say, argv, | 506.31 | 3.55 |
which again is the name of my
list-- or to be clear, an array. | 509.86 | 2.898 |
And if I want the first
elements, I'm going | 512.758 | 1.791 |
to use square brackets, which you
might not often used under a keyboard. | 514.549 | 3.341 |
>> But 0 just means, get me the first. | 517.89 | 2.26 |
So on occasion and as
time passes, we're going | 520.15 | 2.01 |
to start to see these dichotomies
between Scratch and C, | 522.16 | 2.41 |
whereby Scratch uses one. | 524.57 | 1.5 |
We in C use 0 here. | 526.07 | 1.6 |
But you'll quickly see
once you understand | 527.67 | 1.75 |
the foundations of each language, that
these things start to get all the more | 529.42 | 3.5 |
familiar through practice and practice. | 532.92 | 3.94 |
>> So let's actually look now at a program. | 536.86 | 2.84 |
Here shall be the first of our C
source code for complete programs. | 539.7 | 4.331 |
And the program we're going
to offer for consideration | 544.031 | 2.249 |
is the one that's equivalent
to that earlier Scratch piece. | 546.28 | 3.06 |
>> So in here, we have what's
arguably the simplest C program | 549.34 | 3.87 |
you can write that
actually does something. | 553.21 | 2.2 |
Now, we'll look past,
for now, has include, | 555.41 | 2.84 |
standard io.h, and these angle
brackets, and int, and void, | 558.25 | 2.94 |
and the curly braces, and the like. | 561.19 | 1.65 |
>> And let's just focus on
what, at least intuitively, | 562.84 | 2.55 |
might jump out at you already. | 565.39 | 1.47 |
In fact, main, I don't
necessarily know what this is, | 566.86 | 3.44 |
but much like Scratch had that when
green flag clicked puzzle piece, | 570.3 | 4.28 |
so does C as a programming language
have a main piece of code that | 574.58 | 4.49 |
gets executed by default. And, indeed,
it's literally going to be called main. | 579.07 | 4.31 |
>> So main is a function. | 583.38 | 1.34 |
And it's a special function that exists
in C that when you run a program, | 584.72 | 4 |
it is main that gets run by
default. In the world of Scratch, | 588.72 | 4 |
it was usually when green flag
clicked that got run by default. | 592.72 | 4.25 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.