4. Initialization of Variables

So, what is a variable? Why do I need it?

A variable is a reassignable nickname given to a piece of information (i.e. words, numbers, etc.) that makes your code easier to read and maintain. Think about the following example. Imagine you are twelve and have an older brother who is four years older than you. When people ask you how old your brother is, you might say, “he is four years older than me”, or “he is sixteen years old”. Notice how the former is always true (your brother will always be four years older than you) and the latter becomes invalid immediately after your brother hosts his next birthday party. In this case, your age is a variable. It is reassigned, or changed, to a new number after every birthday but still holds the same meaning: your age. Everything else in relation to your age also gets updated automatically. To recap, a variable is often useful in describing relations. In the example above, your brother is always four years older than you rather than being at a specific constant age.

In C++, this is expressed as:

int myAge = 12;

int broAge = myAge + 4;

Wait a sec, what did you just write?

Good question! To initialize a variable in C++, you need to first tell C++ the type of information this variable needs to hold, give it a name, and set it equal to a value. If a value is not set, a newly initialized variable holds a default value of its type. In later topics, we will be able to come up with arbitrary types (i.e. Cat). For now, we will stick with predefined types of numbers.

There are two main types of numbers: int and double

Int

stands for integer or “whole number” (as some of you learned it this way). It means there are no decimal points in the world of integers. Integers have a special property shown below.

Given this property for variables of type int, what do you think will be the output of this program? 

We can see here that dividing an int set to hold the value 1 by another int with a value of 2 does not equal 0.5 as we would normally expect. Since the result must also be an int and is always rounded down, 0 will be the result of dividing 1/2. 

Double

actually stands for decimals. It is used to contain numerical values with decimal points.

Unlike ints, doubles are signed, meaning they can hold positive or negative values. Two rules pertain here: 

Using this information about ints and doubles, what will be the output in these two scenarios above?

The output will be the same! Remember, operations with a double and an int produce a result that is a double. 

Constant

When declaring a variable, it is by default understood it can be changed later. However, for some reason, if you decide a variable should be a constant, to avoid accidental changes or reassignments, you can put the word const in front of a variable declaration. An example is shown below.

In the example, because we flagged the variable a as a constant, any change to it would result in an error on the right. 

A word of caution 

Never name your variables randomly. Always give them meaningful names so you won’t be lost in the world of a’s and b’s. (In other words, unless you are doing a show of concepts, don’t name all your variables a’s and b’s. It will get out of hand quickly)

Speaking of naming things, there are three popular naming conventions that you can read more about.

camelCase, PascalCase, snake_case 

Find the one you like and stick to it!