Iteration (AQA GCSE Computer Science)

Revision Note

James Woodhouse

Expertise

Computer Science

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)

  Has been used Has not been used
Sequence    
Selection    
Iteration    

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

  Has been used Has not been used
Sequence    
Selection    
Iteration    

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

// Program to print a multiplication table up to a given number

// Prompt the user to enter a number
PRINT "Enter a number: "
number ← INPUT

// Set the initial value of the counter for the outer loop
outer_counter ← 1

// Outer loop to iterate through the multiplication table
WHILE outer_counter ≤ number DO
    // Set the initial value of the counter for the inner loop
    inner_counter ← 1

    // Inner loop to print the multiplication table for the current number
        WHILE inner_counter ≤ 10 DO
            // Calculate the product of the outer and inner counters
            product ← outer_counter * inner_counter
        
            // Print the multiplication table entry
            PRINT outer_counter, " x ", inner_counter, " = ", product
        
            // Increment the inner counter
            inner_counter ← inner_counter + 1
        END WHILE


    // Move to the next number in the multiplication table
    outer_counter ← outer_counter + 1
END WHILE

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

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

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.