WEBVTT

00:01.020 --> 00:08.210
In this lecture I want to go over the fundamental building blocks of all programs variable's most basic

00:08.240 --> 00:15.080
and important things computer can do is to store data for later use in calculations to do this.

00:15.080 --> 00:22.690
We use what is called the variable variable is a container that is used to store information in memory.

00:22.730 --> 00:30.390
They are able to be labeled or named by the programmer in order to be able to be more understandable.

00:30.520 --> 00:33.540
The name of a variable is called an identifier.

00:33.580 --> 00:40.750
You'll be seeing that word a lot when you make mistakes with the compiler just know that an identifier

00:40.750 --> 00:46.930
is what we call something that the programmer has named of course the definition of variable actually

00:46.930 --> 00:48.690
means it can be changed.

00:48.820 --> 00:52.510
And we use the assignment operator to do this.

00:52.590 --> 01:00.480
The last thing we need to know about variables and C++ is that they all have a specific type all variables

01:00.480 --> 01:08.220
are stored somewhere in memory as ones and zeroes are programs usually don't need to know or care about

01:08.220 --> 01:15.070
the exact location of a variable because we can just refer to it by the identifier.

01:15.150 --> 01:22.680
It does need to know however the kind of data that is stored to interpret it properly storing a number

01:22.680 --> 01:25.770
is different than storing say a character.

01:25.830 --> 01:30.980
In fact depending on the type of number it is it might require a different type to.

01:31.010 --> 01:36.420
So let's go through some basic types and write some examples of variables.

01:36.440 --> 01:38.810
So here's our first example.

01:40.040 --> 01:47.140
Rissi int x that's the name of our variable This is our identifier.

01:47.400 --> 01:50.040
And we're going to end it with a semi-colon

01:52.450 --> 01:56.560
so the first part is the type.

01:56.680 --> 02:02.200
This is this entier is the type of our variable.

02:02.200 --> 02:09.340
So it is short for what's called an integer which is and which is a whole number so a number with no

02:09.340 --> 02:12.570
fractional parts no decimal point after it.

02:12.610 --> 02:22.370
So for example it could be something like 10 or 3 but it can't be something like three point one.

02:22.370 --> 02:26.850
Say that's no good.

02:26.900 --> 02:31.580
So the names of all variables have some restrictions.

02:32.770 --> 02:36.090
Our names can only be made up of letters and digits.

02:36.100 --> 02:39.610
But the first character must be a letter.

02:39.680 --> 02:46.170
We can use underscores since they are also considered letters here characters.

02:47.000 --> 02:55.850
So our convention for our variables will be something called camel case so let me show you an example

02:55.850 --> 02:57.210
of camel case.

02:58.860 --> 03:00.440
In case will

03:05.140 --> 03:17.110
so note here that we have each word except for the very first word will be uppercase So the first letter

03:17.110 --> 03:24.160
of every word after the first word is uppercase is what's called camel case and this is going to be

03:24.160 --> 03:31.650
our convention when we make our programs.

03:31.700 --> 03:36.740
So you may wonder why we have a semi-colon after all are variables.

03:37.840 --> 03:47.200
Because in C and C++ the whole line is considered a statement and all statements must end with a semi-colon

03:47.770 --> 03:49.590
in our variable x.

03:49.600 --> 03:50.780
Example here.

03:52.210 --> 03:54.540
The.

03:54.810 --> 04:04.740
This is what's called a declaration which is just an introduction for the compiler so it understands

04:05.070 --> 04:11.880
that there is an identifier that's going to be called X and it's going to be used for later use.

04:11.880 --> 04:17.970
So let's start changing our variables since that's the whole point of very well.

04:18.040 --> 04:23.050
So let's say we have it's the value 10.

04:23.580 --> 04:28.750
Notice how similar this is to our examples we did in the last section.

04:28.860 --> 04:31.380
So we have some variable that we have.

04:31.680 --> 04:45.850
And then this is the assignment signs this equal sign this one equals sign assigns X the value 10 so

04:46.530 --> 04:54.790
note that it doesn't test for equality it's actually like giving it real value so and of course this

04:54.790 --> 04:56.100
is a statement as well.

04:56.130 --> 04:58.290
So always needs a semi-colon.

05:00.530 --> 05:12.010
So if we tried to print out X let's say you x is

05:14.910 --> 05:20.920
print x in it Bill that it's

05:32.360 --> 05:39.260
so there's Council value of x is that right.

05:39.430 --> 05:43.060
So it works exactly the way you think it should.

05:43.060 --> 05:52.550
You know just give you the output of 10 because we signed to be 10 so you may wonder then if we didn't

05:52.550 --> 05:59.700
give it the value 10 what would happen.

05:59.720 --> 06:01.550
So what would it output.

06:01.610 --> 06:09.230
So let's see.

06:09.440 --> 06:19.310
See it's giving me some warnings of saying it's not initialize it doesn't have default value.

06:19.510 --> 06:22.000
Let's ignore all that.

06:22.000 --> 06:25.880
So the value is zero which is kind of interesting.

06:27.730 --> 06:29.800
So I'm assuming then that

06:32.580 --> 06:38.880
gcc or clip's or something gives it a default value at the start.

06:38.880 --> 06:43.020
This isn't always the case for different compilers.

06:43.050 --> 06:47.680
Usually it's just given whatever value that existed before.

06:47.910 --> 06:59.240
So if you made a variable and you didn't assign a default value then wherever that variable is in memory

06:59.630 --> 07:03.970
should have some value associated with it and they'll just print out that value.

07:03.980 --> 07:08.660
Usually it's just some some kind of garbage value that you probably don't want.

07:08.660 --> 07:14.800
So what we should always do when we make a variable is give it some default value.

07:14.830 --> 07:21.680
So here in this case zero now you're never going to have a problem.

07:21.730 --> 07:22.540
Always be

07:27.030 --> 07:28.840
something that we actually care about

07:31.950 --> 07:35.250
so let's take a look at some other built in types.

07:35.250 --> 07:41.540
Let's say we have this is a character type char.

07:41.880 --> 07:49.650
Char c is going to be a here and we can print this out as well.

07:51.700 --> 07:55.310
Now you should give it should print out a worse

07:59.250 --> 08:01.650
see is

08:10.890 --> 08:13.220
so the value of C is right.

08:14.870 --> 08:25.620
So here this char and we call it C remember is the identifier and all char's will always have this kind

08:25.620 --> 08:30.740
of format ever is going to give it some kind of literal value like a.

08:31.020 --> 08:33.770
And always have the single quotes around it.

08:33.780 --> 08:41.810
So if you want to have some specific value for a char c always have these quotes especially if it's

08:41.810 --> 08:45.110
a letter you know it's one of these quotes here.

08:47.900 --> 08:51.200
So let's look at some other ones.

08:51.220 --> 08:56.790
There's also a floating point number.

08:57.240 --> 09:03.180
So let's say pi 3.1 for pi or 8.

09:04.550 --> 09:14.200
It's not exact but now that you know you pi is

09:26.270 --> 09:33.070
so and it prints out 3.1 for so floating point or float.

09:33.070 --> 09:38.430
Here stands for floating point number which is represents real value.

09:38.440 --> 09:45.430
So it has it can be something like three point 1 4 1 5 etc..

09:47.150 --> 09:56.570
Unlike an int which can only be a whole number like pen and these floating point numbers come with different

09:56.570 --> 10:00.970
precisions precisions get pretty complicated.

10:01.190 --> 10:08.830
But just know that you really need much more precision than floating point type in this course anyway.

10:09.170 --> 10:15.820
So if you want to add more precision say we have something called a double

10:19.200 --> 10:21.990
double pi.

10:22.220 --> 10:47.590
Let's say 1 for 1.

10:47.730 --> 10:51.750
Print this out.

10:51.840 --> 10:55.140
See it prints out the whole thing.

10:55.150 --> 11:04.800
In fact this probably can have this level of precision as well.

11:04.840 --> 11:07.900
So if you really want a lot of precision.

11:07.900 --> 11:13.990
Use a double instead you can have many many more

11:17.510 --> 11:23.720
although guess not for this one but usually it shouldn't really matter too much.

11:26.310 --> 11:30.680
So the amount of precision really will depend on your system.

11:30.960 --> 11:37.380
And we're not really going to worry about it too much in this course but either just know either float

11:37.380 --> 11:40.860
or double or we'll do just fine for what we need.

11:41.860 --> 11:46.370
So we're going to talk more about types in the next lecture.
