Iteration (Edexcel GCSE Computer Science): Revision Notes
Iteration in programming
What is iteration?
Iteration is a fundamental programming concept that allows you to repeat the same set of actions or commands multiple times. Think of it as a way to automate repetitive tasks in your code. When you need to perform the same operation on every item in a data structure (like a list), iteration is the perfect solution.
The key characteristic of iteration is that you know in advance how many times you need to repeat the process. This makes it different from other types of loops where you might not know exactly when to stop.
Understanding for loops
In Python, the most common way to create iteration is by using for loops. These loops are designed to work through each element in a data structure, such as a list, one at a time.

The basic structure follows a simple pattern where you specify what you want to happen to each item in your data structure. The loop automatically moves from one element to the next until it has processed every single item.
Basic For Loop Structure:
The loop automatically moves from one element to the next until it has processed every single item, following a predictable pattern that makes your code both readable and efficient.
Key components of a for loop
When writing a for loop, you need several essential parts:
- The for keyword that starts the loop
- The in keyword that connects your loop variable to the data structure
- A loop variable (identifier) that represents the current item being processed
- A data structure containing the items you want to iterate through
- An indented command block that specifies what action to perform on each item
The indentation is crucial in Python - it tells the computer which commands belong inside the loop.
Using for...in range() loops
Sometimes you don't want to iterate directly through the items in a list. Instead, you might want to access items by their position (index) or repeat an action a specific number of times. This is where the range() function becomes incredibly useful.

The range() function generates a sequence of numbers that you can use to control your loop. This approach is particularly helpful when you need to:
- Access list elements by their index position
- Perform calculations based on position
- Create more complex iteration patterns
Understanding the range() function
Understanding Range Parameters:
The range() function accepts three parameters:
- Start: The number to begin counting from (optional, defaults to 0)
- Stop: The number to stop before (required - note that this number is not included)
- Step: How much to increase by each time (optional, defaults to 1)
For example, range(0, 6) would generate the numbers 0, 1, 2, 3, 4, 5 (stopping before 6).
Practical applications
Iteration is everywhere in programming! Here are some common real-world uses:
- Data processing: Analysing survey responses, calculating averages, or finding maximum values
- File handling: Reading through lines in a text file
- User interfaces: Displaying menu options or processing user inputs
- Games: Moving through game levels or updating character positions
- Web development: Processing form submissions or displaying database results
Exam tips and common pitfalls
Watch out for these common mistakes:
- Indentation errors: Python requires proper indentation for loop bodies
- Off-by-one errors: Remember that range(5) gives you 0,1,2,3,4 (not 1,2,3,4,5)
- Variable naming: Use meaningful names for your loop variables
- Forgetting the colon: Every for loop line must end with a colon
::
Exam strategies:
- Always trace through your loops step by step to check they work correctly
- When using range(), double-check your start and stop values
- Practice reading code that uses both direct iteration and index-based iteration
- Be prepared to explain the difference between iterating through values vs. iterating through indices
Sample exam question
Typical GCSE-style Question:
A programme needs to display the contents of a list containing integer numbers. The list is: myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Requirements:
- Use a for...in loop to print all numbers in the list
- Use a for...in range() loop to print only the even numbers in reverse order
This type of question tests your understanding of both iteration methods and requires you to think about how to access specific elements based on their properties.
Remember!
Key Points to Remember:
- Iteration repeats actions on every item in a data structure when you know how many times to loop
- For...in loops work directly with the values in your data structure
- For...in range() loops give you more control using index positions and counting
- Indentation is essential - Python uses it to understand which commands belong in the loop
- Range() stops before the end number - range(5) gives you 0,1,2,3,4, not 1,2,3,4,5