Recap of Integer Variables

What We’ve Covered So Far

Declaring an integer variable:

Declaring and assigning an integer variable at the same time:

Adding to an integer variable:

Subtracting from an integer variable:

Multiplying an integer variable:

Dividing an integer variable:

Don’t do it! If you wind up with a decimal the computer will just throw it away without tell you. But if you must it looks like this-

Adding and subtracting just one with an integer variable in shorthand:

Creating a random integer value:

Questions and Activities to Try

1. Declare three different integer variables and use them to control the forward movement of Sparki using the sparki.moveForward() command. Here’s a little help to get you started-

2. Using the code you create above to make Sparki move three different distances add some addition and multiplication commands so that Sparki continues to move three different distances, but two of the distances increase each time Sparki executes the loop function code. For example Sparki might move forward three centimeters, wait one second, move forward two centimeters, wait one second, move forward eight centimeters and then wait again one second before the loop function starts over again. The second time through Sparki might move forward three centimeters, wait one second, move forward four centimeters (because this variable is being multiplied by two), wait one second, move forward ten centimeters (because this variable gets two added to it) and then waits a second before starting loop a third time.

3. Add a variable and commands to the code above that causes Sparki to wait a different amount of time between movement commands. Once you’re successful doing that try changing your code a little bit more so that, while Sparki waits a different amount of time between each movement command, the first time Sparki waits at least one second, the second time at least two seconds and the before the third movement command Sparki waits at least three seconds. (Hint, there are actually two different ways to do this last particular challenge.)

Next Step-

Now that you feel a little more comfortable with integer variables let’s move on to decimal points with floating point numbers….

Next Lesson – Floating Point Numbers