Iteration

An algorithm is a plan, a set of step-by-step instructions designed to solve a problem. There are three basic building blocks (constructs) to use when designing algorithms:

Algorithms are used to help design programs that perform particular tasks.

What is iteration?

An explanation of iteration, as used in algorithms and programming

Algorithms consist of steps that are carried out (performed) one after another. Sometimes an algorithm needs to repeat certain steps until told to stop or until a particular condition has been met.

curriculum-key-fact
Iteration is the process of repeating steps.
Iteration is the repetition of a sequence. In an arcade game, the sequence is Pop up a new rat, hit the rat increase score. This will continue to iterate as the game is being played.

For example, a very simple algorithm for eating breakfast cereal might consist of these steps:

  1. put cereal in bowl
  2. add milk to cereal
  3. spoon cereal and milk into mouth
  4. repeat step 3 until all cereal and milk is eaten
  5. rinse bowl and spoon

The algorithm will repeat steps 3 and 4 until all the cereal and milk has been eaten.

Why is iteration important?

Iteration allows us to simplify our algorithm by stating that we will repeat certain steps until told otherwise.

This makes designing algorithms quicker and simpler because they don’t have to include lots of unnecessary steps.

Iteration in programming

Once an algorithm has been designed and perfected, it must be translated – or programmed – into code that a computer can read.

We create programs to implement algorithms. Algorithms consist of steps. Programs consist of statements. A statement is a single instruction - in other words, a single step.

Iteration is implemented in programming using FOR and WHILE statements.

In programming, iteration is often referred to as ‘looping’, because when a program iterates it ‘loops’ to an earlier step.