Lesson 9.3a introduced the repeat loop. Another type of loop is known as the "for" loop. It can do the same thing as the repeat loop, but its syntax is a little more complicated: Though most programming languages label the for loop with the word "for," RoboBlockly uses "count" to emphasize that the for loop implements a counting type of iteration. The for loop uses a counter variable (often named "i") that runs from a minimum to a maximum value (1 to 10 in the example above) with a specified interval size (1 in this example; if it's 2, it would be counting by 2's, etc.). The name "for loop" comes from the idea that we want to do something "for a certain number of times." Even though the repeat loop syntax is simpler, the for loop is more versatile, and therefore some programming languages don't implement both. It's therefore important to know about both, and RoboBlockly has both available (though, as seen, it labels the for loop as a "count" loop). So in this lesson we will see how to use a for loop to do the same thing as the previous lesson.
|
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
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
|
Using the pre-placed code blocks, insert a driveDistance and a turnLeft command into the for loop, where the distance to be moved is 20 units and the turn angle is 137 degrees. Make the number of iterations 12, with a speed of 8, so you can see how it works. After you get it running, experiment with different values for the number of iterations, the speed, the distance, and the angle. (The onscreen virtual robot can move very fast, up to about 200 inches/second. Hardware robots are limited to about 8 inches/second.) |
|
|
|||||||||||
|
|||||||||||