Y3: 18. Scratch Loops

This lesson is based on teachcomputing.org lessons.

Today, you are going to….

Programmers can use infinite loops, and count-controlled loops.

‘something that is infinite has no limit or end’

Sunrise & Sunset

Breathing

Time

Can you think of any more examples of things that are infinite?

Programmers can use infinite loops, where the commands within the loop are repeated over and over again, without an end point.

In Scratch, this is called the repeat forever block.

For example, in the code below, the sprite will keep changing to the next costume continually. This means it will keep changing the way it looks.

Look at the three dancing dinosaurs in this Scratch project.

What actions are they doing?

Click See inside to show the code for the three dancing dinosaurs.

Look at the blue dinosaur first. Click on the code for the blue dinosaur and carefully examine what actions it is doing, and how many times it does them. To stop the code, click on the red octagon next to the green flag.

Now, do the same for the green and the red dinosaurs.

How many times do they repeat the actions?

Have you noticed the repeat forever block inside the red dinosaur’s code?

What does it mean?

The actions will be repeated over and over again…

Complete the ‘Dancing dinosaurs’ worksheet.

Use the ‘Dancing dinosaurs’ worksheet. Look carefully at what each dinosaur is doing in turn (on the sheet), and then fill in the sheet. Carefully look at the number of times these actions are repeated.

Have you noticed that the blue and red dinosaurs repeat forever, but the green dinosaur stops after a few moves?

What kind of repetition loop does the red dinosaur sprite use?

How many times will the blocks inside be repeated?

Why does the green dinosaur stop dancing?

What kind of repetition loop does the green dinosaur sprite use?

How many times will the blocks inside the loop be repeated?

The green dinosaur uses a count-controlled loop of repeat 2, rather than an infinite loop.

In an infinite loop, any blocks within the loop will be repeated over and over again without an end point (forever).

The count-controlled loop repeats the blocks for a set number of times.

(Programmers don’t use infinite loops very often because they can get stuck, and the only way to stop them is to close the program and start again.)

Let’s look at the dancing dinosaurs again.

Can we get all the dinosaurs to repeat their moves forever?

We need to modify the code for the green dinosaur. We need to swap the count-controlled loop for an infinite loop!

Here is a bit of guidance…

Watch the video (or look at a live demonstrate) how to move the blocks out of the count-controlled loop and into a forever loop.

Loop Changing. (0:30 minutes long)

Reference Costumes. (0:49 minutes long)

Modifying code — different kinds of loops:

Use the ‘Modifying code’ worksheet to write down the algorithms, and then modify the Scratch code. (You need to circle the code that needs to be changed to match the tasks.)

Then modify the Scratch code for each sprite (using the “same “Dancing Dinosaurs” Scratch project used previously) on a computer to check that the changes are correct.

— Question 1 —

You want to make your dinosaur sprite a bit bigger and pause for a few times before it starts dancing. Which loop would you use?

Infinite Loop

Count-controlled Loop

ANSWER

A count-controlled loop would be more suitable, as an infinite loop would mean the character would keep getting bigger until it is too big to see properly.

— Question 2 —

You want to make a bird sprite fly backwards and forwards across the top of the screen. Which loop would you use?

Infinite Loop

Count-controlled Loop

ANSWER

An infinite loop would be more suitable, as it would keep the bird flying. A count-controlled loop would mean that the bird would fly a short way and then stop.

— Question 3 —

What is the problem with this code?

ANSWER

The move 100 block is outside the forever loop, so it will never be run.

— Question 4 —

What will happen when the green flag is pressed?

ANSWER

The sprite will turn, wait, and move, and keep repeating the sequence, as all the blocks are within the forever loop.

— Question 5 —

If there are two processes on the code area, what will happen when the green flag is pressed?

ANSWER

Both processes would be started, so the sprite would move and turn at the same time. (In Scratch, the green flag can be used to start lots of different things happening at the same time.)

ANSWERS

Use this worksheet to create the codes in Scratch.

Access SCRATCH here.

(WordPress advertisements may appear below.)