Return to Video

For Loops in the Bee puzzle of Course 4

  • 0:05 - 0:10
    When you use a repeat block to loop your code,
    how does the computer know when it's repeated
  • 0:10 - 0:16
    enough times? The repeat block is actually
    hiding a more sophisticated piece of code
  • 0:16 - 0:22
    called a for loop which counts from a starting
    value up to an ending value by a specific
  • 0:22 - 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:40
    a counter variable which is set to the starting
    value at the beginning of the loop and has
  • 0:40 - 0:44
    the increment added to it each time the loop
    is run. As soon as the counter variable is
  • 0:44 - 0:51
    greater than the ending value, the loop stops
    running. The benefit of using a real for loop
  • 0:51 - 0:55
    instead of the repeat block is that you can
    actually see the counter variable and use
  • 0:55 - 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:12
    loop to tell the bee to collect 'counter'
    nectars each time, which would one at the
  • 1:12 - 1:18
    first flower, two at the second and three
    at the third. Also in a for loop, you can
  • 1:18 - 1:23
    increment the counter by a number other than
    one each time. You can potentially count by
  • 1:23 - 1:27
    2s, 4s or even an amount that changes every
    time through.
Title:
For Loops in the Bee puzzle of Course 4
Description:

more » « less
Video Language:
English
Team:
Code.org
Project:
CSF '21-'22
Duration:
01:29

English subtitles

Revisions Compare revisions