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 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 dropdown 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 important to know this fact if your program is doing any dividing.
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.
What if you need to do math using more than two numbers? To calculate an equation with more than two values, you will need to “nest” math operation blocks. Meaning, you will put 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 anwers.
- Build the following program, being careful to nest the math operation blocks exactly as shown.
- Load your program to 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, 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.
Without paretheses, using the standard order of operations, multiplication and division would be evaluated before addition and subtraction, and then working from left to right.
- 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 = ___
10 - 2 × 6 + 1 = ___
100 × 4 ÷ 25 - 6 = ___
20 ÷ 10 - 1 + 55 × 2 = ___