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
- Program = sequence + loops + conditions (+ variables)
- Nested loops: inner blocks run outer count x inner count times
- Loop 'repeat n [ b blocks ]' runs n x b blocks
- Robot cycle: sense -> decide -> act
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
- Putting a block outside the loop when it should be inside. Check which blocks are indented inside the loop shape.
- Forgetting to change the variable. If steps is never changed, the spiral stays a square.
- Mixing up 'turn right' (a turn in place) with 'move right' (a slide).
- Thinking the robot knows what you mean. It follows the blocks literally, so test and fix one block at a time.