# Get Loopy

Meta Description

In this activity, the programming concept of looping is introduced through a basic dancing routine.

Learning Objectives

Understand the function of a loop through an interactive, repetitive dance routine.

Understand why loops are used to reduce code repetition.

Exercise coordination skills when translating the pictures into a choreographed dance.

Key Terms

Algorithm
A list of commands or steps that one must follow in order to complete a task.

Computer Program
This is usually an application/software on a PC/Mac/mobile that can have different functions according to the needs of the user.

Loop (Noun)
In computer programming, a loop is used to execute an instruction or a group of instructions, a number of times.

Loop (Verb)
To do the same action repeatedly.

Syntax
Syntax refers to the structure used when writing in a particular programming computer language.

Step 1
Prepare an instruction sheet containing pictures or written instructions depicting a simple dance routine. An example of such a routine is:

Clap, clap, clap;
Look left, look right, look left, look right;
Left arm up, right arm up, squat, arms down;
Left arm up, right arm up, squat, arms down;
Clap, clap;
Your favourite dance move;

Step 2
Explain the instruction sheet for the dance routine to the participants and explain the different steps of the dance, possibly through a demonstration.

Step 3
Alterations to the routine which are requested by the participants can be made using the marker. The instruction sheet can be fixed to a wall or some other surface which the participants can face during the activity so it can be used as a reference, if necessary.

Step 4
Encourage the participants to start dancing, possibly carrying out the routine with them. During the experiment, point out that the loops are represented by the instructor saying ‘Clap three times’ and ‘Clap twice’ instead of ‘Clap, clap, clap’ and ‘Clap, Clap’.

• The dance program and dance moves are completely up to the instructor’s discretion and can be made more simple or complex, according to the skill of the individuals.
• The instruction sheet can be simplified by omitting the pictures and just clearly listing the dance moves in the routine in order. The demonstrator can teach the routine to the participants by performing it themselves a few times and encouraging the participants to imitate them.
1. To avoid accidents, the demonstrator must ensure that there is enough free space in the room for the participants to move freely.
2. It is imperative that the floor is not a slippery surface and that it is dry.
3. Participants should wear appropriate, sturdy footwear for the experiment.
• The instructor can ask the participants if they can name any examples of looping activity in everyday life. Such activities include bus routes, clocks, the Earth spinning on its own axis and the Earth revolving around the sun. It is advisable that the instructor name a few examples of their own before asking the participants, so that the participants have a clearer picture of what to think of.
• The instructor could introduce the participants to the activity by the asking them to repeat a sentence or phrase multiple times and then asking them whether it would have been easier to actually repeat the sentence multiple times or to state that the sentence mentioned should be repeated a certain number of times.

What is this process of repetition called?
Looping

For a repetitive action in the dance, is it easier to add more pictures/instructions to the program or to change the number of times that the instructions ask participants to loop?
Change the number of times that the instructions ask participants to loop.

Can this system of looping be applied to other activities apart from dance moves?
Yes, for example to singing or computer programming.

Could the individuals alter the program to dance to popular songs?
This is up to the instructor’s discretion – lots of choreographed dance programs can be found on Youtube.

What can we call the whole dance program?
It is a type of algorithm.

Why is looping used in computer programming?
Looping is used in code development to run the same lines of code for a specified number of times.

What are the benefits of using looping in computer programming?
It reduces the length of the code, making it easier to understand and identify problems.

Recall that the dance program consisted of a number of steps, some of which were repetitive:

Clap, clap, clap;
Look left, look right, look left, look right;
Left arm up, right arm up, squat, arms down;
Left arm up, right arm up, squat, arms down;
Clap, clap;
Your favourite dance move;

The sequence “clap, clap, clap” can be summarized to “clap for three times”. This demonstrates the concept of a ‘for’ loop in a computer program: the loop removes code repetition by directly telling the computer to repeat an action a certain number of times.

These loops can be applied to the entire dance program as follows:

Clap for three times;
Look left look right for two times;
Left arm up, right arm up squat for two times;
Clap for two times;
Your favourite dance move;

Adding these ‘loop commands’ to the dance program makes it much simpler for participants to follow the routine and memorise it. Looping becomes even more valuable as the dance program becomes longer and much more complex. Likewise, looping in computer programs simplifies the code, making it easier for programmers to find problems (bugs) in the code and to understand how the code functions.

In programming, looping is an essential tool which is used in most programs. Each programming language may have a different syntax for ‘for’ loops, however, the logic behind the looping process is always the same. In programming, developers can also use other loops such as ‘while loops’ and ‘do while’ loops as well as ‘nested for’ loops.

‘For’ loops are used in programming to repeat the same block of code several times. Since a block of code is usually associated with a certain action, looping results in a repetition of an action. For example. a single line of code may instruct an alarm to go off within the computer. In order for the alarm to be sounded 5 times in quick succession, instead of writing the line of code 5 times, the programmer could place that line of code within a loop which iterates 5 times. Each repetition in a loop is known as an iteration.

The template of a code structure for a ‘for’ loop can be seen below:

for (counter; condition; increment) {

.    .    .

This where you add your code!

.    .    .

}

While loops are used in code to repeat a function for several times until a set condition is met. The template for a ‘while’ loop:

while  (condition) {

.    .    .

This where you add your code!

.    .    .

}

When the exact number of iterations required is not known. The difference between the ‘do while’ and the ‘while’ loop is the fact that in the case of the former, the contents of the loop will be executed at least once. This is not the case for the ‘while’ loop – if the condition is not met when the loop is encountered in the program, the program flow never enters the loop. The template for a ‘do while’ loop is as follows:

do {

.    .    .

This where you add your code!

.    .    .

} while  (condition);

The templates for each of the loops can be implemented in any programming language available.

Consider a practical example: the code snippet below is written in Python, a popular programming language, and shows how a ‘for’ loop can be implemented in code:

for counter in range(3):

print (“clap”)

The code above will print the word “clap” 3 times, one after the other. The ‘counter’ is  a variable that will start from 1 and adds an integer of 1 after every iteration until it reaches the value of 3 as specified by the function range(3) (condition). Hence the for loop iterates the line ‘print (“clap”)’ three times in total.

A do while loop is similar to a ‘while’ loop and it is also mostly used when the exact number of iterations required is not known. The difference between the ‘do while’ and the ‘while’ loop is the fact that in the case of the former, the contents of the loop will be executed at least once. This is not the case for the ‘while’ loop – if the condition is not met when the loop is encountered in the program, the program flow never enters the loop. The template for a ‘do while’ loop is as follows:

do {

.    .    .

This where you add your code!

.    .    .

} while  (condition);

The templates for each of the loops can be implemented in any programming language available.

Consider a practical example: the code snippet below is written in Python, a popular programming language, and shows how a ‘for’ loop can be implemented in code:

for counter in range(3):

print (“clap”)

The code above will print the word “clap” 3 times, one after the other. The ‘counter’ is a variable that will start from 1 and adds an integer of 1 after every iteration until it reaches the value of 3 as specified by the function range(3) (condition). Hence the for loop iterates the line ‘print (“clap”)’ three times in total.

Application
Loops in programming include the ‘for’ loop and the ‘do while’ loop. These help programmers to develop a program that must run through various lines of code repeatedly. An introduction to for loops and do while loops in Python can be seen in the link.

Loops also help programmers by keeping all of the code structured and organised. They also save a lot of time and space in coding files, since the developer writes just a few simple lines of code instead of multiple lines of code.

Additionally, looping can also be seen in everyday applications such as traffic lights, where the lights are set to switch to red to green and vice versa  approximately every 40 seconds. A small computer would ensure that traffic light operation operates in this way using a program which loops.

Research
In Meteorology, research is being carried out into magnetohydrodynamic (MHD) oscillations. These oscillations cause changes in solar activity observed in the sun’s outer layer. Scientists use radio telescopes to record this activity. The recorded data can then be used to simulate these oscillatory patterns and study their properties. Physicists can create computer programs which successfully simulate the recorded data and run models of this solar activity. The coding structure for such periodic events would require a lot of loops being used.

• Introduce the basic concept of a ‘for’ loop by asking the participants to repeat the routine a certain number of times, say three times. Ask whether they think it is easier to write the routine and then say ‘Repeat three times’ or to actually write out the routine three times on a paper.
• Introduce the basic concept of a ‘while’ loop by asking the participants to repeat the routine as long as a certain condition is met. This condition could be, for example, the demonstrator holding up their hand or the demonstrator standing- thus when the demonstrator lowers their hand or sits down, the loop is exited and the dance stops once the final step of the current iteration of the sequence, ‘Your favourite dance move’, has been executed.

Education

Time Required

• ~30 minutes

• Preparation: 20 minutes

• Conducting: 5 minutes

• Clean Up: 1 minute

Cost

Recommended Age

Number of People

Supervision

Location

Materials

Marker

Paper with pictures and/or written instructions describing a simple dance routine

Contributors

Sources

Getting Loopy

Getting Loopy

FOR Loops

Additional Content

FOR Loops (Beginner)

JavaScript do/While Statement (Beginner)

While Loops (Beginner)

Computer Programming Loops (Intermediate)

For Loops in C (Intermediate)

Nested Loops (Intermediate)

How to Construct a Loop in Minecraft (Advanced)

The for Loop vs. forEach in JavaScript (Advanced)

Underscore (Advanced)

Cite this Experiment

de Marco, A., & Padfield, N. (2017, December 09). Get Loopy. Retrieved from http://steamexperiments.com/experiment/get-loopy/

First published: December 9, 2017
Last modified: October 29, 2019

0 0

We can always improve. Tell us how.