COMPUTER PROGRAMMING – 9. Repetition
There are three basic building blocks to use when designing algorithms:
What is repetition?
Repetition is often also referred to as loops. In computer programming, repetition is the process of looping or repeating sections of a computer program.
There are different types of loop. The most basic is where a set of instructions is repeated a set number of times. Another type of loop repeats continuously until a certain condition is met.
For example, a very simple algorithm for eating breakfast cereal might consist of these steps:
Repetition in practice: Cleaning teeth
A simple algorithm can be created for cleaning teeth. Suppose a person has ten top teeth. To make sure that every one of the top teeth is cleaned, the algorithm would look something like this:
Steps 2 to 11 are basically the same step repeated, just cleaning a different tooth every time.Repetiton can be used to greatly simplify the algorithm. Look at this alternative:
The second algorithm is much simpler. However, we also need to include a condition for this algorithm to work.
A condition is a situation that is checked every time instructions are repeated. The condition, in this case, will be to check if the number of teeth cleaned equals ten. If that condition is ‘False’ (the number of teeth cleaned is less than ten), then the instructions are repeated. If the condition is ‘True’ (the number of teeth cleaned equals ten), then there would be no more repetition.
Why is repetition important?
Repetition 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.
Repetition using Scratch
Scratch game tutorials:
It is so much fun creating games using Scratch! Practising is the best way to learn how to program.
There are many types of games you can have a go at creating. Click on the links below to go to video tutorials that talk you through some of the most popular ones:
Follow this hyperlink to learn more:-