3 minute read

Developing programs with repetition

Now we will look at how repetition works in Scratch programming.

Programming task 1

Work with a partner. Look at these two different programs and answer the questions.

1 What extra code is there in the second program?

2 What does the extra code do in the second program?

3 What will each program do when the green flag is clicked?

Programming task 2

You will need: a desktop computer, laptop or tablet, access to Scratch

Create the second program with the forever block from Programming task 1. Any code inside a forever block is repeated. You will find the forever block in the Control category.

Run the program to see if your predictions from Programming task 1 were correct.

When you click on the green flag, the sprite will move and change colour forever because the code is inside a forever loop. The sprite will only stop when you click on the stop button.

Programming task 3

You will need: a desktop computer, laptop or tablet, access to Scratch

Create a new program that will change the sprite’s colour, and then copy the sprite into random locations on the screen. The program should run forever. Use only the following blocks of code:

How am I doing?

Don’t forget to run your program to check that it is working correctly. Compare your program to a partner’s. Did you both create it in exactly the same way, or was your partner’s code slightly different to yours?

Sometimes, the order of the code in a program is not important. Look at the programs below. The order of the code is different in each program, but all of the programs produce the same output.

There are many other ways of ordering the blocks that produce the same outcome when the code repeats. Can you think of any other ways to order the blocks to produce the same outcome?

Did you know?

Some computers use moving screen savers. A screen saver fills the screen with a forever-changing pattern if the computer has not been used for a long time. Screen savers are used to stop people from reading what is on the screen. Many of these screen savers use a very similar algorithm and program to the one you have just created.

Programming task 4

Work with a partner to change the program you have just created so that it runs faster or slower. Run it to check whether you have done it correctly.

What happens if you change the code so that the ‘change color effect’ block is under the ‘when flag clicked’ and before the ‘forever’ loop? An example is shown below to help you. Discuss your ideas with your partner.

0.5 seconds is the same as half a second.

Start Change colour

The algorithm diagram would look like this: Go to random position

Create clone of myself Wait 0.5 seconds REPEAT FOREVER

The written algorithm would look like this:

1 Change colour.

2 REPEAT FOREVER.

3 Go to random position.

4 Create clone of myself.

5 Wait 0.5 seconds.

Adding code before a loop

Sometimes, you may want some blocks of code to run before the loop starts.

In the algorithm in Programming task 4, the ‘change color effect’ instruction is above the loop. This means the ‘change color effect’ instruction will only run once before the loop starts and will never run again.

Programming task 5

You will need: a desktop computer, laptop or tablet, access to Scratch

Work in pairs to create a program in Scratch with an instruction that sits outside of a loop.

The sprite in your program must say ‘Hello!’ once for two seconds, then keep spinning around in a circle by moving 10 steps and then turning 15 degrees until the program is stopped.

Start by writing or drawing an algorithm. Once you have developed your algorithm, create the program in Scratch and test it to make sure it works correctly.

Did it help you to plan your program first using an algorithm? Why?

Look what I can do!

I know how to create repeating algorithms.

I can predict the outcome of repeating algorithms.

I can use the forever block in Scratch to create indefinite loops.

1.3 Count-controlled loops

1.3 Count-controlled loops

We are going to:

• follow and understand algorithms that use count-controlled loops

• edit and correct algorithms that use count-controlled loops

• compare different algorithms that have the same outcome to decide which is best

• create programs that use count-controlled loops.

Getting started

What do you already know?

• Algorithms are sets of instructions.

• Algorithms can contain instructions that are repeated.

• In Scratch, a forever block can be added to repeat code until the program is stopped.