1 00:00:05,500 --> 00:00:09,860 In this video, we'll learn what variables are from a conceptual perspective. 2 00:00:09,860 --> 00:00:13,360 This will allow you to better understand how to use variables in your programs. 3 00:00:14,360 --> 00:00:16,660 I'm assuming in this course that everyone is familiar with 4 00:00:16,660 --> 00:00:19,560 the basic architecture of a typical computer system 5 00:00:19,560 --> 00:00:22,110 that is memory, a CPU 6 00:00:22,110 --> 00:00:26,010 and a bus that allows the movement of data between the CPU and memory. 7 00:00:26,010 --> 00:00:30,210 Random access memory or ram is a contiguous block of storage 8 00:00:30,210 --> 00:00:32,210 used by the computer to store information. 9 00:00:32,610 --> 00:00:36,210 This information can include computer instructions as well as data. 10 00:00:37,110 --> 00:00:39,910 Ram can be thought of as having memory cells 11 00:00:39,910 --> 00:00:42,510 where each cell has an associated location. 12 00:00:43,310 --> 00:00:46,710 If we had to program using the specific memory locations, 13 00:00:46,710 --> 00:00:50,310 it wouldn't be a lot of fun, and we'd likely have a lot of programmer errors. 14 00:00:50,910 --> 00:00:55,810 Imagine saying something like move 21 to memory location 1002. 15 00:00:56,810 --> 00:01:00,410 In many very low level languages, this is sometimes what we do. 16 00:01:00,660 --> 00:01:04,560 However, most programming languages allow you to associate a name 17 00:01:04,560 --> 00:01:05,860 with a memory location. 18 00:01:06,460 --> 00:01:08,580 Let's see the same example using a variable. 19 00:01:10,280 --> 00:01:14,540 In this example, you can see that the memory location 1002has been associated 20 00:01:14,540 --> 00:01:18,740 with the name age. In computing this is called a binding, 21 00:01:18,740 --> 00:01:21,100 and we can move 21 to age. 22 00:01:21,100 --> 00:01:24,800 This allows us not to have to worry about what exact memory location 23 00:01:24,800 --> 00:01:26,160 age is associated with. 24 00:01:26,960 --> 00:01:28,760 If we run the program again, 25 00:01:28,760 --> 00:01:31,560 age will likely be associated with a different memory address. 26 00:01:31,560 --> 00:01:35,860 And that's okay since we never knew about it in the first place. Our code will still work. 27 00:01:36,660 --> 00:01:39,660 Also thinking of names that make sense to us 28 00:01:39,660 --> 00:01:42,020 makes writing and reading programs much easier. 29 00:01:42,620 --> 00:01:45,220 That's the basic idea behind names and variables. 30 00:01:45,580 --> 00:01:49,780 Of course, since age is a variable that means that its contents can vary. 31 00:01:50,080 --> 00:01:55,070 So if we turn 22 years old, we can change the value of age from 21 to 22. 32 00:01:56,870 --> 00:02:01,070 So to recap, a variable is an abstraction for a memory location. 33 00:02:01,670 --> 00:02:05,870 We use abstractions all the time in programming to make it easier for us to deal 34 00:02:05,870 --> 00:02:06,670 with complexity. 35 00:02:07,470 --> 00:02:10,970 Variables allow us to use a meaningful name to represent a value. 36 00:02:11,630 --> 00:02:16,030 It's important to understand that variables have two main properties. First is their type. 37 00:02:16,430 --> 00:02:20,790 The compiler needs to know what types of values are legal to store in age. 38 00:02:21,590 --> 00:02:23,590 In the first code snippet the left, 39 00:02:23,590 --> 00:02:26,190 we tell the compiler to store 21 in age, 40 00:02:26,690 --> 00:02:29,690 but we never told the compiler what is valid to store an age. 41 00:02:30,190 --> 00:02:34,050 This will produce a compiler error telling us that age hasn't been declared. 42 00:02:34,650 --> 00:02:38,850 In the code snippet on the right, we first declare age to be an integer. 43 00:02:38,850 --> 00:02:42,350 Now the compiler knows that only integers are allowed to be stored in age. 44 00:02:42,550 --> 00:02:45,150 This is called static typing because the compiler 45 00:02:45,150 --> 00:02:49,150 is enforcing these rules when the program is compiled rather than when the program 46 00:02:49,150 --> 00:02:50,150 is executing. 47 00:02:50,510 --> 00:02:53,610 So remember, variables must be declared 48 00:02:53,610 --> 00:02:55,810 before you use them in c++. 49 00:02:56,610 --> 00:03:00,110 Their value or contents may change as the program executes. 50 00:03:00,770 --> 00:03:04,770 In the next video, we'll learn how to declare and initialize variables, 51 00:03:04,770 --> 00:03:07,770 so they're ready to use in our programs. Let's go there next.