Addition, Subtraction, Multiplication, and Division

Like all modern programming languages, Scratch provides programmers with the ability to add, subtract, multiply, and divide numeric data. This capability is offered through the code blocks shown in Figure 8.1.

Figure 8.1. These code blocks provide Scratch programmers with the ability to perform arithmetic calculations.


The use of these code blocks is quite intuitive, with each code block clearly identifying its usage. These code blocks can be embedded within any Scratch code block that accepts numeric input. For example, the following script demonstrates how to use these code blocks to modify the value assigned to a variable named Count.

Here, the script begins by assigning an initial value of 10 to Count. Next, four sets of code blocks are executed. Each set consists of one stack block and two reporter blocks. The first set of statements sets the value of Count equal to the value currently assigned to Count plus 5, making Count equal to 15. The second set of code blocks sets Count equal to the value currently assigned to Count minus 5, making Count equal to 10. The third set of code blocks sets Count equal to the current value of Count times 5, making Count equal to 50. Lastly, the last set of code blocks changes the value of Count to 10 by dividing its current value by 5.

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

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