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:

  1. Pass 1: 5 + 3 = 8
  2. Pass 2: 8 + 3 = 11
  3. Pass 3: 11 + 3 = 14
  4. 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.

After every pass it goes back to the check. The only way out is through “true.”

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.

  1. Before every pass: is the number greater than 100? For 3, 6, 12, 24, 48, and 96 that is false, so it is doubled.
  2. After pass 6 the number is 192. Now the condition is true, and the loop ends.
  3. Result: 192 after 6 passes
PassNumberGreater than 100?
Start3false
16false
212false
324false
448false
596false
6192true

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

A loop repeats steps: a fixed number of times or until its condition is true. If the condition never becomes true, the loop never stops.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Input

Start with 7. Repeat 5 times: add 4. What number do you get?

Enter the result.

Result =

Task 2 · Order

Start with 6. Repeat until the number is greater than 25: add 13. In what order does it run?

Drag the rows into the right order.

  1. 1Pass: 6 + 13 = 19
  2. 2Pass: 19 + 13 = 32
  3. 3Check: 32 is greater than 25, stop
  4. 4Check: 19 is not greater than 25
  5. 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?

Enter the result.

passes

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

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?

Enter the result.

weeks

Task 5 · Choice · optional

Start with 75. Repeat until the number is greater than 60: double it. How many passes are there?

One answer is right.

Task 6 · Sorting · optional

Which loop ends, and which runs forever?

Drag every card into its group, or tap the group.

6 cards left

From 1, double until the number is less than 1

Back to the chapter