Home · Academy · Robotics & Coding · Algorithms · Loops: Making Repetitive Work Easier

Loops: Making Repetitive Work Easier

Learn to automate repeated steps with loops — both fixed-count and condition-based repetition.

LESSON COMPASS

What will you use this page for?

Core idea

A loop lets us repeat the same steps many times without rewriting them. A fixed-count (for) loop is used when we know the number of repetitions in advance. A conditional (while) loop runs as long as a condition holds and depends on its stopping condition. A counter is a variable that tracks which pass the loop is on. Infinite loops and off-by-one errors are…

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Infinite loops If the stopping condition of a while loop is never met, the program never stops. This is called an infinite loop . let step be 0 Repeat while step is less than 50 move forward 1 step In this code step never increases, so it stays 0 and 0 < 50 is always true. The loop runs forever. The fix: always…

Next connection

Variables: Storing Information

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration25–35 min
PrerequisiteConditions
ContentStandard lesson · 1,342 words
Last updated

In one sentence

A loop is a structure that lets a computer or robot repeat the same steps many times, without us writing those steps over and over.

Why does it matter?

Imagine you want a character to draw a square with 100-step sides. You could write the same two commands ("move forward", "turn") four separate times. But what if you want a robot to move forward 50 steps? Writing the same command 50 times is tiring and easy to get wrong.

This is exactly where a loop helps. By saying "repeat these steps N times," we describe a long job with one small block. The code becomes shorter, easier to read, and if we want to change something, fixing a single line is enough.

In the previous lesson we learned about conditions: the program looked at a situation and made a decision. Loops often work together with conditions, because a condition is what decides when a loop should stop.

Fixed-count loops: "repeat N times"

If we already know how many times we need to repeat, we use a fixed-count loop. In coding this is usually called a for loop.

Example: A character drawing a square

A square has four sides, and at each corner you turn 90 degrees. We repeat the same two steps four times.

Repeat 4 times
  move forward 100 steps
  turn right 90 degrees

If we change the count and the angle, we get other shapes. For a triangle we repeat 3 times and turn 120 degrees. For a hexagon we repeat 6 times and turn 60 degrees. The rule is: the turning angle is 360 divided by the number of sides.

What is a counter?

A loop may want to know which repetition it is on. The variable that holds this number is called a counter. A counter usually starts at 0 or 1 and increases by one on each repetition.

let counter be 1
Repeat 5 times
  display "Step number: " and the counter
  increase the counter by 1

This loop prints "Step number: 1", "Step number: 2", and so on up to 5.

A for loop in Python

In Python, a fixed-count loop is written with for and range. range(4) gives us the numbers 0, 1, 2, 3, which means four repetitions.

# Make the robot move and turn 4 times (a square)
for side in range(4):
    print("Move forward 100 steps")
    print("Turn right 90 degrees")

Here side is a counter, taking values from 0 to 3 on each pass.

Conditional loops: "until a condition is met"

Sometimes we do not know in advance how many times to repeat. In a task like "move forward until you see an obstacle," the loop continues as long as a condition is true. In coding this is called a while loop.

Example: A robot moving until it reaches an obstacle

Repeat while the distance is greater than 10 centimetres
  move forward 1 step
  measure the distance again

The robot measures the distance in front of it on every step. When the distance drops to 10 centimetres, the condition is no longer met and the loop stops on its own. We never had to know the exact number of steps in advance.

A while loop in Python

# Make the robot move forward 50 steps (controlled by a counter)
step = 0
while step < 50:
    print("Move forward 1 step")
    step = step + 1

The variable step starts at 0. It increases by 1 each pass. When step reaches 50, the condition step < 50 becomes false and the loop ends. Exactly 50 steps are taken.

Nested loops

We can put one loop inside another. This is called a nested loop. On each of its passes, the outer loop runs the entire inner loop.

Example: A 3-by-3 grid of dots

Repeat 3 times   (rows)
  Repeat 3 times   (columns)
    place a dot and move sideways
  move down one row and return to the start

The outer loop runs 3 times, and the inner loop runs 3 times on each of those. In total, 3 × 3 = 9 dots are placed. Nested loops are very useful for tables, grids and anything arranged in rows and columns.

Mini practice

Write a loop that makes a character draw a pentagon (a 5-sided shape).

  1. How many times do you need to repeat?
  2. How many degrees should you turn at each corner? (Hint: divide 360 by the number of sides.)
  3. Write your pseudocode, then translate it into Python if you like.

When you are done, change the repeat count from 5 to 8 and recalculate the turning angle. How did the shape change?

Common mistakes

Infinite loops

If the stopping condition of a while loop is never met, the program never stops. This is called an infinite loop.

let step be 0
Repeat while step is less than 50
  move forward 1 step

In this code step never increases, so it stays 0 and 0 < 50 is always true. The loop runs forever. The fix: always increase the counter inside the loop.

Off-by-one (one too few or one too many)

A very common mistake is a loop that runs one time too few or one time too many. If a character repeats 3 times instead of 4 when drawing a square, one side is missing. With range, remember that range(1, 5) does not include 5; it gives 1, 2, 3, 4.

A simple check: after the loop, count how many passes it made with a small example to confirm.

Safety note

When you use a while loop on a real robot, the motors may keep running without stopping as expected. For this reason, always leave a safe way to stop: a stop button, a time limit, or an upper bound with a counter. Do experiments involving motors and batteries together with an adult, and run the robot in a soft, open, empty area on the first try.

Review questions

  1. What problem does a loop solve better than copying the same instruction many times?
  2. How do a fixed-count loop and a condition-controlled loop differ?
  3. What evidence would show that a loop has an off-by-one error?
  4. Why must a condition-controlled loop have a realistic way to stop?
  5. How would you test a loop that processes every sensor reading in a list?
  6. When is repetition clearer as a function call rather than one very large loop?

Answers

  1. A loop expresses repeated behaviour once, making the program shorter, easier to change and less likely to contain inconsistent copies.
  2. A fixed-count loop repeats a known number of times; a condition-controlled loop continues while or until a logical condition changes.
  3. The action happens one time too many or one time too few, often visible at the first or last item in a test sequence.
  4. Without a stop path the program can run forever, ignore new work or keep a motor active longer than intended.
  5. Use an empty list, one reading, several normal readings and a boundary value, then confirm that each item is processed exactly once.
  6. A function is clearer when the repeated operation has its own purpose, inputs and result and is reused in more than one place.

Lesson summary

Check your understanding

  1. What is the main difference between a fixed-count loop and a conditional loop?
  2. To make a character draw a hexagon, how many times must the loop repeat and how many degrees should it turn at each corner?
  3. What is a counter and what is it used for?
  4. Why does the while loop below run forever?
let step be 0
Repeat while step is less than 10
  display "move forward"
  1. How many times does the line for number in range(3): run the loop?

Answers

  1. In a fixed-count loop we know the number of repetitions in advance. In a conditional loop the loop continues as long as a condition is true, and we do not know beforehand how many times it will run.
  2. It must repeat 6 times and turn 60 degrees (360 ÷ 6) at each corner.
  3. A counter is a variable that tracks which pass the loop is on. It usually increases by one each repetition, letting us count the passes.
  4. Because the value of step never increases inside the loop. Since it stays 0, the condition step is less than 10 is always true and the loop never stops. The value of step must be increased inside the loop.
  5. 3 times. range(3) produces the values 0, 1, 2, which means three repetitions.

Source and verification note

For “Loops: Making Repetitive Work Easier”, verification focuses on whether the relationship between In one sentence and Example: A character drawing a square remains consistent across examples. The algorithms in this lesson are checked by tracing sample inputs by hand and comparing them with expected outputs. Pseudocode is used to make the reasoning sequence visible without tying it to one programming language.

Next lesson

Variables: Storing Information

Start QuizBack to Algorithms
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.