Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic 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.

0:00
[MUSIC]

0:04
Programming languages use strings a lot, but they use numbers even more often.

0:09
Almost any language will let you do addition, subtraction, multiplication,

0:13
division and many other math prostrations, and C# is no exception.

0:18
Our next requirement for the Cat Food Store is to calculate a total price for

0:22
the order.

0:22
Math operations will let us multiply the quantity of cans by the price to get

0:27
the total.

0:27
Math operators take the values to their left and right, and

0:30
perform a math operation on them.

0:33
The four most common operators, add, subtract, multiply, or divide values.

0:38
The addition and subtraction operators look just like they do in most elementary

0:41
school math textbooks.

0:43
But most keyboards don't have a key for the traditional multiplication or

0:47
division symbols.

0:48
So like most programming languages, C# uses an asterisk for

0:52
multiplication and a forward slash for division.

0:56
Here's a new program with all four of the math operators we just showed you.

1:00
Here, we add the numbers 1 and 2 together.

1:02
Here, we subtract 4 from 12.5.

1:06
Here, we multiply 5 by 8.

1:08
And here, we divide 7 by 4.

1:10
You might notice that on that last line, the division operation, that even though

1:14
we're working with whole numbers, we included a decimal point in each of them.

1:18
We'll explain why we did that in a little bit.

1:21
Let's try running this.

1:23
Dotnet run.

1:27
And you can see we get the results we'd expect.

1:29
1 plus 2 is 3.

1:30
12.5 minus 4 is 8.5.

1:33
5 times 8 is 40.

1:35
And 7 divided by 4 is 1.75.

1:38
Variables can be used in any part of the math operation.

1:41
So here, we take the integer 2 and assign it to a variable named number.

1:46
Oops, forgot the var keyword.

1:49
So let me add that real quick.

1:51
And let me make sure there is a semicolon there at the end of the line.

1:55
And now, let's try printing the results of taking number and adding 3 to it.

1:59
And let's also print the results of taking 4 and

2:02
multiplying it by the value in number.

2:05
Save this.

2:07
And try running it.

2:10
And again, the results aren't too surprising.

2:12
Number contains 2, so the result of adding 3 to 2 is 5.

2:16
And the results of multiplying 4 by 2 is 8.

2:21
Using a variable in a math operation leaves the value in that

2:25
variable unchanged.

2:27
So if we were to try to print the value in number after doing theses

2:30
various operations, and then try running it,

2:36
You can see that even though we add 3 to number and then multiply 4 by that number,

2:41
the value that number contains after all that is still 2.

2:49
If you wanna actually change the value on a variable, you'll need to do your math

2:53
operation on the variable, and then assign the result back to that same variable.

2:57
So we start by assigning the initial value 2 to the variable number.

3:04
And here, we add 1 to the value in number and

3:07
then assign it back to that same variable.

3:10
Let's try printing that out.

3:14
Let me save that and try running it real quick.

3:18
And you can see that we take the value in number, which is 2,

3:23
we add 1 to it, and assign it back to number.

3:25
And down here, when we print out the value in number, we get 3.

3:31
Let's try adding 1 to the value in number again.

3:34
And then print that updated value again.

3:41
Number starts at 2, we update it to 3, and then we update it again, and we get 4.

3:48
Let's try a few more operations.

3:50
I'll subtract 1 from number, and then update the value it holds.

3:56
Print the updated value.

3:59
I'll multiply the number by 2, and update the value it holds.

4:03
And then print it again.

4:06
And let's try running this.

4:09
And our starting value is 2, we add 1 to get 3.

4:13
We add 1 again to get 4.

4:15
We subtract 1 and wind up with 3 again.

4:17
And then, we multiply the result by 2, and now number contains the value 6.

4:22
By the way, all the math operations we've shown you so

4:25
far work just like this in just about every programming language out there.

4:29
So you'll be able to apply what you've seen in almost any programming

4:32
language you want.

4:34
The next C# feature we're going to show you is something not every language has.

4:39
Abbreviated assignment operators let you take the value in a variable and

4:43
add to it, subtract from it, multiply it, or

4:45
divide it, then reassign the result back to the same variable.

4:50
So we can rewrite the previous statements like this.

4:53
number equals number plus 1 can be written as number plus equals 1.

4:58
number equals number minus 1 can be rewritten as number minus equals 1.

5:03
And number equals number times 2 can be rewritten as number times equals 2.

5:10
There's also an abbreviated assignment operation for division,

5:14
which is written as slash equals.

5:19
Let's trying running this.

5:21
And we get the same results as before, but with much shorter code.

5:25
Now, let's go back and

5:26
look at this line from when we introduced the division operator.

5:31
Notice that even though we're working with whole numbers,

5:34
we included a decimal point in each of them.

5:38
So 7.0 divided by 4.0 gives us a result of 1.75.

5:44
What would happen if we remove the decimal points?

5:51
We get a whole number result of 1.

5:54
There's a big difference between 1 and 1.75.

5:57
If a supermarket cashier owed you a $1.75 in change,

6:01
you probably wouldn't be too happy if they just handed you $1.

6:08
So let's add those decimal points back in.

6:13
If we rerun the program, we'll see we have a result of 1.75 again.

6:21
It also works fine if only the first number has a decimal point.

6:29
And it will work fine if only the last number has a decimal point.

6:38
So why the difference?

6:40
When you include a decimal point in a number, C# treats its type as double,

6:46
a doubleprecision, floatingpoint number.

6:49
When you leave the decimal point out, C# treats its type as int and integer.

6:55
When doing division, as long as either the dividend or the divisor is

7:00
a floating point number, the result will be a floating point number.

7:04
So if the point on either side of the division operator is a floating point

7:08
number, you'll be okay.

7:10
But when doing division with integers, the result is always an integer.

7:15
C# turns the result into an integer by throwing away the fractional portion.

7:20
So even though the result of 7 divided by 4 would normally be 1.75,

7:26
C# turns the result into an integer by discarding the 0.75, leaving 1.

7:33
This isn't just C# that does this.

7:35
Many other programming languages do the same.

7:38
The whole reason integer data types exist is to save space in computer memory.

7:44
When you have a floating point number,

7:46
you need to store every one of those digits following the decimal point.

7:50
If you have an integer, you can just get rid of all those decimal places.

7:54
It requires a lot less memory to store.

7:56
There are some situations where using an integer is appropriate.

8:00
If you're counting people, or cars, or

8:02
houses, you can safely assume you'll only be working with whole numbers.

8:07
In cases like that, you can save a lot of memory by using an integer data type.

8:13
When you use an integer type in a computer program,

8:16
you're basically saying to the compiler that saving memory is more important to

8:21
you than keeping track of fractional numbers.

8:24
So if you use integers in a division operation and

8:27
the result isn't a whole number,

8:29
your program will simply throw those pesky memory hogging decimal places away.

8:34
But of course, if you're even doing a division operation,

8:37
you're probably not working just with whole numbers.

8:40
You might be counting dollars, or pies, or hours, or

8:43
something else where the fractional portions matter a whole lot.

8:48
Which is why it's important to follow this rule.

8:50
Unless you're certain you know what you're doing,

8:53
never ever use integer values in a division operation.

8:57
Even if they're whole numbers, add a decimal point and

9:00
a 0 so they'll be treated as floating point numbers.

9:03
That way, the result will also be a floating point number.

9:07
You won't lose those important decimal places, and

9:10
your users will be much happier.
You need to sign up for Treehouse in order to download course files.
Sign up