Iteration
Iteration is where the same set of sequence events is played over and over for the required number of times.
Options available for this are
- Repeat forever
- Repart a given number of steps

Other options that can be considered later are
- Do Forever If <condition>
- Repeat Until <condition>
- Wait Until <condition>



Repeat (Forever)
This is used to enable a set of statements to be run continuously or to be available at any time. The block of code is active over the entire time the program is running.
In this program, a sprite is moved and rotated a random value for an indefinite period. As well, the colour scheme is changed.

Exerecises
1. Write a program that randomly moves a sprite of a fly to move around a simple scene.
2. Write a program that creates a random musical tune.
Repeat (a set number of times)
This is used to repeat a set of statements a given number of times. In this case, the system is instructed to draw a line in a particular direction and of a given length six times. This draws a hexagon.

This is the output of the program.

Exercises
1. Write programs that draw the following: Square, Octagon, Pentagon
2. Write a program that plays a tune as a round. Hint: have several versions of the tune with appropriate blank bars of music. They will all be started when the green flag is pressed.
Nested repeat blocks
Repeat blocks can be put inside other repeat block. In the example below, the hexagon is repeated the appropriate number of times to create a circle of hexagons.

From the code, you can see that the outside block just rotates the system 10 times over 36 degrees to make a complete circle.

Exercises
1. Extend your programs from the exercise above to rotate the images through 360 degrees.
2. Other interesting patterns can be created by thinking outside the circle. Try creating a hexagon at each vertex of a pentagon. Other variations should also be investigated.
3. Some interesting music can be created from using nested loops ie more than one loop within another or multiple loops in other loops. See what you can create!!
Comments (0)
You don't have permission to comment on this page.