In this lecture I want to go over the fundamental building blocks of all programs variable's most basic
and important things computer can do is to store data for later use in calculations to do this.
We use what is called the variable variable is a container that is used to store information in memory.
They are able to be labeled or named by the programmer in order to be able to be more understandable.
The name of a variable is called an identifier.
You'll be seeing that word a lot when you make mistakes with the compiler just know that an identifier
is what we call something that the programmer has named of course the definition of variable actually
means it can be changed.
And we use the assignment operator to do this.
The last thing we need to know about variables and C++ is that they all have a specific type all variables
are stored somewhere in memory as ones and zeroes are programs usually don't need to know or care about
the exact location of a variable because we can just refer to it by the identifier.
It does need to know however the kind of data that is stored to interpret it properly storing a number
is different than storing say a character.
In fact depending on the type of number it is it might require a different type to.
So let's go through some basic types and write some examples of variables.
So here's our first example.
Rissi int x that's the name of our variable This is our identifier.
And we're going to end it with a semi-colon
so the first part is the type.
This is this entier is the type of our variable.
So it is short for what's called an integer which is and which is a whole number so a number with no
fractional parts no decimal point after it.
So for example it could be something like 10 or 3 but it can't be something like three point one.
Say that's no good.
So the names of all variables have some restrictions.
Our names can only be made up of letters and digits.
But the first character must be a letter.
We can use underscores since they are also considered letters here characters.
So our convention for our variables will be something called camel case so let me show you an example
of camel case.
In case will
so note here that we have each word except for the very first word will be uppercase So the first letter
of every word after the first word is uppercase is what's called camel case and this is going to be
our convention when we make our programs.
So you may wonder why we have a semi-colon after all are variables.
Because in C and C++ the whole line is considered a statement and all statements must end with a semi-colon
in our variable x.
Example here.
The.
This is what's called a declaration which is just an introduction for the compiler so it understands
that there is an identifier that's going to be called X and it's going to be used for later use.
So let's start changing our variables since that's the whole point of very well.
So let's say we have it's the value 10.
Notice how similar this is to our examples we did in the last section.
So we have some variable that we have.
And then this is the assignment signs this equal sign this one equals sign assigns X the value 10 so
note that it doesn't test for equality it's actually like giving it real value so and of course this
is a statement as well.
So always needs a semi-colon.
So if we tried to print out X let's say you x is
print x in it Bill that it's
so there's Council value of x is that right.
So it works exactly the way you think it should.
You know just give you the output of 10 because we signed to be 10 so you may wonder then if we didn't
give it the value 10 what would happen.
So what would it output.
So let's see.
See it's giving me some warnings of saying it's not initialize it doesn't have default value.
Let's ignore all that.
So the value is zero which is kind of interesting.
So I'm assuming then that
gcc or clip's or something gives it a default value at the start.
This isn't always the case for different compilers.
Usually it's just given whatever value that existed before.
So if you made a variable and you didn't assign a default value then wherever that variable is in memory
should have some value associated with it and they'll just print out that value.
Usually it's just some some kind of garbage value that you probably don't want.
So what we should always do when we make a variable is give it some default value.
So here in this case zero now you're never going to have a problem.
Always be
something that we actually care about
so let's take a look at some other built in types.
Let's say we have this is a character type char.
Char c is going to be a here and we can print this out as well.
Now you should give it should print out a worse
see is
so the value of C is right.
So here this char and we call it C remember is the identifier and all char's will always have this kind
of format ever is going to give it some kind of literal value like a.
And always have the single quotes around it.
So if you want to have some specific value for a char c always have these quotes especially if it's
a letter you know it's one of these quotes here.
So let's look at some other ones.
There's also a floating point number.
So let's say pi 3.1 for pi or 8.
It's not exact but now that you know you pi is
so and it prints out 3.1 for so floating point or float.
Here stands for floating point number which is represents real value.
So it has it can be something like three point 1 4 1 5 etc..
Unlike an int which can only be a whole number like pen and these floating point numbers come with different
precisions precisions get pretty complicated.
But just know that you really need much more precision than floating point type in this course anyway.
So if you want to add more precision say we have something called a double
double pi.
Let's say 1 for 1.
Print this out.
See it prints out the whole thing.
In fact this probably can have this level of precision as well.
So if you really want a lot of precision.
Use a double instead you can have many many more
although guess not for this one but usually it shouldn't really matter too much.
So the amount of precision really will depend on your system.
And we're not really going to worry about it too much in this course but either just know either float
or double or we'll do just fine for what we need.
So we're going to talk more about types in the next lecture.