C++ Basics 2 - Using variables

Sooner or later, in almost any computer program you will need to store a number or a sentence for use later in your program. Variables are used for just that purpose.

Variables are just boxes in memory

A variable is just a named box in memory that's big enough to store an item of data. You access the box by using its name (both to put data in the box and to take data out).

Creating a variable is easy (programmers call this "declaring a variable"). You just have to tell C++ what sort of data you want to store in the box and give the box a name.

Declaring a variable

Create a new program or use the skeleton program and move your cursor inside the main function. Enter the following code:

int iValue;

You have just declared a variable. This one is called iNumA and it will store an integer (a whole number).

An empty variable boxThe reason I called it iNumA is because it's considered to be good programming practice to prefix your variable name with a character or two that explains what you are storing in it. In this case I want to store an integer so I used the letter, "i".

You're now going to put a number in your variable.

Assigning a value to your variable

Press and, on the next line, add the following code:

iValue = 96;

You have just assigned the value of 96 to the variable. In simpler terms, you've just stored 96 in the box called iValue.

The "=" sign is called the assignment operator and is only ever used in C++ to set the value of a variable to something.

Declaring a variable with a default value

 
When you create a new variable, you can assign a value to it at the same time. It's like combining the two lines of code above into one single line. Delete the two lines of code that you've written so far and replace them with the following lines of code instead:
// Create an integer variable called iNumA and store 12 in it
int iNumA = 12;    
 
// Create an integer variable called iNumB and store 47 in it
int iNumB = 47;    

Using the lines above you've now created two variables called iNumA and iNumB and stored a different value in each one of them.

12 is now stored in iNumAiNumB now contains 47

Now that you have a couple of variables you can do something with them. Why not add them together? To do this you will need to add the contents of one of the variables to the contents of the other.

Here's the code to do this. Hopefully the comments should make everything clear for you.

// Add the value in iNumA to the value in iNumB
// and store the result in iNumA

iNumA = iNumA + iNumB

// Display the value stored in iNumA on the screen

cout << iNumA << endl;

In pictures, this is what's happening:

  1. iNumA and iNumB are added together which results in 59.
  2. The value 59 is then assigned to iNumA, overwriting what is already there (12 in this case)

An example in pictures of what is happening in the code

Did you notice that to understand the code you work from right to left? You add together the two numbers on the right and then assign them to the variable on the left.