Sequencing and repetition

Starting from Scratch - Lesson 4

Objectives

  • To sequence a series of commands accurately and in the most efficient way.
  • To understand and explain different types of loops in code, the differences between them and when you would use them.
  • To add and use different types of loops in code to achieve a desired outcome.

Introduction

In this lesson we will be getting deeper into the PRIMM model, and giving students the opportunity to:

 

Predict - What will this code do? Is the sequencing correct? Which loop is best?

Run - Was the sequencing correct? Was it the correct loop?

Investigate - Students are given required code blocks, can they sequence them correctly to achieve specific objectives?

Modify - Students will begin to remix existing projects with their own ideas.

 

Begin by recapping the key vocabulary from lesson one:

  • Algorithm - A precise step-by-step set of instructions for achieving a goal or solving a problem.
  • Code - Instructions for a computer written in a programming language.
  • Debug - To find and correct the errors in a computer program.
  • Predict - Say or estimate what will happen in the future or as a consequence of something.
  • Program (verb) - To give a series of instructions to a machine so that it will perform a task automatically. This could involve pressing buttons or writing code.
  • Program (noun) - A sequence of instructions written to perform a specific task on a computer.
  • Loop - A programming feature where one or more instructions are repeated, perhaps a certain number of times, until a condition is met or until the program is stopped.
  • Sequence - To place programming instructions in a particular order, with each carried out one after the other.
  • Test - To try something out to see if it works.

 

Ask the students to talk to a partner and take it in turns to explain their understanding of each word. Gather feedback from the class at the end.

 

Sequencing

As a whole class focus on sequencing.

As we’ve been looking at and reading code in the last few lessons we have already been thinking about the order that things happen. We want students to learn that there is one right order for some sequences but not for all, and that the order of instructions in a sequence can be very important to maximise elegance and/or efficiency in a program. It is not enough to just have all the right instructions we often need them in the best order.

 

Ask the class to take a look at this sequence of instructions for making a cup of tea:

  1. Fill the kettle.
  2. Boil the kettle.
  3. Collect a mug.
  4. Place a teabag in the mug.
  5. When the kettle has boiled, add the boiling water to the mug until it is almost full.
  6. Add sugar to the mug (if wanted).
  7. Add milk to the mug (if wanted).
  8. Use a teaspoon to squeeze and then remove the teabag.
  9. Stir the tea.

 

Ask the class: Is this sequence correct? Is it the only way it could have been sequenced? Ask them to discuss with a partner which instructions could have been put in a different order and still produced a successful outcome.

 

Sequencing task

Still working with a partner, ask the students to write their own sequence of actions on small whiteboards, for making toast with butter and jam. 

Collect feedback from the class about the steps they included and the order they chose. Were everyone's the same? Compare two examples and consider with the class if both sequences work, if one could be considered better than the other and why. Ask the students to justify and explain their reasons.

Which steps have an essential place in the sequence? e.g:

  • You have to get the toaster before you can make the toast.
  • You have to toast the bread before you can butter it.
  • Butter is usually added before the jam (however, it would still be edible if added afterward).

 

Repetition

One of the main ways to achieve efficiency and elegance in code is by reducing the code to the smallest number of steps. A good way to do this is to use loops where parts of code are repeated. This can also save a lot of time when creating the code as we don't need to write the same code again and again.

There are three key types of loops in Scratch for the students to understand, all of which are very common in other programming languages as well:

 

A counted loop

 

A counted loop lets us repeat something a specific number of times and then move on to other actions. Ask the class, can you think of a real-life example of where this occurs?  e.g. 'Do ten press-ups, then do ten star jumps', 'Add four tablespoons of sugar to the mix', 'run four laps of the track'.

 

An infinite 'forever' loop

 

An infinite loop is for when you want something to repeat and never stop. Ask the class, can you think of a real-life example of where this occurs? e.g. Time, or more specifically seconds increasing on a digital clock, traffic light sequences, adverts changing on a digital billboard.

 

A conditional loop

 

A conditional loop repeats actions until a condition happens, or stops happening. In Scratch, the condition must be added to the hexagonal space in the block, in a similar way to 'if' or 'if else' blocks.

These could be blue sensing blocks that detect a sprite coming into contact with other things, like the edge of the screen or a particular colour:

 

 

Waiting for an input from the user, such as pressing a key or a mouse-click on the stage:

 

 

Or using green operator blocks to measure a variable. For example in a game, a character might be able to move until the time or their lives run out.

 

 

In all these cases though, it's important to remember that when the loop is terminated, you would still normally specify what happens next by adding blocks under the loop.

 

 

 

Ask the class, can you think of a real-life example of where conditional loops occur? e.g. 'Stir until the butter has melted', 'Go north until you get to the river'.

 

Let's look again at those instructions for making a cup of tea. There are certainly some opportunities to use loops to make the sequence more precise and efficient. Ask the class - Where could there we add a loop in this sequence? Which type of loop would be best? 

  1. Fill the kettle.
  2. Boil the kettle.
  3. Collect a mug.
  4. Place a teabag in the mug.
  5. When the kettle has boiled, add the boiling water to the mug until it is almost full.
  6. Add sugar to the mug (if wanted).
  7. Add milk to the mug (if wanted).
  8. Use a teaspoon to squeeze and then remove the teabag.
  9. Stir the tea.

There are a number of opportunities to improve this:

  1. Add water to the kettle until you have enough for the number of cups you are making - Conditional loop.
  2. Boil the kettle.
  3. Collect a mug.
  4. Place a teabag in the mug.
  5. When the kettle has boiled, add the boiling water to the mug until it is almost full - Conditional loop.
  6. Add a teaspoon of sugar to the mug (if wanted). Repeat until it is sweet enough for your taste - Conditional loop.
  7. Add milk to the mug (if wanted). Repeat until there is enough for your taste - Conditional loop.
  8. Use a teaspoon to squeeze and then remove the teabag.
  9. Stir the tea ten times - Counted loop

 

Main tasks

Ask the students to work in pairs and take a look at these four Scratch projects. For each one, there is a task in the comment box on the page. They all involve adding loops to the existing code to achieve a specific objective. They can easily access all four challenges from tinyurl.com/whichloop.

 

 

Ask them to:

  1. Read the task.
  2. Read the code and take it in turn to explain as much as they can to their partner.
  3. Run the code as it is and consider what happens.
  4. Complete the task in the comment box.
  5. Remix the code into their own version - How can you edit and improve what is already there?

 

If they have their own Scratch accounts, they can sign in so they can remix and save their edits and share them back with you.

 

Task 1

https://scratch.mit.edu/projects/844257041/editor/

Challenge: "Choose the best loop and add it to this code in the correct place so that the cat draws a HEXAGON. You only need to add ONE of the loops."

Solution: The counted loop is used and set to a value of 6. It is wrapped around the 'move 100 steps' and 'turn clockwise 60 degrees' blocks so there are 6 forward movements and 6 turns to create the hexagon.

 

 

Task 2

https://scratch.mit.edu/projects/844186759/editor/

Challenge: "You'll need TWO loops in this challenge, one for each chunk of code below. I want the music to play and the text to move and change colour whenever the spacebar is pressed. Which loops do you need and where do they go?"

 

Solution: Both loops added are 'forever' loops. This ensures that every time the spacebar is pressed the animation and the music play. You can also hold down the spacebar and it will continuously repeat both actions. Both 'forever' loops must wrap around the two 'if then' blocks as shown below.

 

 

Task 3

https://scratch.mit.edu/projects/844260097/editor/

Challenge: "Here you need to use all the blocks below and position the conditional loop correctly so that Champ99 dances until the timer reaches 0 and then stops."

Solution: There are two loops in this project, but one (the 'forever' loop) is already in the correct place. The students need to build the condition correctly so that the 'repeat until' block works as described in the challenge. The orange 'Timer' variable is already inside the green '=' operator block, and that all goes into the hexagonal space in the loop. We want to animate the sprite by repeatedly switching the costume, so we'll wrap the loop around 'next costume' and the 'wait 0.2 seconds' blocks, then finally add the 'stop all'.

 

 

 

Task 4

https://scratch.mit.edu/projects/844273030/editor/

Challenge: "We want the duck to swim around the pond for a while before it lays its egg. When it touches the grass, then it should get out of the water and lay its egg. Add the 'repeat until' block and the code attached to it in the correct place."

Solution: Again, the challenge here is not to pick the correct loop (there's only one), it's to consider how to get it to work by adding the loop and the condition it contains in the correct place in the existing starter code. The challenge says we want the duck to swim around the pond for a while first. The 'glide 1 sec to a random position' block will create that movement. The conditional loop means it will repeat that until it gets to the grass, which, as it's green, will be recognised by the 'touching colour green' block. To work out where to put the loop, students should read the code and then run it before the loop is added. They should work out that the last 'lay egg' block creates the animated sequence of the duck laying its egg. This is an example of something called a procedure, where a sequence of commands is wrapped together and defined with a single name. The defined name can then be used in your code at any point to run the procedure. You can see the separate stack of code in this example that defines what happens for 'Lay egg'. Procedures like these can be created in the My Blocks category.

 

 

 

Plenary

Discuss the four challenges as a class and ask students to explain and model the solutions to each one.

Ask students to share the modifications they made to the code when they remixed the projects.

Finally, ask them to explain to their partners:

  • What is sequencing? Why does it matter?
  • What types of loops are available?
  • What does each one do?

 

< Previous lesson

Next Lesson >