Variables

Understanding and Using Variables

SparkiDuino
Easy

Lessons You Should Know

Little Containers for Information

Here’s what we’ll be going over in this lesson about variables:  

Creating Variables

Computers work with information. But they really only work with two pieces of information- a one and a zero. In order to work with larger pieces of information the computer uses things called “variables” which are made up of a lot of those little ones and zeros. They are called variables because the information inside of them varies. You can think of variables as containers for information. The information (also called data) that goes inside the variable is called many different things- it’s usually referred to as a “value” or “data”. You can think of the variable as a container, like a mailbox, and the data that goes inside the variables are like mail that gets placed inside a mailbox. (We’ll use many different container examples through out this variable lessons.)

dataVariable
  (Usually) You can change the value of the data inside the variable, use a variable in a mathematical equation (just like algebra, because it is algebra) and control all sorts of things using variables. Let’s look at the command sparki.moveForward, which makes Sparki move forward, as an example. This line of code above just tells Sparki to move forward. That’s it. It doesn’t tell Sparki how long to move forward and there are no variables involved. This line of code above tells Sparki to move forward ten centimeters and then stop. The value of ten is the data that is used by the command. But it’s still not a variable yet. Now we’re starting to use variables! The first line of code above “declares” the variable. That means Sparki creates a container to hold information. In this case the container holds integers because of the word “int.” The name of the variable is “distance.” We could have called it whatever we like (cat, dog, baconIsYummyAndIEatItWithBreakfast or d) but distance makes sense and isn’t too long. Finally we set the variable named distance equal to a value of ten. Now anywhere we type the word “distance” it’s the same as typing the number ten. Sometimes variables are declared with assigning a value. That looks like this:
emptyVariable
    (One note- be careful not to name your variable any “reserved” word. That’s a word that’s already used for something in the programming language. For example, you would never want to name your variable something like “int” since int is already used to create an integer. If you accidentally use a reserved word you’ll get an error and you can simply change the variable name. Reserved words are often highlighted in a different color to help you understand that they are already in use. Handy, right?) Only after you’ve declared a variable can you put data into it. Although, you can declare a variable and assign a value in the same line of code as you saw above.  

Changing Variables: Also Called “Reassigning”

You may be asking, why don’t people just use the number ten then, instead of a variable? After all, “10” is only two characters and “distance” is eight- also you had to write that whole extra line of code to create (also called “declaring” a variable)  the variable. Let’s look at some ways that variables can come in handy. FirstTime10Var
In the code above the first time through the loop Sparki will move forward a distance of ten centimeters. Then the “delay” command tells Sparki to hang out doing nothing for 1000 milliseconds (or one regular second).  After that the data in the variable “distance” is changed (or “reassigned”) to a value of five. Now, when Sparki goes through the loop of code a second time Sparki will only move forward five centimeters. You don’t have to use the word “int” again since the computer already understands that the variable is an integer. So, by manipulating the variable you can make changes in the robot’s actions. (There’s a lot of other stuff like setup and loop, if you don’t understand those click here.) Let’s take it a little further: FirstTime20Var
Now you may be starting to see why variables are so powerful. In the code above Sparki will add five centimeters to the distance it travelled forward last time. Without a variable to change the amount of code we would have to write to recreate this simple program would be enormous! Here’s a start on writing this program (just the loop portion) without using a variable to give you an idea of what I mean: That’s just four times through the code! Let’s look at other ways to use the variable before moving on. You can also use the variable to control multiple aspects of a program: You can reuse a variable however you like and even do math with a variable wherever and whenever you like. This code will make Sparki move forward farther and farther while also making it wait longer and longer between the times when it rolls forward. The line with the code “distance * 100” multiplies the variable “distance” by 100 to get a much bigger value without changing the variable itself. Before suggesting some ways to practice using an integer variable let’s look at one other way to write the code: Writing code is all about finding ways to write less code to do more stuff. Also programmers can be kind of lazy, sometimes. (Some call it lazy, I call it efficient!) So there are often different (shorter) ways to do common commands in programming languages. We could also write the code above as: And it means the exact same thing! Introductory code usually takes it easy on these types of shorthand but it’s good to have seen them before in case you run across them. Before we move on to talk about different types of variables let’s look at two more really common shorthand command for adding or subtracting one with a variable:

Recap of What We’ve Covered So Far

or keep going and learn about:

Different Kinds of Variables

Variable types

In the C language, variables can only store one type of data. If you need to store multiple types of data, then you need multiple types of variables: variablesComposite2
Just like there are different types of containers for different things in real life there are different variables types (or containers) in programming. You would never use your pocket to hold water and you would never use a water balloon to keep your calculator in, would you? That wouldn’t make sense. Just like the example above we use “int” type variables to hold integers. A variable type called “char” is used to hold a single character such as a letter or a punctuation mark (it can even hold weird ones like µ, € or °) and the variable type “float” is used to hold a number with a decimal point. You will use the words “int,” “char” and “float” to declare variables of each of these types. Here’s some more information about each of these types of variables: Booleans- bool Integer Numbers – int Floating Point Numbers – float Characters – char A couple more thing to note is that can put an integer value into a float variable. You also can put a float value into an integer variable, the computer just throws away the decimal point and anything that comes after it.  Think of it like this- you can store two different materials water and sugar in a glass and you can also store them both in a water balloon. (Just not at the same time if you expect to get them back out in the original form.)  

Variable Size

Each variable type has a different size. This means that you can’t put an enormous number into an integer variable because it won’t fit, there’s not enough space to store the value. Going back to our mailbox example, it’s kind of like trying to stuff a really big envelope containing something more than just mail (maybe a robot or an inflatable pool) into a mailbox. It just won’t fit. Here are the values that the three variables we are learning about can contain in SparkiDuino:
  • Integer (int) – any number between -32,768 to +32,767
  • Float (float) – can be as large as 3.4028235E+38 and as small as -3.4028235E+38. Float values can also only have 6 to 7 digits of precision, meaning that there can only be 6 or 7 numbers after the decimal point (Scientific notation as shown above allows for an exception to this rule.)
  • Character (char) – can hold any single character you can create with your keyboard
In C, variables are only big enough to store one piece of data. If you need to store more than one piece of data, then you need multiple variables. Just like you can’t use a shoe to hold two feet, if you need to store two different numbers, you’re going to need two different variables.

2FeetNo

Arrays

And finally, to help store and manage multiple values of a variable we’ll be talking about Arrays – [] Arrays are similar to a bunch of school lockers in a row. Each of the individual lockers in the row can contain a single backpack or variable value. (I know backpacks and locker size differ, for the sake of argument let’s say they’re all the same size in our array example.) Also- each array can only contain one type of data. You can’t store water in the row of school lockers no matter how hard you try, No putting character values into a bunch of integer school lockers! That’s like putting a hose into the lockers and expecting to be able to come back later to get a quick drink from the water you tried to store in the lockers. It won’t work.

lockersArray

 

Recap of What We’ve Covered So Far

or

Next Step:

Now that you feel comfortable with the basics of the variables we’re going to discuss and use, let’s look at the variable types in more depth. First up, integers!

Next Lesson – Boolean Variables