Return to Video

Jie Jin introduces For Loops

  • 0:06 - 0:11
    When you use a repeat block to loop your code,
    how does the computer know when it's repeated
  • 0:11 - 0:17
    enough times? The repeat block is actually
    hiding a more sophisticated piece of code
  • 0:17 - 0:23
    called a for loop which counts from a starting
    value up to an ending value by a specific
  • 0:23 - 0:31
    increment. For example, a repeat three block
    counts from 1 to 3 by 1. Every time it counts,
  • 0:31 - 0:36
    it runs the code inside the loop. The for
    loop knows how many times it has run by using
  • 0:36 - 0:41
    a counter variable which is set to the starting
    value at the beginning of the loop and has
  • 0:41 - 0:45
    the increment added to it each time the loop
    is run. As soon as the counter variable is
  • 0:45 - 0:52
    greater than the ending value, the loop stops
    running. The benefit of using a real for loop
  • 0:52 - 0:56
    instead of the repeat block is that you can
    actually see the counter variable and use
  • 0:56 - 1:02
    it in your loop. For example, if I have a
    series of flowers and the first one has one
  • 1:02 - 1:07
    nectar, the second one has two nectars and
    the third one has three, I can use the for
  • 1:07 - 1:13
    loop to tell the bee to collect 'counter'
    nectars each time, which would one at the
  • 1:13 - 1:19
    first flower, two at the second and three
    at the third. Also in a for loop, you can
  • 1:19 - 1:24
    increment the counter by a number other than
    one each time. You can potentially count by
  • 1:24 - 1:28
    2s, 4s or even an amount that changes every
    time through.
Title:
Jie Jin introduces For Loops
Video Language:
English
Duration:
01:30

English subtitles

Revisions