© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_9

9. Using Variables

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

This chapter presents how to create variables to hold various types of data such as numbers, strings, arrays, and Boolean values. It also presents how to take data out from the variables for further processing, and how to change data stored in the variables during the execution of the code.

9-1. Creating Integer Variables

Problem

You want to create a variable and store the value 100 in it, then display the content on the micro:bit display.

Solution

  • In the Toolbox, click on the Variables category and then click the Make a Variable... button (Figure 9-1 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig1_HTML.jpg
    Figure 9-1.

    The Variables toolbox

  • In the New Variable name: window, type x as the variable name . Then click on the Ok button (Figure 9-2 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig2_HTML.jpg
    Figure 9-2.

    Creating a new variable

  • In the Toolbox, click on the Variables category again. Then click and drag the set x to block over, and place it inside the on start block (Figure 9-3 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig3_HTML.jpg
    Figure 9-3.

    Placing a set variable to block inside the on start block

  • Type the value 100 in the text box of the set x to block (Figure 9-4 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig4_HTML.jpg
    Figure 9-4.

    Assigning a value to a variable

  • In the Toolbox, click the Basic category. Then click and drag the show number block over, and place it inside the on start block just below the set x to block.

  • In the Toolbox, click the Variables category. Then click and drag the variable x block over, and place it inside the placeholder of the show number block (Figure 9-5 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig5_HTML.jpg
    Figure 9-5.

    Placing a variable into the show number block

  • Once completed, your code should look something like this (Figure 9-6 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig6_HTML.jpg
    Figure 9-6.

    Full code listing

How It Works

When you create a variable, MakeCode doesn’t know the type of the variable, until you first assign data to it. MakeCode supports the following standard data types :
  • Number

  • String

  • Array

  • Boolean

In the above example, the name of the variable x is known as the operand . When you store the number 100 as the initial value in the variable x, it will implicitly declare as an integer variable. MakeCode supports the following numerical types.
  • int (includes signed and unsigned integers)

  • float (including floating-point real values, signed and unsigned floating-point numbers)

Figure 9-7 shows how to assign a signed integer (negative integer value) to a variable by typing with a minus sign in front of it.
../images/474604_1_En_9_Chapter/474604_1_En_9_Fig7_HTML.jpg
Figure 9-7.

Assigning a negative number to a variable

9-2. Creating Float Variables

Problem

You want to create a variable and store the value 10.1 in it, then display the content on the micro:bit display .

Solution

  • In the Toolbox, click on the Variables category and then click the Make a Variable... button (Figure 9-8 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig8_HTML.jpg
    Figure 9-8.

    The Variables toolbox

  • In the New Variable name: window , type y as the variable name. Then click on the Ok button (Figure 9-9 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig9_HTML.jpg
    Figure 9-9.

    Creating a variable

  • In the Toolbox, click on the Variables category again. Then click and drag the set y to block over, and place it inside the on start block (Figure 9-10 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig10_HTML.jpg
    Figure 9-10.

    Placing a set variable to block inside the on start block

  • Type the value 10.1 in the text box of the set y to block (Figure 9-11 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig11_HTML.jpg
    Figure 9-11.

    Assigning a value to a variable

  • In the Toolbox, click the Basic category. Then click and drag the show number block over, and place it inside the on start block just below the set y to block.

  • In the Toolbox, click the Variables category. Then click and drag the variable y block over, and place it inside the placeholder of the show number block (Figure 9-12 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig12_HTML.jpg
    Figure 9-12.

    Placing a variable into the show number block

  • Once completed, your code should look something like this (Figure 9-13 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig13_HTML.jpg
    Figure 9-13.

    Full code listing

How It Works

Numbers created using a float variable declaration will have digits on both sides of a decimal point. When you first assign a decimal value to a variable, it will implicitly declare as a decimal variable. The decimal value could be unsigned or signed. Figure 9-14 shows how to assign a signed decimal value (negative decimal value) to a variable by typing with a minus sign in front of.
../images/474604_1_En_9_Chapter/474604_1_En_9_Fig14_HTML.jpg
Figure 9-14.

Assigning a negative number to a variable

9-3. Creating String Variables

Problem

You want to create a variable and store the string “micro:bit” in it, then display the content on the micro:bit display .

Solution

  • In the Toolbox, click on the Variables category and then click the Make a Variable... button (Figure 9-15 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig15_HTML.jpg
    Figure 9-15.

    The Variables toolbox

  • In the New Variable name : window, type y as the variable name. Then click on the Ok button (Figure 9-16 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig16_HTML.jpg
    Figure 9-16.

    Creating a variable name

  • In the Toolbox, click on the Variables category again. Then click and drag the set hardware to block over, and place it inside the on start block (Figure 9-17 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig17_HTML.jpg
    Figure 9-17.

    Placing a set variable to block inside the on start block

  • In the Toolbox, click on the Text category. Then click and drag the text block over , and place it inside the placeholder of the set hardware to block (Figure 9-18 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig18_HTML.jpg
    Figure 9-18.

    Placing a text block into the set variable to block

  • Type the string micro:bit in the text block of the set hardware to block (Figure 9-19 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig19_HTML.jpg
    Figure 9-19.

    Assigning a text to a variable

  • In the Toolbox, click the Basic category. Then click and drag the show string block over, and place it inside the on start block just below the set hardware to block.

  • In the Toolbox, click the Variables category. Then click and drag the variable hardware block over, and place it inside the placeholder of the show string block (Figure 9-20 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig20_HTML.jpg
    Figure 9-20.

    Placing a variable into the show string block

  • Once completed, your code should look something like this (Figure 9-21 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig21_HTML.jpg
    Figure 9-21.

    Full code listing

How It Works

By default, the set variable to block only accepts numbers and doesn’t allow you to type strings in its input box. As a solution, you can replace the default input box with a text block from the Text category. This will allow the set variable to block to accept and hold any string .

9-4. Creating a Variable to Hold an Array of Numbers

Problem

You want to create a variable to hold an array of scores and find the score at index 4.

Solution

  • In the Toolbox, click on the Variables category and then click the Make a Variable... button (Figure 9-22 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig22_HTML.jpg
    Figure 9-22.

    The Variables toolbox

  • In the New Variable name: window, type scores as the variable name. Then click on the Ok button (Figure 9-23 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig23_HTML.jpg
    Figure 9-23.

    Creating a variable

  • In the Toolbox, click on the Array category. Then click and drag the set list to block over, and place it inside the on start block. The set list to block has an array of block that can hold numbers. By default, it has two number boxes for numeric inputs.

  • Click on the drop-down box of the set list to block and from the drop-down list, choose the variable scores.

  • Click on the + icon to add three more number boxes. Then type the scores in each number box (2, 3, 0, 2, 1) (Figure 9-24 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig24_HTML.jpg
    Figure 9-24.

    Using the array of block

  • In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block just below the set scores to block.

  • In the Toolbox, click on the Arrays category. Then click and drag the get value at block over , and place it inside the placeholder of the show number block (Figure 9-25 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig25_HTML.jpg
    Figure 9-25.

    Placing the get value at block

  • In the get value at block, choose the variable scores from the drop-down list. Then in the text box, type the index of the value you want to find (e.g., 4).

  • Once completed, your code should look something like this (Figure 9-26 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig26_HTML.jpg
    Figure 9-26.

    Full code listing

How It Works

The set list to block allows you to store an array of numbers. Each number in the array has an index and starts from 0. In the above example, the first number, which is, 2 has the index 0. The last number, which is 1, has the index 4. The get value at block is used to find the value at any valid index. For the above example, the valid indexes are, 0, 1, 2, 3, and 4. The show number block is used to display the retrieved value on the micro:bit display.

9-5. Creating a Variable to Hold an Array of Text

Problem

You want to create a variable to hold an array of five names and find the name at index 4.

Solution

  • In the Toolbox, click on the Variables category and then click the Make a Variable... button (Figure 9-27 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig27_HTML.jpg
    Figure 9-27.

    The Variables toolbox

  • In the New Variable name: window, type names as the variable name. Then click on the Ok button (Figure 9-28 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig28_HTML.jpg
    Figure 9-28.

    Creating a variable

  • In the Toolbox, click on the Array category. Then click and drag the set text list to block over, and place it inside the on start block. The set text list to block has an array of blocks that can hold strings. By default, it has two text boxes for inputs.

  • Click on the drop-down box of the set text list to block and from the drop-down list, choose the variable names.

  • Click on the + icon to add three more text boxes. Then type the names in each text box (Emma, Olivia, Ava, Isabella, Sophia) (Figure 9-29 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig29_HTML.jpg
    Figure 9-29.

    Using the array of block

  • In the Toolbox, click on the Basic category. Then click and drag the show string block over, and place it inside the on start block just below the set names to block.

  • In the Toolbox, click on the Arrays category. Then click and drag the get value at block over, and place it inside the placeholder of the show string block (Figure 9-30 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig30_HTML.jpg
    Figure 9-30.

    Placing the get value at block

  • In the get value at block, choose the variable names from the drop-down list. Then in the text box, type the index of the name (string) you want to find (e.g., 4).

  • Once completed your code should look something like this (Figure 9-31 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig31_HTML.jpg
    Figure 9-31.

    Full code listing

How It Works

The set text list to block allows you to store an array of strings. Each string in the array has an index and starts from 0. In the above example, the first number, which is 2, has the index 0. The last number, which is 1, has the index 4. The get value at bock is used to find the value at any valid index. For the above example, the valid indexes are, 0, 1, 2, 3, and 4. The show string block is used to display the retrieved string on the micro:bit display.

9-6. Creating a Variable to Hold Boolean Value

Problem

You want to create a variable to hold the value false.

Solution

  • In the Toolbox, click on the Variables category and then click the Make a Variable... button (Figure 9-32 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig32_HTML.jpg
    Figure 9-32.

    The Variables toolbox

  • In the New Variable name : window, type win as the variable name. Then click on the Ok button (Figure 9-33 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig33_HTML.jpg
    Figure 9-33.

    Creating a variable

  • In the Toolbox, click on the Variables category. Then click and drag the set variable to block over, and place it inside the on start block. Then choose the variable win from the drop-down list.

  • Click on the Logic category. Then click and drag the Boolean false block over, and place it inside the placeholder of the set variable to block (Figure 9-34 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig34_HTML.jpg
    Figure 9-34.

    Placing a boolean block

  • Click on the Basic category. Then click and drag the show string block over, and place it inside the on start block just below the set win to block.

  • Click on the Variables category. Then click and drag the variable block named win over, and place it inside the placeholder of the show string block (Figure 9-35 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig35_HTML.jpg
    Figure 9-35.

    Placing a variable

  • Once completed, your code should look something like this (Figure 9-36 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig36_HTML.jpg
    Figure 9-36.

    Full code listing

How It Works

MakeCode allows you to create variables that can hold two statuses: either true or false and known as Boolean variables. There are two Boolean blocks that can be found in the Logic category: true and false. You can choose either one and change the status by choosing true or false from the drop-down list.

9-7. Changing the Value of an Integer Variable

Problem

You want to change the value stored in the integer variable in Recipe 9-1 by 10.

Solution

  • Open the project you have created in Recipe 9-1.

  • In the Toolbox, click on the Variables category. Then click and drag the change variable by block over, and place it inside the on start block just below the show number block.

  • In the change variable by block, choose the variable x from the drop-down list. Then type the value 10 in the number box.

  • Duplicate the show number block, and place it just below the change x by block. Then type 10 in the number box.

  • Once completed, your code should look something like this (Figure 9-37 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig37_HTML.jpg
    Figure 9-37.

    Full code listing

How It Works

Once assigned a value to an integer or float variable, you can change its value using one of the following ways.
  • Use a set variable to block to assign a new value.

  • Use a change variable by block to increment or decrement the current value by a specified value.

9-8. Updating String Variables

Problem

You want to change the content of the string variable created in Recipe 9-3.

Solution

  • Open the code that you have created in Recipe 9-3.

  • Duplicate the set y to block, and place the duplicated block just below the show string block. Then type Calliope Mini in the text box.

  • Duplicate the show string block, and place the duplicated block just below the second set y to block.

  • Once completed, your code should look something like this (Figure 9-38 ).
    ../images/474604_1_En_9_Chapter/474604_1_En_9_Fig38_HTML.jpg
    Figure 9-38.

    Full code listing

How It Works

By storing text in a string variable, you can change its content by using a set variable to block . You can also store numbers but make MakeCode treat them as just text and not used for mathematical calculations unless you first convert them to numbers using a parse to number block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.216.75