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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up