9E. Repetition

Some problems can be solved in a fixed number of steps, regardless of what the parameters are. For example, squaring an integer only requires doing one multiplication, no matter what that integer is.

Other problems involve a variable number of steps. For example, you can compute 1 + 2 + … + n by performing additions, but the number of steps needed depends on n; if n = 3, computing 1 + 2 + 3 requires two additions, and if n = 5, computing 1 + 2 + 3 + 4 + 5 takes four additions. When a problem specification involves "…", it probably takes a variable number of steps.

Expressing algorithms that take a variable number of steps require some some kind of repetition. With this page, we begin to look at mechanisms for expressing algorithms that require repetition.