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

7. Loops and Logic

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 
This chapter presents some recipes about how to use loops and logic with MakeCode. MakeCode provides four type of loops to continually repeat blocks until a certain condition is reached:
  • repeat

  • while

  • for

  • for element

It also provides three types of block categories for decision making:
  • conditional

  • comparison

  • Boolean

7-1. Repeating Some Code Blocks Several Times

Problem

You want to display numbers from 1 to 10 using a loop.

Solution

  • In the Toolbox, click the Variables category. Then click on the Make a Variable… button. In the New variable name modal box, type x. Then click on the Ok button.

  • Again, click on the Variables category, and then click and drag the set variable to block over and place it inside the on start block . Then choose the variable name x from the drop-down list if it has not already been selected. Also type 1 for the initial value.

  • In the Toolbox, click the Loops category. Then click and drag the repeat n times block over again, and place it inside the on start block just below the set variable to block. Type 10 for the number of times that you want to repeat the action.

  • In the Toolbox, click the Basic category. Then click and drag the show number block over, and place it inside the repeat 10 times block. Choose the variable x from the drop-down list if it has not already been selected.

  • In the Toolbox, click the Variables category. Then click and drag the change variable by block over, and place it inside the repeat 10 times block just below the show number block . Choose the variable x from the drop-down list if it has not already been selected.

  • Once completed, your code should look something like this (Figure 7-1 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig1_HTML.jpg
Figure 7-1.

Full code listing

How It Works

The repeat n times block allows you to execute a group of blocks several times. The number of times can be defined in the text box of the repeat n times block.

7-2. Run a Same Sequence of Actions While a Condition Is Met

Problem

You want to print numbers from 1 to 10 using a while loop.

Solution

  • In the Toolbox, click the Variables category. Then click on the Make a Variable… button. In the New variable name modal box, type x. Then click on the Ok button.

  • Again, click on the Variables category, and then click and drag the set variable to block over and place it inside the on start block. Then choose the variable name x from the drop-down list if it has not already been selected. Also type 1 for the initial value.

  • In the Toolbox, click the Loops category. Then click and drag the while-do block over again, and place it inside the on start block just below the set variable to block.

  • In the while block, choose the variable x from the first drop-down list. Then choose less than or equal (≤) from the second drop-down list for the condition. After that, type 10 in the text box for the value you want to compare with the result using the condition.

  • In the Toolbox, click the Basic category. Then click and drag the show number block over, and place it inside the while-do block. Choose the variable x from the drop-down list if it has not already been selected.

  • In the Toolbox, click the Variables category. Then click and drag the change variable by block over, and place it inside the while-do block just below the show number block. Choose the variable x from the drop-down list if it has not already been selected.

  • Once completed, your code should look something like this (Figure 7-2 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig2_HTML.jpg
Figure 7-2.

Full code listing

How It Works

The while-do loop allows you to repeat a block until a specific condition is met. In the above example, the while loop prints numbers from 1 and increments by 1 in each step until the result is less than or equal to 10 where 10 is considered as the condition. In each step, the show number block prints the result, and the change x by 1 block increments the result by 1.

The while-do block supports the following conditions.
  • =    Return true if both inputs are equal each other.

  • ≠    Return true if both inputs are not equal to each other.

  • <    Return true if the first input is smaller than the second input.

  • ≤    Return true if the first input is smaller than or equal to the second input.

  • >    Return true if the first input is greater than the second input.

  • ≥    Return true if the first input is greater than or equal to the second input.

7-3. Using for Loop

Problem

You want to display even numbers from 0 to 10 on the micro:bit screen.

Solution

  • In the Toolbox, click the Variables category. Then click on the Make a Variable… button. In the New variable name modal box, type x. Then click on the Ok button.

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

  • In the Toolbox, click the Loops category. Then click and drag the for block over, and place it inside the on start block just below the set variable to block . In the textbox, type 5 for the end number (end step).

  • In the Toolbox, click the Basic category. Then click and drag the show number block over, and place it inside the for block. Choose the variable x from the drop-down list.

  • In the Toolbox, click the Variables category. Then click and drag the change variable by block over, and place it inside the for block just below the show number block. Choose the variable x from the drop-down list, and type 2 for the increment value.

  • Once completed, your code should look something like this (Figure 7-3 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig3_HTML.jpg
Figure 7-3.

Full code listing

How It Works

The for loop allows you to run same code over and over again, the number of times you specify. In the above solution under Recipe 7-3, the for loop repeats the code 6 times (0 to 5), and every time the value of the variable x is displayed on the micro:bit LED screen and incremented by 2. Table 7-1 shows how the output is calculated in each step.
Table 7-1.

Calculation steps of the ‘for’ loop

Index

Print value of  x

Calculation (x = x + 2)

0

0

0 + 2 = 2

1

2

2 + 2 = 4

2

4

4 + 2 = 6

3

6

6 + 2 = 8

4

8

8 + 2 = 10

5

10

Calculation stops

7-4. Decision Making with if-then

Problem

You want to display the ‘yes’ icon on the micro:bit LED screen if the randomly generated number is greater than 5.

Solution

  • In the Toolbox, click the Input category and then click on the on button A pressed event block.

  • In the Toolbox, click the Variables category. Then click on the Make a Variable… button. In the New variable name modal box, type x. Then click on the Ok button.

  • In the Toolbox, click on the Variables category again. Then click and drag the set variable to block over, and place it inside the on button A pressed block. After that, choose the variable x from the drop-down list.

  • In the Toolbox, click the Math category . Then click and drag the pick random 0 to 10 block over, and place it on the placeholder of the set x to block (Figure 7-4 ).
    ../images/474604_1_En_7_Chapter/474604_1_En_7_Fig4_HTML.jpg
    Figure 7-4.

    Placing the pick random block

  • In the Toolbox, click on the Logic category. Then click and drag the if-then block over, and place it inside the on button A pressed block just below the set x to block.

  • In the Toolbox, click on the Logic category again. Under the Comparison section, click and drag one of the comparison blocks over, and place it inside the placeholder of the if-then block. Choose > (greater than) from the drop-down list. Then click on the Variables category . Then click and drag the variable x over, and place it inside the first placeholder of the comparison block. Then type 5 in the second placeholder.

  • Click on the Basic category. Then click and drag the show icon block over, and place it inside the if-then block. Choose the “yes” icon from the drop-down list if it has not already been selected. Also, drag and drop the clear screen block from the Basic category, and place it inside the if-then block just below the show icon block.

  • Once completed, your code should look something like this (Figure 7-5 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig5_HTML.jpg
Figure 7-5.

Code listing

How It Works

The if-then block allows you to identify if a certain condition is true or false and executes a block of code accordingly. In the above solution under Recipe 7-4, when you press the button A, a random number (0 to 10 between mix and max included) will assign to the variable x. Next, the if section of the if-else block is used to determine whether the variable x is greater than 5. If true, the “yes” icon will display on the LED screen and then clear the screen to prepare it for the next event .

7-5. Decision Making with If-then-else

Problem

You want to display the ‘yes’ icon on the micro:bit LED screen if the randomly generated number is greater than 5 and display the ‘no’ icon if the randomly generated number is less than 5.

Solution

  • In the Toolbox, click the Input category, and then click on the on button A pressed event block.

  • In the Toolbox, click the Variables category. Then click on the Make a Variable… button. In the New variable name modal box, type x. Then click on the Ok button.

  • In the Toolbox, click on the Variables category again. Then click and drag the set variable to block over, and place it inside the on button A pressed block. After that, choose the variable x from the drop-down list.

  • In the Toolbox, click the Math category . Then click and drag the pick random 0 to 10 block over, and place it on the placeholder of the set x to block (Figure 7-6 ).
    ../images/474604_1_En_7_Chapter/474604_1_En_7_Fig6_HTML.jpg
    Figure 7-6.

    Placing the pick random block

  • In the Toolbox, click on the Logic category. Then click and drag the if-then-else block over, and place it inside the on button A pressed block just below the set x to block.

  • In the Toolbox, click on the Logic category again. Under the Comparison section , click and drag one of the comparison blocks over, and place it inside the placeholder of the if-then block. Choose > (greater than) from the drop-down list. Then click on the Variables category. Then click and drag the variable x over, and place it inside the first placeholder of the comparison block. Then type 5 in the second placeholder.

  • Click on the Basic category. Then click and drag the show icon block over, and place it inside the then section of the if-then-else block. Choose the “yes” icon from the drop-down list. Also, drag and drop another show icon block from the Basic category, and place it inside the else section of the if-then-else block. Then choose the “no” icon from the drop-down list.

  • Once completed, your code should look something like this (Figure 7-7 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig7_HTML.jpg
Figure 7-7.

Code listing

How It Works

The if-then-else block allows you to identify if a certain condition is true or false and executes a block of code accordingly. In the above solution under Recipe 7-5, when you press the button A, a random number (0 to 10 between mix and max included) will assign to the variable x. Next, the if section of the if-else block is used to determine whether the variable x is greater than 5. If true, the then section of the if-then-else will execute and display the “yes” icon on the LED screen. If the variable x is less than 5, the block inside the else section will execute and the “no” icon will display on the LED screen .

7-6. Decision Making with if-then-else if-then-else

Problem

You want to display the ‘yes’ icon on the micro:bit LED screen if the randomly generated number is greater than 5 and display the ‘no’ icon if the randomly generated number is less than 5. Also, display the square icon, if the random number is equal to 5.

Solution

  • In the Toolbox, click the Input category, and then click on the on button A pressed event block.

  • In the Toolbox, click the Variables category. Then click on the Make a Variable… button. In the New variable name modal box, type x. Then click on the Ok button.

  • In the Toolbox, click on the Variables category again. Then click and drag the set variable to block over, and place it inside the on button A pressed block. After that, choose the variable x from the drop-down list.

  • In the Toolbox, click the Math category. Then click and drag the pick random 0 to 10 block over, and place it on the placeholder of the set x to block (Figure 7-8 ).
    ../images/474604_1_En_7_Chapter/474604_1_En_7_Fig8_HTML.jpg
    Figure 7-8.

    Placing the pick random block

  • In the Toolbox, click on the Logic category. Then click and drag the if-then-else block over, and place it inside the on button A pressed block just below the set x to block. Click on the plus icon to add another section to the if-then-else block.

  • In the Toolbox, click on the Logic category again. Under the Comparison section , click and drag one of the comparison blocks over, and place it inside the placeholder of the if-then block. Choose > (greater than) from the drop-down list. Then click on the Variables category. Then click and drag the variable x over, and place it inside the first placeholder of the comparison block. Then type 5 in the second placeholder.

  • Click on the Basic category. Then click and drag the show icon block over, and place it inside the then section of the if-then-else block. Choose the “yes” icon from the drop-down list.

  • In the Toolbox, click on the Logic category. Under the Comparison section, click and drag one of the comparison blocks over, and place it inside the second placeholder if it belongs to the else if section of the if-then block. Choose < (less than) from the drop-down list. Then click on the Variables category. Then click and drag the variable x over, and place it inside the first placeholder of the comparison block. Then type 5 in the second placeholder.

  • Also, drag and drop another show icon block from the Basic category , and place it inside the else if section of the if-then-else block. Then choose the “no” icon from the drop-down list.

  • Finally, drag and drop one more show icon block from the Basic category, and place it inside the else section of the if-then-else block. Then choose “square” icon from the drop-down list.

  • Once completed, your code should look something like this (Figure 7-9 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig9_HTML.jpg
Figure 7-9.

Code listing

How It Works

The if-then-else block allows you to identify if certain conditions are true or false and executes a block of code accordingly. In the above solution under Recipe 7-6, when you press the button A, a random number (0 to 10 between mix and max included) will assign to the variable x. Next, the if section of the if-else block is used to determine whether the variable x is greater than 5. If true, the code block inside the first then section of the if-then-else block will execute and display the “yes” icon on the LED screen. If the variable x is less than 5, the block inside the else if section will execute, and the “no” icon will display on the LED screen. If the variable x is equal to 5, the block inside the else section will execute, and the “square” icon will display on the LED screen .

7-7. Comparing Numbers

Problem

You want to compare two numbers.

Solution

  • In the Toolbox, click on the Variables category and then click on the Make a Variable… button. In the New variable name box, type x. Finally, click on the Ok button.

  • Follow the above step again to make another variable named y.

  • In the Toolbox, click on the Variables category. Then click and drag the set y to block over, and place it inside the on start block . Now right-click on the set y to block, and from the shortcut menu, choose Duplicate. Place the duplicated block just above the set y to block and choose the variable x from the drop-down list. Type the value 5 for both variables.

  • In the Toolbox, click on the Logic category. Then click and drag the if-then-else block over and place it inside the on start block just below the set y to block.

  • Click on the Logic category again. Under the Comparison section, click and drag one of the blocks over, and place it on the placeholder of the if-then-else block (by default, the placeholder has a true-false block). Then choose “=” from the drop-down list.

  • Click on the Variables category. Then click and drag the variable x over and place it on the first placeholder of the comparison block. Also, click and drag the variable y block over, and place it on the second placeholder of the comparison block.

  • Click on the Basic category. Then click and drag the show icon block over, and place it inside the then section of the if-then-else block. After that, choose the “yes” icon from the drop-down list.

  • Follow the above step to place another show icon block inside the else section of the if-then-else block, and choose the “no” icon from the drop-down list.

  • Once completed, your code should look something like this (Figure 7-10 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig10_HTML.jpg
Figure 7-10.

Code listing

How It Works

The comparison block allows you to compare two numbers (inputs).
  • =    Return true if both inputs equal each other.

  • ≠    Return true if both inputs are not equal to each other.

  • <    Return true if the first input is smaller than the second input.

  • ≤    Return true if the first input is smaller than or equal to the second input.

  • >    Return true if the first input is greater than the second input.

  • ≥    Return true if the first input is greater than or equal to the second input.

7-8. Using Boolean Operators

Problem

You want to check if the user has pressed both buttons connected to the pin0 and pin1 .

Solution

You will need following things to build the circuit.
First, build the circuit as shown in Figure 7-11 .
../images/474604_1_En_7_Chapter/474604_1_En_7_Fig11_HTML.jpg
Figure 7-11.

Wiring diagram

Then follow the steps below to build the code with MakeCode.
  • In the Toolbox, click on the Input category and then click on the on button A pressed event block .

  • In the Toolbox, click on the Variables category and then click on the Make a Variable… button. In the New variable name box, type x. Finally, click on the Ok button.

  • Follow the above step again to make another variable named y.

  • In the Toolbox, click on the Variables category. Then click and drag the set y to block over, and place it inside the on button A pressed block. Now right-click on the set y to block and from the shortcut menu, choose Duplicate . Place the duplicated block just above the set y to block and choose the variable x from the drop-down list.

  • Click on the Pins category. Then click and drag the digital read pin P0 block over, and place it inside the placeholder of the set x to block.

  • Follow the above step to place another digital read pin P0 block inside the placeholder of the set y to block . Then choose the pin P1 from the drop-down list.

  • In the Toolbox, click on the Logic category. Then click and drag the if-then-else block over, and place it inside the on start block just below the set y to block.

  • Click on the Logic category again. Under the Boolean section, click and drag the Boolean and block over, and place it on the placeholder of the if-then-else block (by default, the placeholder has a true-false block).

  • Click on the Variables category. Then click and drag the variable x over, and place it on the first placeholder of the comparison block. Also, click and drag the variable y block over, and place it on the second placeholder of the comparison block.

  • Click on the Basic category. Then click and drag the show icon block over, and place it inside the then section of the if-then-else block. After that, choose the “yes” icon from the drop-down list.

  • Follow the above step to place another show icon block inside the else section of the if-then-else block, and choose the “no” icon from the drop-down list.

  • Once completed, your code should look something like this (Figure 7-12 ).

../images/474604_1_En_7_Chapter/474604_1_En_7_Fig12_HTML.jpg
Figure 7-12.

Code listing

How It Works

Boolean operators allow you to take Boolean inputs (true and false, 1 and 0) and evaluate to a Boolean output . MakeCode provides three Boolean operators:
  • And: Evaluates to true if-and-only-if both inputs are true. Table 7-2 shows the truth table for the Boolean And .

Table 7-2.

Truth table for AND operator

Input A

Input B

Output

True

Ture

True

True

False

False

False

True

False

False

False

False

  • Or: Evaluates to true if-and-only-if either input is true. Table 7-3 shows the truth table for the Boolean operator Or .
    Table 7-3.

    Truth table for OR operator

    Input A

    Input B

    Output

    True

    Ture

    True

    True

    False

    True

    False

    True

    True

    False

    False

    False

  • Not: Evaluates to the opposite of the input. Table 7-4 shows the truth table for the Boolean operator Not .

Table 7-4.

Truth table for NOT operator

Input

Output

True

False

False

True

In the above solution under Recipe 7-8, when you press the button A, the variables x and y take the status of the switches connected to the pin0 and pin1. The status of a switch can be either 1 or 0 (ON or OFF). Then the Boolean And operator evaluates to true if-and-only-if both inputs are 1 (both switches are turned ON). If true, the ‘yes’ icon will display on the LED screen. If false, the ‘no’ icon will display on the LED screen .

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

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