Hello and welcome to a new lesson today, we're going to talk about variables and types.
So the first thing that we need to talk about is a.
Variable is a container that we use to store of value.
We can name our variable whatever we want.
But there are some rules to keep in mind that you must follow when we create the variable.
And the first thing that we need to decide is what type of data we will actually host in our variable,
because that will dictate what type of variable we need to create.
So for example, if we want to create a variable that will just hold a whole number, we would make
it a type and which is an integer, and we'll get to that in just a little bit.
But your method thing that I want to say about variables is that the variable gets assigned a memory
address where the value will be stored at.
So to store a value in a variable, we will need to use the equals sign, also called an assignment
operator.
So I sort of drew this to demonstrate how a variable gets stored in memory.
So let's pretend that this is our computers memory.
And you can see that we are creating a new variable here called value, which is of type IND, so that
the thing that happens here is that one memory address gets assigned to that variable.
Or the variable gets assigned to a memory address that already exists.
And now we are trying to sort of value 10 inside of that variable.
What will happen is, as you can see, 10 will be stored exactly that memory address that the variable
gets mapped with.
So the computer will just go there to that memory whenever you call the value name.
So naming a variable is very important.
And here as well, there are a few rules to keep in mind when you create variables.
A very important one is that you cannot use anything other than a letter or an underscore for the first
character of the variable name.
And then after that first character, you can use any number, any combinations of numbers or letters.
Uppercase and lowercase will be different.
So if you create a variable that's called value and it's all lowercase, it won't be the same as creating
a variable called value that starts with a capital V. But everything else is lowercase.
So that's a very important thing that you must keep in mind, and you should always call your variable
a meaningful name.
It has to be related with what the variable is doing if you need to count the number of times a certain
thing happens.
You can call your variable count, for example, or number of times those are both meaningful names.
So that makes it easier for you also to realize what you are doing in your code, and that makes the
code more readable in case other people need to read your code.
Now, you cannot use any words that the C++ language reserves for syntax, for example, and using include
words like that are reserved.
So the programmer cannot use them to name variables.
When it comes to creating a variable, you must create all your variables before using, name them in
the program.
Otherwise, the program won't know what the word is unless you tell it explicitly beforehand.
So there are two ways of declaring variables you can define without initializing, and you can define
and initialize and what that means if you define a variable without initialization.
You basically are just putting aside memory for that variable.
You haven't decided on a type of data that you will store in your variable, but you don't have yet
a value to put in there.
So in this case, you will just do something like this and value that puts aside the memory for it.
The variable exists, but you don't really have anything meaningful there yet, as you have to assign
a value like this using the assignment operator that we previously talked about.
So one thing that I do want to mention if you do create a variable like this, a value may still exist
when you created that is called garbage.
It's either the data that already existed in that memory address previously or C++ just generates something
at the moment of creation.
So just be mindful of that when you use your variables.
If you have defined the dam but you have an initialize them, the desired values that you're trying
to use will not yet be there.
Now, in defining a variable with initialization, that means that you will create the variable and
assign it a value that you want it, you want it to have when it starts existing.
For example, this or this or this statement.
These are all the same.
You only need to use one of them.
But all these statements do the same thing.
They create a value variable.
And they put in a value 10 inside of it, and the type of variable will be in it.
So a big advantage when doing this is that you know exactly what value gets to be stored in your variable
and you can just use it right away.
So another thing that I want to touch on is the scope of a variable, so scope refers to the program
area that has access to your variable.
Like I said before, if you create a variable, you can use it afterwards, which in C++ is basically
in an in the lines below your variable definition.
So be mindful of that.
Define your variables before you use them.
I think that's enough talking about the scope right now, but we will touch on scope in a later lecture.
So the variable types we do have two main types of variable near our normal variables.
The ones that you can change.
The value that gets stored in them over and over again as many times as you want.
And there are something called constant variables where you create the variable and the value will not
change for the entire duration of the program.
It stays the same.
So for you to create a constant variable, you just need to put the constant word in in front of your
variable definition.
Like this, whether you, well, actually like with the constant variables, you have to initialize
them at the moment of creation because you can't create them and change them afterwards, since they
are not changeable.
So here we are talking about data types and data types.
Exist in C++, because that's just how they exist in real life, actually, because you can't add oranges
and apples together, they are different things.
So that's why we have types and the purpose of the type is to tell the variable what type of data they
can hold.
So a variable of a type is reserved space in memory, and each type occupies a different memory size.
So your type will dictate how much memory your variable will occupy in memory.
So when talking about data types, we have two different kinds, we have primitive data types, the
ones that are built in, and you can already use you, you start writing your program and you can use
them right away and you have non primitive data types.
We're only going to give examples of the non primitive data types, but we're going to talk about primitive
data types a little longer.
So here you can see all the primitive data types and you have Boolean character, integer floating point
and double floating point.
And you can see the sizes that these.
Data types occupy in most computer systems.
So non primitive data types, like I said before, some examples of it would be strings, drug classes,
unions, enemies and there is these are basically.
Programmer defined for the most part.
So you get to create these later on.
So we're going to start talking about the Boolean data type, and this is a very simple one.
So the Boolean will be either true or false.
It occupies one bite of memory.
And the key word for it is bool.
So here you can see an example of a bool variable that has been defined and initialized.