Repetition (Edexcel GCSE Computer Science): Revision Notes
Algorithms: Repetition
What is repetition?
Repetition is a fundamental concept in programming where we make the computer perform the same set of instructions multiple times until we achieve what we want. Think of it like asking someone to keep knocking on a door until someone answers, or counting sheep until you fall asleep!
In computing, we can show repetition in several ways:
- Flowcharts - visual diagrams that map out the process
- Written descriptions - plain English explanations of what should happen
- Program code - actual instructions written in a programming language
Repetition is also called looping because the programme goes around in a circle, doing the same thing over and over. You'll often hear these terms used interchangeably in programming.
Types of repetition
There are two main types of repetition that you need to understand for your GCSE:
Condition-controlled repetition
This type of repetition happens when you don't know exactly how many times you need to repeat something. Instead, you keep going until a certain condition becomes true.
For example, imagine you're asking users to guess your favourite number. You don't know how many guesses it will take - they might get it right on the first try, or it might take 20 attempts! You keep the loop running until they guess correctly.
Key characteristics of condition-controlled repetition:
- Unpredictable number of repetitions
- Continues until a condition is met
- Uses while loops in most programming languages
The flowchart shows how condition-controlled repetition works. Notice how it keeps asking for user input and checking if the number equals 22. The loop could run once or a hundred times - we simply don't know in advance!
Count-controlled repetition
This type of repetition is used when you know exactly how many times you want something to happen before you start.
For example, if you want to print "Happy Birthday" exactly 16 times for someone's birthday, you know from the start that you need exactly 16 repetitions - no more, no less.
Key characteristics of count-controlled repetition:
- Predictable number of repetitions
- You know the exact count before starting
- Uses for loops in most programming languages
The flowchart shows a count-controlled loop that runs exactly 5 times. It starts with num = 0 and keeps adding 1 until num reaches the maximum value of 5.
Flowchart symbols for repetition
Common Exam Mistake: There is no special symbol for loops in flowcharts! This often catches students out in exams.
Instead, loops are created using:
- Diamond shapes (selection symbols) to test conditions
- Arrows that point backwards to create the loop
Both types of repetition use the same diamond-shaped decision symbol, with an arrow going back to repeat the process. This backward-pointing arrow is what creates the actual loop structure.
Programming repetition in Python
Let's look at how we write these two types of repetition in Python:
Worked Example: Condition-controlled repetition (while loops)
num = 0
while (num != 22):
num = int(input("Number: "))
This code keeps asking the user to enter a number until they type 22. We use the while keyword followed by a condition in brackets.
Step-by-step breakdown:
- Initialise num to 0
- Check if num is not equal to 22
- If true, ask for user input
- Repeat from step 2
Worked Example: Count-controlled repetition (for loops)
num = 0
maxim = 5
for count in range(num, maxim):
print(count)
This code will run exactly 5 times (from 0 to 4). We use the for keyword with range() to specify how many times to repeat.
Step-by-step breakdown:
- Set starting value num = 0 and maximum maxim = 5
- Loop count from 0 to 4 (5 times total)
- Print each value of count
When to use each type
Use condition-controlled repetition when:
- Validating user input (keep asking until they give a valid answer)
- Searching through data until you find what you're looking for
- Processing data until you reach the end
- Any situation where you can't predict how many attempts it will take
Use count-controlled repetition when:
- You need to do something a specific number of times
- Processing a list where you know the size
- Creating patterns or sequences with exact requirements
- Any situation where you can count the repetitions in advance
Real-world examples
Real-world Applications:
Condition-controlled examples:
- ATM asking for PIN until correct entry (you don't know how many attempts)
- Game continuing until player loses all lives
- Temperature monitoring until it reaches safe level
Count-controlled examples:
- Displaying exactly 10 search results on a webpage
- Processing exactly 30 student records
- Playing a song's chorus 3 times
Exam tips
Essential Exam Strategies:
- Remember: Flowcharts use diamond shapes for loop conditions, not special loop symbols
- Keywords to look for: while, until, for, repeat, "go back to" indicate repetition
- Count the iterations: If you can count them beforehand, it's count-controlled
- Check the condition: If it depends on something unpredictable, it's condition-controlled
Key takeaways
Key Points to Remember:
- Repetition means doing the same instructions multiple times until you get the result you want
- Condition-controlled repetition continues until a condition is met - you don't know how many times in advance
- Count-controlled repetition runs for a specific number of times that you decide beforehand
- Flowcharts show loops using diamond shapes (for decisions) with backward-pointing arrows
- In Python, use while for condition-controlled loops and for...in range() for count-controlled loops