Iteration (AQA GCSE Computer Science)

Revision Note

James Woodhouse

Written by: James Woodhouse

Reviewed by: Lucy Kirkham

Iteration

What is iteration?

  • Iteration is repeating a line or a block of code using a loop

  • Iteration can be:

    • Definite (count controlled) - this is when the code is repeated a fixed number of times (e.g. using a for loop)

xp66-srn-for-loop-pseudocode-computer-science-revision-notes
  • Indefinite (condition controlled) - this is when the code is repeated until a condition is met (e.g. using a while loop or a do while loop)

while-loop-password-pseudocode-computer-science-revision-notes

Examples

Iteration

Pseudocode

Python

FOR loop

(Definite Iteration)

FOR x ← 0 to 9

OUTPUT("Hello")

ENDFOR

for x in range(10):

print("Hello")

This will print the word "Hello" 10 times (0-9 inclusive)

FOR x ← 2 to 10 step 2

OUTPUT(x)

ENDFOR

for x in range(2,12,2):

print(x)

# Python range function excludes end value

This will print the even numbers from 2 to 10 inclusive

for x ← 10 to 0 step -1

OUTPUT(x)

ENDFOR

for x in range(10,-1,-1):

print(x)

# Python range function excludes end value

This will print the numbers from 10 to 0 inclusive

WHILE loop

(Indefinite Iteration)

while colour != "Red"

colour ← input("New colour")

ENDWHILE

while colour != "Red":

colour = input("New colour")

 

This will loop until the user inputs the colour "Red". Check condition is carried out before entering loop

DO WHILE loop

(Indefinite Iteration)

DO

colour ← input("New colour")

UNTIL answer == "Red"

# Not used in Python

 

This will loop until the user inputs the colour "Red". Loop iterates once before a check is carried out

How to identify programming constructs

  • You can identify which programming constructs are used by looking at certain keywords

  • The keywords if, elseif, else, endif indicate that the construct is selection

  • The keywords for, while, do indicate that the construct is iteration

  • If none of these keywords are used, this is a good indication that the construct is sequence

Worked Example

Tick (✓) one box in each row to identify whether each programming construct has or has not been used in the program [3]

total ← 0

for i ← 1 to 5

num ← USERINPUT

total ← total + num

next i

print(total)

A table with three rows and three columns. The first column lists "Sequence," "Selection," "Iteration." The other columns are "Has been used" and "Has not been used."

How to answer this question

  • Always look for keywords first, if you find any then that construct must have been used

  • Sequence must always be used if the program works

Answer

Table with three rows labeled "Sequence," "Selection," and "Iteration." Columns indicate use, with green showing "Has been used" and white for "Has not been used."

Nested Iteration

What is nested iteration?

  • Nested means to be 'stored inside the other'

  • An example of nested iteration is one loop inside of another loop

  • It is worth noting that selection can be nested as well

Examples of nested programming

Prgm1_Iteration
Prgm2_Iteration

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

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

the (exam) results speak for themselves:

Did this page help you?

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.

Lucy Kirkham

Author: Lucy Kirkham

Expertise: Head of STEM

Lucy has been a passionate Maths teacher for over 12 years, teaching maths across the UK and abroad helping to engage, interest and develop confidence in the subject at all levels.Working as a Head of Department and then Director of Maths, Lucy has advised schools and academy trusts in both Scotland and the East Midlands, where her role was to support and coach teachers to improve Maths teaching for all.