📘 CodingMarble Learn

Block Programming: Algorithms, Loops, Variables, Events and Sensors

In block programming you snap coloured blocks together to build a program. An algorithm is the plan; the program is the plan written in blocks. Loops repeat blocks, variables store changing values, conditions choose between actions, events start the program, and sensors feed the robot facts about the world. Custom blocks (subprograms) give a name to a group of blocks so you can reuse it.

🎬 Step-by-step story

  1. Here is a robot on a grid and a green flag. Goal: reach the flag. The robot has no brain of its own: it only obeys blocks.
  2. Sequence: forward, forward, forward, turn right, forward, forward. Blocks run top to bottom, one by one. Watch the blue trail.
  3. Loop: repeat 4 times [forward 2, turn right]. Only two blocks, but the robot draws a full square. A loop saves typing.
  4. Variable: steps starts at 1. Go forward 'steps', then add 1 to steps. Because the number changes, the path becomes a spiral.
  5. Condition and sensor: the wall sensor asks 'wall ahead?'. If yes, turn right, else go forward. The program starts when the green flag is pressed: that is an event.
  6. Free play: choose how many times to repeat [forward 2, turn right] and press Run. Try 2, 3, 4 and 6 and predict the shape first.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why does the robot not just 'know' where the flag is?

A robot has no understanding of its own. It follows only the blocks you give it. Everything it knows must come from blocks or from a sensor.

Is a loop really better than typing blocks again and again?

Yes. It is shorter and a mistake is fixed in one place. The square in step 2 needs two blocks inside a loop, not eight outside.

Why does the path change when only the variable changes?

The variable decides how far the robot goes each time. When it grows, each side is longer, so the path spirals.

What is the difference between a condition and an event?

An event starts a script (green flag, key press). A condition is a question the script asks while running (is a wall ahead?).

What if there are two walls in my path?

The same loop with the same condition handles them all, because it checks the sensor again every pass. This is why loops with conditions are powerful.

Which repeat number draws a closed square?

4, because each pass turns right 90 degrees and four turns make 360 degrees. Try 2 and 3 in the free play and see open shapes.

Algorithm and program

An algorithm is a list of clear steps that solves a problem. A program is an algorithm written so that a computer or robot can run it. In block programming each instruction is a coloured block; blocks snap together like bricks, so there are no spelling mistakes (syntax errors).

To modify an existing program: run it, watch what goes wrong, change one block, run again. Change only one thing at a time so you know which change fixed or broke the result. This habit is called debugging.

Sequence: blocks run in order

The simplest program is a sequence: block 1, then block 2, then block 3. The order matters. 'Forward, then turn' is not the same as 'turn, then forward'. The 3D robot shows this: the same blocks in a different order end on a different square.

Loops and nested loops

A loop repeats the blocks inside it. 'Repeat 4 [forward 2, turn right]' makes a square. This is shorter than writing eight blocks and easier to fix: change the 2 once, not four times.

A nested loop is a loop inside another loop. Example: repeat 3 times [ repeat 4 times [forward 2, turn right], forward 4 ] makes three squares in a row. The inner loop runs completely each time the outer loop runs once, so the inner blocks run 3 x 4 = 12 times.

Several loops one after another are also common: first loop draws a square, second loop draws a triangle.

Variables

A variable is a named box that stores a value which can change, such as steps, score or lives. Blocks: set steps to 1, change steps by 1, and a round block showing the value of steps.

Example: set steps to 1; repeat 4 [forward (steps), turn right, change steps by 1]. The robot goes 1, 2, 3, 4 cells and draws a spiral.

Conditions, events and sensors

A condition chooses what to do: if wall ahead then turn right else go forward. The question has only two answers, true or false.

An event is something that starts a script: the green flag, a key pressed, a button, a message. Programs that wait for events are called event-driven.

A sensor measures the world (distance, light, touch, temperature). An actuator does something in the world (motor, light, buzzer). A robot program loops: read the sensor, decide with a condition, drive the actuator. For example a line-following robot reads the light sensor 50 times a second and steers the motor.

To improve behaviour of a system, test it, find a case where it fails (wall in a new place) and add a condition for it.

Custom blocks and subprograms

A custom block (subprogram, function) gives a name to a group of blocks. Define square once as 'repeat 4 [forward 2, turn right]', then use it anywhere. If you fix the square, every use is fixed. Custom blocks can take inputs, for example square (size).

Why use them? Shorter programs, fewer repeated mistakes, easier to read: 'square, move, square' says what the robot does.

Key formulas and definitions

Worked examples

1. What shape does 'repeat 4 [forward 2, turn right 90 degrees]' draw?

A square with sides of 2 cells. Four sides, four right turns of 90 degrees = 360 degrees, so the robot ends where it began.

2. How many 'forward' blocks run in 'repeat 3 [ repeat 4 [forward 1, turn right], forward 3 ]'?

Inner loop: 4 forwards, run 3 times = 12. Plus 3 forwards of 'forward 3' = 3. Total 15 forward blocks (12 + 3).

3. set steps to 1; repeat 4 [forward (steps), turn right, change steps by 1]. How many cells does the robot travel in total?

Steps take the values 1, 2, 3, 4. Total = 1 + 2 + 3 + 4 = 10 cells.

4. A robot repeats forever: if wall ahead then turn right else forward. It starts facing a wall. What does it do first?

It turns right, because the sensor says wall ahead = true. Then it checks again; if no wall is ahead now, it moves forward.

5. A program draws two squares one after another using 8 blocks each. Show how a custom block makes it shorter.

Make a custom block 'square' = repeat 4 [forward 2, turn right] (3 blocks). Then the main program is just 'square, forward 3, square' (3 blocks). Short and easy to fix.

6. The robot should stop at the flag, not walk past it. Which block types do you add?

A sensor (colour or distance) to detect the flag, and a condition: if flag seen then stop, else forward, inside a loop.

Common mistakes

Practice quiz

1. A loop is used to:
2. A variable:
3. Pressing the green flag to start a script is an:
4. Which part of a robot measures distance?
5. A custom block is useful because it:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is block programming?

A way to write programs by snapping visual blocks together, as in Scratch, instead of typing code. It teaches the same ideas: sequence, loops, variables, conditions and events.

What is the difference between an algorithm and a program?

An algorithm is the plan in steps. A program is that plan written in a language, or blocks, that a computer can run.

What are sensors and actuators?

A sensor measures something (light, distance, touch). An actuator does something (a motor, buzzer or light). A robot reads sensors and drives actuators.

Where this is taught

FranceQuatrièmeAlgorithms and programming
FranceQuatrièmeComputing and programming
FranceTroisièmeAlgorithms and programming
FranceTroisièmeComputing and programming

Learn first

Learn next

Related lessons

All Computer Science lessons