Iteration in algorithms
An algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. 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 programmingThe process of writing computer software..
What is iteration?
Iteration in programming means repeating steps, or instructionA single action that can be performed by a computer processor., over and over again. This is often called a 鈥榣oop鈥.
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 conditionIn computing, this is a statement or sum that is either true or false. A computation depends on whether a condition equates to true or false. has been met.
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鈥檛 need to include lots of unnecessary steps.