Session 2: Loops & Variables - The Engine of Continuous Motion
Theory & practice
🔁 Lesson 2: Loops and Variables
🎯 Learning objectives
After this lesson, you will be able to:
- Tell the difference between the repeat and forever loops.
- Use loops to make programs shorter.
- Create variables to store counts, scores or speed.
- Combine loops and variables to control a sprite.
1. What is a loop?
Many things in everyday life happen again and again:
- A heart beats continuously.
- A wheel turns many times.
- A warning light blinks on and off.
- A robot checks its sensors over and over.
In programming, a loop runs a group of blocks again without you having to write the same block many times.
For example, instead of using the block move 10 steps ten times, you can write:
when green flag clicked
repeat 10
move 10 stepsThe sprite moves a total of: 10 times × 10 steps = 100 steps
2. Two main kinds of loop
🔢 Repeat a set number of times
The repeat [10] block runs the blocks inside it exactly the chosen number of times, then stops.
when green flag clicked
repeat 4
move 80 steps
turn right 90 degreesThis program moves the sprite around a square.
Use it when:
- You know in advance how many times to repeat.
- You want a light to blink exactly 5 times.
- You want the robot to drive exactly 3 laps.
- You want to draw a shape with a fixed number of sides.
♾️ Repeat forever
The forever block keeps running until you click the red Stop button 🛑.
when green flag clicked
forever
move 5 steps
if on edge, bounceif on edge, bounce is a block in the Motion category. The sprite turns around by itself when it touches the edge of the stage.Use it when:
- The robot must keep checking its sensors.
- A game must keep checking the keyboard.
- A traffic light runs in a cycle.
- The robot must keep moving until it meets an obstacle.
| Loop type | Number of runs | Stops by itself |
|---|---|---|
repeat [ ] | Fixed in advance | Yes |
forever | Unlimited | No |
3. Variables — boxes that store information
A variable is like a labelled box that stores information in your program.
For example, the variable Speed starts at 5:
set [Speed] to [5]To increase the speed by 1:
change [Speed] by [1]The value of the variable changes:
5 → 6 → 7 → 8Some common variables:
Speed: how fast the sprite moves.Score: the player's score.Laps: the number of laps completed.Distance: a reading from a sensor.Time: the time remaining.
4. Combining loops and variables
Program the sprite to speed up after each move:
when green flag clicked
set [Speed] to [2]
repeat 8
move [Speed] steps
change [Speed] by [1]
wait 0.2 secondsEach time round the loop, the sprite moves further:
2 → 3 → 4 → 5 → 6 → 7 → 8 → 9To stop the speed from growing too high, we need the if ... then condition block, which you will learn in Lesson 3. For now, limit the speed by choosing a suitable number of repeats.
5. Practice: the polygon-drawing robot
Task
Use variables so the robot can draw different shapes:
- 1Create the variables
SidesandShapes. - 2The robot draws every side of the shape using a loop.
- 3After each side, the robot turns by
360 ÷ Sidesdegrees. - 4After each shape, increase
Shapesby 1 andSidesby 1.
Suggested program
when green flag clicked
erase all
pen down
set [Sides] to [5]
set [Shapes] to [0]
repeat 3
repeat [Sides]
move 60 steps
turn right (360 ÷ Sides) degrees
change [Shapes] by [1]
change [Sides] by [1]
wait 0.5 secondsThe robot draws a 5-sided, a 6-sided and a 7-sided shape in turn. The variable Shapes shows how many shapes have been drawn.
erase all and pen down blocks are in the Pen extension. Click Extension to add it.if ... then block so the robot knows to stop when it meets an obstacle.6. Common mistakes
Forgetting to set a starting value
Reset your variables every time the program starts:
when green flag clicked
set [Laps] to [0]Putting the change block outside the loop
If the change variable block sits outside the loop, the variable only changes once.
Using forever without a wait
The program may run too fast. Add:
wait 0.1 seconds🧩 Challenge
Build a countdown from 5 to 1:
when green flag clicked
set [Countdown] to [5]
repeat 5
say [Countdown] for 1 seconds
change [Countdown] by [-1]
say "Go!" for 2 secondsCan you change the program to count down from 10?
✅ Remember
repeat [ ]is for when you know how many times to repeat.foreveris for programs that must run non-stop.- Variables store information that can change.
set variablegives it a new value.change variableincreases or decreases the current value.- Loops combined with variables make programs shorter and smarter.
Quick questions
- 1How do loops help a program?
- 2When should you use the
foreverloop? - 3If
Speed = 5and you increase it by2, what is the new value? - 4Why should you limit the speed when controlling a robot?