{"captions":[{"content":"Precendence is how we answer the question, what operation should we do first?","startTime":7780,"duration":2760,"startOfParagraph":false},{"content":"Whether solving math equations or parsing lines of computer code,","startTime":10540,"duration":3710,"startOfParagraph":false},{"content":"there are strict rules of precedence to which we adhere","startTime":14250,"duration":2980,"startOfParagraph":false},{"content":"so that all computers and people can get the same result.","startTime":17230,"duration":3040,"startOfParagraph":false},{"content":"First off, the most important rule to remember, especially in bug testing,","startTime":20270,"duration":4440,"startOfParagraph":true},{"content":"is that we always work from the innermost parentheses outward.","startTime":24710,"duration":2970,"startOfParagraph":false},{"content":"Using extra parentheses can be a helpful debugging tactic,","startTime":27680,"duration":3440,"startOfParagraph":false},{"content":"but it's not good practice to litter your code with unneeded parentheses.","startTime":31120,"duration":3520,"startOfParagraph":false},{"content":"Take the time to learn basic operator precedence rules.","startTime":34640,"duration":3580,"startOfParagraph":false},{"content":"The second general rule is that when operators have equal priorty,","startTime":38220,"duration":4230,"startOfParagraph":true},{"content":"you simply solve from left to right.","startTime":42450,"duration":2370,"startOfParagraph":false},{"content":"When dealing with simple math we start with parentheses, ","startTime":44820,"duration":2870,"startOfParagraph":false},{"content":"then do multiplication and division, and lastly do addition and subtraction.","startTime":47690,"duration":4420,"startOfParagraph":false},{"content":"Multiplication and division have the same priority,","startTime":52110,"duration":2290,"startOfParagraph":false},{"content":"because they are essentially performing the same operation.","startTime":54400,"duration":2470,"startOfParagraph":false},{"content":"After all division is simply multiplying by the inverse of a value.","startTime":56870,"duration":4010,"startOfParagraph":false},{"content":"Similarly, subtraction is simply adding a negative value.","startTime":60880,"duration":3420,"startOfParagraph":false},{"content":"Let's do an example.","startTime":64300,"duration":1850,"startOfParagraph":true},{"content":"Following the order of precedence, we'll start with the parentheses. Nine minus 1.","startTime":74470,"duration":3830,"startOfParagraph":false},{"content":"That will give us 8. Then we can move on to the division and multiplication.","startTime":78300,"duration":5110,"startOfParagraph":false},{"content":"We'll solve from left to right. So 10 divided by 2 is 5.","startTime":83410,"duration":4040,"startOfParagraph":false},{"content":"We have 5 times 8 here, and that will give us 40.","startTime":87450,"duration":3840,"startOfParagraph":false},{"content":"Then we move on to the next order of precedence.","startTime":93230,"duration":2180,"startOfParagraph":false},{"content":"So we're left with 3 plus 40 minus 1.","startTime":95410,"duration":3320,"startOfParagraph":false},{"content":"Again just solving left to right,","startTime":102400,"duration":1300,"startOfParagraph":false},{"content":"because there's equal priority between the addition and subtraction.","startTime":103700,"duration":3950,"startOfParagraph":false},{"content":"We can say 3 plus 40 is 43, minus 1 is 42. That's our answer.","startTime":107650,"duration":3860,"startOfParagraph":false},{"content":"There are 2 types of decrement and increment operators;","startTime":113920,"duration":2810,"startOfParagraph":true},{"content":"The prefix form, and the suffix form.","startTime":116730,"duration":4270,"startOfParagraph":false},{"content":"The suffix form, i++, is commonly used in for loops,","startTime":121000,"duration":5130,"startOfParagraph":false},{"content":"which means that the current value is used in the expression, and then it is incremented.","startTime":126130,"duration":4370,"startOfParagraph":false},{"content":"So value will only be different the next time the variable is used.","startTime":130500,"duration":3740,"startOfParagraph":false},{"content":"On the other hand, the prefix increment or decrement means that the current value","startTime":134240,"duration":3670,"startOfParagraph":false},{"content":"is incremented or decremented first, and then it is used in the expression.","startTime":137910,"duration":4850,"startOfParagraph":false},{"content":"Let's take an example with the integer x.","startTime":142760,"duration":2550,"startOfParagraph":true},{"content":"We'll set it equal to 5.","startTime":145310,"duration":1910,"startOfParagraph":false},{"content":"If we use the suffix operator on it and say x++, x on this line is still 5.","startTime":147220,"duration":9280,"startOfParagraph":false},{"content":"If we were to print it out we would get the value 5.","startTime":156500,"duration":2730,"startOfParagraph":false},{"content":"But going forward x1 fact equals 6.","startTime":159230,"duration":3310,"startOfParagraph":false},{"content":"So right here on this line x is equal to 6, and if we printed it out we would get the value 6.","startTime":162540,"duration":6230,"startOfParagraph":false},{"content":"Now if we used the prefix operator, ++x, x is incremented first, and then the value is used.","startTime":168770,"duration":8610,"startOfParagraph":false},{"content":"So it's equal to 7 on this line.","startTime":177380,"duration":2730,"startOfParagraph":false},{"content":"Incrementing of course 6 to 7, and if we were to print it out we would get the value 7.","startTime":180110,"duration":4640,"startOfParagraph":false},{"content":"The last nuance in precendence that we will look at deals with pointer notation.","startTime":184750,"duration":4410,"startOfParagraph":true},{"content":"The dereference operator, star, has priority over basic math operators,","startTime":189160,"duration":5890,"startOfParagraph":false},{"content":"but not over the suffix incement and decrement operators.","startTime":195050,"duration":3500,"startOfParagraph":false},{"content":"This leads us to our final example.","startTime":198550,"duration":2140,"startOfParagraph":false},{"content":"Let's take the integer x and set it equal to 7.","startTime":200690,"duration":3810,"startOfParagraph":false},{"content":"We'll also make a pointer y and set it equal to the address of x.","startTime":204500,"duration":6040,"startOfParagraph":false},{"content":"So that when we dereference y we should get the value 7.","startTime":210540,"duration":4380,"startOfParagraph":false},{"content":"Now in this line of code, we have a somewhat ambiguous situation.","startTime":214920,"duration":4460,"startOfParagraph":false},{"content":"Are we dereferencing y first, and then incrementing the value 7?","startTime":219380,"duration":4930,"startOfParagraph":false},{"content":"Or are we incrementing the pointer and then dereferencing it?","startTime":224310,"duration":3990,"startOfParagraph":false},{"content":"In fact, because the suffix increment operator has precedence over ","startTime":228300,"duration":4500,"startOfParagraph":false},{"content":"the dereference operator, we're attempting to increment the pointer y,","startTime":232800,"duration":2570,"startOfParagraph":false},{"content":"which would move the pointer by size of int bytes.","startTime":235370,"duration":3800,"startOfParagraph":false},{"content":"Essentially giving us an address in some entirely different point in memory,","startTime":239170,"duration":3870,"startOfParagraph":false},{"content":"and then we're dereferencing it.","startTime":243040,"duration":1970,"startOfParagraph":false},{"content":"So this is very meaningless line.","startTime":245010,"duration":2340,"startOfParagraph":false},{"content":"If we actually wanted to increment the value of 7, ","startTime":247350,"duration":2900,"startOfParagraph":false},{"content":"we would have to put the dereference operator with y in parentheses.","startTime":250250,"duration":4010,"startOfParagraph":false},{"content":"Then we could increment it.","startTime":254260,"duration":3030,"startOfParagraph":false},{"content":"So while we wouldn't be incrementing the value x with the second to last line of code,","startTime":257290,"duration":3799,"startOfParagraph":false},{"content":"in the last line of code we would infact dereference y ","startTime":261089,"duration":2291,"startOfParagraph":false},{"content":"to get the value x and increment that.","startTime":263380,"duration":3000,"startOfParagraph":false},{"content":"We would be left with the value x equals 8.","startTime":266380,"duration":3160,"startOfParagraph":false},{"content":"Here's a quick recap of the precendence rules that we've talked about.","startTime":271580,"duration":2000,"startOfParagraph":true},{"content":"We'll start with the innermost parentheses and work outward.","startTime":273580,"duration":3630,"startOfParagraph":false},{"content":"Then we move on to suffix operators like i++ or i--.","startTime":277210,"duration":4000,"startOfParagraph":false},{"content":"Then dereference and address of operators like star x or ampersand x,","startTime":281210,"duration":4710,"startOfParagraph":false},{"content":"and the prefix operators like ++i or --i.","startTime":285920,"duration":4340,"startOfParagraph":false},{"content":"Finally we do the simple math operations like multiplication, division, modulo.","startTime":290260,"duration":4660,"startOfParagraph":false},{"content":"Then addition, subtraction.","startTime":294920,"duration":3480,"startOfParagraph":false},{"content":"That's precendence. I'm Jordan Jozwiak, and this is CS50.","startTime":298400,"duration":3770,"startOfParagraph":false},{"content":"We'll dereference and use the address and--how do you phrase that?","startTime":304160,"duration":6320,"startOfParagraph":false},{"content":"I'm done. Okay.","startTime":312380,"duration":810,"startOfParagraph":false}]}