Learn the basics

Blocks: when does my program run?

MakeCode programs are blocks that snap together. on start runs once, forever runs again and again, and on button pressed runs when something happens. The same three in Python and C++.

Real MakeCode blocks: on button A pressed shows a smile, forever shows a big and a small heart again and again, on start shows the name SAM once

Blocks: when does my program run?

The outer block decides when the blocks inside run: on start once, forever again and again, on button A pressed each time it happens.

Inside a block, the steps run from top to bottom, one after the other.

Blocks inside on start run once, when the Micro:bit switches on. Then they are done.

Blocks inside forever run from top to bottom, then start again at the top. Round after round.

on button A pressed waits. Each time you press A, its blocks run once. That is an event.

Inside a block, the steps run one after the other, from the top. Change the order, and the screen changes too.

on start runs first. Then forever goes round. Press A: its blocks run in between.

pause waits before the next step. 500 ms is half a second. Without a pause, it blinks too fast to see.

Every language has them: once at the start, again and again, and when something happens. C++ calls them setup and loop.

Press A three times quickly: the presses wait in a line. Each one runs its blocks, one after the other.

Arduino C++ has no button event. loop asks every round: is it pressed now? A long delay can miss a quick tap.

More: tips for your first program video
Your first MakeCode program, from an empty project to the Micro:bit.How to use a micro:bit | Tutorial #1 · Science Buddies · on YouTube
  • A block must click in. A grey, loose block does nothing: drag it until it snaps into place.
  • Find blocks by colour. Basic (blue) shows things, Input (pink) has the buttons and shake, Music (red) plays sounds.
  • Try it first on the screen. The simulator on the left runs your program: click A and B there.
  • Undo with the arrow at the bottom, or press Ctrl and Z.
  • Text in quotes. Click in the white field of show string and type your own words.
BlocksPython (Micro:bit)C++ (Arduino)
onceon startthe lines at the topvoid setup() { }
again and againforeverbasic.forever(on_forever)void loop() { }
when it happenson button A pressedinput.on_button_pressed(Button.A, …)if (digitalRead(2) == LOW) inside loop()