Link Search Menu Expand Document

Repetition

Repeating lines of code is one of the fastest ways to get something done. Quite often in computer science, it makes more sense to repeat lines of code rather than write out another set of instructions. For example, the square you created earlier uses the same two instructions four times. Rather than writing them out four times, you could write them out once but add an instruction to repeat them.

In Java there are two types of loops that you are likely to use: a while loop and a for loop. If you want a section of code to repeat forever, or until a condition is set, then a while loop might be best. If you want to loop for a set number of times, then a for loop is preferable.

Here, we have used a while true loop. This means that the code inside the loop (i.e. the code which is indented) will repeat forever. If you try to run it, Greenfoot will actually get stuck in a execution loop and freeze. DO NOT TRY TO RUN IT!

// while true loop, this doesn’t work in Greenfoot because the act function is in its own event loop
public void act()
{
	// Add your action code here.
	int r = 255;
	int g = 0;
	int b = 124;
	setColor(r, g, b);

	while (true)
    {
        moveAndDraw(50);
        turn(90);
        moveAndDraw(50);
    }
}

This type of loop is not going to be very useful for drawing shapes with Turtle where you want to be more precise.

In this example, a for loop has been used. Press Run to see what happens.

public void act()
{
	// Add your action code here.
	int r = 255;
	int g = 0;
	int b = 124;
	setColor(r, g, b);

	for (int i = 0; i < 8; i++)
    {
        // Print out i to the terminal window.
        System.out.println(i);
        moveAndDraw(50);
        turn(45);
    }
}

A for loop repeats instructions a set number of times, in this case 8 times. A for loop has an associated variable (called i here). In this example, i starts from 0 and increases by 1 each time. Let’s apply this to the code to draw a square:

for (int j = 0; j < 4; j++)
{
    moveAndDraw(50);
    turn(90);
}

Copy and paste this code into the Greenfoot editor above and run it. The turtle has been asked to repeat two instructions four times to make a square.

Once you have created one shape using a loop, you can repeat the shape again and again by putting it inside another loop. This is a great way to draw spirals. Adapt your code by making it look like this:

for (int i = 0; i < 30; i++)
{
	for (int j = 0; j < 4; j++)
	{
		moveAndDraw(50);
		turn(90);
	}
    turn(25);
}

A spiral can be made by turning a small degree and then moving forward a small amount. The section of code for making a square is inside another for loop that repeats it 30 times, each time turning the cursor 25 degress to make a pleasing spiral shape.

Challenge

Try to complete each of the challenges below.

  • Can you alter the for loop so that it draws a more interesting spiral using one of the shapes you made earlier, like a triangle or circle?
  • Adding a few extra lines where you alter the variables r, g, and b would allow you to make a multicoloured spiral. Have a go at creating a rainbow spiral.
Hint 1

Just like in the previous exercise, you can add to or subtract from the r, g, and b variables.

Hint 2

Just alter the variables within the for loop:

for (int i = 0; i < 30; i++)
{
	setColor(r, g, b);
	for (int j = 0; j < 4; j++)
	{
		moveAndDraw(50);
		turn(90);
    	// ADD SOMETHING HERE
	}
    turn(25);
}
Hint 3

Try this to get started:

for (int i = 0; i < 30; i++)
{
	setColor(r, g, b);
	for (int j = 0; j < 4; j++)
	{
		moveAndDraw(50);
		turn(90);
		r -= 1;
		g += 1;
	}
    turn(25);
}