Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Our next requirement for the cat food store is to calculate a total price for the order. Math operations will let us multiply the quantity of cans by the price to get the total.
Programming languages use strings a lot, but they use numbers even more often. Almost any language will let you do addition, subtraction, multiplication, division, and many other math operations. And C# is no exception.
Our next requirement for the cat food store is to calculate a total price for the order. Math operations will let us multiply the quantity of cans by the price to get the total.
Cat Food Store Features
Display welcome messageAsk for quantity Calculate total
 Discount for large orders
Math operators take the values to their left and right and perform a math operation on them. The four most common operators add, subtract, multiply, or divide values. The addition and subtraction operators look just like they do in most elementary school math textbooks. But most keyboards don't have a key for the traditional multiplication or division symbols. So like most programming languages, C# uses an asterisk for multiplication, and a forward slash for division.
+ Add
 Subtract
* Multiply
/ Divide
 Here's a new program with all four of the math operators we just showed you.
Console.WriteLine(1 + 2); // => 3
Console.WriteLine(12.5  4); // => 8.5
Console.WriteLine(5 * 8); // => 40
Console.WriteLine(7.0 / 4.0); // => 1.75
 You might notice on that last line, the division operation, that even though we're working with whole numbers we included a decimal point in each of them.
 We'll explain why we did that in a little bit.
Variables can be used in any part of the math operation.
number = 2
Console.WriteLine(number + 3); // => 5
Console.WriteLine(4 * number); // => 8
 Using a variable in a math operation leaves the value in that variable unchanged.
 To change the value in a variable, you can do a math operation on a variable, and then assign the result back to that variable.
number = number + 1;
Console.WriteLine(number);
number = number + 1;
Console.WriteLine(number);
number = number  1;
Console.WriteLine(number);
number = number * 2;
Console.WriteLine(number);
number = number / 2.0;
Console.WriteLine(number);
By the way, all the math operations we've shown you so far work just like this in just about every programming language out there. So you'll be able to apply what you've seen in almost any programming language you want. The next C# feature we're going to show is something that not every language has...
 Abbreviated assignment operators let you take the value in a variable and add to it, subtract from it, multiply it, or divide it, then reassign the result back to the same variable.
 We can rewrite the previous statements like this:
number += 1;
Console.WriteLine(number); // => 3
number += 1;
Console.WriteLine(number); // => 4
number = 1;
Console.WriteLine(number); // => 3
number *= 2;
Console.WriteLine(number); // => 6
Now, let's go back and look at this line from when we introduced the division operator:
Console.WriteLine(7.0 / 4.0);
 You might notice on that last line, the division operation, that even though we're working with whole numbers we included a decimal point in each of them.

7.0 / 4.0
gives us a result of1.75
.  What would happen if we removed the decimal points?
 We get a result of
1
.  There's a big difference between
1
and1.75
! If a supermarket cashier owed you $1.75 in change, you probably wouldn't be too happy if they just handed you $1.  Let's add those decimal points back in.
 If we rerun the program, we'll see that we have a result of
1.75
again.  It also works fine if only the first number has a decimal point.
 It also works if only the second number has a point.
 Why the difference?
 When you include a decimal point in a number, C# treats its type as
double
, a doubleprecision floatingpoint number.  When you leave the decimal point out, C# treats its type as
int
, an integer.  When doing division, as long as either the dividend or the divisor is a floatingpoint number, the result will be a floating point number. So if the number on either side of the division operator is a floatingpoint number, you'll be OK.
 But when doing division with integers, the result is always an integer.
 C# turns the result into an integer by throwing away the fractional portion.
 So even though the result of
7 / 4
would normally be1.75
, C# turns the result into an integer by discarding the .75, leaving1
.  This isn't just C# that does this, many other programming languages do the same.
 When you include a decimal point in a number, C# treats its type as
The whole reason integer data types exist is to save space in computer memory. When you have a floatingpoint number, you need to store every one of those digits following the decimal point.
If you have an integer, you can just get rid of all those decimal places. It requires a lot less memory to store.
There are some situations where using an integer is appropriate. If you're counting people, or cars, or houses, you can safely assume you'll only be working with whole numbers. In cases like that, you can save a lot of memory by using an integer data type.
 When you use an integer type in a computer program, you're basically saying to the compiler that saving memory is more important to you than keeping track of fractional numbers.
 So if you use integers in a division operation, and the result isn't a whole number, your program will simply throw those pesky memoryhogging decimal places away.
 But of course, if you're doing a division operation, you're probably not working just with whole numbers.
 You might be counting dollars, or pies, or hours, or something else where the fractional portions matter a whole lot!
 Which is why it's important to follow this rule: unless you're certain you know what you're doing, never, ever use integer values in a division operation.
 Even if they're whole numbers, add a decimal point and a zero so they get treated as floatingpoint numbers, and the result is also a floatingpoint number.
 That way, you won't lose those important decimal places, and your users will be much happier.
[MUSIC] 0:00 Programming languages use strings a lot, but they use numbers even more often. 0:04 Almost any language will let you do addition, subtraction, multiplication, 0:09 division and many other math prostrations, and C# is no exception. 0:13 Our next requirement for the Cat Food Store is to calculate a total price for 0:18 the order. 0:22 Math operations will let us multiply the quantity of cans by the price to get 0:22 the total. 0:27 Math operators take the values to their left and right, and 0:27 perform a math operation on them. 0:30 The four most common operators, add, subtract, multiply, or divide values. 0:33 The addition and subtraction operators look just like they do in most elementary 0:38 school math textbooks. 0:41 But most keyboards don't have a key for the traditional multiplication or 0:43 division symbols. 0:47 So like most programming languages, C# uses an asterisk for 0:48 multiplication and a forward slash for division. 0:52 Here's a new program with all four of the math operators we just showed you. 0:56 Here, we add the numbers 1 and 2 together. 1:00 Here, we subtract 4 from 12.5. 1:02 Here, we multiply 5 by 8. 1:06 And here, we divide 7 by 4. 1:08 You might notice that on that last line, the division operation, that even though 1:10 we're working with whole numbers, we included a decimal point in each of them. 1:14 We'll explain why we did that in a little bit. 1:18 Let's try running this. 1:21 Dotnet run. 1:23 And you can see we get the results we'd expect. 1:27 1 plus 2 is 3. 1:29 12.5 minus 4 is 8.5. 1:30 5 times 8 is 40. 1:33 And 7 divided by 4 is 1.75. 1:35 Variables can be used in any part of the math operation. 1:38 So here, we take the integer 2 and assign it to a variable named number. 1:41 Oops, forgot the var keyword. 1:46 So let me add that real quick. 1:49 And let me make sure there is a semicolon there at the end of the line. 1:51 And now, let's try printing the results of taking number and adding 3 to it. 1:55 And let's also print the results of taking 4 and 1:59 multiplying it by the value in number. 2:02 Save this. 2:05 And try running it. 2:07 And again, the results aren't too surprising. 2:10 Number contains 2, so the result of adding 3 to 2 is 5. 2:12 And the results of multiplying 4 by 2 is 8. 2:16 Using a variable in a math operation leaves the value in that 2:21 variable unchanged. 2:25 So if we were to try to print the value in number after doing theses 2:27 various operations, and then try running it, 2:30 You can see that even though we add 3 to number and then multiply 4 by that number, 2:36 the value that number contains after all that is still 2. 2:41 If you wanna actually change the value on a variable, you'll need to do your math 2:49 operation on the variable, and then assign the result back to that same variable. 2:53 So we start by assigning the initial value 2 to the variable number. 2:57 And here, we add 1 to the value in number and 3:04 then assign it back to that same variable. 3:07 Let's try printing that out. 3:10 Let me save that and try running it real quick. 3:14 And you can see that we take the value in number, which is 2, 3:18 we add 1 to it, and assign it back to number. 3:23 And down here, when we print out the value in number, we get 3. 3:25 Let's try adding 1 to the value in number again. 3:31 And then print that updated value again. 3:34 Number starts at 2, we update it to 3, and then we update it again, and we get 4. 3:41 Let's try a few more operations. 3:48 I'll subtract 1 from number, and then update the value it holds. 3:50 Print the updated value. 3:56 I'll multiply the number by 2, and update the value it holds. 3:59 And then print it again. 4:03 And let's try running this. 4:06 And our starting value is 2, we add 1 to get 3. 4:09 We add 1 again to get 4. 4:13 We subtract 1 and wind up with 3 again. 4:15 And then, we multiply the result by 2, and now number contains the value 6. 4:17 By the way, all the math operations we've shown you so 4:22 far work just like this in just about every programming language out there. 4:25 So you'll be able to apply what you've seen in almost any programming 4:29 language you want. 4:32 The next C# feature we're going to show you is something not every language has. 4:34 Abbreviated assignment operators let you take the value in a variable and 4:39 add to it, subtract from it, multiply it, or 4:43 divide it, then reassign the result back to the same variable. 4:45 So we can rewrite the previous statements like this. 4:50 number equals number plus 1 can be written as number plus equals 1. 4:53 number equals number minus 1 can be rewritten as number minus equals 1. 4:58 And number equals number times 2 can be rewritten as number times equals 2. 5:03 There's also an abbreviated assignment operation for division, 5:10 which is written as slash equals. 5:14 Let's trying running this. 5:19 And we get the same results as before, but with much shorter code. 5:21 Now, let's go back and 5:25 look at this line from when we introduced the division operator. 5:26 Notice that even though we're working with whole numbers, 5:31 we included a decimal point in each of them. 5:34 So 7.0 divided by 4.0 gives us a result of 1.75. 5:38 What would happen if we remove the decimal points? 5:44 We get a whole number result of 1. 5:51 There's a big difference between 1 and 1.75. 5:54 If a supermarket cashier owed you a $1.75 in change, 5:57 you probably wouldn't be too happy if they just handed you $1. 6:01 So let's add those decimal points back in. 6:08 If we rerun the program, we'll see we have a result of 1.75 again. 6:13 It also works fine if only the first number has a decimal point. 6:21 And it will work fine if only the last number has a decimal point. 6:29 So why the difference? 6:38 When you include a decimal point in a number, C# treats its type as double, 6:40 a doubleprecision, floatingpoint number. 6:46 When you leave the decimal point out, C# treats its type as int and integer. 6:49 When doing division, as long as either the dividend or the divisor is 6:55 a floating point number, the result will be a floating point number. 7:00 So if the point on either side of the division operator is a floating point 7:04 number, you'll be okay. 7:08 But when doing division with integers, the result is always an integer. 7:10 C# turns the result into an integer by throwing away the fractional portion. 7:15 So even though the result of 7 divided by 4 would normally be 1.75, 7:20 C# turns the result into an integer by discarding the 0.75, leaving 1. 7:26 This isn't just C# that does this. 7:33 Many other programming languages do the same. 7:35 The whole reason integer data types exist is to save space in computer memory. 7:38 When you have a floating point number, 7:44 you need to store every one of those digits following the decimal point. 7:46 If you have an integer, you can just get rid of all those decimal places. 7:50 It requires a lot less memory to store. 7:54 There are some situations where using an integer is appropriate. 7:56 If you're counting people, or cars, or 8:00 houses, you can safely assume you'll only be working with whole numbers. 8:02 In cases like that, you can save a lot of memory by using an integer data type. 8:07 When you use an integer type in a computer program, 8:13 you're basically saying to the compiler that saving memory is more important to 8:16 you than keeping track of fractional numbers. 8:21 So if you use integers in a division operation and 8:24 the result isn't a whole number, 8:27 your program will simply throw those pesky memory hogging decimal places away. 8:29 But of course, if you're even doing a division operation, 8:34 you're probably not working just with whole numbers. 8:37 You might be counting dollars, or pies, or hours, or 8:40 something else where the fractional portions matter a whole lot. 8:43 Which is why it's important to follow this rule. 8:48 Unless you're certain you know what you're doing, 8:50 never ever use integer values in a division operation. 8:53 Even if they're whole numbers, add a decimal point and 8:57 a 0 so they'll be treated as floating point numbers. 9:00 That way, the result will also be a floating point number. 9:03 You won't lose those important decimal places, and 9:07 your users will be much happier. 9:10
You need to sign up for Treehouse in order to download course files.
Sign up