Computer Programming: How Variables work 

Easy computer programming tutorial for beginners on working with variables.

What is a ‘Variable’ in computer programming?

Image a box.

Let’s name this box, “Toy Box”. This box is special because it’s made of oak wood and holds Jimmy’s favorite toy nerf-guns.

A variable is just that. It is a box that holds something important; information — more specifically a value.

Just like a wooden ‘Toy Box’ that holds some nerf guns. A variable is a box that has a type, a name and a some thing [a value] it stores.

Note: A variable can be used to hold something different later on in the program. For example, Jimmy can decide to hold another toy in his ‘Toy Box’ instead of nerf-guns when ever he wants to.

What is the purpose of a variable: Let’s say I want to create a really cool game called Tetris, and want to save the highest score achieved by a player.

I can use a variable to hold the highest score scored in the game.

Type of a variable

Although you can give your variable box any name you want, the value it can hold can only be of a certain type.

The type of information — value — e.g. numbers, letters, answers etc. your variable can hold depends on the type of variable box it is.

Type of value must equal the type of variable

Like a wooden “toy box” in the real world, a variable box can be one of several types — mainly the 8 primitive data types.

But for now you only need to know the 3 of 8 data types.

  1. Int —A variables of data type int can hold whole integers.
  2. char — A variable of data type char can hold a single character.
  3. double — A variable of data type double can hold decimal value integer.

Naming a variable

Of course you can give it any kind of name you want, however, it is good code practice to begin the name [of a variable] with only the first letter written in small letters.

For example: I can declare a variable of type int and give it a name: “evenNumber” or “oddNumber” or just “number”.
int evenNumber;
float oddNumber;
char letter;

Notice the first letter of the the word ‘even’ is in small letters and the first letter of the second word is capitalized.

It is a good habit write clean code with proper programming conventions as you’re learning how to program.

Finally, how to use a variable in an actual program.

A) You must declare the variable

Declaring a variable simply means to define it by giving it a name and a type.

You have just learnt this. Here is another example:

Int wholeNumbers;

Where: Int is the Type and ‘wholeNumbers is name of the variable.

B. Next you must assign a value you want the variable to hold

Assigning a value to a variable is done through ‘assignment statements’.

This is easy.

Assignment statement is a statement for assigning a value that your variable needs to hold e.g. like a set of nerf-guns.

Note: The first time you assign a value to a variable, it is called initializing the variable. We use this terminology because the value of the variable was null when you initially declared it. But now that you gave it a value through an assignment statement, you have initialized the variable for the first time!

You can assign a value to a variable using the ‘=’ operator.

Here’s an example:

int evenNumber; //Declaring [define] a variable
evenNumber = 2; // initializing [assigning] a value

The variable type is int so it can only store whole integers. Name of the variable is evenNumber and the ‘=’ operator expresses the value given to the variable which is 2.


Let’s see what you have learnt so far. Use the comments section to submit your answers!

  1. Declare a variable of type int and give it name ‘age’, and assign your age to it.
  2. Declare a variable of type char and assign it the value of the 9th letter of the alphabet.
  3. Declare a variable and assign it with the highest score of 13.5 in a game of Tetris.

If you have found this article useful and want to see more tutorial posts like this one, please hit that recommend button!