Once you start creating programs of your own, you may need to have your program do some math. There are a few tricks that are helpful to know about values and operators and how they work in BlocklyProp.
Math Operation Block
You have already used the math operation block. It allows you to insert two number value blocks and to choose a math operation to perform on them:
This block will give a value of 100, since 52 + 48 = 100. If you put it into a Terminal print number block, you can see the value given by the math operation block:
- Make a new program, and set up the block shown above.
- Try the other math operators with these same values.
If you choose subtraction (-) from the drop-down menu, the result will be 4. If you choose multiplication (*), the result will be 2496.
Division (/) will result in 1, which is a bit different than you might expect. 52 ÷ 48 = 1.0833…, but Blockly only works with integers, which are positive and negative whole numbers. It is essential to know this fact if your program is doing any division.
The last operation, modulus, gives a result of 4. Modulus is the remainder after division, so if 48 goes into 52 one time, the remainder is 4. Sometimes, it is helpful to use both division and modulus in your program if you need to divide numbers.
Using more than two values
What if you need to use more than two numbers? The math operation block can be mutated. By clicking the gear icon on the block, you can add more inputs, allowing you to do math with more than two numbers.
The Propeller follows the standard order of operations, which means that the program completes multiplication and division operations from left to right and before any addition or subtraction operations (also done from left to right). You can learn more about the order of operations from Kahn Academy by clicking this link. Try it out by adding another term to the math operations block in the program you created above:
- Click the gear icon on the math operation block to open its mutation window.
- Drag a term block into the math block.
- Click the gear icon again to close the mutation window.
- Add a number value block into the new input you just created in the math operation block.
- Run your program and see the result in the terminal.
Try This
The rule of operations states that any multiplication will happen before any addition, so that in the statement 5 + 6 * 3, the 6 and 3 will be multiplied first (6 * 3 = 18), then the 5 will be added, resulting in a value of 23. What if you need the addition to happen first? If you were doing this math problem on paper and you wanted to make sure the 5 and 6 were added first and then that answer was multiplied by 3, you would write it like this: (5 + 6) * 3. The parentheses indicate that you should do the math inside of them first. In Blockly, you can force an operation to be done first by "nesting" it. To “nest” a math operation block, you place one math operation block within another math operation block. It is much like the nesting dolls pictured below.
Let’s build a program with three terms and two operations: an addition and a multiplication. To help you understand how nested math operation blocks work you will group the terms two different ways, which will give two different answers.
- Build the following program, being careful to nest the math operation blocks exactly as shown.
- Load your program in RAM
After the program loads, the terminal will display:
Why are they different? See if you can figure it out. If you are not sure, keep reading!
How it Works
It’s a matter of what your BlocklyProp program evaluates first.
- In the first Terminal print number block, BlocklyProp finds (6 + 4) = 10 first, and then multiplies that by 2, resulting in 20.
- In the second Terminal print number block, BlocklyProp finds (4 × 2) = 8 first, and then adds 6, resulting in 14.
This shows that when math operation blocks are nested, BlocklyProp evaluates them from the inside out. You can imagine that each math operation block is enclosed in parentheses, just as in the descriptions above.
Your Turn
Write a program that solves and displays the answers to the following problems. Choose your block nesting carefully to get the same answer you would using the standard order of operations:
(9 + 6) ÷ 3 - 2 = ___
10 - 2 × (6 + 1) = ___
100 × 4 ÷ (25 - 17) + 8 = ___
20 ÷ (10 - 1 × 55) + 2 = ___