English:Loops and Repetition

Loops and Repetition
Introduction
Loops are one of the most useful ideas in computer programming. A loop tells a computer to repeat one or more instructions. Instead of writing the same command again and again, you can write the command once and tell the computer how often, or under what condition, it should repeat.
You already meet repetition in everyday life. You may clap the same rhythm several times, walk a repeated route around a sports field, practise a dance move, or notice a repeated shape in a pattern. Programming loops use the same big idea: do something again in a planned way.

A repeat sign in music tells a performer to play a section again. A programming loop gives a computer a similar instruction.

A tessellation is made from shapes that repeat to cover a surface. Looking for repeated patterns can help you notice where loops might be useful in an algorithm.
As you work through this aiMOOC, you will learn how to recognize repeated actions, choose a suitable loop, trace what happens during each repetition, find common loop mistakes, and design small programs with loops.
What Is a Loop?
A computer follows instructions exactly. Imagine that you want a character to take five steps. One way is to write the same command five times:
move forward move forward move forward move forward move forward
A shorter plan is:
repeat five times move forward
The second plan uses a loop. The command that repeats is called the loop body. One trip through the loop body is called an iteration. If the loop runs five times, it has five iterations.
Loops are useful because they can make an algorithm shorter, easier to read, and easier to change. If you later want ten steps instead of five, you can change the repeat count instead of copying more commands.
Three Questions to Ask About a Loop
When you see or design a loop, ask yourself these questions:
- Loop body: What instruction or group of instructions repeats?
- Iteration: What happens during one repetition?
- Stopping condition: How does the loop know when to stop?
These questions help you understand both block-based programs such as Scratch and text-based programs you may meet later.
Why Programmers Use Loops
Loops help programmers avoid unnecessary repetition. They are especially useful when the same action must happen many times. For example, a game can keep checking whether a player pressed a key, an animation can move a sprite a little during each frame, and a drawing program can repeat movement and turning to make a shape.
A loop can also work through a collection of items. A program might check every name in a list, add every score, or show every picture in a slideshow. The repeated action stays similar, even though the data can change from one iteration to the next.
A good programmer does not use a loop simply because repetition exists. The programmer decides whether the repetition is clear, safe, and likely to make the program easier to understand.
Types of Loops
For this course, it is useful to think about two main kinds of loops: count-controlled loops and condition-controlled loops.
A count-controlled loop repeats a known number of times. A condition-controlled loop repeats while something is true or until something becomes true. Different programming languages use different words, but the ideas are closely related.
Repeat a Fixed Number of Times
Suppose you want a sprite to draw a square. A square has four equal sides and four right-angle turns, so this pair of actions can repeat four times:
move forward turn right
That is a good place for a count-controlled loop. In Scratch, the repeat block can run the blocks inside it a chosen number of times.

The image above shows Scratch instructions placed inside a repeat loop. Notice how the repeated commands sit inside the loop block.

A flow diagram can also show a counted loop. The important idea is that the program keeps returning to the repeated action until the planned number of iterations is complete.
Repeat While or Until a Condition
Sometimes you do not know the number of repetitions in advance. Imagine a game character moving until it reaches a wall. The number of steps depends on where the character starts.
A condition is a statement that can be checked as true or false. Examples include:
- Touching the edge: The loop can stop when the sprite reaches the edge.
- Score below ten: The loop can continue while the score is below ten.
- Answer is correct: The loop can repeat until the learner gives the correct answer.
A while loop usually repeats while a condition is true. A repeat-until loop usually repeats until a condition becomes true. The exact block or command depends on the programming language.

A flowchart shows the decision clearly: check the condition, do the repeated action when appropriate, and return to check again.
Counters and Changing Values
A loop does not have to do exactly the same thing each time. A value can change during each iteration.
Imagine a score counter that starts at zero. Each time a player collects a coin, the program adds one. A loop could repeatedly check for coins and update the score.
A counter is a variable used to keep track of how many times something has happened. Counters can help a loop stop at the right time or help a program show progress.
For example:
start count at zero repeat five times add one to count show count
The displayed values would be 1, 2, 3, 4, and 5. Tracing these changing values is an important programming skill.
Nested Loops
A nested loop is a loop inside another loop. Nested loops are useful when a repeated action contains another repeated action.
Imagine drawing a row of three squares. The inner loop can draw one square by repeating a side-and-turn action four times. The outer loop can repeat the whole square-drawing process three times, moving to a new position after each square.

Nested loops can save many repeated instructions, but they require careful thinking. If an outer loop repeats three times and an inner loop repeats four times during each outer iteration, the inner action runs twelve times altogether.
Tracing a Loop Step by Step
Tracing means following a program carefully and recording what happens. Tracing helps you predict the output before running the code.
Suppose a variable called score starts at 2. A loop repeats three times, and each iteration adds 2 to score.
Before the loop, score is 2. After the first iteration, score is 4. After the second iteration, score is 6. After the third iteration, score is 8.
A trace table can make this easier:
| Stage | Score |
|---|---|
| Before the loop | 2 |
| After first iteration | 4 |
| After second iteration | 6 |
| After third iteration | 8 |
Tracing is useful when you are debugging because it shows where the program starts to behave differently from what you expected.
Debugging Loops
A bug is a problem in a program. Debugging means finding and fixing that problem. Loops can produce several common mistakes.
Wrong repeat count: A loop may repeat too many or too few times. Check the number and think about what one iteration does.
Wrong condition: A condition-controlled loop may stop too early or continue too long. Read the condition as a sentence and test what happens when it is true and false.
Value never changes: A while loop may depend on a value that never changes. Then the stopping condition might never be reached.
Infinite loop: A loop that never reaches its stopping point is called an infinite loop. Some programs use continuous loops on purpose, but an accidental infinite loop is a bug.
Off-by-one error: Sometimes a loop repeats one time too many or one time too few. Tracing the first and last iterations can help you spot this error.
When debugging, make one change at a time, run or trace the program again, and explain why the change should help.
Loops in Everyday Life and Other Subjects
Loops are a programming idea, but repetition appears in many school subjects and everyday activities.
In mathematics, repeated addition connects to multiplication. A loop that adds 4 five times models the same total as 5 × 4.
In geometry, repeated moves and turns can create polygons and patterns. A repeated sequence can also create a tessellation or a spiral.
In music, beats, rhythms, and sections often repeat. A musical repeat sign is a useful reminder that instructions can tell someone to perform a section again.
In physical education, a training circuit may repeat a sequence of exercises. You can describe the circuit as an algorithm and decide which parts belong inside a loop.
In daily life, routines such as brushing each tooth area, checking each item on a packing list, or watering each plant contain repeated actions. Thinking in loops helps you break a large task into a clear pattern.
From Repetition to an Algorithm
Before writing code, you can describe a solution in plain language or pseudocode. Pseudocode is not a real programming language. It is a clear way to plan the steps of an algorithm.
For example, a plan for drawing a triangle could be:
repeat three times move forward turn 120 degrees
A strong loop plan should say what repeats and when the repetition ends. If the stopping rule is unclear, the program may not behave as intended.
You can also draw a flowchart. A flowchart uses shapes and arrows to show the order of steps and decisions. Loops in flowcharts contain a path that returns to an earlier point.
Try It in Scratch
Scratch is a block-based programming language designed for creative projects. You can combine movement, sound, drawing, sensing, and control blocks. Loops are found among the control blocks.
Try these ideas in Scratch:
- Animation: Make a sprite move and change costume repeatedly to create motion.
- Geometric drawing: Use repeat with move and turn blocks to draw a square, triangle, or pattern.
- Sound: Repeat a short sound pattern to build a rhythm.
- Game design: Repeat a check for a key press, collision, or score condition.
Start with a small loop, test it, and then add more instructions. This makes debugging easier than building a large program all at once.
Key Vocabulary
| Word | Meaning |
|---|---|
| Loop | A control structure that repeats instructions. |
| Iteration | One complete pass through a loop. |
| Loop body | The instructions that are repeated. |
| Condition | A test that can be true or false. |
| Counter | A variable used to track repetitions or events. |
| Nested loop | A loop placed inside another loop. |
| Infinite loop | A loop that keeps running because its stopping point is never reached. |
| Debugging | Finding and fixing problems in a program. |
Interactive Tasks
Quiz: Test Your Knowledge
What does a loop do in a program? (It repeats instructions) (!It deletes every instruction) (!It turns code into a picture) (!It always stops the whole program)
What is one complete pass through a loop called? (An iteration) (!A condition) (!A sprite) (!A variable name)
Which loop is a good choice when you know an action must happen four times? (A count-controlled loop) (!An accidental infinite loop) (!A condition that never changes) (!A comment)
What does a condition tell a program? (Whether something is true or false) (!How loud the computer is) (!Which keyboard is connected) (!How old the programmer is)
What is a nested loop? (A loop inside another loop) (!A loop with no repeated actions) (!A picture inside a file) (!A variable with no value)
Why can tracing help when debugging? (It shows what happens step by step) (!It makes every loop run forever) (!It removes the need for testing) (!It changes all variables to zero)
A loop repeats move and turn four times. What belongs to the loop body? (The move and turn instructions) (!Only the title of the program) (!The computer screen) (!The file name)
What can cause an accidental infinite loop? (The stopping condition is never reached) (!The program has a clear repeat count) (!The loop body runs once) (!The programmer uses a comment)
Why are loops useful? (They can shorten repeated code) (!They make conditions unnecessary) (!They prevent all bugs) (!They remove every variable)
If an outer loop repeats three times and an inner action repeats four times each time, how often does the inner action run? (Twelve times) (!Seven times) (!Four times) (!Three times)
Memory Game
| Loop | A structure that repeats instructions |
| Iteration | One complete pass through repeated instructions |
| Condition | A test that can be true or false |
| Counter | A value used to track repetitions or events |
| Nested loop | A repeated structure placed inside another repeated structure |
| Loop body | The instructions that are repeated |
| Infinite loop | Repetition that does not reach its stopping point |
Drag and Drop
| Match the correct terms. | Topic |
|---|---|
| Count-controlled loop | Repeats a known number of times |
| Condition-controlled loop | Repeats according to a true or false test |
| Trace | Follow the program step by step |
| Counter | Keep track of repetitions or events |
| Debug | Find and fix a problem in code |
...
Crossword Puzzle
| Loop | What programming structure repeats instructions? |
| Repeat | What action means to do something again? |
| Iteration | What is one complete pass through a loop? |
| Condition | What true or false test can control a loop? |
| Debugging | What process finds and fixes problems in code? |
| Nested | What kind of loop is placed inside another loop? |
LearningApps
Cloze Text
Open-Ended Tasks
Easy
- Repetition Hunt: Find five examples of repetition at home or school. For each one, write what repeats and what makes the repetition stop.
- Human Robot: Write a short set of instructions for a classmate acting as a robot. Replace one repeated action with a loop and test whether the instructions still work.
- Loop Comic: Create a four-panel comic that shows a character doing a repeated action. Add a caption that explains the loop body and the stopping rule.
- Rhythm Loop: Create a short clap or desk-tap rhythm, repeat it four times, and write the rhythm as a simple algorithm using the word repeat.
Standard
- Scratch Shape Project: Build a Scratch project that draws at least two geometric shapes with loops. Compare the repeat counts and turning angles you used.
- Loop Interview: Interview a family member, teacher, coach, or classmate about a repeated routine. Turn the routine into an algorithm and explain where a loop would fit.
- Debugging Challenge: Create a small loop with one planned mistake, exchange it with a partner, and ask the partner to trace and fix the bug.
- Pattern Investigation: Photograph or draw a repeating pattern in architecture, art, textiles, or nature. Describe the smallest repeated unit and design pseudocode that could recreate part of the pattern.
Advanced
- Nested Loop Art: Create a digital or paper design based on a loop inside another loop. Label the inner loop, outer loop, repeat counts, and total number of inner actions.
- Condition Experiment: Build or simulate a program that repeats until a condition becomes true. Test it with at least three starting situations and record how many iterations occur.
- Loop Tutorial Video: Produce a one- to three-minute tutorial video that teaches younger learners how a counted loop works. Include one example, one prediction question, and one debugging tip.
- Computing Place Study: Visit a school computer lab, makerspace, robotics club, or similar learning place if available. Observe or ask how repeated instructions are used, then create a short report connecting one real example to programming loops.
Learning Assessment
- Choose the Loop: Read three short programming situations and decide whether each needs a fixed-count loop, a condition-controlled loop, or no loop. Explain each choice.
- Trace and Explain: Trace a loop in which a variable changes during every iteration, record the values in order, and explain how you know the final value.
- Repair the Loop: Diagnose a loop that runs one time too many and describe the smallest change that fixes the problem.
- Compare Solutions: Solve the same repeated task once by copying instructions and once by using a loop. Compare the two solutions for length, clarity, and ease of changing the repeat count.
- Design a Stopping Rule: Create a condition for a game character that moves until it reaches a goal. Explain how your condition prevents the loop from continuing forever.
- Transfer to Another Subject: Choose a repeated process from mathematics, music, art, science, or physical education and model it as a programming loop. Explain what corresponds to the loop body, iteration, and stopping rule.
Evidence of Learning
- Knowledge
- You can explain loop, iteration, loop body, condition, counter, nested loop, and infinite loop in your own words.
- Recognition
- You can spot repeated instructions and decide where a loop would make an algorithm clearer.
- Reasoning
- You can predict how many times a loop runs and explain how changing a count or condition changes the result.
- Tracing
- You can follow a loop step by step and keep track of changing values.
- Debugging
- You can identify common loop problems such as wrong repeat counts, unreachable stopping conditions, and off-by-one errors.
- Programming product
- You can create a small block-based program that uses at least one loop correctly.
- Communication
- You can explain your loop design with clear vocabulary, diagrams, pseudocode, or a short demonstration.
- Transfer
- You can connect the idea of loops to repetition in another subject or real-life routine and explain the connection accurately.
OERs on the Topic
The English Wikipedia article Loop (statement) gives a broader introduction to loops in programming.
Useful freely accessible learning media in this course include Wikimedia Commons diagrams of programming loops and Scratch blocks, along with the videos embedded above. You can use them to review the same idea in visual, block-based, and flowchart forms.
Linked Learning Areas
aiMOOC Projects
LernweltNOAH fragen