Block-based coding
What you’ll learn
- How block coding works
- Events, sequences and commands in blocks
- Where block coding is used for robots, and when to move to text
Blocks instead of typing
Block editors let you snap together code pieces that look like puzzle blocks. Their shapes only fit where they make sense, so there are no syntax errors, and you can focus on logic.
| Editor | Used with |
|---|---|
| Microsoft MakeCode | BBC micro:bit and many robot add-ons |
| LEGO SPIKE App / Education apps | LEGO SPIKE and Computer Science & AI kits |
| mBlock | Arduino and Makeblock robots |
| Scratch | Games and stories; same ideas |
Key ideas
- Event blocks start code: “on start”, “forever”, “on button A pressed”.
- Command blocks do things: “show icon”, “set motor speed”, “wait 1 second”.
- Control blocks repeat or choose: “repeat 4 times”, “if … then … else”.
- Value blocks (round or pointed) hold numbers, sensor readings and true/false.
Moving to text
Blocks and text code express exactly the same ideas. MakeCode can show your blocks as JavaScript or Python, which is a great bridge. When programs get long, text becomes quicker to write and read.
Example: the same program three ways
Blocks (MakeCode): forever → if light level < 50 then show icon Heart else clear screen
JavaScript (MakeCode text view):
basic.forever(function () { if (input.lightLevel() < 50) { basic.showIcon(IconNames.Heart) } else { basic.clearScreen() }})Python (MakeCode):
def on_forever(): if input.light_level() < 50: basic.show_icon(IconNames.HEART) else: basic.clear_screen()basic.forever(on_forever)Try it in MakeCode
This is Microsoft’s MakeCode for the BBC micro:bit, with a virtual micro:bit on the left. Start a new project and build with blocks; the simulator runs as you edit.
Runs on Microsoft MakeCode, free, no account needed.
Name badgeEasy
On start, scroll your name across the LEDs, then show a smiley.
Button counterMedium
Make a variable count. Button A adds 1 and shows the number; button B resets it to 0.
Peek at the textHard
Build the light-level heart program in blocks, then switch to JavaScript view. Change the threshold in the text and switch back: did the blocks update? Yes. Blocks and text are two views of the same program, which is why blocks are a good bridge to text coding.
Quick check
Finished this lesson?