1 00:00:00,720 --> 00:00:02,570 Using types and variables. 2 00:00:02,580 --> 00:00:08,370 So types will be covered in more detail in the next lectures, but it's useful to give a basic information 3 00:00:08,370 --> 00:00:08,700 here. 4 00:00:08,700 --> 00:00:13,620 So Cplusplus is a strong type language as we mentioned earlier. 5 00:00:13,620 --> 00:00:18,540 So which means that you have to declare the type of the variable that you use. 6 00:00:18,660 --> 00:00:25,980 So the reason for this is that the compiler needs to know how much memory to allocate for the variable, 7 00:00:25,980 --> 00:00:29,850 and it can determine this by the type of the variable. 8 00:00:29,850 --> 00:00:38,580 So in addition to the compiler needs to know how to initialize a variable if it has not been explicitly 9 00:00:38,580 --> 00:00:39,510 initialized. 10 00:00:39,510 --> 00:00:47,670 And to perform this initialization, the compiler needs to know the type of the variable so the cplusplus 11 00:00:47,670 --> 00:00:52,590 11 provides with the auto keyword which you will learn. 12 00:00:52,590 --> 00:00:59,880 So which relaxes this concept of strong typing and it will be covered in the next chapters. 13 00:00:59,880 --> 00:01:06,820 So however, the type checking for the compiler is so important that you should use type checking as 14 00:01:06,820 --> 00:01:14,500 much as possible so cplusplus variables can be declared anywhere in your code as long as they are declared 15 00:01:14,500 --> 00:01:15,970 before they are used. 16 00:01:15,970 --> 00:01:22,210 So where you declare a variable determines how you use it. 17 00:01:22,210 --> 00:01:25,720 So this is called the scope of variable. 18 00:01:25,720 --> 00:01:32,560 So in general, it is best to declare the variable as close as possible to where you will use it. 19 00:01:32,560 --> 00:01:39,790 And within the most restrictive scope, this prevents name clashes where you will have to add additional 20 00:01:39,790 --> 00:01:44,590 information to disambiguate two or more variables. 21 00:01:44,590 --> 00:01:50,080 So you may and should give your variables descriptive names. 22 00:01:50,080 --> 00:01:55,060 So this makes your code much more readable and easier to understand. 23 00:01:55,060 --> 00:02:03,540 So Cplusplus must start with the alphabetic character or an underscore so you can start. 24 00:02:03,550 --> 00:02:05,340 So for example, Cplusplus starts. 25 00:02:05,350 --> 00:02:11,320 I have to start with alphabetic character or an underscore so you can write variables like this. 26 00:02:11,350 --> 00:02:12,190 INT for example. 27 00:02:12,190 --> 00:02:14,800 Integer my variable. 28 00:02:14,800 --> 00:02:18,520 And here, as you can see, there is no error at all. 29 00:02:18,520 --> 00:02:23,110 And you can write your variables by. 30 00:02:24,520 --> 00:02:24,820 Mhm. 31 00:02:25,240 --> 00:02:28,250 To upper score variable. 32 00:02:28,270 --> 00:02:29,380 My variable. 33 00:02:30,320 --> 00:02:36,230 To you can underscore my very well. 34 00:02:40,160 --> 00:02:45,650 Two and integer my. 35 00:02:46,750 --> 00:02:48,790 My variable. 36 00:02:50,240 --> 00:02:51,380 You're here. 37 00:02:51,920 --> 00:02:57,050 So as you can see here, these are names are the almost same, except there's a underscore here. 38 00:02:57,050 --> 00:02:58,820 But these three names are same. 39 00:02:58,820 --> 00:03:04,250 But as you can see, we didn't get an error If you write this exactly like this underscores and underscores 40 00:03:04,250 --> 00:03:09,200 like this, for example, you will get an error because of this redefinition. 41 00:03:09,200 --> 00:03:14,720 So we have to remove the previous declaration of a local variable, as Cplusplus suggests. 42 00:03:14,720 --> 00:03:17,090 So let's compile it and see what happens. 43 00:03:17,090 --> 00:03:24,980 So the declaration of an integer variable note here, integer variable previously declared here. 44 00:03:24,980 --> 00:03:31,610 When we click on this here, as you can see, we can not declare the same names, but if we change this 45 00:03:32,330 --> 00:03:40,310 here, this alphabet here with make it and make it underscore they are no longer the same variable here 46 00:03:40,320 --> 00:03:41,720 as let's try it. 47 00:03:41,720 --> 00:03:42,470 So. 48 00:03:44,590 --> 00:03:45,190 Here. 49 00:03:45,940 --> 00:03:48,620 And the C plus plus names are case sensitive. 50 00:03:48,640 --> 00:03:51,460 As I said earlier, and the first 51 00:03:52,090 --> 00:03:59,320 2084, 2048 characters are significant. 52 00:03:59,320 --> 00:04:05,290 So you can start variable name with an underscore, but you cannot use two underscores here, as you 53 00:04:05,290 --> 00:04:06,340 can see here. 54 00:04:06,580 --> 00:04:08,470 Variable here. 55 00:04:10,890 --> 00:04:14,760 So now you can use an underscore followed by a capital letter here. 56 00:04:18,350 --> 00:04:22,610 As you can see, local variable is only assigned but never accessed. 57 00:04:22,640 --> 00:04:23,180 Here. 58 00:04:24,290 --> 00:04:25,840 We got no error here. 59 00:04:25,850 --> 00:04:33,710 So and clearly you cannot use type name as variable names either built in type names, for example, 60 00:04:34,040 --> 00:04:34,580 integer. 61 00:04:34,580 --> 00:04:43,700 So we can use the long as a variable name because it's a keyword and it's reserved for it's reserved 62 00:04:43,700 --> 00:04:44,210 keyword. 63 00:04:44,210 --> 00:04:50,750 So or we cannot use while or other keywords in our language here, as you can see here. 64 00:04:51,380 --> 00:04:57,320 So you declare a variable in a statement ending with a semicolon here, as you can see here. 65 00:04:57,320 --> 00:05:07,430 So the basic syntax of declaring a variable is that you specify the type, then the name and optionally 66 00:05:07,430 --> 00:05:09,710 an initialization variable. 67 00:05:09,710 --> 00:05:16,100 Here, this is a type name and initialization variable. 68 00:05:16,100 --> 00:05:20,570 So built in types must be initialized before you use them. 69 00:05:20,570 --> 00:05:32,990 As you can see here, Integer E here, let's increment E by one and here STD see out and E here. 70 00:05:33,530 --> 00:05:40,280 And let's actually let's make a return statement here and let's run this program here. 71 00:05:40,280 --> 00:05:41,840 As you can see, we got an error. 72 00:05:41,840 --> 00:05:43,580 We got not error. 73 00:05:43,610 --> 00:05:44,930 This is a correct program. 74 00:05:45,620 --> 00:05:48,680 This has this program compiled successfully. 75 00:05:48,680 --> 00:05:51,110 And as you can see here, there is a output one. 76 00:05:51,110 --> 00:05:56,660 So this gave us output one, this is the zero by default, as you know earlier. 77 00:05:56,660 --> 00:06:02,240 And we incremented increment by one here, as you can see here. 78 00:06:02,240 --> 00:06:08,330 And we and so zero plus one equals one. 79 00:06:08,330 --> 00:06:10,490 And we got this one here. 80 00:06:10,580 --> 00:06:16,160 So there are essentially three ways to initialize variables. 81 00:06:16,790 --> 00:06:25,440 You can assign a value, you can call a type, you can call the type constructor, or you can initialize 82 00:06:25,440 --> 00:06:27,540 a variable using function syntax. 83 00:06:27,540 --> 00:06:33,810 By the way, you will learn constructors for classes in next lectures. 84 00:06:33,810 --> 00:06:44,040 So let's create some program here, some syntax here, The code here A equals one and assign it one 85 00:06:44,040 --> 00:06:53,430 integer be integer two and integer C here three. 86 00:06:53,700 --> 00:06:57,240 So now I want to explain what these are. 87 00:06:57,240 --> 00:07:07,380 So obviously they are almost they they we can compile our program without a problem and actually E equals 88 00:07:07,380 --> 00:07:10,440 to one, B equals to two and C equals to three. 89 00:07:10,440 --> 00:07:13,980 I will show you what these are here. 90 00:07:14,310 --> 00:07:14,670 A. 91 00:07:17,640 --> 00:07:19,620 So firstly, a. 92 00:07:20,680 --> 00:07:22,990 Then the AB. 93 00:07:25,480 --> 00:07:27,850 Okay, then. 94 00:07:28,870 --> 00:07:29,770 See? 95 00:07:33,320 --> 00:07:35,350 And and yeah that's it. 96 00:07:35,360 --> 00:07:36,920 And new line here. 97 00:07:36,920 --> 00:07:42,380 So let's A, B, C and let's compile our program. 98 00:07:44,370 --> 00:07:44,940 UPS. 99 00:07:52,620 --> 00:07:53,160 Yeah. 100 00:07:54,250 --> 00:07:55,740 Let's delete this. 101 00:07:55,780 --> 00:07:56,200 C. 102 00:07:56,230 --> 00:07:56,490 A. 103 00:07:56,530 --> 00:07:56,770 B. 104 00:07:56,770 --> 00:07:57,550 C. 105 00:08:00,750 --> 00:08:01,170 Here. 106 00:08:02,980 --> 00:08:09,610 As you can see here, we got this and there are three legal cplusplus. 107 00:08:09,610 --> 00:08:14,890 But statistically, the first is the better because it is more obvious. 108 00:08:14,890 --> 00:08:16,750 So the variable is an integer. 109 00:08:16,750 --> 00:08:21,250 It's called a and it's assigned to value one. 110 00:08:21,370 --> 00:08:23,440 The third look confusing. 111 00:08:23,440 --> 00:08:30,870 As you can see here, it looks like the declaration of function when it is actually declaring variable. 112 00:08:32,110 --> 00:08:39,850 The next lecture will have a a variation of assigning a value using the initialization list syntax. 113 00:08:39,850 --> 00:08:48,370 So is the reason why you will want to do this will be you will learn that in the next sections. 114 00:08:48,370 --> 00:08:49,060 So. 115 00:08:51,220 --> 00:08:57,280 And and also you will learn classes will cover classes, your own custom types. 116 00:08:57,280 --> 00:09:03,640 So a custom type may be defined to have a default value, which means that you have you may decide not 117 00:09:03,640 --> 00:09:07,360 to initialize a variable of a custom type before using it. 118 00:09:07,360 --> 00:09:13,180 So however, this will result in a poorer performance because the compiler will initialize the variable 119 00:09:13,180 --> 00:09:18,580 with a default value and subsequently your code will sign a value. 120 00:09:18,580 --> 00:09:23,020 So resulting in a sign being performed twice here. 121 00:09:23,020 --> 00:09:28,780 So let's let's print this with here. 122 00:09:29,170 --> 00:09:30,100 Let's print this. 123 00:09:32,570 --> 00:09:33,860 So let's print this with. 124 00:09:34,940 --> 00:09:37,010 Uh, see our function here. 125 00:09:37,040 --> 00:09:38,060 See out. 126 00:09:40,660 --> 00:09:41,560 Stdcout. 127 00:09:41,680 --> 00:09:45,010 Stdcout here. 128 00:09:45,010 --> 00:09:47,440 And C here. 129 00:09:49,530 --> 00:09:49,830 Oops. 130 00:09:51,730 --> 00:10:00,070 As you can see here, if we delete this, see, as you can see, we got an error format specifier w.r.t. 131 00:10:01,450 --> 00:10:03,970 The argument has a type init here. 132 00:10:03,970 --> 00:10:08,140 As you can see here, our program is. 133 00:10:10,210 --> 00:10:11,140 Fixed here. 134 00:10:11,140 --> 00:10:14,910 And let's delete this also here. 135 00:10:14,920 --> 00:10:15,940 Actually, no. 136 00:10:17,380 --> 00:10:17,860 Here. 137 00:10:18,790 --> 00:10:20,570 As you can see the container. 138 00:10:20,590 --> 00:10:28,680 So I want to say why we got this hurt and this strange number. 139 00:10:28,690 --> 00:10:32,200 It's because of we need to. 140 00:10:33,820 --> 00:10:36,480 And as you can see there, a C here. 141 00:10:36,490 --> 00:10:42,190 Let's delete this and you will see our program has fixed the error here. 142 00:10:42,190 --> 00:10:44,740 We used C format specifier. 143 00:10:44,740 --> 00:10:56,920 So if you put this operator before in printf function before any alphabet here you this C will turn 144 00:10:56,920 --> 00:10:59,440 into just some format specifier here. 145 00:10:59,440 --> 00:11:08,170 That's because you have to do the space here and our code will fix here as you can see here. 146 00:11:08,170 --> 00:11:09,700 Let's run it again. 147 00:11:09,700 --> 00:11:18,520 And as you can see, we got an A one, two, three and there is a three times three assignment ways 148 00:11:18,520 --> 00:11:19,750 in C plus plus. 149 00:11:19,750 --> 00:11:22,030 So I'm waiting you in next lecture.