Pīkau 9: Programming with loops


Why this matters

The idea of a loop in programming is very powerful - in just a few commands you can get the computer to do hundreds, or even millions, of operations. This could involve getting it to look at every pixel of the millions in a photograph, locating someone’s information where there are millions of names stored in a database, or processing all the purchases that customers have made in one day. This is where we go from having a device simply follow some one-off instructions, to doing them over and over, day after day.

We’ll also look in more detail at how to get input from the user, and storing the input so that we can do interesting things with it.

Remember that this is a readiness programme. In this pīkau we’ll be spending less than an hour on these ideas. We won’t be able to fully cover everything there is to know, but we’ll get you started.

At this level our main goal is that you understand the point of Progress Outcome 3 in the New Zealand Curriculum.

Also, there’s an important part of Progress Outcome 3 that we’re not covering here, but will be in a later pikau: “They understand that digital devices store data using just two states represented by binary digits (bits).”

The idea of binary digits is fundamental to how everything is stored on computers and is relevant to nearly everything in Digital Technologies, including programming, but it’s worth looking at on its own.

Pīkau 16 – Representing data in binary

By the end of this pīkau you will have been shown how to:

  • Identify inputs, outputs, sequence and iteration in simple programs
  • Describe the role of a variable
  • Predict the behaviour of simple programs
  • Explain how multiple algorithms can be used to solve the same problem

Links to existing knowledge

You might already know some of this.

The material we’re covering here builds on the previous pīkau about computational thinking. We’ll be looking in a lot more detail at what was covered in pīkau [LINK to EM-P7] What is programming, but we’re also building on the ideas in the first two Progress Outcomes, which were covered in:

We’re mainly going to use the Scratch programming language. If you’d like to save your work, it’s worth setting up a (free) Scratch account.

  • Start by visiting the Scratch website.
  • Click on “Join Scratch”.
  • Choose a username that you don’t mind using in front of your class!

Please remember: this isn’t a lesson in programming, just an overview of how programming works.

A quick introduction to variables

While variables aren’t explicitly mentioned in the Progress Outcomes until Progress Outcome 5, we are going to introduce them here because you can’t do much without them. Variables are for storing information you want to use later. Find out more by watching the following video.

The Scratch program used in the above video is available on Scratch:

Activity - Playing with variables

Try these programs, and by clicking on “See inside”, see if you can predict what the cat will say before you click on the green flag to run the program! You can check your answer by clicking on the green flag in Scratch. (These programs don’t do anything very useful - they are just an exercise to check your understanding!)

Activity - Playing with variables Q1

See if you can predict what the cat will say before you click on the green flag to run the program!

Check your answer:

Activity - Playing with variables Q2

See if you can predict what the cat will say before you click on the green flag to run the program!

Check your answer:

Activity - Playing with variables Q3

See if you can predict what the cat will say before you click on the green flag to run the program!

Check your answer:

Inputs in Scratch

Inputs are how we get information from the user or other external sources into our programs. There are many ways to get input.

In this video we’ll have a look at a few of them that are more relevant in an education setting, using the Scratch programming language as an example.

Programming in an authentic context

In the following video Joanne and Tim discuss inputs in projects done by some of Joanne’s students. It’s a good example of how programming can be integrated practically into other learning areas. The students are in a Māori immersion kura, so the programs are written in te reo. Their programs are related to a field trip to Te Koroneihana (annual Māori monarchy coronation celebrations).

Inputs in other languages

We’ve looked at inputs in Scratch. Let’s look at how input can be done in other languages, in this case, Python and JavaScript. You’ll see how a block based coding language really does scaffold up to more complex languages.

The Python program used in this demonstration is available here:

The simpler JavaScript program can be explored here:

By the way, the unusual URLs are made up by the repl.it site, and are assigned randomly - we didn’t choose to use words like “Guiltware”!

Iteration in Scratch

Iteration is a powerful idea in coding as it makes the computer do things over and over. This means a programmer can write a small amount of program code to quickly instruct the device to do hundreds - or even millions - of instructions over and over. It can also prevent bugs being introduced in repetitive code since the programmer only needs to get it right once, and the computer repeats the correct code over and over. This all makes for efficient use of a programmer’s time!

The Scratch program used in the above video is available at:

Link to programme design

As we saw in the video Programming in an authentic context, writing programs in Scratch can be used to support other learning areas. Writing the code itself can give many mathematical concepts an authentic context, such as coordinate geometry and angles.

Programming can also be used to bring Digital Technologies / Hangarau Matihiko into other subjects in the curriculum; in this case it gave students a way to share their experience from a field trip.

Wrapping up and where to next

The elements of Progress Outcome 3 that have been covered in this pīkau are highlighted in the following video. We’ve set the scene to look at Progress Outcome 4, which will be covered in the next pīkau, Getting programs right: the end-user, and fast algorithms (CT PO 4) (EMP09).


Facilitation notes

If you are working through this pīkau as a group feel free to download and use these facilitation notes:


Some Year 7 students from Te Kura Kaupapa Māori o Te Whānau Tahi in Ōtautahi (Christchurch) kindly spoke about and shared their Scratch projects for this pīkau.