Cambridge Primary Computing Learner's Book 4 with Digital Access (1 Year)

Page 1

DRAFT

Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
DRAFT

Primary Computing

Learner’s Book 4
Cattanach-Chell & Nichola Lacey
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Ceredig
CAMBRIDGE

University Printing House, Cambridge CB2 8BS, United Kingdom

One Liberty Plaza, 20th Floor, New York, NY 10006, USA

477 Williamstown Road, Port Melbourne, VIC 3207, Australia

314–321, 3rd Floor, Plot 3, Splendor Forum, Jasola District Centre, New Delhi – 110025, India

103 Penang Road, #05–06/07, Visioncrest Commercial, Singapore 238467

Cambridge University Press is part of the University of Cambridge.

It furthers the University’s mission by disseminating knowledge in the pursuit of education, learning and research at the highest international levels of excellence.

www.cambridge.org

Information on this title: www.cambridge.org/9781009309257

© Cambridge University Press 2023

This publication is in copyright. Subject to statutory exception and to the provisions of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press.

First published 2023

20 19 18 17 16 15 14 13 12 11 10 9 8

Printed in Malaysia by Vivar Printing

A catalogue record for this publication is available from the British Library

ISBN 978-1-009-30925-7 Paperback with Digital Access (1 Year)

ISBN 978-1-009-32047-4 Digital Learner’s Book (1 Year)

ISBN 978-1-009-32048-1 eBook

Additional resources for this publication at www.cambridge.org/go

Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate. Information regarding prices, travel timetables, and other factual information given in this work is correct at the time of first printing but Cambridge University Press does not guarantee the accuracy of such information thereafter.

Third-party websites and resources referred to in this publication have not been endorsed by Cambridge Assessment International Education.

NOTICE TO TEACHERS

It is illegal to reproduce any part of this work in material form (including photocopying and electronic storage) except under the following circumstances:

(i) where you are abiding by a licence granted to your school or institution by the Copyright Licensing Agency;

(ii) where no such licence exists, or where you wish to exceed the terms of a licence, and you have gained the written permission of Cambridge University Press;

(iii) where you are allowed to reproduce without permission under the provisions of Chapter 3 of the Copyright, Designs and Patents Act 1988, which covers, for example, the reproduction of short passages within certain types of educational anthology and reproduction for the purposes of setting examination questions.

7 6 5 4 3 2 1
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Introduction

Welcome to Stage 4 of Cambridge Primary Computing!

Computers and technology play an important role in our lives and are all around us. Learning about technology will help you to shape the world you live in!

In this book you will:

• discover computational thinking and create programs in Scratch that use repetition

• learn how to collect and store data so it can be useful

• look at computer networks and the technology used to transfer messages across the world

• learn about the jobs that computer scientists do and how robots help us.

We learn best when we are active and working together. It is also good to learn outside of school. You can do this by looking at technology that interests you and learning about it. You may want to join a local computer club. You could also help other people around you learn about computers and technology.

This book is full of activities and questions where you can work with a partner or a group. Sharing your ideas with other learners is fun and helps you have exciting conversations about how computers and technology are used.

There is also a project for you to complete at the end of each unit. These will cover the learning in each unit and help you to develop your understanding.

We hope you find learning about computers and technology exciting – and that you are inspired to keep learning about computers as you grow older.

4
Introduction
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
DRAFT

Note for teachers: Throughout the resource there is a symbol to indicate where additional digital only content is required. This content can be accessed through the Digital Learner’s Book on Cambridge GO. It can be launched either from the Media tab or directly from the page. The symbol that denotes additional digital content is: The source files can also be downloaded from the Source files tab on Cambridge GO. In addition, this tab contains a teacher guidance document which supports the delivery of digital activities and programing tasks in this Learner’s

5 8.1 How to compare the size of angles How to use this book 6 1 Computational thinking and programming 1.1 Repeating algorithms 9 1.2 Indefinite loops 21 1.3 Count-controlled loops 37 1.4 Input and output 58 1.5 Decomposition and sub-routines 64 1.6 Creating good programming solutions 74 1.7 Using input and output with a physical device 85 2 Managing data 2.1 Collecting data 97 2.2 Storing data 118 2.3 Using data 124 3 Networks and digital communication 3.1 Network structures 135 3.2 Efficient networks 145 3.3 Transferring data securely 159 4 Computer systems 4.1 Using computer systems 169 4.2 Types of data 178 4.3 Working with computer systems 188 Glossary 197 Acknowledgements 205 Contents
Book. Contents DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

How to use this book

In this book you will find lots of different features to help your learning.

What you will learn in the topic. Important words to learn.

A reminder about what you already know and an activity to start you off.

Fun activities about computing. Sometimes, you will use a computer.

Some activities don't need a computer. These are called unplugged activities. They help you to understand important ideas about computing. Sometimes, you will see this question. It will help you to think about your work.

6
How to use this book DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Tasks to help you to practise what you have learnt.

Programming tasks are in Unit 1.

Practical tasks are in Unit 2.

Look out for this icon. You are going to do an activity at the computer. Your teacher will help you to get started.

Questions that help you to practise what you have just learnt. Are you ready to move on?

Things to remember when you are doing a task.

Interesting facts connected to the topic.

7 8.1 How to compare the size of angles How to use this book 7
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Questions to help you think about how you learn.

What

you

have

learnt in the topic.

At the end of each unit, there is a project for you to carry out, using what you have learnt. You might make something or solve a problem.

Questions that cover what you have learnt in the unit. If you can answer these, you are ready to move on to the next unit.

8 4 Living Things
How to use this book DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

1 Computational thinking and

DRAFT

9
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Now try this!

You will need: paper and a pen

Work in pairs.

Partner A: Draw a simple picture (for example, a house, a fish, a flower…). Don’t show it to your partner! Now give instructions to your partner to draw the same picture, but don’t tell them what it is.

Partner B: Follow your partner’s instructions to draw the picture. Can you guess what it is? Are the two pictures the same?

Swap roles and try the activity again.

Draw an oval. Inside the oval, draw a smaller circle at the end.

Talk about the activity with your partner. Think about these questions.

1 What was the easiest part for you?

2 What was the most difficult part?

3 Which instructions were the best?

4 Did you have to change anything? If so, what?

10 1 Computational thinking and programming
Continued
? DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Follow and understand repeating algorithms

An algorithm is a set of instructions. When we follow the instructions carefully, they help us complete a task successfully. Algorithms can be written instructions, spoken instructions, or even diagrams.

Programmers use algorithms to plan their programs and identify possible problems. We use algorithms in our everyday life, for example when we use:

• instructions to play a game

Give each player five cards.

1.1 Repeating algorithms
11
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
DRAFT

Linear algorithms

Linear algorithms contain instructions that we must follow in the correct order, one after the other. When you get to the end of the instructions, the algorithm stops.

This is an example of a linear algorithm that we can use to shop for food.

1 Walk to the shop.

2 Choose items.

3 Pay for shopping.

4 Walk home.

This linear algorithm can also be shown as a diagram:

These algorithms look very different, but they both give the same instructions and solve the same problem.

Algorithms can be presented as written instructions or as diagrams, depending on what is most helpful for the user. For example, a diagram is often easier to follow when building a model using toy building bricks. But to cook a meal, the written instructions in a recipe are more helpful.

12 1 Computational thinking and programming
Start Walk to the shop Choose items Pay for shopping Walk home Stop
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

1.1 Repeating algorithms

Questions

1 What is a linear algorithm?

2 Why is it important to check each instruction in an algorithm is in the correct order?

3 Why can it be better to present an algorithm as a diagram sometimes?

Loops

Sometimes algorithms contain instructions that need to be repeated. This is known as a loop.

Look at the diagram below. It shows an algorithm that uses a loop. Use your finger to trace the direction that you take through the algorithm as you follow the instructions.

What is this algorithm for?

13
Sun sets Start Sun rises REPEAT FOREVER
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Questions

Think about how the sun rises and the sun sets. Discuss the answers to the following questions with a partner.

4 Does this only happen once and then stop?

5 What is being repeated?

6 When would the loop stop?

The instructions in the sun rises, sun sets algorithm repeat and never stop. These types of instructions are known as a forever loop. In a forever loop, the cycle keeps repeating forever and does not stop.

This algorithm in written instructions looks like this:

1 Sun rises.

2 Sun sets.

3 Sun rises.

4 Sun sets.

5 …

It’s impossible to use written instructions for a forever loop –they go on forever!

A much faster and easier algorithm would be:

1 REPEAT FOREVER.

2 Sun rises.

3 Sun sets.

The words REPEAT FOREVER show that the instructions indented below the REPEAT FOREVER instruction continue to repeat.

The algorithm will repeat forever. Another name for a forever loop is an indefinite loop.

14 1 Computational thinking and programming
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
In programming it is not possible to have a program that will keep repeating forever, but programmers use the word ‘forever’ to mean that once the loop starts it will not stop until the program stops.

Unplugged activity 1

Work in pairs to follow this algorithm that uses an indefinite loop. Repeat the actions in this indefinite loop until your teacher tells you to stop.

1.1 Repeating algorithms

This algorithm could go on forever and make the class very, very tired! But we only need to repeat it until the teacher says ‘Stop’.

1 REPEAT FOREVER.

2 Take 1 step to the right.

3 Clap your hands.

4 Take one step to the left.

5 High-5 with a partner.

6 Say ‘indefinite loop’.

DRAFT

15
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

We only write the instructions inside an indefinite loop once. This makes the algorithm more concise. This means the algorithm is shorter and uses the fewest number of instructions possible. We do not have to write the same instructions over and over again. Also, when we need to change one of the instructions, we only need to do it once!

Edit and correct repeating algorithms

In algorithms, the instructions need to be as clear as possible to the person who has to follow those instructions.

When an algorithm is wrong, a programmer corrects the algorithm to make it work.

Sometimes, an algorithm isn’t wrong but a programmer thinks of a better way of doing something in the algorithm. When this happens, the programmer edits the algorithm to change it and make it better.

Imagine a person is learning a new dance. They want the instructions to be easy to follow. They also want the instructions to be as concise as possible, so they can learn the dance quickly.

Look at the instructions for a simple dance routine on the next page.

1 Computational thinking and programming
16 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

1 Step forward, starting with left leg for 2 steps.

2 Step backwards, starting with left leg for 2 steps.

3 Clap to the left.

4 Clap to the right.

5 Put your hands on your hips.

6 Jump forwards.

7 Jump backwards.

8 Jump a quarter turn right.

9 Step forward, starting with left leg for 2 steps.

10 Step backwards, starting with left leg for 2 steps.

11 Clap to the left.

12 Clap to the right.

13 Put your hands on your hips.

14 Jump forwards.

15 Jump backwards.

16 Jump a quarter turn right.

17 Step forward, starting with left leg for 2 steps.

18 Step backwards, starting with left leg for 2 steps.

19 Clap to the left.

20 Clap to the right.

21 Put your hands on your hips.

22 Jump forwards.

23 Jump backwards.

24 Jump a quarter turn right.

25 …

This is difficult to read. It is easy to get lost in the long list of instructions.

An algorithm with a repeating loop is much more concise and easier to follow.

1.1 Repeating algorithms

17
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

1 Computational thinking and programming

1 REPEAT FOREVER.

2 Step forward, starting with left leg for 2 steps.

3 Step backwards, starting with left leg for 2 steps.

4 Clap to the left.

5 Clap to the right.

6 Put your hands on your hips.

7 Jump forwards.

8 Jump backwards.

9 Jump a quarter turn right.

Unplugged activity 2

You will need: paper and a pen

This is a diagram for a different dance algorithm.

• Identify what is wrong with the algorithm.

• Draw a more concise diagram of this dance algorithm.

18
Clap your hands Start Clap your hands Step to the right Step to the left Step to the left Step to the right Jump up and down Jump up and down REPEAT FOREVER DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

1.1 Repeating algorithms

Did you know?

A roller coaster is a good example of an indefinite loop. The rollercoaster will go on repeating the loop until it stops, when the operator switches off the ride and the theme park closes.

The roller coaster follows the same algorithm each time the ride runs to make sure passengers can safely get on and off the ride.

19
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Remember, an indefinite loop is another name for a forever loop and will go on repeating until the algorithm stops.
DRAFT

Unplugged activity 3

You will need: paper and a pen

The following algorithm for a roller coaster is written in the wrong order:

1 REPEAT FOREVER.

2 New passengers take their seat.

3 The safety harness is released.

4 The ride stops.

5 The safety harness is locked in place.

6 Passengers leave their seats.

7 The ride starts.

Work in pairs and talk about the correct order for the instructions. Then work together to:

• write an algorithm for the roller coaster using written instructions

• draw a diagram that shows your algorithm.

How am I doing?

Did you remember to add the words REPEAT FOREVER to the top to show the algorithm is an indefinite loop?

How did you decide on the order for the instructions?

Which form of algorithm (written instructions or a diagram) did you find easier to understand and create? Why?

Look what I can do!

I can follow and understand algorithms that use repeating loops. I can edit and correct algorithms that use repeating loops.

I know that repetition can make an algorithm more concise.

20 1 Computational thinking and programming
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

1.2 Indefinite loops

1.2 Indefinite loops

We are going to:

• understand how to create repeating algorithms

• predict the outcome of repeating algorithms

• use the forever block in Scratch to create indefinite loops.

Getting started

What do you already know?

• Algorithms can contain instructions that are repeated.

developing algorithms forever block predict random

• Algorithms can be presented as written instructions or as a diagram.

• An indefinite (forever) loop keeps running until the program is stopped.

• You can run programs in Scratch.

• You can create simple programs in Scratch by dragging blocks of code into the script area.

Now try this!

You will need: paper and a pen

Draw a diagram for the following written algorithm that uses an indefinite loop.

1 REPEAT FOREVER.

2 Change the colour of the sprite.

3 Move 10 steps.

4 If on edge, bounce.

21 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Creating repeating algorithms

Developing algorithms

Developing algorithms is the process of planning algorithms before creating them. This helps you to make sure all the instructions in the algorithm are presented in the correct order.

When you develop an algorithm, you must decide on a starting point. Sometimes the starting point is obvious. At other times you can choose any point to start from as they all have the same importance.

1 Computational thinking and programming
22 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Let’s look at an indefinite (forever) loop in nature.

A female butterfly lays her eggs on a plant. After a few days, a caterpillar comes out of each egg. The caterpillar starts eating the plant and grows bigger. Then it makes a chrysalis and goes to sleep inside. While the caterpillar sleeps, the chrysalis changes into a pupa. After a few weeks, the pupa changes again and becomes a butterfly. The butterfly comes out of the pupa and flies away. The adult butterfly then lays its eggs on a plant and the cycle starts again.

23
1.2 Indefinite loops
Caterpillar makes a chrysalis Caterpillar eats leaves Chrysalis changes into a pupa Butterfly comes out of the pupa
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Butterfly lays eggsCaterpillar hatches from egg

Developing repeating algorithms

When you develop a repeating algorithm, follow this checklist:

• First decide on the starting point. Sometimes this is obvious, sometimes you can choose any point in the cycle.

• Write a clear, concise instruction for the starting point. For example, the butterfly lays eggs

• Think about the next step. For example, a caterpillar comes out of the egg. You don’t want to miss out any instructions, otherwise your algorithm won’t work!

• Write the instruction for each step in the most logical order until the instructions are back at the starting point.

• Write REPEAT FOREVER at the top of the algorithm to show that all the steps in that indefinite loop keep repeating.

Unplugged activity 1

You will need: paper and a pen

Work in pairs. Write a complete indefinite loop algorithm for the life cycle of a butterfly.

24 1 Computational thinking and programming
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
There are six steps before the cycle repeats.

In Unplugged activity 1, you used the steps of the butterfly life cycle to create an algorithm with an indefinite loop. Usually when we write an algorithm, we do not know the instructions and have to decide on them ourselves.

This time you are going to develop an algorithm for a different situation. You must choose the instructions to include.

Unplugged

activity 2

You will need: paper and a pen

Imagine a bus journey that starts and finishes at the same place. The bus driver makes the same journey many times each day. There are lots of stops along the way where the driver picks up passengers and drops them off.

Create a repeating algorithm for a bus journey that starts and finishes at the same place. Choose five destinations for the bus to stop along the journey.

Tips to help you:

• You can develop your algorithm using either a written algorithm or a diagram.

• You can pick any destinations you would like the bus to visit on the journey.

• Try to include at least five different locations on the bus journey.

25 1.2 Indefinite loops
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Remember, the bus must return to the start before the journey begins again.

How are we doing?

Work in pairs. Check your partner’s algorithm. Record your answers in a table like this:

Checkpoint

Is each destination clear?

Does the journey start and finish at the same location?

Does the algorithm clearly show an indefinite loop?

Predicting the outcome of repeating algorithms

It is important to be able to predict what algorithms will do. Predicting means to say what you think will happen.

This is helpful when you create your own algorithms. It means that you can identify possible errors before you create the program.

1 Computational thinking and programming
Continued 26 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Questions

Practise your predicting skills and then test each situation to see if you are correct.

1 How many steps will it take your teacher to cross the classroom?

2 How many people are left-handed in your class?

3 If your teacher drops a small piece of paper, where will it land?

Predicting an output

Some things are easier to predict than others.

It can be difficult to predict what will happen when there are random elements, such as dropping a piece of paper. But you will know some things about what will happen – for example, the paper will fall down and not up.

Predicting an output in an algorithm means saying what you think will happen after each instruction in an algorithm is carried out.

Some algorithms will always have the same output each time they are run. In other algorithms that include more random elements, the output can be

1.2 Indefinite loops
27 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Unplugged activity 3

You will need: paper and a pen

Look at the algorithm below. You can choose any number to start with, but the final number will always be the same. Predict what the final number is each time.

Use a pen and paper and run the algorithm a few times to check if your prediction is correct.

1 REPEAT FOREVER.

2 Think of a number between 1 and 10 and write it on your paper.

3 Add 10 to that number.

4 Subtract 3.

5 Subtract the first number you thought of.

6 Multiply by 2.

7 Say the number out loud.

Cool trick! I can use this algorithm to make my friends think I can read their minds!

Now let’s look at a different algorithm. You need to work out the answer when the algorithm is run over and over again. This time it will not always be the same answer.

28 1 Computational thinking and programming
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Unplugged activity 4

You will need: paper and a pen

Follow this algorithm. This is an indefinite loop and could keep running forever, but we only need to run it a few times to see the pattern.

1 Think of a number between 1 and 10 and write it down.

2 REPEAT FOREVER.

3 Multiply the last number you wrote by 2 and write it down.

We can repeat this pattern forever because it is an indefinite loop. The answers will continue to get bigger and bigger.

When trying to predict the outcome of an algorithm, it can help to run it a few times and record your answers, as we have just done. It is not possible and not necessary to keep repeating an indefinite loop forever. After repeating it a few times, you will be able to understand the pattern and should be able to decide if it is working correctly.

1.2 Indefinite loops
29 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

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?

30 1 Computational thinking and programming
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

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.

1.2 Indefinite
loops
31
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Remember, a forever loop will keep repeating until the program is stopped.
DRAFT

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?

32 1 Computational thinking and programming
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
DRAFT

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?

33 1.2 Indefinite loops
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

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.

34 1 Computational thinking and programming
You will need: a desktop computer, laptop or tablet, access to Scratch
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

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.

35 1.2 Indefinite loops
Continued DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

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.

36 1 Computational thinking and programming
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

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.

DRAFT

count-controlled loop debug efficient iteration nested loop repeat block

37
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Now try this!

Work in pairs. Write an algorithm to direct the robot in square B5 to the star in square E5, following the white road. Use commands like these to write your instructions: Forward 3 (to move forward 3 squares), Turn right. When moving, you should not count the square the robot is currently on. For example, the command ‘Forward 3’ will move the robot to square B2.

38 1 Computational thinking and programming
Continued
A 1 2 3 4 5 6
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
BCDEF

1.3 Count-controlled loops

Count-controlled loops

Forever loops repeat instructions forever. But it is more common to use a loop that repeats an exact number of times. This is called a count-controlled loop

Once a count control has finished its number of repetitions, it can leave the loop and continue with the rest of the instructions. If there are no more instructions after the count-controlled loop, the algorithm will end. Look at the algorithm below for crossing the road. We repeat the

Start
Cross the road Stop REPEAT 3 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Look left Look right

Iteration is another name for a loop. A count-controlled loop is one example of an iteration.

Iteration makes algorithms and programs more efficient . An efficient program will run faster and use less power and computer memory. The best way to create an efficient algorithm is to get rid of lines of code that repeat and use iteration instead.

Iteration repeats the instruction or code, so you don’t have to write it lots of times.

Think about the robot puzzle in the Getting started activity.

We could write the instructions as:

But it is easier to use a count-controlled loop, like this:

1 Computational thinking and programming
1 Forward 3. 2 Turn right. 3 Forward 3. 4 Turn right. 5 Forward 3. 1 REPEAT 3. 2 Forward 3.
A 1 2 3 4 5 6 BCDEF 40 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
3 Turn right.

The number next to the word REPEAT tells the algorithm how many times to repeat the instructions.

In the second algorithm the robot turns right three times. The last turn is unnecessary. So, which algorithm is more efficient?

The extra turn in the second algorithm doesn’t change the outcome. The algorithm still moves the robot to the star. An algorithm should be short and concise. The first algorithm has five instructions. The second algorithm has only three instructions, so it is still more efficient.

This loop could also be shown like this:

Why does this diagram have a stop instruction at the end but some of the other diagrams we’ve looked at don’t?

An indefinite loop keeps repeating forever so will not stop. The count-controlled loop repeats a number of times and then the loop stops.

41 1.3 Count-controlled
Start Forward 3 REPEAT 3 Turn right Stop
loops
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Follow this iteration algorithm. Use your finger to trace your way through the maze.

Using iteration makes programming much faster because programmers don’t need to type the same line of code lots of times.

Remember, debug means to find and correct errors in code.

Programmers can also debug their code more easily. This is because programmers only have to correct an error once, instead of lots of times.

Questions

Work in pairs to answer these questions.

1 What is the difference between an indefinite loop and a count-controlled loop?

2 Why do programmers use loops?

42 1 Computational thinking and programming
Unplugged activity 1
A 1 2 3 4 5 BCDE
1 REPEAT 4. 2 Forward 1. 3 Turn left. 4 Forward 1.
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
5 Turn right.

1.3 Count-controlled loops

Correcting count-controlled loops

When trying to debug algorithms and programs, you should follow the iteration to find out where the program went wrong. Once you find the error you can correct it.

Unplugged activity 2

You will need: paper and a pen

Look at this simple maze. The robot should follow the path in a square until it gets back to where it started. Follow the algorithm that is written below the maze. Debug the algorithm.

A 1 2 3 BC 1
4.
2.
43 DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
REPEAT
2 Forward
3 Turn left.

Nested loops

Sometimes a loop can contain smaller loops inside it. When a loop is inside another loop it is known as a nested loop

Robots are now used in some restaurants to take orders and serve food to the customers. The programming for robot waiters is very complicated, but a simple algorithm for them is shown below. This algorithm shows how a robot waiter takes orders for food and brings the food to the customer’s table.

1 REPEAT FOREVER.

2 Go to table.

3 Welcome customers.

4 Count how many people are at the table (save this number as ‘N’).

5 REPEAT ‘N’ times.

6 Ask 1 person for their order.

7 Send this order to the kitchen.

8 Move onto the next person.

nested loop

9 When food is ready and put on tray, pick up tray.

10 Bring food to table.

Did you notice that the code is indented? The position of the text is very important when you are using nested loops as it shows where the loops start and finish.

44 1 Computational thinking and programming
Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
DRAFT

1.3 Count-controlled loops

The robot waiter repeats the process to take the order from each person using a count-controlled loop. The count-controlled loop is inside a bigger indefinite loop that repeats until the waiter is switched off. The count-controlled loop is a nested loop.

Did you know?

Digital clocks and timers use nested count-controlled loops and indefinite loops. The seconds are repeated 60 times, the minutes are repeated 60 times and the hours in the day are repeated 24 times. These are all count-controlled loops. The whole sequence is repeated in an indefinite loop.

Unplugged activity 3

Learn a new dance together as a class.

1 Read the instructions in the algorithm.

2 Follow the instructions to do the dance together.

3 Say the underlined words when you do each action. (This will help you follow the instructions.)

1 REPEAT 3.

2 Jump once on the spot.

3 REPEAT 4.

4 Take a step to the

5 Take a step to the

6 Clap your hands.

7 Wave your hands above your head.

8 Take a bow.

DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.

Questions

3 How many times did you jump?

4 How many times did you step left?

5 How many times did you wave your hands above your head?

Using loops, the algorithm is much shorter than it would be if we had not used loops at all.

If we were going to write out these instructions without using the count-controlled loops it would look like this:

17

Question

6 Imagine you want to change the dance slightly. Instead of moving left and right, you want to change it to backwards and forwards. Which algorithm would be easier to change – the first one that uses loops or the second one where each instruction is written out in a linear algorithm?

46 1 Computational thinking and programming
1 Jump once on the spot.
a
to the
a step to the left
Take a step to the right .
Take a step to the left .
Take a step to the right .
Clap your hands.
Jump once on the spot.
Take a step to the left
Take a step to the right .
Take a step to the left .
Take a step to the right .
Take a step to the left .
2 Take a step to the left . 3 Take a step to the right . 4 Take a step to the left . 5 Take
step
right . 6 Take
. 7
8
9
10
11
12
13
14
15
16
Take a step to the right .
Take a step to the left .
Take a step to the right .
Clap your hands.
Jump once on the spot.
Take a step to the left .
Take a step to the right .
Take a step to the left .
Take a step to the right .
Take a step to the left .
Take a step to the right
Take a step to the left
Take a step to the right .
Clap your hands.
Wave your hands above your head.
Take a bow.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
DRAFT Original material © Cambridge University Press 2022. This material is not final and is subject to further changes prior to publication.
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.