Session 245 minutes

Session 2: Loops & Variables - The Engine of Continuous Motion

Learning objective: Mastering forever and repeat loops, and declaring state variables like Score and Velocity.

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:

text
when green flag clicked
repeat 10
    move 10 steps

The sprite moves a total of: 10 times × 10 steps = 100 steps

2. Two main kinds of loop

Mục Trọng Tâm

🔢 Repeat a set number of times

The repeat [10] block runs the blocks inside it exactly the chosen number of times, then stops.

text
when green flag clicked
repeat 4
    move 80 steps
    turn right 90 degrees

This 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.
Mục Trọng Tâm

♾️ Repeat forever

The forever block keeps running until you click the red Stop button 🛑.

text
when green flag clicked
forever
    move 5 steps
    if on edge, bounce
if 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 typeNumber of runsStops by itself
repeat [ ]Fixed in advanceYes
foreverUnlimitedNo

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:

text
set [Speed] to [5]

To increase the speed by 1:

text
change [Speed] by [1]

The value of the variable changes:

text
5 → 6 → 7 → 8

Some 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.
💡 Give variables meaningful names so your program is easy to read and fix.

4. Combining loops and variables

Program the sprite to speed up after each move:

text
when green flag clicked
set [Speed] to [2]

repeat 8
    move [Speed] steps
    change [Speed] by [1]
    wait 0.2 seconds

Each time round the loop, the sprite moves further:

text
2 → 3 → 4 → 5 → 6 → 7 → 8 → 9

To 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

Mục Trọng Tâm

Task

Use variables so the robot can draw different shapes:

  1. 1Create the variables Sides and Shapes.
  2. 2The robot draws every side of the shape using a loop.
  3. 3After each side, the robot turns by 360 ÷ Sides degrees.
  4. 4After each shape, increase Shapes by 1 and Sides by 1.
Mục Trọng Tâm

Suggested program

text
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 seconds

The 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.

💡 The erase all and pen down blocks are in the Pen extension. Click Extension to add it.
In Lesson 3, you will learn the if ... then block so the robot knows to stop when it meets an obstacle.

6. Common mistakes

Mục Trọng Tâm

Forgetting to set a starting value

Reset your variables every time the program starts:

text
when green flag clicked
set [Laps] to [0]
Mục Trọng Tâm

Putting the change block outside the loop

If the change variable block sits outside the loop, the variable only changes once.

Mục Trọng Tâm

Using forever without a wait

The program may run too fast. Add:

text
wait 0.1 seconds

🧩 Challenge

Build a countdown from 5 to 1:

text
when green flag clicked
set [Countdown] to [5]

repeat 5
    say [Countdown] for 1 seconds
    change [Countdown] by [-1]

say "Go!" for 2 seconds

Can you change the program to count down from 10?

✅ Remember

  • repeat [ ] is for when you know how many times to repeat.
  • forever is for programs that must run non-stop.
  • Variables store information that can change.
  • set variable gives it a new value.
  • change variable increases or decreases the current value.
  • Loops combined with variables make programs shorter and smarter.
Mục Trọng Tâm

Quick questions

  1. 1How do loops help a program?
  2. 2When should you use the forever loop?
  3. 3If Speed = 5 and you increase it by 2, what is the new value?
  4. 4Why should you limit the speed when controlling a robot?

Ready to test your knowledge?

Open the dedicated exam room to answer questions and review your results.

Part 1 Mastery Quiz: Scratch Logic & Core Blocks →14 questions · 15 minutes