TactivergeLite

Lite / Discovering How the World Works / Week 23

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 · Reviewed by the founders' mentors · Full interactive version

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.

Predict

x starts at 2. A loop runs 'x = x + 3' four times. What is x at the end?

  1. 5
  2. 14
  3. 12
Guess first, then reveal

14. Each pass adds 3: 2 → 5 → 8 → 11 → 14. Four passes add 4 × 3 = 12 in total, and 2 + 12 = 14. A trace table makes this easy to check.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Programming Basics: Statements & Functions: Crash Course Computer Science #12” by CrashCourse (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

'Do 10 star jumps' is a loop. You don't need ten separate instructions; you need one instruction and a count.

Technical

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

Mathematical

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.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 4. x starts at 7. A loop says 'repeat 0 times: x = x + 5'. What is x afterwards?

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.

Reflect

What in your life is a 'repeat N times' loop? How do you keep count?

Write your answer on paper or in your own notes. Lite never stores what you write.

← AND, OR, NOT: combining conditionsRepeat until: loops that decide when to stop →