Saturday, October 12, 2013

C++ Tutorial #5.5: Variables

Okay.  I want you to take a deep breath.  We're going to venture ever-so-slightly into the math arena.  No, not calculus.  But I do want to talk about variables, because they're the one component about the C++ language that I didn't get to in my last post.

Before learning any kind of programming, you should have a good basic theoretical knowledge of how algebra works.  A variable is any factor of piece of information that can change or have multiple values.  For example:

x + 3 = 7

Before doing any math, just think about the problem.  What do we know right off the bat?  We know that 3 = 3, and 7 = 7.  These numbers, whose value we can see and understand at first glance, are called constants.  We have faith that 3 will always equal 3, and 7 will always equal 7.

However, x is another story.  In this problem, x is the variable.  Okay, maybe you were able to figure out pretty easily that 4 + 3 = 7 and therefore x = 4.  However, x = 4 in this equation ONLY.  Look what happens when I put it in another equation:

x -12 = 1

In this case, x = 13.  Whenever you have an equation with a constant, the value of the constant will not change.  So if I were to present the problems 4 + 3 and 17 - 3, the value of 3 stays the same.  3 is always 3.  However, in the above two examples, x was given a different value in each problem.  The value of a variable can change depending on the problem.

So what does this have to do with programming?  Well, C++ uses variables too, though not always in the same way.  In math, variables are always numbers.  However, in C++, there are many types of variables for many types of information.  Here's a list of the one's we'll be using most often:

Int: an integer, or whole real number that is positive or negative.
Char: a character, such as B, 7, or $.
String: a bunch of characters together, such as "button" or "I love to swim".

Computers store all kinds of information—names, dates, passwords, confirmation numbers, etc.  These are just a few of the several types of variables that are required to store all this information.  We'll discuss the other types once you're comfortable with the programming/compiling process in general.

Declaring Variables

The syntax of declaring variables is actually quite simple:

type name = value;
int age = 20;
char grade = A;

You can assign a value to a variable immediately, or save it for later.

int age;
//other code blah blah blah
age = 20;

You can also do math in programming.  However, the nice thing about that is that you don't actually have to solve the problems.  You just have to write them out and make the computer solve them for you, like so:

int first = 12;
int second = 5;
int third = first + second;

The computer will read that third line, check the values of first and second (12 and 5 respectively), add them, and then store the resulting value (17) in the variable third.  Then, you can do more computations with the variable third, and in the future the computer will always read third as having a value of 17.

This is a short post, but I'm going to stop here.  The reason is that, while it's nice to be able to make your programs do your math for you, what good is it if the final product can't be displayed on your screen?  In Tutorial #6, we'll talk more about cout, its good buddy cin, and what roles they play in making a program interact with a user.


  1. I'm guessing that cin has something to do with input.

    1. Yup! It's like this: cout asks a question, and cin allows a user to answer. Or something like that. : )