Precendence is how we answer the question, what operation should we do first?
Whether solving math equations or parsing lines of computer code,
there are strict rules of precedence to which we adhere
so that all computers and people can get the same result.
>> First off, the most important rule to remember, especially in bug testing,
is that we always work from the innermost parentheses outward.
Using extra parentheses can be a helpful debugging tactic,
but it's not good practice to litter your code with unneeded parentheses.
Take the time to learn basic operator precedence rules.
>> The second general rule is that when operators have equal priorty,
you simply solve from left to right.
When dealing with simple math we start with parentheses,
then do multiplication and division, and lastly do addition and subtraction.
Multiplication and division have the same priority,
because they are essentially performing the same operation.
After all division is simply multiplying by the inverse of a value.
Similarly, subtraction is simply adding a negative value.
>> Let's do an example.
Following the order of precedence, we'll start with the parentheses. Nine minus 1.
That will give us 8. Then we can move on to the division and multiplication.
We'll solve from left to right. So 10 divided by 2 is 5.
We have 5 times 8 here, and that will give us 40.
Then we move on to the next order of precedence.
So we're left with 3 plus 40 minus 1.
Again just solving left to right,
because there's equal priority between the addition and subtraction.
We can say 3 plus 40 is 43, minus 1 is 42. That's our answer.
>> There are 2 types of decrement and increment operators;
The prefix form, and the suffix form.
The suffix form, i++, is commonly used in for loops,
which means that the current value is used in the expression, and then it is incremented.
So value will only be different the next time the variable is used.
On the other hand, the prefix increment or decrement means that the current value
is incremented or decremented first, and then it is used in the expression.
>> Let's take an example with the integer x.
We'll set it equal to 5.
If we use the suffix operator on it and say x++, x on this line is still 5.
If we were to print it out we would get the value 5.
But going forward x1 fact equals 6.
So right here on this line x is equal to 6, and if we printed it out we would get the value 6.
Now if we used the prefix operator, ++x, x is incremented first, and then the value is used.
So it's equal to 7 on this line.
Incrementing of course 6 to 7, and if we were to print it out we would get the value 7.
>> The last nuance in precendence that we will look at deals with pointer notation.
The dereference operator, star, has priority over basic math operators,
but not over the suffix incement and decrement operators.
This leads us to our final example.
Let's take the integer x and set it equal to 7.
We'll also make a pointer y and set it equal to the address of x.
So that when we dereference y we should get the value 7.
Now in this line of code, we have a somewhat ambiguous situation.
Are we dereferencing y first, and then incrementing the value 7?
Or are we incrementing the pointer and then dereferencing it?
In fact, because the suffix increment operator has precedence over
the dereference operator, we're attempting to increment the pointer y,
which would move the pointer by size of int bytes.
Essentially giving us an address in some entirely different point in memory,
and then we're dereferencing it.
So this is very meaningless line.
If we actually wanted to increment the value of 7,
we would have to put the dereference operator with y in parentheses.
Then we could increment it.
So while we wouldn't be incrementing the value x with the second to last line of code,
in the last line of code we would infact dereference y
to get the value x and increment that.
We would be left with the value x equals 8.
>> Here's a quick recap of the precendence rules that we've talked about.
We'll start with the innermost parentheses and work outward.
Then we move on to suffix operators like i++ or i--.
Then dereference and address of operators like star x or ampersand x,
and the prefix operators like ++i or --i.
Finally we do the simple math operations like multiplication, division, modulo.
Then addition, subtraction.
That's precendence. I'm Jordan Jozwiak, and this is CS50.
We'll dereference and use the address and--how do you phrase that?
I'm done. Okay.