Материал готовится,
пожалуйста, возвращайтесь позднее
пожалуйста, возвращайтесь позднее
So the topic of this section, is what's
called a for-loop. And this is going to be a
big increase in what we can do with the
code. So, if you look at the original
flowers image, here, it's 457
pixels wide by 360 pixels high. So, if you
multiply just to get the total number of
pixels that's 164,000 and a few odd
pixels. And this is a pretty small image.
So, that's a lot of pixels. So the way we
were writing code before, where you would
have a line like pixel.setRed(255) to
change one pixel to red, that's just,
that's not a practical way to do an
operation on a whole image. I mean, this
is a small image and has over 100,000
pixels. So what we want is a construct
where we can write a few lines of code
that capture some change we want to make
and then let the computer take care of the
bookkeeping of running those lines of code
again and again once for each pixel on the
image. So the for-loop, the topic of the
section, is gonna do exactly this and this
is gonna be a big increase in what we can
do with, with the code. So let me talk
about the, the structure of this thing.
I'm just going to identify the parts then
I'll do an example. So here in the blue
box here I have a picture of a for-loop
and I'll just talk about what the parts
are. So, it starts off - and I'll use my
pen - this, this begins the for loop so it
has the word for and then in parenthesis
it says pixel colon image and then there's
a left curly brace to start to set off the
next line and so what this means is. And
all the syntaxes require the parenthesis
and the curly brace and everything.
Usually in my examples or in the exercises
we'll, it's just, it's the same every
time, so usually we'll provide it and then
just ask you to write the code on the
following line. So what this means is, for
every pixel in this image please to the
following and then the following is to
defined as whatever is in the
colored braces here so. These lines.
Inside of the thing. That's the, called
the body of the for-loop. And this is
just a few lines of code that can do,
whatever we want. So, the way the
for-loop works, is, and let's say we're, you
know, working on the flowers image here.
Is it. Here we have three lines. So it's
gonna take, let's say, pixel number one of
the whole image, the upper left pixel. And
so it, it isolates pixel number one. And then
the for-loop runs these, these first
three lines. So it says; pixel.setRed(0),
pixel.setGreen(0), pixel.setBlue(0). So those are all
happening to pixel number one. And then it,
really, what that does is it changes it to
black, right? It sets red, green, and blue
all to zero. So when the for-loop gets to
the bottom a funny thing happens and I
sort of have this black arrow. It loops
back up to the top of the three lines and
now it's gonna isolate pixel number two,
so the second pixel, and then it runs
these three lines again. So it does them
to pixel two and then it loops back to the
top and it runs the three lines again from
pixel number three, and so on. It just
runs the lines again and again and again
once for each pixel in the image. So. The,
oh the other thing I should point out here
is you'll notice that the, the three lines
in the body are indented and that not
required but it's a, it's a common
convention to show that the lines in the
body are kind of different from the other
lines of code. Where this image equals new
called a for-loop. And this is going to be a
big increase in what we can do with the
code. So, if you look at the original
flowers image, here, it's 457
pixels wide by 360 pixels high. So, if you
multiply just to get the total number of
pixels that's 164,000 and a few odd
pixels. And this is a pretty small image.
So, that's a lot of pixels. So the way we
were writing code before, where you would
have a line like pixel.setRed(255) to
change one pixel to red, that's just,
that's not a practical way to do an
operation on a whole image. I mean, this
is a small image and has over 100,000
pixels. So what we want is a construct
where we can write a few lines of code
that capture some change we want to make
and then let the computer take care of the
bookkeeping of running those lines of code
again and again once for each pixel on the
image. So the for-loop, the topic of the
section, is gonna do exactly this and this
is gonna be a big increase in what we can
do with, with the code. So let me talk
about the, the structure of this thing.
I'm just going to identify the parts then
I'll do an example. So here in the blue
box here I have a picture of a for-loop
and I'll just talk about what the parts
are. So, it starts off - and I'll use my
pen - this, this begins the for loop so it
has the word for and then in parenthesis
it says pixel colon image and then there's
a left curly brace to start to set off the
next line and so what this means is. And
all the syntaxes require the parenthesis
and the curly brace and everything.
Usually in my examples or in the exercises
we'll, it's just, it's the same every
time, so usually we'll provide it and then
just ask you to write the code on the
following line. So what this means is, for
every pixel in this image please to the
following and then the following is to
defined as whatever is in the
colored braces here so. These lines.
Inside of the thing. That's the, called
the body of the for-loop. And this is
just a few lines of code that can do,
whatever we want. So, the way the
for-loop works, is, and let's say we're, you
know, working on the flowers image here.
Is it. Here we have three lines. So it's
gonna take, let's say, pixel number one of
the whole image, the upper left pixel. And
so it, it isolates pixel number one. And then
the for-loop runs these, these first
three lines. So it says; pixel.setRed(0),
pixel.setGreen(0), pixel.setBlue(0). So those are all
happening to pixel number one. And then it,
really, what that does is it changes it to
black, right? It sets red, green, and blue
all to zero. So when the for-loop gets to
the bottom a funny thing happens and I
sort of have this black arrow. It loops
back up to the top of the three lines and
now it's gonna isolate pixel number two,
so the second pixel, and then it runs
these three lines again. So it does them
to pixel two and then it loops back to the
top and it runs the three lines again from
pixel number three, and so on. It just
runs the lines again and again and again
once for each pixel in the image. So. The,
oh the other thing I should point out here
is you'll notice that the, the three lines
in the body are indented and that not
required but it's a, it's a common
convention to show that the lines in the
body are kind of different from the other
lines of code. Where this image equals new
Загрузка...
Выбрать следующее задание
Ты добавил
Выбрать следующее задание
Ты добавил