Return to Video

For Loops

  • 0:01 - 0:05
    So the topic of this section, is what's
    called a for-loop. And this is going to be a
  • 0:05 - 0:10
    big increase in what we can do with the
    code. So, if you look at the original
  • 0:10 - 0:15
    flowers image, here, it's 457
    pixels wide by 360 pixels high. So, if you
  • 0:15 - 0:20
    multiply just to get the total number of
    pixels that's 164,000 and a few odd
  • 0:20 - 0:25
    pixels. And this is a pretty small image.
    So, that's a lot of pixels. So the way we
  • 0:25 - 0:30
    were writing code before, where you would
    have a line like pixel.setRed(255) to
  • 0:30 - 0:34
    change one pixel to red, that's just,
    that's not a practical way to do an
  • 0:34 - 0:39
    operation on a whole image. I mean, this
    is a small image and has over 100,000
  • 0:39 - 0:44
    pixels. So what we want is a construct
    where we can write a few lines of code
  • 0:44 - 0:49
    that capture some change we want to make
    and then let the computer take care of the
  • 0:49 - 0:55
    bookkeeping of running those lines of code
    again and again once for each pixel on the
  • 0:55 - 1:00
    image. So the for-loop, the topic of the
    section, is gonna do exactly this and this
  • 1:00 - 1:05
    is gonna be a big increase in what we can
    do with, with the code. So let me talk
  • 1:05 - 1:09
    about the, the structure of this thing.
    I'm just going to identify the parts then
  • 1:09 - 1:14
    I'll do an example. So here in the blue
    box here I have a picture of a for-loop
  • 1:14 - 1:21
    and I'll just talk about what the parts
    are. So, it starts off - and I'll use my
  • 1:21 - 1:27
    pen - this, this begins the for loop so it
    has the word for and then in parenthesis
  • 1:27 - 1:33
    it says pixel colon image and then there's
    a left curly brace to start to set off the
  • 1:33 - 1:39
    next line and so what this means is. And
    all the syntaxes require the parenthesis
  • 1:39 - 1:43
    and the curly brace and everything.
    Usually in my examples or in the exercises
  • 1:43 - 1:48
    we'll, it's just, it's the same every
    time, so usually we'll provide it and then
  • 1:48 - 1:52
    just ask you to write the code on the
    following line. So what this means is, for
  • 1:52 - 1:56
    every pixel in this image please to the
    following and then the following is to
  • 1:56 - 2:04
    defined as whatever is in the
    colored braces here so. These lines.
  • 2:04 - 2:09
    Inside of the thing. That's the, called
    the body of the for-loop. And this is
  • 2:09 - 2:15
    just a few lines of code that can do,
    whatever we want. So, the way the
  • 2:15 - 2:20
    for-loop works, is, and let's say we're, you
    know, working on the flowers image here.
  • 2:20 - 2:25
    Is it. Here we have three lines. So it's
    gonna take, let's say, pixel number one of
  • 2:25 - 2:30
    the whole image, the upper left pixel. And
    so it, it isolates pixel number one. And then
  • 2:30 - 2:33
    the for-loop runs these, these first
    three lines. So it says; pixel.setRed(0),
  • 2:33 - 2:37
    pixel.setGreen(0), pixel.setBlue(0). So those are all
  • 2:37 - 2:41
    happening to pixel number one. And then it,
    really, what that does is it changes it to
  • 2:41 - 2:45
    black, right? It sets red, green, and blue
    all to zero. So when the for-loop gets to
  • 2:45 - 2:50
    the bottom a funny thing happens and I
    sort of have this black arrow. It loops
  • 2:50 - 2:55
    back up to the top of the three lines and
    now it's gonna isolate pixel number two,
  • 2:55 - 2:59
    so the second pixel, and then it runs
    these three lines again. So it does them
  • 2:59 - 3:04
    to pixel two and then it loops back to the
    top and it runs the three lines again from
  • 3:04 - 3:09
    pixel number three, and so on. It just
    runs the lines again and again and again
  • 3:09 - 3:15
    once for each pixel in the image. So. The,
    oh the other thing I should point out here
  • 3:15 - 3:19
    is you'll notice that the, the three lines
    in the body are indented and that not
  • 3:19 - 3:23
    required but it's a, it's a common
    convention to show that the lines in the
  • 3:23 - 3:27
    body are kind of different from the other
    lines of code. Where this image equals new
  • 3:27 - 3:31
    simpleImage, well that just happens once and
    the print image that just happens once.
  • 3:31 - 3:36
    But the lines inside the loop are sort of
    special in that they had this quality that
  • 3:36 - 3:40
    they're going to be run again and again.
    So let's try. We just run that, that exact
  • 3:40 - 3:45
    example. So here I have it in runnable
    code so the, this loads the image. Then
  • 3:45 - 3:49
    I've got the, the for-loop setting off
    the body. Here's the three lines of the
  • 3:49 - 3:54
    body. And then it, it's kind of small here
    but then there's a, a right curly brace
  • 3:54 - 3:59
    sort of balancing the left curly brace up
    here that closes off the body. So let's
  • 3:59 - 4:03
    just run it. See what we get. So you can
    see, what we get is just this pure black
  • 4:03 - 4:08
    rectangle, and it's, it's not very useful
    but it, it does show what the for-loop
  • 4:08 - 4:12
    does. So what's happened is, we have this
    original flowers image with all sorts of
  • 4:12 - 4:17
    red, and green and yellow and whatever in
    it. And what this code has done, is it has
  • 4:17 - 4:22
    visited every pixel on that image and done
    these three lines. And what this do is
  • 4:22 - 4:26
    they take, they set the red, green and
    blue all to zero, so in fact it just
  • 4:26 - 4:31
    changes the pixel to be a pure black
    pixel. And so then, if you imagine that
  • 4:31 - 4:36
    just happening over the entire flower's
    image, obliterating all the data, this is
  • 4:36 - 4:41
    what we're left with, just this pure black
    rectangle. Alright. So let me a try a,
  • 4:41 - 4:48
    more sane example. So here I'm gonna
    say, for each pixel set red, green - red
  • 4:48 - 4:54
    and green to 255 and blue to zero. So I
    wanna think about, well what's the code to
  • 4:54 - 4:58
    do that. Very often, for these problems,
    we'll have the pattern that in English,
  • 4:58 - 5:02
    I'll say well, have, you know, do this
    effect, set red to 255 or whatever. And
  • 5:02 - 5:07
    then ultimately, on the exercise, it will
    be your job to sort of translate that into
  • 5:07 - 5:12
    code. So here is this is an example of
    that pattern. So the code to put in the
  • 5:12 - 5:17
    body to change red and green to 255 and
    blue to zero is just to say first, the
  • 5:17 - 5:21
    first two lines say pixel.setRed(255)
    and pixel.setGreen(255). And then the
  • 5:21 - 5:26
    third line is pixel.setBlue(0).
    So if I run that. What we see is this
  • 5:26 - 5:30
    bright yellow rectangle so this is kinda
    similar to the previous example. We've
  • 5:30 - 5:35
    just gone through and changed the green
    and blue for every pixel in this thing in
  • 5:35 - 5:40
    this case to sort of get this vivid yellow
    color. So my, so those two examples are a
  • 5:40 - 5:44
    little unrealistic, right? I just, we just
    obliterated all the data, and just made
  • 5:44 - 5:48
    this, colored rectangle. So now I'd like
    to try one, that's a little more
  • 5:48 - 5:52
    realistic, where we, instead of
    obliterating all the data in the flowers
  • 5:52 - 5:57
    image, we work with it. So here's the, the
    original flowers image. And as we know
  • 5:57 - 6:03
    yellow equals red plus green so we would
    expect that here where I've got the yellow
  • 6:03 - 6:09
    flowers that the red and green are, are
    high there. So what I'd like to do for
  • 6:09 - 6:14
    this example is set the red to zero for
    the whole image. Think about what that's
  • 6:14 - 6:19
    gonna do. So the first question is
    alright, what's the code to do that? And
  • 6:19 - 6:24
    in this case what you see is, I just have
    the line pixel.setRed(0) as the
  • 6:24 - 6:28
    body. Now in my previous examples the body
    was like three lines of code. It could, it
  • 6:28 - 6:33
    could be anything. So it could be three
    lines or six lines. In this case it's just
  • 6:33 - 6:38
    gonna be one line. And what this says, you
    know, the sort of English translation of
  • 6:38 - 6:42
    this for-loop is, for each pixel in the
    image, set the red to zero. So let's see
  • 6:42 - 6:47
    what that looks like. So if I run that,
    what you see is there's sort of a...
  • 6:47 - 6:52
    the flowers have changed to look like these
    kind of weird radioactive green flowers.
  • 6:52 - 6:56
    And that can make sense if you sort of
    unpack like what made the flowers yellow
  • 6:56 - 7:00
    before well they were yellow because there
    was a combination of red and green light.
  • 7:00 - 7:04
    What this code does is, it just zeroes out
    all the red light. It's like we just
  • 7:04 - 7:08
    turned that lamp down to zero everywhere.
    And so what we're left with is just the
  • 7:08 - 7:12
    green light that was making the images.
    The other thing to notice is, look over
  • 7:12 - 7:15
    here at the, the green leaves kind of on
    the lower right. They, they don't look
  • 7:15 - 7:19
    much different at all so probably this was
    mostly green light to start with. The red
  • 7:19 - 7:23
    light was maybe, you know, a value like
    eight or twenty or something. And so when
  • 7:23 - 7:30
    we changed that to zero it doesn't really
    make much, much visible difference. Let's
  • 7:30 - 7:35
    try one more example here. So for this
    one, I'm gonna start with the flowers
  • 7:35 - 7:41
    image and I'm gonna say set green and blue
    to zero and leave red unchanged. So what
  • 7:41 - 7:46
    is the code for that? Well, I'm gonna have
    a two-line body here. I'm just gonna say
  • 7:46 - 7:52
    pixel.setGreen(0) and pixel.setBlue(0), and I just, I just don't change
  • 7:52 - 7:58
    the red, so it'll be left at whatever it
    is. So we can run that. And what you see,
  • 7:58 - 8:05
    is now we get these, red flowers. And
    what?s happened here is, this is actually
  • 8:05 - 8:09
    called the red channel of the image. And
    what's happened is, normally, the image is
  • 8:09 - 8:14
    this combination of red, green, and blue
    light. What we've done is we've changed
  • 8:14 - 8:18
    both the green and the blue to zero. So
    we've turned those down to just be
  • 8:18 - 8:22
    nothing. And what we're left with is sort
    of a map of, where was the red light in
  • 8:22 - 8:27
    his image? Where was red bright, and where
    was red dark? And what we see is, well,
  • 8:27 - 8:31
    over here on the left, where it was dark,
    there is not a lot of red light. And also,
  • 8:31 - 8:35
    here, the green leaves. Not much red
    there. And so, really, it's just the
  • 8:35 - 8:40
    yellow flowers, was this, prominent area
    of red light. So that's a, that's just a
  • 8:40 - 8:44
    way of looking at the image. There's
    actually an analogous, blue channel and
  • 8:44 - 8:48
    green channel we could make to sort of
    look at where, those lights are,
  • 8:48 - 8:55
    alternately. Alright. So, the for-loop is,
    I said it turns out this is a very
  • 8:55 - 8:59
    powerful feature that allows us to write a
    few lines of code and let the computer run
  • 8:59 - 9:03
    them over just, you know, some sort of big
    data set. In this case, we're doing
  • 9:03 - 9:08
    images. I should mention for completeness,
    JavaScript language you're using actually
  • 9:08 - 9:12
    doesn't have a for-loop that's as compact
    as this. It's just an omission in the
  • 9:12 - 9:16
    language. So I added it just for this
    class to make it work here. So if you're
  • 9:16 - 9:20
    doing some other JavaScript problem I'm
    afraid you won't have this. But, many
  • 9:20 - 9:24
    languages do have a for-loop that looks
    just like this. It's just sort of a weird
  • 9:24 - 9:30
    omission from JavaScript. So the pattern
    here is that we get to write code, you
  • 9:30 - 9:34
    know, just in, in the sense of short bit
    of code capturing what we want, and I
  • 9:34 - 9:39
    would say that this reflects the theme of
    the idea that computers are powerful yet
  • 9:39 - 9:44
    kind of stupid. So you write the code that's kind of interesting, like, well I
  • 9:44 - 9:48
    wanna change the green this way or that
    way or whatever. And then by putting it in
  • 9:48 - 9:53
    the for loop we have this partnership with
    the computer where the computer will take
  • 9:53 - 9:57
    care of the powerful thing, like
    we're running that, 100,000's or even
  • 9:57 - 10:02
    1,000,000's of times. The computer handles
    that end, but it is also, the computer is
  • 10:02 - 10:05
    really just doing something very
    mechanical there. And so I think that,
  • 10:05 - 10:10
    that does show the general theme of, of a,
    how computers are. That their sort of, the
  • 10:10 - 10:14
    computer handles the mechanical part very
    quickly, but the person has to add the
  • 10:14 - 10:19
    creativity to like control what's actually
    going to happen. So in the next section
  • 10:19 - 10:24
    - after there's some excersises of looking kind of like
    this - and then in the next session I need
  • 10:24 - 10:28
    to add just one more feature that will
    allow us to start doing image processing
  • 10:28 - 10:31
    examples like this that are a lot more
    interesting.
Title:
For Loops
Video Language:
English
duvin1 edited English subtitles for For Loops
duvin1 edited English subtitles for For Loops
duvin1 edited English subtitles for For Loops
stanford-bot edited English subtitles for For Loops
stanford-bot edited English subtitles for For Loops
stanford-bot added a translation

English subtitles

Revisions