Iteration in algorithms

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

  • sequencing
  • selection
  • iteration

These building blocks help to describe solutions in a form ready for programming.

What is iteration?

An explanation of iteration, as used in algorithms and programming

Iteration in programming means repeating steps, or instructions, over and over again. This is often called a ‘loop’.

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.

Algorithms consist of instructions 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.

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

  • put cereal in bowl
  • add milk to cereal
  • spoon cereal and milk into mouth
  • repeat step 3 until all cereal and milk is eaten
  • rinse bowl and spoon

Why is iteration important?

Iteration allows algorithms to be simplified by stating that certain steps will repeat until told otherwise. This makes designing algorithms quicker and simpler because they don’t need to include lots of unnecessary steps.