



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




When designing programs, there may be some instructions that need repeating. This is known as iteration, and is implemented in programming using FOR and WHILE statements.
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:
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. Iteration is the process of repeating steps.
For example, a very simple algorithm for eating breakfast cereal might consist of these steps:
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.
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.
There are two ways in which programs can iterate or ‘loop’:
In this example, the variable ‘count’ is used to keep track of how many times the algorithm has iterated. This variable controls the loop. The algorithm will continue to iterate until the value of count has reached 5. As soon as count reaches 5, the algorithm stops iterating.
A condition-controlled loop is so called because iteration continues while, or until, a condition is met. Consider this simple algorithm for entering a correct password:
In this example, the condition is whether or not the inputted password equals “ilovecomputing”. The algorithm tests the condition to see if it is true. If true, the algorithm outputs a message. If false, the algorithm loops back to the beginning and will continue to do so until the tested condition is true.
Sometimes an algorithm needs to iterate steps a specific number of times. In programming, count-controlled loops are implemented using FOR statements. Python uses the statements for and range (note the lowercase syntax that Python uses):