Lesson 3 of 5 · Open
Repeating steps
Write it down once, carry it out as often as needed.
A set of instructions says: Take 5. Add 3. Add 3. Add 3. Add 3. The same thing appears four times. With a hundred repetitions, the instructions would fill pages. And sometimes you do not even know in advance how often a step is needed.
A loop, also called a repetition, writes the steps down only once and says how often they run. The four identical lines become: “repeat 4 times: add 3.” Carrying out all the steps in the loop once is called a pass.
Example
Adding 3 four times
Start with 5, then: repeat 4 times: add 3. The 5 is already there before the loop begins. It is not a pass. Only the passes are counted:
- Pass 1: 5 + 3 = 8
- Pass 2: 8 + 3 = 11
- Pass 3: 11 + 3 = 14
- Pass 4: 14 + 3 = 17. Four passes are done, and the loop ends. Result: 17
Repeating until a condition is true
Often you do not know in advance how many passes are needed. Then the loop depends on a condition, in the form “repeat until” followed by the condition. Before every pass, the condition is checked. If it is false, a pass follows, and then it is checked again. If it is true, the loop ends, and things go on with the next step after it.
Quick check
The loop runs until the number is greater than 100. Right now it is 96. What happens now?
Example
Doubling until the number is greater than 100
Start with 3: repeat until the number is greater than 100: double it. The table below records every pass.
- Before every pass: is the number greater than 100? For 3, 6, 12, 24, 48, and 96 that is false, so it is doubled.
- After pass 6 the number is 192. Now the condition is true, and the loop ends.
- Result: 192 after 6 passes
| Pass | Number | Greater than 100? |
|---|---|---|
| Start | 3 | false |
| 1 | 6 | false |
| 2 | 12 | false |
| 3 | 24 | false |
| 4 | 48 | false |
| 5 | 96 | false |
| 6 | 192 | true |
Some programming languages only check the condition at the end of a pass. Then the loop runs at least once, even if the condition is already true at the start. In this chapter, the check always comes before every pass, as in the example.
A loop that never ends
Start with 4: repeat until the number is 30: add 8. The numbers are 4, 12, 20, 28, 36, 44, and so on. Exactly 30 never comes up, so the condition never becomes true. A loop like this never stops and is called an infinite loop.
Instructions with an infinite loop are no longer an algorithm, because an algorithm is finite. A different condition fixes it: with “until the number is at least 30,” the same loop ends at 36, after 4 passes.
New terms
- loop
- repeats steps: a fixed number of times or until a condition is true. Also called a repetition
- pass
- carrying out all the steps in the loop once. The start does not count
- infinite loop
- a loop whose condition never becomes true. It never stops
Typical mistake
Counting the start as a pass. In the first example that would make 5 passes, and adding 3 five times would give 20 instead of 17. But the starting number is already there before the loop begins. That is why it has no number in the table and is called Start.
Remember
Tasks
Task 1 · Input
Start with 7. Repeat 5 times: add 4. What number do you get?
Task 2 · Order
Start with 6. Repeat until the number is greater than 25: add 13. In what order does it run?
- 1Pass: 6 + 13 = 19
- 2Pass: 19 + 13 = 32
- 3Check: 32 is greater than 25, stop
- 4Check: 19 is not greater than 25
- 5Check: 6 is not greater than 25
Task 3 · Input
Start with 2. Repeat until the number is greater than 150: triple it. How many passes are there?
Go deeper
Task 4 · Input · optional
Your savings account is empty. Every week you put in €30 until there is at least €200 in it. How many weeks does that take?
Task 5 · Choice · optional
Start with 75. Repeat until the number is greater than 60: double it. How many passes are there?
Task 6 · Sorting · optional
Which loop ends, and which runs forever?
6 cards left