Repetition & Iteration (Edexcel GCSE Computer Science)

Revision Note

Repetition & Iteration

What is the difference between repetition and iteration?

  • Repetition is executing the same instructions whilst a condition is True

  • Iteration is is executing the same instructions until all items in a data structure have been processed

  • Repetition & iteration are both examples of loops

Repetition

Python examples

num = 10

while num > 0:

print(num, "...")

num = num - 1

print("Blast off!")

The instructions inside the loop repeat until the num reaches 0

A higher starting number would result in more repeating

print("Do you accept the T&Cs? (Y/N)")

accept = input()

while accept != "Y":

print("Do you accept the T&Cs? (Y/N)")

accept = input()

print("Thank you for accepting the T&Cs")

Whilst the user does not input a "Y" to accept the terms & conditions the question is repeated infinitely

Iteration

Python examples

scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]

for i in scores:

print(i * 2)

Outputs the values of the scores array, multiplied by 2 on separate lines

students =[

[1,"Bob",78],

[2,"Fred",24],

[3,"Julie",32]]

for student in students:

print ("Name:",

student[1],

"Test score:",

student[2])

The for loop processes each student in the students table.

In each loop the individual fields are outputted.

The output from this loop is:

Name: Bob Test score: 78

Name: Fred Test score: 24

Name: Julie Test score: 32

  • Repetition & iteration can be further confused with the addition of different types of loops

Type of loop

Description

Count-controlled

The number of loops is already known or can be determined before the loop starts

Condition-controlled

The number of loops is not known, it continues until the condition is True

Python example

Repetition or iteration?

Type of loop?

choice = "Y"

while (choice != "N"):

choice = input("choose: ")

Repetition

Condition-controlled

count = 0

while (count < 5):

count = count + 1

print (count)

Repetition

Count-controlled

table = ["cat", "dog", "fox"]

for word in table:

print (word)

Iteration

Count-controlled

  • In Python, when the range() function is used, the loop is count-controlled

  • If the loop is used to access a data structure it is iteration

Examiner Tip

If the word 'iteration' is used in an instruction, you can use a 'for' loop to process a data structure.

If the word 'repetition is used in an instruction, you must choose whichever construct ('for', 'while') is suitable for the program

Last updated:

You've read 0 of your 10 free revision notes

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.