Repeat N times
A robot must draw a square. You could write eight instructions. Could you write it in three?
About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.Why should I care?
Machines are brilliant at doing the same thing again and again without getting bored or making a mistake. Loops are how you tell them to. They make programs shorter, easier to read and easier to fix: change one line inside the loop and every repeat changes.
- ◇ Use a 'repeat N times' loop to shorten a program.
- ◇ Trace a loop with a trace table and predict its final value.
- ◇ Calculate a loop's result with multiplication.
2.Predict
x starts at 2. A loop runs 'x = x + 3' four times. What is x at the end?
Guess first. Being wrong here is how the surprise works.
3.Discover
In 'Repeat N times' mode, set x to start at 2, add 3, repeat 4 times. Press Next pass one at a time and fill in the trace table. Then try adding a negative number.
Loop tracer
A loop repeats the same instructions. Step through one pass at a time and fill in the trace table, the way engineers check a loop by hand.
x = 0
repeat 4 times:
x = x + 3| Pass | x before | x after |
|---|---|---|
| No passes run yet. | ||
After 0 passes, x = 0.
Can't use the interactive? Read a description instead
The loop tracer can be replaced by this trace table for x = 2, repeat 4 times x = x + 3: pass 1, 2 → 5; pass 2, 5 → 8; pass 3, 8 → 11; pass 4, 11 → 14. Final x = 14 = 2 + 4 × 3.
4.Watch
→ Watch for the for loop: code that runs a set number of times.
Walks through if-statements, while loops and for loops with a simple game, the same building blocks this term uses.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
5.Understand
Write it once, run it many times
A square is 'Forward, Turn right' four times. Instead of eight lines, write: repeat 4 times: Forward, Turn right. The instructions inside the loop are called the loop's body.
Variables change each pass
A variable is a named box holding a value, like x = 2. A loop body like x = x + 3 means 'take what's in x, add 3, put it back'. After each pass, x has changed.
Trace tables
To check a loop by hand, draw a table: pass number, value before, value after. Fill one row per pass. This is exactly how professional programmers check tricky loops.
The shortcut
If the body adds the same number every time: final = start + N × step. A trace table and the formula should always agree; if they don't, one of them has a mistake.
Explain it another way
'Do 10 star jumps' is a loop. You don't need ten separate instructions; you need one instruction and a count.
A counted loop runs its body a fixed number of times, usually with a counter variable from 1 to N. In Python it looks like: for i in range(4): x = x + 3
Adding step s, N times, to a start value a gives a + N·s. With a = 2, s = 3, N = 4: 2 + 4 × 3 = 14. If s is negative, x goes down: a = 10, s = −2, N = 3 gives 10 − 6 = 4.
A 3D printer builds an object as a loop: repeat for every layer: trace this layer, move up 0.2 mm. A 10 cm tall print is 500 passes of the same loop.
Worked example 1
Write a loop that makes the grid robot walk in a square with sides of 2 squares.
repeat 4 times: Forward 1, Forward 1, Turn right. Each pass draws one side and turns the corner. After 4 passes it's back where it started, facing the same way.
Common mix-ups
- x = x + 3 is impossible because x can't equal x + 3.
✓ In programs, = means 'store'. It works out the right side and stores it in x. - A loop runs its body once and then repeats the last line.
✓ The whole body runs on every pass.
6.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1 · a fresh version every time
x starts at 8. A loop runs "x = x + 2" exactly 6 times. What is x when the loop ends?
Practice 2 · a fresh version every time
x starts at 2. A loop runs "x = x + -1" exactly 4 times. What is x when the loop ends?
Practice 3
Without a loop, how many instructions does it take to drive a square with 'Forward 1, Turn right' for each side?
Practice 4
x starts at 7. A loop says 'repeat 0 times: x = x + 5'. What is x afterwards?
7.Apply it
Write a loop for the grid robot that makes a staircase: three steps up and to the right. Then trace it by hand and write down the robot's position after each pass.
8.Reflect
Concepts: Decisions and loops · Variables, types and expressions