iGCSE Computer Science - Distance Learning

Tutor Marked Assignment 6
Tutor Marked Assignment 8 (Programming)

Using Iteration in Programming

In programming the word ‘iteration’ is used as the correct technical term for looping or repeating a section of code. It is important to know though that not all loops work in the same way, and we are expected to know which loop to use and when.

Iteration Statements - GCSE Computer science

There are three types of loop that you should be aware of:




A FOR loop is used when we know how many times we want the code to repeat – this could be because we want it to repeat exactly n times, or because we want it to repeat for each item in a list. To help us do this, we use two types of FOR loop: a Ranged FOR loop and a FOR EACH loop.

Imagine we want to output each item of our list of fruit. This would be best achieved using a FOR EACH loop because the loop will directly access the data. In pseudocode, this would look like:

fruit =[‘Banana’, ‘Apple’,’Strawberry’]

FOR EACH piece IN fruit DO

     OUTPUT piece

The code below shows how the pseudocode could be implemented using Python. Try it yourself by updating the code below this to allow the user to add more fruit to the list (this is a practice task & not part of your assignment):

In much the same way, the WHILE and DO UNTIL loops are very similar, but have specific differences that we need to be aware of. Unlike FOR loops, you don’t need to know how many times  that the code will be repeated, and because of this these loops run the risk of creating an infinite loop (although in some programs you may actually want to create one).

AWHILE loop tests a condition at the top, if the condition is true will run the section of code and continue to do so until the condition is no longer met. In pseudocode, this might look like:

Fruit = “”

WHILE fruit <> “banana” DO

   OUTPUT “What fruit is yellow?”

   Fruit = INPUT

The DO UNTIL loop will allow the code to run at least once, then tests the condition at the end. If the condition is false, then the loop will repeat. In the pseudocode below, the algorithm that was used in the WHILE loop is made more efficient because we are able to remove lines of code by testing the condition at the borrom of the loop instead of the top. This is often referred to as a post-condition loop (all others are pre-condition loops).

Unfortunately, this style of iteration doesn’t exist within python, so for the purposes of the course, we must be aware of its use within pseudocode.


   OUTPUT “What fruit is yellow?”

   Fruit = INPUT

UNTIL fruit = “Banana”

Lesson Tasks:

Download the worksheet below & complete this by connecting the types of iteration to the examples. This is a similar some of the exam questions you may see in paper 1:

Assessment Task

As a practical activity at the end of this lesson, I would like you to attempt to solve the code below to help crack the code for the safe at the end of the page.

The code has been partically written for you, but there are some elements missing. Solve the clues in the instructions to write the rest of the code, then test your answer by entering the code into the interactive safe at the end of the page.

The practice version of this code is below. Please submit your final version of this code on the Tutor Marked Assignment Trinket pages.

Check your answer by trying to open the safe! (click on the safe code buttons to enter your number, then click the ‘Check Answer’ button)