1 00:00:00,000 --> 00:00:04,575 [MUSIC] 2 00:00:04,575 --> 00:00:09,500 Programming languages use strings a lot, but they use numbers even more often. 3 00:00:09,500 --> 00:00:13,360 Almost any language will let you do addition, subtraction, multiplication, 4 00:00:13,360 --> 00:00:18,260 division and many other math prostrations, and C# is no exception. 5 00:00:18,260 --> 00:00:22,080 Our next requirement for the Cat Food Store is to calculate a total price for 6 00:00:22,080 --> 00:00:22,990 the order. 7 00:00:22,990 --> 00:00:27,020 Math operations will let us multiply the quantity of cans by the price to get 8 00:00:27,020 --> 00:00:27,900 the total. 9 00:00:27,900 --> 00:00:30,850 Math operators take the values to their left and right, and 10 00:00:30,850 --> 00:00:33,210 perform a math operation on them. 11 00:00:33,210 --> 00:00:38,260 The four most common operators, add, subtract, multiply, or divide values. 12 00:00:38,260 --> 00:00:41,920 The addition and subtraction operators look just like they do in most elementary 13 00:00:41,920 --> 00:00:43,650 school math textbooks. 14 00:00:43,650 --> 00:00:47,220 But most keyboards don't have a key for the traditional multiplication or 15 00:00:47,220 --> 00:00:48,550 division symbols. 16 00:00:48,550 --> 00:00:52,730 So like most programming languages, C# uses an asterisk for 17 00:00:52,730 --> 00:00:56,270 multiplication and a forward slash for division. 18 00:00:56,270 --> 00:01:00,170 Here's a new program with all four of the math operators we just showed you. 19 00:01:00,170 --> 00:01:02,940 Here, we add the numbers 1 and 2 together. 20 00:01:02,940 --> 00:01:06,203 Here, we subtract 4 from 12.5. 21 00:01:06,203 --> 00:01:08,410 Here, we multiply 5 by 8. 22 00:01:08,410 --> 00:01:10,320 And here, we divide 7 by 4. 23 00:01:10,320 --> 00:01:14,770 You might notice that on that last line, the division operation, that even though 24 00:01:14,770 --> 00:01:18,810 we're working with whole numbers, we included a decimal point in each of them. 25 00:01:18,810 --> 00:01:21,200 We'll explain why we did that in a little bit. 26 00:01:21,200 --> 00:01:22,259 Let's try running this. 27 00:01:23,689 --> 00:01:24,772 Dotnet run. 28 00:01:27,326 --> 00:01:29,465 And you can see we get the results we'd expect. 29 00:01:29,465 --> 00:01:30,778 1 plus 2 is 3. 30 00:01:30,778 --> 00:01:33,333 12.5 minus 4 is 8.5. 31 00:01:33,333 --> 00:01:35,145 5 times 8 is 40. 32 00:01:35,145 --> 00:01:38,430 And 7 divided by 4 is 1.75. 33 00:01:38,430 --> 00:01:41,838 Variables can be used in any part of the math operation. 34 00:01:41,838 --> 00:01:46,420 So here, we take the integer 2 and assign it to a variable named number. 35 00:01:46,420 --> 00:01:49,530 Oops, forgot the var keyword. 36 00:01:49,530 --> 00:01:50,660 So let me add that real quick. 37 00:01:51,840 --> 00:01:55,390 And let me make sure there is a semicolon there at the end of the line. 38 00:01:55,390 --> 00:01:59,716 And now, let's try printing the results of taking number and adding 3 to it. 39 00:01:59,716 --> 00:02:02,329 And let's also print the results of taking 4 and 40 00:02:02,329 --> 00:02:04,440 multiplying it by the value in number. 41 00:02:05,890 --> 00:02:06,614 Save this. 42 00:02:07,888 --> 00:02:08,790 And try running it. 43 00:02:10,420 --> 00:02:12,570 And again, the results aren't too surprising. 44 00:02:12,570 --> 00:02:16,800 Number contains 2, so the result of adding 3 to 2 is 5. 45 00:02:16,800 --> 00:02:20,363 And the results of multiplying 4 by 2 is 8. 46 00:02:21,971 --> 00:02:25,146 Using a variable in a math operation leaves the value in that 47 00:02:25,146 --> 00:02:27,020 variable unchanged. 48 00:02:27,020 --> 00:02:30,785 So if we were to try to print the value in number after doing theses 49 00:02:30,785 --> 00:02:33,587 various operations, and then try running it, 50 00:02:36,458 --> 00:02:41,338 You can see that even though we add 3 to number and then multiply 4 by that number, 51 00:02:41,338 --> 00:02:44,827 the value that number contains after all that is still 2. 52 00:02:49,320 --> 00:02:53,128 If you wanna actually change the value on a variable, you'll need to do your math 53 00:02:53,128 --> 00:02:57,780 operation on the variable, and then assign the result back to that same variable. 54 00:02:57,780 --> 00:03:01,902 So we start by assigning the initial value 2 to the variable number. 55 00:03:04,102 --> 00:03:07,064 And here, we add 1 to the value in number and 56 00:03:07,064 --> 00:03:10,930 then assign it back to that same variable. 57 00:03:10,930 --> 00:03:11,954 Let's try printing that out. 58 00:03:14,092 --> 00:03:16,085 Let me save that and try running it real quick. 59 00:03:18,605 --> 00:03:23,139 And you can see that we take the value in number, which is 2, 60 00:03:23,139 --> 00:03:25,995 we add 1 to it, and assign it back to number. 61 00:03:25,995 --> 00:03:29,188 And down here, when we print out the value in number, we get 3. 62 00:03:31,877 --> 00:03:34,825 Let's try adding 1 to the value in number again. 63 00:03:34,825 --> 00:03:37,125 And then print that updated value again. 64 00:03:41,278 --> 00:03:45,951 Number starts at 2, we update it to 3, and then we update it again, and we get 4. 65 00:03:48,831 --> 00:03:50,927 Let's try a few more operations. 66 00:03:50,927 --> 00:03:55,387 I'll subtract 1 from number, and then update the value it holds. 67 00:03:56,609 --> 00:03:57,733 Print the updated value. 68 00:03:59,180 --> 00:04:02,082 I'll multiply the number by 2, and update the value it holds. 69 00:04:03,963 --> 00:04:05,215 And then print it again. 70 00:04:06,831 --> 00:04:08,055 And let's try running this. 71 00:04:09,689 --> 00:04:13,280 And our starting value is 2, we add 1 to get 3. 72 00:04:13,280 --> 00:04:15,170 We add 1 again to get 4. 73 00:04:15,170 --> 00:04:17,475 We subtract 1 and wind up with 3 again. 74 00:04:17,475 --> 00:04:22,800 And then, we multiply the result by 2, and now number contains the value 6. 75 00:04:22,800 --> 00:04:25,450 By the way, all the math operations we've shown you so 76 00:04:25,450 --> 00:04:29,840 far work just like this in just about every programming language out there. 77 00:04:29,840 --> 00:04:32,990 So you'll be able to apply what you've seen in almost any programming 78 00:04:32,990 --> 00:04:34,468 language you want. 79 00:04:34,468 --> 00:04:39,390 The next C# feature we're going to show you is something not every language has. 80 00:04:39,390 --> 00:04:43,480 Abbreviated assignment operators let you take the value in a variable and 81 00:04:43,480 --> 00:04:45,992 add to it, subtract from it, multiply it, or 82 00:04:45,992 --> 00:04:50,010 divide it, then reassign the result back to the same variable. 83 00:04:50,010 --> 00:04:53,060 So we can rewrite the previous statements like this. 84 00:04:53,060 --> 00:04:58,268 number equals number plus 1 can be written as number plus equals 1. 85 00:04:58,268 --> 00:05:03,115 number equals number minus 1 can be rewritten as number minus equals 1. 86 00:05:03,115 --> 00:05:10,690 And number equals number times 2 can be rewritten as number times equals 2. 87 00:05:10,690 --> 00:05:14,015 There's also an abbreviated assignment operation for division, 88 00:05:14,015 --> 00:05:15,713 which is written as slash equals. 89 00:05:19,466 --> 00:05:21,287 Let's trying running this. 90 00:05:21,287 --> 00:05:25,180 And we get the same results as before, but with much shorter code. 91 00:05:25,180 --> 00:05:26,209 Now, let's go back and 92 00:05:26,209 --> 00:05:29,052 look at this line from when we introduced the division operator. 93 00:05:31,182 --> 00:05:34,022 Notice that even though we're working with whole numbers, 94 00:05:34,022 --> 00:05:36,091 we included a decimal point in each of them. 95 00:05:38,159 --> 00:05:44,234 So 7.0 divided by 4.0 gives us a result of 1.75. 96 00:05:44,234 --> 00:05:47,160 What would happen if we remove the decimal points? 97 00:05:51,800 --> 00:05:54,500 We get a whole number result of 1. 98 00:05:54,500 --> 00:05:57,890 There's a big difference between 1 and 1.75. 99 00:05:57,890 --> 00:06:01,205 If a supermarket cashier owed you a \$1.75 in change, 100 00:06:01,205 --> 00:06:04,941 you probably wouldn't be too happy if they just handed you \$1. 101 00:06:08,715 --> 00:06:11,051 So let's add those decimal points back in. 102 00:06:13,215 --> 00:06:17,610 If we rerun the program, we'll see we have a result of 1.75 again. 103 00:06:21,067 --> 00:06:24,610 It also works fine if only the first number has a decimal point. 104 00:06:29,942 --> 00:06:33,454 And it will work fine if only the last number has a decimal point. 105 00:06:38,900 --> 00:06:40,900 So why the difference? 106 00:06:40,900 --> 00:06:46,110 When you include a decimal point in a number, C# treats its type as double, 107 00:06:46,110 --> 00:06:48,610 a double-precision, floating-point number. 108 00:06:49,960 --> 00:06:55,600 When you leave the decimal point out, C# treats its type as int and integer. 109 00:06:55,600 --> 00:07:00,050 When doing division, as long as either the dividend or the divisor is 110 00:07:00,050 --> 00:07:04,410 a floating point number, the result will be a floating point number. 111 00:07:04,410 --> 00:07:08,380 So if the point on either side of the division operator is a floating point 112 00:07:08,380 --> 00:07:09,500 number, you'll be okay. 113 00:07:10,650 --> 00:07:15,055 But when doing division with integers, the result is always an integer. 114 00:07:15,055 --> 00:07:20,730 C# turns the result into an integer by throwing away the fractional portion. 115 00:07:20,730 --> 00:07:26,340 So even though the result of 7 divided by 4 would normally be 1.75, 116 00:07:26,340 --> 00:07:32,149 C# turns the result into an integer by discarding the 0.75, leaving 1. 117 00:07:33,330 --> 00:07:35,820 This isn't just C# that does this. 118 00:07:35,820 --> 00:07:38,780 Many other programming languages do the same. 119 00:07:38,780 --> 00:07:44,380 The whole reason integer data types exist is to save space in computer memory. 120 00:07:44,380 --> 00:07:46,270 When you have a floating point number, 121 00:07:46,270 --> 00:07:50,210 you need to store every one of those digits following the decimal point. 122 00:07:50,210 --> 00:07:54,060 If you have an integer, you can just get rid of all those decimal places. 123 00:07:54,060 --> 00:07:56,700 It requires a lot less memory to store. 124 00:07:56,700 --> 00:08:00,530 There are some situations where using an integer is appropriate. 125 00:08:00,530 --> 00:08:02,610 If you're counting people, or cars, or 126 00:08:02,610 --> 00:08:07,720 houses, you can safely assume you'll only be working with whole numbers. 127 00:08:07,720 --> 00:08:12,490 In cases like that, you can save a lot of memory by using an integer data type. 128 00:08:13,550 --> 00:08:16,390 When you use an integer type in a computer program, 129 00:08:16,390 --> 00:08:21,030 you're basically saying to the compiler that saving memory is more important to 130 00:08:21,030 --> 00:08:24,280 you than keeping track of fractional numbers. 131 00:08:24,280 --> 00:08:27,220 So if you use integers in a division operation and 132 00:08:27,220 --> 00:08:29,040 the result isn't a whole number, 133 00:08:29,040 --> 00:08:34,090 your program will simply throw those pesky memory hogging decimal places away. 134 00:08:34,090 --> 00:08:37,040 But of course, if you're even doing a division operation, 135 00:08:37,040 --> 00:08:40,710 you're probably not working just with whole numbers. 136 00:08:40,710 --> 00:08:43,910 You might be counting dollars, or pies, or hours, or 137 00:08:43,910 --> 00:08:48,180 something else where the fractional portions matter a whole lot. 138 00:08:48,180 --> 00:08:50,830 Which is why it's important to follow this rule. 139 00:08:50,830 --> 00:08:53,130 Unless you're certain you know what you're doing, 140 00:08:53,130 --> 00:08:57,630 never ever use integer values in a division operation. 141 00:08:57,630 --> 00:09:00,310 Even if they're whole numbers, add a decimal point and 142 00:09:00,310 --> 00:09:03,410 a 0 so they'll be treated as floating point numbers. 143 00:09:03,410 --> 00:09:06,550 That way, the result will also be a floating point number. 144 00:09:07,840 --> 00:09:10,765 You won't lose those important decimal places, and 145 00:09:10,765 --> 00:09:12,593 your users will be much happier.