GoldBorder Title GoldBorder Title
Programming: Lesson 4, Using A Number
"Now that you have created a variable and placed a number into it, what can you do with it?"

Since Number variables can be used in Math tasks, let's do that. Remember, the computer can use Number variables to do Math, but, not Text variables.

Programs run code in the order you enter it. Just like GB, who will do what you say in the order you say it.

What if we told GB to do the following:

  • give a big smile
  • now look surprised
GB would first 'smile', then act 'surprised'.

However, if we ask him the same things, but, switch the two requests:

  • now look surprised
  • give a big smile
GB would first act 'surprised', then 'smile'.

As you see, we asked GB to do the same things, but, in different orders.

The same is true with making a program (programming). You type the lines of code in the order you want them used.

Let's get back to your first line of code:

    var Age=44;
This time, we want to place a number into the Age variable and then double the number. To do this, we just add another line of code that doubles the number. The nice thing is that most programming languages do math in the same way a student does it on paper.

If you wanted to double a number on a piece of paper and that numbers was '44', you would write:

You could then write the answer as well (this is where you get a good grade from an instructor):
The nice thing about creating programs is that you give the work of calculations to the computer.

To do this, we must do the following logic in our code:

  • put a number into a variable
  • put the math calculation we want done
  • tell the computer where to put the answer
When you do something stored in a variable, like multiplying it by two (2), you also need to give a place for the computer to store the answer. The wonderful thing about programming is that you can re-use the same variable (remember: it is a block of memory) to be part of the math equation, as well as the storage of the answer. This is because the computer will perform the math in its memory and then replace the contents in the variable with the answer, as long as you have told it to do so.

Here is the new code doing what we want:

    var Age=44;
What does each part of that line mean?
    First Line
  • var: asking for a new variable
  • Age: giving the variable a Label of 'Age'
  • =: assigning content to the variable
  • 44: the content being assigned is '44'
  • ; (semicolon): ending a command line
    Second Line
  • Age: what variable will contain the answer
  • =: assigning content to the variable
  • 44: get what is in the 'Age' variable before doing the math
  • *: do a multiplication on the variable contents
  • 2: what number is being multiplied by what is stored in the variable
  • ; (semicolon): ending a command line

Now for you to do some more coding. I have entered the first line you already coded in the box below. Add the second line of code and click the button to check your code.

    • press the ENTER key after the semicolon (;) to start a new line
    • to figure out what the second line should be, look at the two lines of code above
    • the purpose of the second line is to double (*2) the number
    • don't forget, programming IS case-sensitive, 'Age' is not the same as 'age'
    • the multiplication character (*) is usually created by holding your SHIFT key and pressing '8'
    • don't forget the semicolon on the second line

Once you have verified your code with the 'Check My Code' button, go back into the box and change the '2' to another number, then re-click the 'Check My Code' and see how the number changes.

Things to notice about the code you just created:

  • when doing math and placing the answer into a variable, programs do the calculations to the right of the equal (=) sign, BEFORE putting content into the variable. This is why you can use the same variable for both part of the calculation and where to store the answer
  • once the calculation is done, the new contents of 'Age' is '88' (or whatever number you caused by your math). The '44' is replaced and no longer exists.

Heh! That's now two lines of code you have created. Way to go!