<< Chapter < Page Chapter >> Page >

Constructing the script

That seems straightforward enough. However, the process of constructing that script contains some new material, so I will walk you through the process.

Image 3 shows the state of the development process immediately after the blocks for the variables named Sum and Diff have been dragged from the Variables panel to the center panel and connected to the block that reads when button clicked .

Image 3. preparing to use the arithmetic operators.

Missing image
Image 3. Preparing to use the arithmetic operators.

Not exactly what we want to happen

In Image 3 , as it stands now, the variables named Sum and Diff will be set to a value of zero when the user clicks the button. That isn'twhat we want, so we have some more programming to do.

The desired behavior is for the value of the variable named Sum to be set to the sum of the variables named LeftOperand and RightOperand when the user clicks the button. Similarly, we want the value of thevariable named Diff to be set to the difference between those two variables.

The Operators panel

The left panel in Image 3 shows the blocks that are exposed when the green button labeled Operators is clicked.

Eleven of those blocks are of the correct shape to fit into the white boxes in the two bottom orange blocks inthe center panel. This means that we could drag any one of those eleven blocks and drop them into those white boxes.

Four arithmetic operators

The top four green blocks in the left panel of Image 3 are particularly interesting. They contain the following arithmetic symbols as part of their labels:

  • +
  • -
  • *
  • /

These are the symbols that are commonly used for addition, subtraction, multiplication, and division in computer programming. The top two matchwhat you are accustomed to seeing on your hand calculator, but the bottom two probably don't match.

Use an asterisk for multiplication

Most programming languages use the * character instead of the X character for multiplication in order to keep the multiplication operator frombeing confused with the letter that uses the X symbol.

Use a forward slash for division

Insofar as division is concerned, most computer keyboards don't have a symbol that matches the symbol commonly used for division on hand calculators, so the /character is used instead.

More blocks of the correct shape

Before leaving the discussion of the left panel in Image 3 , I will explain the behavior of four additional blocks that are of the correct shape forbeing dropped into the white boxes in the variable blocks in the center panel. (The remaining blocks beyond those four deal with strings, which I will ignore for now.)

Random numbers

The block labeled pick random 1 to 10 lets you enter other values in place of 1 and 10. Then this block will deliver a random number withinthat range when it is called upon to do so. Random numbers are often used in game programs to simulate the throwing of dice or the spinning of a wheel offortune.

The modulus

The block labeled mod will produce the modulus of two values when called upon to do so. The modulus of two numbers is the remainder thatresults from dividing one number by another number. You may remember the remainder from when you learned to do long division in elementary school butbefore you learned about decimals.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask