Lesson 7: Variables
In our previous programming lessons we’ve looked at a lot of history and background theory. Now that you have this background information, we’ll move into the more practical aspects of programming. We’ll begin with one of the most basic ideas in programming, variables.
What is a Variable?
If you are familiar with basic algebra, you are probably already familiar with variables. You’ve probably seen something like this before:
Algebra
Solve for x: 50 = 5*x+10 50-10 = 5*x+10-10 40 = 5*x 40/5 = 5*x/5 8 = x x = 8
In that example, “x” is our variable. In computers the concept of variables is the same, but we use them a bit differently. Computers can’t really “solve” for a variable like that (though you could write a program to do so), but we use variables to store data which we may want to use or change later. So, on computers, variables are just containers of data.
It’s All In The Name
Another difference from algebraic variables and programming variables is that algebraic variables are typically only a single letter (such as “x”, “y”, etc.). In programming (for most high-level languages), variables can, and usually are, words instead of just letters.
When you name a variable, it is slightly different for each language. Usually languages will have keywords or “reserve words” which are words that have special meaning in the language (such as “if”, “else”, “while”, “this”, etc.). These reserve words cannot be used as variable names.
Also, usually, you cannot begin a variable name with a number. In most languages, an underscore is the only symbol allowed to appear in the variable name and you can’t have spaces.
Please note that these rules aren’t set in stone and each language has it’s own rules. These are just the more common rules.
Most programmers also use what is called a “naming convention” which is simply a style of naming variables so you can easily tell what they are. For example, a lot of programmers will name object-oriented class member data (you’ll learn about this in a future lesson) with a lowercase m at the beginning, and each consequent word begins with a capital letter, so if you made a variable for employee name, the variable would be named “mEmployeeName”.
Then, non-member variable would start the first word with a lowercase letter, and each consequent word would begin with a capital letter. So, if you wanted to have a variable for system wattage, it would be systemWattage.
Naming conventions aren’t requirements created by the programming language, but by the programmer to help them better understand all of the dozens or hundreds of variables a program may have. It is usually good to develop your own naming convention and stick with it, whatever it may be.
These are just examples. There are also sometimes other naming requirements you must be aware of, which are specific to the language. For example, PHP requires all variables to begin with a $.
Here is a list of possible variable names. Try to pick out which ones are generally valid and which ones are not:
Variable Names
aDollar fiveDollars 5Dollars box3 ghost_health grumpy-monkey
In many high-level programming languages, all but 5Dollars and grumpy-monkey would be valid variable names. However, like I’ve said before, each language can have it’s own naming requirements so all or none of those may be valid in any given language.
Variable Types
Some programming languages are “typed”, which means each variable must be a certain type of variable. C++ and Visual Basic are examples of typed programming languages. PHP and Javascript are both typeless programming languages.
In typed programming languages, when you create a variable a specific amount of room (memory) is set aside for that variable, depending on the type of variable. For example, an integer is typically 8-bits (1 byte) long, so 1 byte of memory is reserved for that integer.
Below is a table of common variable types and a short description. Note that these can be different for other languages, and many languages will also have their own unique variable types (or even allow you to create new types).
| Variable Type | Description |
| Integer | A whole-number (non-decimal) |
| Float | A floating-point decimal number |
| Character | A single ASCII character |
| String | Multiple characters (a string of character) |
| Boolean | A true/false value |
| Array | A collection of data (a group of variables) |
There are usually a multitude of additional variable types, but they vary greatly from language to language.
Also take note that even in languages that are typeless, the variable type still matters sometimes. If you try to do arithmetic on two strings, you’ll get some weird values. The only thing that typeless usually means is that variable types aren’t forced, and can usually change (you could use a single variable to store an integer then later use it for a string, which isn’t possible in typed languages).
Both typed and typeless languages have their advantages and disadvantages. An advantage of typed languages is that memory management becomes easier since you know how much memory each variable will need right when it is created, something you don’t know with typeless. An advantage to typeless is that it is sometimes easier to convert values, something which can sometimes be tricky with typed languages.
In this lesson we took a brief look at some concepts for variables. This lesson was brief because the difference between languages when it comes to variables is so great that we don’t want to get too in-depth in our general lesson.
In our next lesson we’ll take a look at some common operators and some basic arithmetic. We’ll also expand our use of variables a bit as well.




