hey what’s going on everyone it’s your

bro here and in this video we’re going to cover some arithmetic in c-sharp

let’s get into it just a fair warning that this topic is

somewhat dry and boring but I’ll try my best to keep it fresh and interesting

there’s just some necessary stuff that we need to cover related to arithmetic

and you’ll want to learn this stuff if you ever plan on using any sort of math

in your programs which is like almost always the case for most programs so

arithmetic is the branch of mathematics dealing with the properties and

manipulation of numbers I didn’t come up with that definition I found it online

I’m not taking credit for it so let’s say that we have two variables and we

want to do something with those two variables integer variables let me

clarify so let’s say we have integer X and let’s give it a value of how about

10 and let’s create another variable how about y equals how about three alright

so these variables will behave as the number that’s contained within them just

like in middle school math class let’s say we have integer Z equals what we can

do is that we can say something such as X plus y equals Z and this will actually

work you can essentially like perform math on variable numbers because like I

said they’d behave as the numbers that’s contained within them

except if their strings and I’ll show you that in just a moment so let’s say

we want to print the result so console dot right or right line and then let’s

put Z in here and let’s see what happens let’s run the program so the result is

13 so you’ll want to be sure that you’re adding integers or doubles you actually

can’t do this with strings because they don’t necessarily behave as numbers so

let me show you so let’s say we have string X and string Y and Z will also be

a string now remember you need to put a string value in double quotes alright

let’s add these together and see what happens see what we ended up doing we

ended up with 103 it’s because these numbers are behaving as strings so what

we ended up doing was concatenating these strings together we appended the 3

onto the end of 10 and they’re behaving as strings and not numbers so you’ll

want to be sure that you if you want to work with a value and you

want it to behave as a integer or a double you have to give it the integer

or double type and not string so that’s why you can assign a number as a string

and it will still appear to be a number but it doesn’t behave as an integer or a

double so keep that in mind if you ever need to assign a value like a number to

a variable you want to be sure to get the correct data type integer or double

if you need to perform any math on it or you can convert it either one works all

right let’s run through a few of the basic arithmetic operators we have

addition coverage so subtraction take a wild guess 10 minus 3 is what you

guessed it 7 all right so multiplication you commonly see people to use like X

here if they need to multiply something in real life right well in programming

we actually use the asterisk symbol to multiply so 10 times 3 is you guessed it

30 all right next division you actually use

a forward slash for division so 10 divided by 3 should be what 3.3333 or 3

something right so let’s run this and pay attention to this we actually ended

up getting 3 and this is the reason when you divide an integer by another integer

it’s called integer division and it actually can’t store the decimal portion

of the answer so it ends up getting cut off it get gets truncated so if you need

to perform any division with integers 1 you can either just make these doubles

and you know that works and make sure the answer is a double two but one thing

that would be better is this concept called casting if you ever need to do

integer division so right next to the equation just put in the word double

within parenthesis and then you have to make sure the answer is a double the

variable or wherever you’re storing it okay so this will work then and the

decimal portion won’t get truncated done so here is our answer 3.3333

you know whatever all right so that’s one way you can perform integer division

without the decimal portion getting cut off

although you could just make these double values

that works as well likewise you can do the same thing with casting this as a

integer value so if you ever want to get rid of the decimal portion you could

always cast it as an int value so that’s one thing you can do if you ever need to

perform integer division all right let’s move on then so there’s this operator

called a modulus and what you do for a modulus is that you use the percent sign

so what this does is that it’ll divide this value by this value and the modulus

will give you the remainder if there is any remainder if we were to divide 10 by

3 3 goes into 10 what three times and we shouldn’t have a remainder of one then

so if we were to run this our remainder after using the modulus operator is one

then so this could be really useful if you need to determine if a number is

even or odd you could like divide a number by two and if it divides evenly

the modulus should be zero the result should be zero then if we

made this number so like a nine or something something that’s odd this will

give you a modulus of one so when we get to the video on if statements I’ll show

you how we can make a program to determine if a number is even or odd so

that’s one way that the modulus operator is useful it will give you the remainder

of any division I would say that’s enough for arithmetic operators for now

the next topic that we should cover is the order of operations in programming

so let’s say that you have a multi-part let’s say equation such as this two plus

three times four so what part of this do you resolve first you added two plus

three first and then multiply by four or do you multiply three by four first and

then add two well do you guys remember in middle school math class maybe it was

around middle school there is a an acronym it was PEMDAS and there was a

certain phrase to help you remember that order it was a please excuse my dear

Aunt Sally although there might be some variations of that let me know in the

comments if there’s any others because I kind of want to hear

so based on that phrase on that acronym you resolve anything within parentheses

first then exponents multiplication division addition then subtraction in

that order well we have one for programming it’s a little more simple

you resolve anything within parenthesis first so if I were to put two plus three

in parenthesis here we would actually add a two plus three first and then

multiply by four so you can use parentheses to force operator precedents

in a multi-part equation such as this so if there are no parentheses what we end

up doing is resolving any multiplication or division first so that’s why we

resolved three times four first and then added two after instead of two plus

three first and then multiplying by four so it’s parentheses first followed by

any multiplication and division and then lastly we resolve any addition or

subtraction so before when we ran this we got 14 so if you want to change this

or force a certain part of this equation to resolve first you can force operator

precedents by just adding a set of parentheses so the answer should be 20

then if we have the parentheses because we resolve anything within parentheses

first and that’s all there really is to it to operate a precedence all right for

the last topic let’s clear out all of this and let’s clear out Z so in

programming we do a ton of counting we actually do so much counting that we

have our own shorthand ways of incrementing and decrementing values so

let’s create a new variable how about X and let’s set X to 0 now if you want to

increment this variable as if we’re counting what you’d normally do is type

in X or whatever variable you’re working with or value x equals x plus 1 so oh

then we have to display this alright so this is obviously going to be 1 right

all right so there’s a shorthand way of writing this because we’re lazy so if

you want the shorthand way of writing this just say the variable name then add

two plus signs and then a semicolon and this works just the same and it’s a lot

faster and what I mean it’s a lot faster I mean

a lot faster too right so that’s what you can do if you want to increment

something by one likewise if you want to decrement a value by one the longhand

way of writing that is something such as x equals x minus 1 and the answer here

would be negative 1 well what do you think we do here what you can do is just

type in X minus minus and this works the same all right so then you can also

count up or count down by a certain value so let’s say that we want to count

up by 2 the long hand way of writing that is x equals x plus 2 and the result

will be 2 now the shorthand way of writing this is that you can type in X

plus equals 2 semicolon and that’s a shorthand way of writing that same

equation there then if you want to subtract something by 2 you say X minus

equals 2 so these are called augmented assignment

operators I wouldn’t say they’re necessary but you might see them and

it’s just a shorthand way of writing certain equations if you just want like

a faster way of writing things well hopefully this video didn’t put you to

sleep it was a somewhat dry topic but that will conclude this lesson on using

arithmetic in c-sharp hey you if you enjoyed this lesson then you can help me

help you in 3 easy steps by smashing that like button drop a comment down

below and subscribe if you’d like to become a fellow bro