Iteration (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

Iteration

What is iteration?

Iteration is the process of repeating a set of instructions until a specific condition is met. It is an important programming concept and is used to automate repetitive tasks.

There are three main types of iteration:

  • Count-controlled loops
  • Precondition loops
  • Postcondition loops

Count-controlled Loops

  • A count-controlled loop is used when the number of iterations is known beforehand
  • It is also known as a definite loop
  • It uses a counter variable that is incremented or decremented after each iteration
  • The loop continues until the counter reaches a specific value

Example in Pseudocode:

count 1

FOR i <= 10

OUTPUT count

NEXT i

Example in Python:

for count in range(1, 11):

print(count)

Example in Java:

for(int count=1; count<=10; count++){

System.out.println(count);

}

Example in Visual Basic:

For count = 1 To 10

Console.WriteLine(count)

Next count

Pre-condition Loops

  • A precondition loop is used when the number of iterations is not known beforehand and is dependent on a condition being true
  • It is also known as an indefinite loop
  • The loop will continue to execute while the condition is true and will stop once the condition becomes false

Example in Pseudocode:

INPUT temperature

WHILE temperature > 37 DO

OUTPUT "Patient has a fever"

INPUT temperature

END WHILE

Example in Python:

temperature = float(input("Enter temperature: "))

while temperature > 37:

print("Patient has a fever")

temperature = float(input("Enter temperature: "))

Example in Java:

Scanner input = new Scanner(System.in);

float temperature = input.nextFloat();

while (temperature > 37) {

System.out.println("Patient has a fever");

temperature = input.nextFloat();

}

Example in Visual Basic:

temperature = InputBox("Enter temperature")

Do While temperature > 37

Console.WriteLine( "Patient has a fever")

Console.WriteLine("Enter temperature")

temperature=Console.ReadLine()

Loop

Postcondition Loops

  • A post-condition loop is used when the loop must execute at least once, even if the condition is false from the start
  • The condition is checked at the end of the loop

Example in Pseudocode:

REPEAT

INPUT guess

UNTIL guess = 42


Example in Python:

Postcondition loops don’t exist in Python and would need to be restructured to a precondition loop


Example in Java:

Scanner input = new Scanner(System.in);

int guess = 0;

do {

System.out.print("Enter guess: ");

guess = input.nextInt();

} while (guess != 42);


Example in Visual Basic:

Do

Console.WriteLine("Enter guess")

guess = Console.ReadLine()

Loop Until guess = 42

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?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.