In this lesson, students will be introduced to count loops. |
Students will be able to:
|
1. The count Loop Like the repeat block, the count with block allows you to repeat a sequence of instructions multiple times. But it does so in a slightly different way by using a counter variable. We have seen how to use a repeat loop to have the robot trace a square:
We can do the same thing with a count loop like this (see Example 1): The count loop keeps track of which iteration it is on by using a variable. The default name for the variable is "i", but you can name it whatever you wish. We specify the starting and ending values of the counter, as well as the increment to count by (such as count by 1s, or 2s, or etc.). In the example above, the value of i is 1 for the first iteration of the loop (tracing the first side of the square), then its value changes to 2 during the second iteration, to 3 during the third iteration, and 4 during the fourth and last iteration. As another example, if we make it "count with i from 0 to 10 by 2", i will take on the following values for each iteration of the loop: 0, 2, 4, 6, 8, 10. So the loop has 6 iterations.
2. Why Use the count Loop? The advantage of the count loop is that we can use the counter variable in the loop to do different things, depending on the value of the variable. A simple example is displaying the odd numbers from 1 to 13 (see Example 2): The result looks like this: If we don't want the "i is" part of the display, so just the numbers appear, we can write the code like this (remember that the \n gives a line break): Or, if we want the numbers displayed on one line with spaces between them:
Example 4 shows a more creative use of a count loop to have the robot trace out a squared spiral. The count loop is especially useful when doing things like animation, which is covered in a later lesson.
|
|
The robot traces out an 8x8 square using a count loop. |
The code uses a count loop to display the odd numbers from 1 to 13. |
The code has two more variations on using a count loop to display the odd numbers from 1 to 13. |
The robot traces out a squared spiral using the counter in the count loop to indicate the distance to move each time. |
Grid Size: S M L | Simple View: | MinView: |
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
|
|||||||||||
|
|||||||||||