In this first post we are going to look at the very basics. Creating (and declaring) variables for storing our data, and then giving them different values.
Note that indeed, these are two different operations. Declaring a variable, need not give it a value. In fact, all this really does is setting aside the memory needed to store the variable. Given that different variable types take up different amounts of memory (something your compiler will handle for you), declaration is important, although much of the difficulty is hidden from you.
In C++ declaring a variable (in this case an int), looks like this:
int a;
This sets aside the memory for an integer in your computer program (we will get back to this). But nothing useful is stored in it as of yet.
Calculating an expression is referred to as evaluation.
The two types of initialization
What form of initialization should you prefer when you want to initialize a variable with a specific value? By default, it is recommended that you use the brace `{}` initializer syntax. Although it has it's quirks.
Brace Initalization
int a {3};
Declares and initialises a variable on the same line.
Assignment Initalization
int a = 3;
Bonus: Assignment and Brace Initialisation
int a = {3};
Which is just the same as Brace Initialization
Another bonus: Initializer List Initalization
We will come back to this later.
How does the different initalisers work
Default Initialisation
Given a class
class A { int a {3}; }
Then initialising it with
A b {};
Will default initialise it. Meaning that b::a
is
3
.
Value Initialisation
Initialising it with a value looks like:
A b {4};
Giving the result you think
Basic Program Execution
A C++ program is imperative, which means that it is run and evaluated in the same order that you write it. From top to the bottom.
What is a statement
A statement in C++ is a single line of execution (ending with a
semicolon ;
)
In essence, the program is executed, one statement at a time, from the first statement, to the last, one after the other.
What is an expression
An expression is a unit of code returning a value. The value can be calculated, and put together from multiple expression, hence giving it it's power.
int a {2+3}; type identifier {expression};
Is an expression, returning the value 4. In this way, an expression is any calculation returning a single value. Hence, any place in code there is a single value, it can be replaced by an expression. For now, this is simple stuff, like multiplication, addition and subtraction, but later on, when we introduce functions, we will see that this is a really useful principle.
Thus, an expression is any value, including literals (which evaluate to themselves).
The difference between an expression and a statement is not binary. A statement is a part of the structure of a program, and defines execution order. A statement need not contain an expression (simple declaration with no initialisation), although a statement can be an expression.
2 + 3;
Is a valid statement, containing an expression only.