Variables

header-lesson-01

Variables#

What is a variable?#

In computer science, a variable is like a container that holds information. This information can be a number, a word, or something else. For example, if you’re writing a game, you might have a variable to keep track of the player’s score. Every time the player scores a point, the computer changes the number in that variable. Variables help computers remember things while running a program, making it possible to do all kinds of tasks, like simple math, keeping track of a game’s progress, or storing sensor data.

To create a variable, select “Variables” in the Toolbox.

makecode-variables-01

Click on “Make a Variable”.

makecode-variables-02

A window will appear with a text box and an “Ok” button.

makecode-variables-03

Enter your “name” in the “New variable name” box, and press “Ok”.

makecode-variables-04

A new set of blocks related to the variable you created will appear.

makecode-variables-05

Select the set block.

makecode-variables-06

Place the set box in the forever block.

makecode-variables-07

Move the forever block.

makecode-variables-08

It is now visible without obstruction.

makecode-variables-09

You now need to add a string to the oval with a 0. Select “Advanced” in the Toolbox.

makecode-variables-10

Next, click on “Text” to see the blocks that are used for strings.

makecode-variables-11

Select the empty string " " block.

makecode-variables-12

Place the empty string " " block in the oval in the set block. Change the string to your name.

makecode-variables-13

Now click on the “Basic” drawer of the Toolbox.

makecode-variables-14

Select the show string block.

makecode-variables-15

Drag the show string block into the forever block.

makecode-variables-18

Click on the “Variables” drawer of the Toolbox.

makecode-variables-19

Select the variable name

makecode-variables-20

Place the variable block name in the show string block.

makecode-variables-21

The code is now ready to be flashed on the micro:bit.