Repeating algorithms
In a repeating algorithm a group of instructions is carried out again and again until a condition is met or a fixed number of times. The repeated part is the loop body, and each execution of it is an iteration. There are three kinds of loop. “While” (the condition is checked first): the body runs if the condition is true; if the condition is false from the start, the body never runs. “Until” (the condition is checked after): the body runs first, then the condition is checked and the loop stops when it is true; the body runs at least once. A parameter loop: the parameter changes from A to B, the number of repetitions is B – A + 1 and is known in advance. Repeating algorithms contain instructions like S = S + i (a sum) and P = P · i (a product); the start value is S = 0 for a sum and P = 1 for a product because these do not change the result.
A sports coach planned a drill to repeat each day: “ten times a day for ten days”. A pupil saw this as a loop and worked out the total number of repeats.
Factory inspectors, programmers and coaches write a loop or repeated plan for doing the same job many times.
On paper, draw a table for a one-week habit such as five minutes of reading a day, and write the number of repeats. Check the count by hand.
| i | 1 | 3 | 5 | 7 | 9 |
|---|---|---|---|---|---|
| S | 1 | 4 | 9 | 16 | 25 |
“Counting steps”: the class says S = S + i aloud: one student says i, the next says the new S; the trace table is filled in on the board.