1
00:00:01,050 --> 00:00:06,360
Hello and welcome to a new lesson today, we're going to talk about variables and types.

2
00:00:06,960 --> 00:00:10,050
So the first thing that we need to talk about is a.

3
00:00:11,250 --> 00:00:15,540
Variable is a container that we use to store of value.

4
00:00:15,870 --> 00:00:18,600
We can name our variable whatever we want.

5
00:00:18,600 --> 00:00:25,590
But there are some rules to keep in mind that you must follow when we create the variable.

6
00:00:26,250 --> 00:00:35,490
And the first thing that we need to decide is what type of data we will actually host in our variable,

7
00:00:35,910 --> 00:00:40,530
because that will dictate what type of variable we need to create.

8
00:00:40,560 --> 00:00:48,870
So for example, if we want to create a variable that will just hold a whole number, we would make

9
00:00:48,870 --> 00:00:53,940
it a type and which is an integer, and we'll get to that in just a little bit.

10
00:00:54,420 --> 00:01:03,150
But your method thing that I want to say about variables is that the variable gets assigned a memory

11
00:01:03,150 --> 00:01:07,140
address where the value will be stored at.

12
00:01:07,710 --> 00:01:17,880
So to store a value in a variable, we will need to use the equals sign, also called an assignment

13
00:01:17,880 --> 00:01:18,810
operator.

14
00:01:19,170 --> 00:01:28,050
So I sort of drew this to demonstrate how a variable gets stored in memory.

15
00:01:28,230 --> 00:01:32,490
So let's pretend that this is our computers memory.

16
00:01:33,060 --> 00:01:44,640
And you can see that we are creating a new variable here called value, which is of type IND, so that

17
00:01:44,850 --> 00:01:52,440
the thing that happens here is that one memory address gets assigned to that variable.

18
00:01:53,590 --> 00:01:58,450
Or the variable gets assigned to a memory address that already exists.

19
00:01:59,050 --> 00:02:07,330
And now we are trying to sort of value 10 inside of that variable.

20
00:02:07,660 --> 00:02:17,500
What will happen is, as you can see, 10 will be stored exactly that memory address that the variable

21
00:02:17,500 --> 00:02:18,950
gets mapped with.

22
00:02:19,360 --> 00:02:27,700
So the computer will just go there to that memory whenever you call the value name.

23
00:02:29,320 --> 00:02:33,090
So naming a variable is very important.

24
00:02:33,100 --> 00:02:39,910
And here as well, there are a few rules to keep in mind when you create variables.

25
00:02:41,110 --> 00:02:49,810
A very important one is that you cannot use anything other than a letter or an underscore for the first

26
00:02:49,810 --> 00:02:51,880
character of the variable name.

27
00:02:52,660 --> 00:03:01,870
And then after that first character, you can use any number, any combinations of numbers or letters.

28
00:03:02,860 --> 00:03:05,650
Uppercase and lowercase will be different.

29
00:03:05,650 --> 00:03:14,350
So if you create a variable that's called value and it's all lowercase, it won't be the same as creating

30
00:03:14,650 --> 00:03:21,370
a variable called value that starts with a capital V. But everything else is lowercase.

31
00:03:21,580 --> 00:03:29,890
So that's a very important thing that you must keep in mind, and you should always call your variable

32
00:03:29,890 --> 00:03:31,360
a meaningful name.

33
00:03:32,260 --> 00:03:39,820
It has to be related with what the variable is doing if you need to count the number of times a certain

34
00:03:39,820 --> 00:03:40,690
thing happens.

35
00:03:40,960 --> 00:03:48,250
You can call your variable count, for example, or number of times those are both meaningful names.

36
00:03:49,270 --> 00:03:56,260
So that makes it easier for you also to realize what you are doing in your code, and that makes the

37
00:03:56,260 --> 00:04:00,100
code more readable in case other people need to read your code.

38
00:04:01,270 --> 00:04:12,790
Now, you cannot use any words that the C++ language reserves for syntax, for example, and using include

39
00:04:13,870 --> 00:04:15,640
words like that are reserved.

40
00:04:16,090 --> 00:04:20,320
So the programmer cannot use them to name variables.

41
00:04:23,050 --> 00:04:34,330
When it comes to creating a variable, you must create all your variables before using, name them in

42
00:04:34,330 --> 00:04:35,650
the program.

43
00:04:36,220 --> 00:04:43,750
Otherwise, the program won't know what the word is unless you tell it explicitly beforehand.

44
00:04:44,290 --> 00:04:54,400
So there are two ways of declaring variables you can define without initializing, and you can define

45
00:04:54,400 --> 00:05:01,060
and initialize and what that means if you define a variable without initialization.

46
00:05:01,390 --> 00:05:06,820
You basically are just putting aside memory for that variable.

47
00:05:07,240 --> 00:05:15,520
You haven't decided on a type of data that you will store in your variable, but you don't have yet

48
00:05:15,520 --> 00:05:18,370
a value to put in there.

49
00:05:18,790 --> 00:05:28,340
So in this case, you will just do something like this and value that puts aside the memory for it.

50
00:05:28,360 --> 00:05:37,180
The variable exists, but you don't really have anything meaningful there yet, as you have to assign

51
00:05:37,180 --> 00:05:44,410
a value like this using the assignment operator that we previously talked about.

52
00:05:44,860 --> 00:05:53,890
So one thing that I do want to mention if you do create a variable like this, a value may still exist

53
00:05:55,600 --> 00:05:58,660
when you created that is called garbage.

54
00:05:59,110 --> 00:06:11,200
It's either the data that already existed in that memory address previously or C++ just generates something

55
00:06:11,200 --> 00:06:13,000
at the moment of creation.

56
00:06:13,180 --> 00:06:17,770
So just be mindful of that when you use your variables.

57
00:06:17,770 --> 00:06:26,320
If you have defined the dam but you have an initialize them, the desired values that you're trying

58
00:06:26,320 --> 00:06:29,740
to use will not yet be there.

59
00:06:31,330 --> 00:06:38,200
Now, in defining a variable with initialization, that means that you will create the variable and

60
00:06:38,200 --> 00:06:45,430
assign it a value that you want it, you want it to have when it starts existing.

61
00:06:45,730 --> 00:06:51,910
For example, this or this or this statement.

62
00:06:51,940 --> 00:06:53,320
These are all the same.

63
00:06:53,320 --> 00:06:55,360
You only need to use one of them.

64
00:06:56,020 --> 00:06:58,510
But all these statements do the same thing.

65
00:06:59,380 --> 00:07:01,930
They create a value variable.

66
00:07:03,090 --> 00:07:11,030
And they put in a value 10 inside of it, and the type of variable will be in it.

67
00:07:11,830 --> 00:07:22,800
So a big advantage when doing this is that you know exactly what value gets to be stored in your variable

68
00:07:22,800 --> 00:07:25,170
and you can just use it right away.

69
00:07:26,340 --> 00:07:34,710
So another thing that I want to touch on is the scope of a variable, so scope refers to the program

70
00:07:34,710 --> 00:07:37,620
area that has access to your variable.

71
00:07:38,280 --> 00:07:47,370
Like I said before, if you create a variable, you can use it afterwards, which in C++ is basically

72
00:07:47,370 --> 00:07:53,730
in an in the lines below your variable definition.

73
00:07:56,070 --> 00:07:58,800
So be mindful of that.

74
00:07:59,850 --> 00:08:03,060
Define your variables before you use them.

75
00:08:03,810 --> 00:08:11,340
I think that's enough talking about the scope right now, but we will touch on scope in a later lecture.

76
00:08:12,480 --> 00:08:22,020
So the variable types we do have two main types of variable near our normal variables.

77
00:08:22,920 --> 00:08:25,560
The ones that you can change.

78
00:08:25,560 --> 00:08:30,510
The value that gets stored in them over and over again as many times as you want.

79
00:08:30,990 --> 00:08:40,530
And there are something called constant variables where you create the variable and the value will not

80
00:08:40,530 --> 00:08:45,120
change for the entire duration of the program.

81
00:08:45,420 --> 00:08:46,740
It stays the same.

82
00:08:47,460 --> 00:08:57,030
So for you to create a constant variable, you just need to put the constant word in in front of your

83
00:08:57,030 --> 00:08:58,530
variable definition.

84
00:08:58,530 --> 00:09:05,370
Like this, whether you, well, actually like with the constant variables, you have to initialize

85
00:09:05,370 --> 00:09:13,020
them at the moment of creation because you can't create them and change them afterwards, since they

86
00:09:13,020 --> 00:09:14,610
are not changeable.

87
00:09:16,520 --> 00:09:23,990
So here we are talking about data types and data types.

88
00:09:25,610 --> 00:09:34,340
Exist in C++, because that's just how they exist in real life, actually, because you can't add oranges

89
00:09:34,340 --> 00:09:36,860
and apples together, they are different things.

90
00:09:37,220 --> 00:09:46,790
So that's why we have types and the purpose of the type is to tell the variable what type of data they

91
00:09:46,790 --> 00:09:47,540
can hold.

92
00:09:47,900 --> 00:09:58,070
So a variable of a type is reserved space in memory, and each type occupies a different memory size.

93
00:09:59,000 --> 00:10:06,140
So your type will dictate how much memory your variable will occupy in memory.

94
00:10:07,530 --> 00:10:16,740
So when talking about data types, we have two different kinds, we have primitive data types, the

95
00:10:16,740 --> 00:10:24,510
ones that are built in, and you can already use you, you start writing your program and you can use

96
00:10:24,510 --> 00:10:28,830
them right away and you have non primitive data types.

97
00:10:29,160 --> 00:10:35,520
We're only going to give examples of the non primitive data types, but we're going to talk about primitive

98
00:10:35,520 --> 00:10:37,350
data types a little longer.

99
00:10:38,010 --> 00:10:48,510
So here you can see all the primitive data types and you have Boolean character, integer floating point

100
00:10:48,510 --> 00:10:49,980
and double floating point.

101
00:10:51,700 --> 00:10:55,120
And you can see the sizes that these.

102
00:10:57,320 --> 00:11:03,080
Data types occupy in most computer systems.

103
00:11:05,540 --> 00:11:15,650
So non primitive data types, like I said before, some examples of it would be strings, drug classes,

104
00:11:15,650 --> 00:11:22,130
unions, enemies and there is these are basically.

105
00:11:23,100 --> 00:11:27,000
Programmer defined for the most part.

106
00:11:27,150 --> 00:11:29,940
So you get to create these later on.

107
00:11:30,570 --> 00:11:39,540
So we're going to start talking about the Boolean data type, and this is a very simple one.

108
00:11:39,900 --> 00:11:44,250
So the Boolean will be either true or false.

109
00:11:44,460 --> 00:11:48,360
It occupies one bite of memory.

110
00:11:48,600 --> 00:11:50,700
And the key word for it is bool.

111
00:11:51,570 --> 00:12:01,200
So here you can see an example of a bool variable that has been defined and initialized.
