This video is going to discuss the arithmetic operators. Now, we’ve discussed these some already and I’m sure you are familiar with them from math classes you’ve taken so this video is really not going to be that hard or confusing.
The arithmetic operators include the +, -, /, and *. These are all binary operators that need two operands.
The / is called a forward slash. Some people call \ a forward slash and they are wrong! The top of the slash is the reference point, not the bottom. The top is leaning forward, therefore / is called the forward slash.
The * is called an astersk. I like to call it capital 8, but I’m weird.
Now, when you have an expression like this:
int x = 2 + 3 * 4 / 3 – 2;
First the multiplication and division happens from left to right, whichever comes first. This means that 3 * 4 happens first. The expression could be simplified to 2 + 12 / 3 – 2. The next thing to happen is 12 / 3. The expression could be simplified to 2 + 4 – 2. Next, the addition and subtraction happens, whichever comes first from left to right. So we get 6 – 2 which evaluates to 4.
You may find it more clear if we did this:
int x = 2 + ((3 * 4) / 3) – 2;
You can use as many parenthesis as you want and wherever you want, as long as you remember to close any parenthesis you open. They always get evaluated from the inside out. So 3 * 4 happens first, then the result of that gets divided by 3.
Now, you can also use parenthesis to force a certain part of the expression to happen first. For example, if we wanted the subtraction to happen first, we could have set it up like this:
int x = 2 + 3 * 4 / (3 – 2);
Let’s output this and make sure we get the result we expect.
Some people choose to use parenthesis for nearly everything because they believe without them it is easier to make mistakes with arithmetic, but this is up to you.
Now, I’m going to introduce you to a new operator…
Check out this expression.
int y = 5 % 2;
Now, I can tell you now that the value of y is going to be 1. Can you figure out what this operator does? We’ll find out in the next video.
Support me! http://www.patreon.com/calebcurry
Subscribe to my newsletter: http://bit.ly/JoinCCNewsletter
More content: http://CalebCurry.com
Amazing Web Hosting – http://bit.ly/ccbluehost (The best web hosting for a cheap price!)