Welcome to Repetition in Programs!
Have you ever had to do something over and over again, like clapping your hands ten times, brushing every single tooth, or taking steps until you reach the playground? In everyday life, we repeat actions constantly. Computers are fantastic at doing this too!
In this guide, you will learn how to make your code smarter, shorter, and faster by using repetition. Don't worry if programming feels tricky at first—we will break everything down step-by-step.
Key Takeaway: Repetition helps us give instructions to a computer to do an action more than once without writing the same code over and over.
Key Words to Know
Before we jump in, let's look at four important computing words:
• Algorithm: A precise set of ordered steps or rules to solve a problem or complete a task.
• Repetition (Loop): The process of repeating a set of instructions within an algorithm or program.
• Procedure: A named set of commands (which often contains a loop) that can be saved and used multiple times.
• Debug: To find and fix errors (bugs) in a program when something isn't working as planned.
Why Use Repetition Instead of a Long Sequence?
Imagine you want to tell a robot to take four steps forward. You could write out a simple sequence like this:
Step forward
Step forward
Step forward
Step forward
That works, but what if the robot needs to take \(100\) steps? Writing Step forward \(100\) times would take forever and clutter your screen! Instead, we can place a single command inside a loop:
Repeat \(4\) times:
Step forward
Did You Know? Using loops makes your code much tidier and easier to debug if something goes wrong!
The Three Types of Loops
In Key Stage 2 computing, we use three main types of loops depending on what we want our program to do.
1. Count-Controlled Loops
A count-controlled loop repeats a set of code a specific, pre-determined number of times. You tell the computer the exact number of repeats before it starts.
• Everyday Example: Do \(5\) star jumps.
• In Scratch: Using the repeat (10) block.
• In Logo: Using the REPEAT 4 [ ... ] command.
2. Infinite Loops
An infinite loop repeats instructions continuously without an end point until someone manually stops the program (like pressing the red stop button in Scratch).
• Everyday Example: The ticking of a clock.
• In Scratch: Using the forever block.
• Important Rule: Any code blocks placed after an infinite loop will never run because the loop never finishes!
3. Condition-Controlled Loops
A condition-controlled loop repeats instructions until a specific event or condition happens to stop it.
• Everyday Example: Keep stirring the hot chocolate until all the powder dissolves.
• In Scratch: Using the repeat until <touching edge?> block.
Key Takeaway: Use count-controlled when you know the exact number, infinite when you want it to run non-stop, and condition-controlled when you are waiting for something specific to happen.
Drawing Shapes: The \(360^\circ\) Magic Rule
One of the most exciting ways to use repetition is drawing regular shapes with turtle graphics (like in Logo or with the pen blocks in Scratch).
To draw any regular polygon (a shape where all sides and angles are equal), the computer needs to walk forward and turn a corner. When the shape is finished, the sprite has completed one full spin of \(360^\circ\).
The External Angle Rule:
\(\text{Number of sides (repeats)} \times \text{Turn angle in degrees} = 360^\circ\)
To find the correct angle to turn, simply divide \(360^\circ\) by the number of sides:
\(\text{Turn angle} = \frac{360^\circ}{\text{Number of sides}}\)
Let's Look at Examples:
Drawing a Square:
• A square has \(4\) sides.
• Calculate the turn angle: \(360^\circ \div 4 = 90^\circ\).
• Instructions: Repeat \(4\) times: Move \(100\) steps, Turn right \(90^\circ\).
• Check: \(4 \times 90^\circ = 360^\circ\).
Drawing an Equilateral Triangle:
• A triangle has \(3\) sides.
• Calculate the turn angle: \(360^\circ \div 3 = 120^\circ\).
• Instructions: Repeat \(3\) times: Move \(100\) steps, Turn right \(120^\circ\).
• Check: \(3 \times 120^\circ = 360^\circ\).
Key Takeaway: Always make sure the number of repeats multiplied by the turn angle equals \(360^\circ\) to close your shape neatly!
Programming Environments: Logo and Scratch
You can create loops using different types of programming tools:
• Logo (Text-based): You type text commands to move a turtle. For example:
REPEAT 4 [FORWARD 100 RIGHT 90]
• Scratch (Block-based): You snap visual code blocks together, like nesting motion blocks inside a yellow repeat or forever block.
Watch Out! Common Mistakes and Pitfalls
Even the best computer scientists make mistakes. Here are four common bugs to watch out for:
1. The "Counting Only" Misconception
Mistake: Thinking repetition is only used for maths and counting numbers.
Fix: Repetition works for any action! You can repeat costume changes to make an animation, repeat sound effects, or repeat movement to make a character patrol across the screen.
2. Off-by-One Errors
Mistake: Typing the wrong repeat number (e.g., repeating \(3\) times instead of \(4\) for a square).
Fix: Check your shape! If a side is missing, count how many times your loop is set to repeat and make sure it matches the number of sides.
3. Sequence vs Loop Confusion
Mistake: Stacking the same block four times in a row and calling it a loop.
Fix: Stacking blocks is just a long sequence. A true loop uses a repeat block or a REPEAT command to wrap around the actions.
4. The Infinite Loop Trap
Mistake: Putting code blocks underneath a forever block and wondering why they don't run.
Fix: An infinite loop never ends. If you want code to run after a loop, use a count-controlled loop or a condition-controlled loop instead.
Quick Review Checklist
Test your knowledge with these quick checks:
• What is a loop? (An instruction that repeats a section of code)
• Which loop runs without ever stopping? (An infinite loop / forever block)
• Which loop stops after a fixed number of times? (A count-controlled loop)
• Which loop waits for an event? (A condition-controlled loop)
• What must the turn angle times the number of repeats equal when drawing a shape? (\(360^\circ\))