Iteration
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 instructionA single action that can be performed by a computer processor. designed to solve a problem. There are three basic building blocks (constructs) to use when designing algorithms:
- sequencingCreating a set of instructions to complete a task.
- selectionA decision within a computer program when the program decides to move on based on the results of an event.
- iterationIn computer programming, this is a single pass through a set of instructions.
Algorithms are used to help design programSequences of instructions for a computer. that perform particular tasks.
What is iteration?
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 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
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鈥檛 have to include lots of unnecessary steps.
Iteration in programming
Once an algorithm has been designed and perfected, it must be translated 鈥 or programmingThe process of writing computer software. 鈥 into code that a computer can read.
We create programs to implementTo put into effect. 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 鈥榣ooping鈥, because when a program iterates it 鈥榣oops鈥 to an earlier step.