Iterations

KS3 Computer Science

11-14 Years Old

48 modules covering EVERY Computer Science topic needed for KS3 level.

GCSE Computer Science

14-16 Years Old

45 modules covering EVERY Computer Science topic needed for GCSE level.

A-Level Computer Science

16-18 Years Old

66 modules covering EVERY Computer Science topic needed for A-Level.

GCSE Programming Resources (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Problem solving and programming (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

Iteration is the term given to the repetition of a block of instructions (code) within a computer program for a number of instances or until status is encountered.  When the first group of instructions is carried out again, it is called an iteration.  When a cycle of instructions is carried out in a repeated manner, it is called a loop.  It is the replication of a process in a computer program, commonly executed with the use of loops.

Iteration is essential as it lets a programmer streamline a design by declaring that definite steps will be repeated.  It is also briefer since a number of irrelevant steps are removed.  Steps that are part of the loop are indented.  Indentation is used to show which steps are to be repeated.  Many computer programs use iterations to execute specific tasks, resolve problems and provide solutions.

Types of Iteration

There are two types of iteration:

Count-controlled loops

– used for iterating steps a specific number of times. It is used when the number of iterations to take place is already known.  It uses a counter to keep track of how many times the set of commands has been repeated.

Count-controlled loops are executed using FOR statements.

  • for – specifies the starting point of the iteration
  • range – indicates how many times the program will iterate

Count Controlled Loop Example

total = 0
for count in range (4):
number = int (input (“Type in a number: “))
total = total + number
print (“The total is: “)
print (total)

A count controlled loop iteration example

The above program works as follows:

  • The variable ‘count’ is used to keep track how may times the iteration happened.
  • The ‘for’ statement is used to determine where the loop starts.
  • The ‘range’ declaration signifies the number of instances the loop is to occur.
  • The variable ‘count’ regulates the repetition.
  • In every repetition, program consequently adds 1 to the value of ‘count’.
  • The program continues to iterate if the value of ‘count’ is in the range declared in the loop. Once it gets to the end of the range, the repetition stops.

In the above example, the program repeats 4 times.  To have a different number of repetitions, the value ’4’ would simply have to be changed to the number of times preferred.

Condition-controlled loops

– used for iterating steps continuously until a condition is met. It is used when the number of iterations to take place is unknown.  The algorithm tests the condition to see if it is true.  If true, the algorithm executes a command.  If false, the algorithm goes back to step 1 and will continue to go back until the condition becomes true.

Condition-controlled loops are executed using WHILE statements.

Condition Controlled Loop Example

total = 0
answer = “yes”
while answer = “yes”:
number = int (input (“Type in a number: “))
total = total + number
answer = input (“Any more numbers? yes/no “)
print (“The total is: “)
print (total)

Code example

The above program works as follows:

  • The ‘while’ statement is used to indicate where the iteration starts.
  • The condition ‘answer = “yes”’ is used to manage the iteration. It must be set to “yes” at the start so the code runs at least once.
  • The program assesses the condition by checking if the variable ‘answer’ equals “yes”.
  • If the condition is true, the program repeats.
  • If the condition is false, the program progresses to the next step.

The program iterates as many times as is necessary and will keep iterating as long as the condition is met.
There are instances where a condition-controlled loop loops forever.  This is called an infinite loop.  An infinite loop ensures that a program runs forever which can be used in controlling a traffic light and other similar scenarios.

Further Readings:

Iterations are the repetition of a block of instructions within a computer program for a number of instances or until status is encountered.