Algorithms (Edexcel GCSE Computer Science): Revision Notes
Algorithms: iteration
What is iteration?
Iteration is one of the fundamental building blocks of computer programming and algorithm design. Think of it as telling a computer to repeat the same task over and over again for different pieces of data.
In simple terms, iteration means going through each item in a collection (like a list of names, numbers, or any data structure) and doing something with each item. It's like having a pile of letters to stamp - you pick up each letter one by one and stamp it until you've processed every single letter in the pile.
Technical Definition
Iteration is the process of repeating a set of instructions for each item in a data structure. It's also known as a type of loop, and you'll see it represented in flowcharts, written algorithm descriptions, and actual programme code.
Iteration in flowcharts
When you're drawing flowcharts to plan your algorithms, iteration has a very specific visual pattern that you need to recognise and create.
The flowchart structure for iteration always includes several key components that work together:
The setup box - This rectangular shape shows you preparing your data structure (like "set aList to 10 integers"). This tells you what collection of data you'll be working with.
The condition test - This diamond-shaped decision symbol asks a question like "is there another item in aList to process?" This is the heart of your iteration because it determines whether the loop continues or stops. The condition keeps checking until every single item has been processed.
The process box - This parallelogram shape (like "display item from aList") shows what action you want to perform on each item. This is where the actual work happens for each piece of data.
The Backwards Arrow - Don't Forget This!
The arrow that loops back from the process to the condition test is your visual signal that iteration is happening. Without this backwards flow, you don't have a loop. This is crucial and often catches students out in exams!
Count-Controlled Repetition
The iteration in flowcharts represents count-controlled repetition involving data structures. This means you know exactly how many items you need to process (the count), and you're working with organised data (the structure).
Written algorithm descriptions
When you're writing out algorithms in plain English rather than drawing flowcharts, iteration uses specific language patterns that you should recognise and be able to use.
Common iteration phrases include:
- "For every..." (like "for every name in the student table")
- "For each..." (such as "for each record in the file, read it in and process it")
- "While..." (like "while there are more characters in the string, display them in upper case")
- "As long as there are more..."
- "Until all items have been..."
These phrases signal that you're about to repeat a process multiple times. The key is that you're working through a collection of data systematically, ensuring that every item gets the same treatment.
Worked Example: Processing Student Records
If you wanted to process every student record in a database, you might write:
"For every student record in the database, read the student's name and calculate their average grade."
This clearly shows you're iterating through all records, with each record receiving the same processing treatment.
Iteration in Python programming
Python gives you several ways to implement iteration, each suited to different situations. Understanding when and how to use each type is essential for GCSE level programming.
The for...in statement
This is probably the most common type of iteration you'll use. It's perfect when you want to work with each item in a collection directly:
Worked Example: Simple for...in Loop
word = "Hello"
for character in word:
print(character)
This code takes the string "Hello" and prints each letter separately. The loop automatically moves through each character without you having to worry about counting or indexing.
The for...in range() statement
Sometimes you need more control over the iteration process, especially when you need to know the position of each item:
Worked Example: Using range() for Index Control
word = "Hello"
for index in range(0, len(word)):
print(word[index])
The range(0, len(word)) creates a sequence of numbers from 0 up to (but not including) the length of the word. This gives you the index positions, which you can then use to access specific characters.
The while statement
While loops are useful when you don't know exactly how many times you need to repeat something, but you know the condition that should stop the loop:
Worked Example: While Loop with Manual Counter
word = "Hello"
index = 0
length = len(word)
while (index < length):
print(word[index])
index = index + 1
This achieves the same result but gives you complete control over the loop counter. You must remember to update the index variable, or you'll create an infinite loop!
Key exam concepts
When tackling iteration questions in your GCSE exam, remember these important points:
Flowchart features that indicate iteration:
- A backwards arrow creating a loop structure
- A condition test (diamond shape) that controls when the loop stops
- A process that works on data from a collection or data structure
Common Mistakes to Avoid
- Forgetting the backwards arrow in flowcharts
- Not updating counter variables in while loops
- Mixing up when to use for loops versus while loops
Real-world Applications
Iteration appears everywhere in computing:
- Processing customer orders in an online shop
- Checking each student's exam results to calculate class averages
- Going through each pixel in an image to apply a philtre
- Validating each field in a form before submission
Key Points to Remember:
-
Iteration repeats instructions for each item in a data structure, ensuring every piece of data gets processed
-
Flowcharts show iteration through backwards arrows, condition tests (diamonds), and process boxes working together in a loop structure
-
Written algorithms use key phrases like "for every", "while", and "as long as there are more" to signal iteration
-
Python offers multiple loop types - use for...in for simple item processing, for...in range() when you need positions, and while loops for conditional repetition
-
The backwards arrow in flowcharts is your visual clue that iteration is happening - never forget this in exam questions!