1 00:00:07,780 --> 00:00:10,540 Precendence is how we answer the question, what operation should we do first? 2 00:00:10,540 --> 00:00:14,250 Whether solving math equations or parsing lines of computer code, 3 00:00:14,250 --> 00:00:17,230 there are strict rules of precedence to which we adhere 4 00:00:17,230 --> 00:00:20,270 so that all computers and people can get the same result. 5 00:00:20,270 --> 00:00:24,710 >> First off, the most important rule to remember, especially in bug testing, 6 00:00:24,710 --> 00:00:27,680 is that we always work from the innermost parentheses outward. 7 00:00:27,680 --> 00:00:31,120 Using extra parentheses can be a helpful debugging tactic, 8 00:00:31,120 --> 00:00:34,640 but it's not good practice to litter your code with unneeded parentheses. 9 00:00:34,640 --> 00:00:38,220 Take the time to learn basic operator precedence rules. 10 00:00:38,220 --> 00:00:42,450 >> The second general rule is that when operators have equal priorty, 11 00:00:42,450 --> 00:00:44,820 you simply solve from left to right. 12 00:00:44,820 --> 00:00:47,690 When dealing with simple math we start with parentheses, 13 00:00:47,690 --> 00:00:52,110 then do multiplication and division, and lastly do addition and subtraction. 14 00:00:52,110 --> 00:00:54,400 Multiplication and division have the same priority, 15 00:00:54,400 --> 00:00:56,870 because they are essentially performing the same operation. 16 00:00:56,870 --> 00:01:00,880 After all division is simply multiplying by the inverse of a value. 17 00:01:00,880 --> 00:01:04,300 Similarly, subtraction is simply adding a negative value. 18 00:01:04,300 --> 00:01:06,150 >> Let's do an example. 19 00:01:14,470 --> 00:01:18,300 Following the order of precedence, we'll start with the parentheses. Nine minus 1. 20 00:01:18,300 --> 00:01:23,410 That will give us 8. Then we can move on to the division and multiplication. 21 00:01:23,410 --> 00:01:27,450 We'll solve from left to right. So 10 divided by 2 is 5. 22 00:01:27,450 --> 00:01:31,290 We have 5 times 8 here, and that will give us 40. 23 00:01:33,230 --> 00:01:35,410 Then we move on to the next order of precedence. 24 00:01:35,410 --> 00:01:38,730 So we're left with 3 plus 40 minus 1. 25 00:01:42,400 --> 00:01:43,700 Again just solving left to right, 26 00:01:43,700 --> 00:01:47,650 because there's equal priority between the addition and subtraction. 27 00:01:47,650 --> 00:01:51,510 We can say 3 plus 40 is 43, minus 1 is 42. That's our answer. 28 00:01:53,920 --> 00:01:56,730 >> There are 2 types of decrement and increment operators; 29 00:01:56,730 --> 00:02:01,000 The prefix form, and the suffix form. 30 00:02:01,000 --> 00:02:06,130 The suffix form, i++, is commonly used in for loops, 31 00:02:06,130 --> 00:02:10,500 which means that the current value is used in the expression, and then it is incremented. 32 00:02:10,500 --> 00:02:14,240 So value will only be different the next time the variable is used. 33 00:02:14,240 --> 00:02:17,910 On the other hand, the prefix increment or decrement means that the current value 34 00:02:17,910 --> 00:02:22,760 is incremented or decremented first, and then it is used in the expression. 35 00:02:22,760 --> 00:02:25,310 >> Let's take an example with the integer x. 36 00:02:25,310 --> 00:02:27,220 We'll set it equal to 5. 37 00:02:27,220 --> 00:02:36,500 If we use the suffix operator on it and say x++, x on this line is still 5. 38 00:02:36,500 --> 00:02:39,230 If we were to print it out we would get the value 5. 39 00:02:39,230 --> 00:02:42,540 But going forward x1 fact equals 6. 40 00:02:42,540 --> 00:02:48,770 So right here on this line x is equal to 6, and if we printed it out we would get the value 6. 41 00:02:48,770 --> 00:02:57,380 Now if we used the prefix operator, ++x, x is incremented first, and then the value is used. 42 00:02:57,380 --> 00:03:00,110 So it's equal to 7 on this line. 43 00:03:00,110 --> 00:03:04,750 Incrementing of course 6 to 7, and if we were to print it out we would get the value 7. 44 00:03:04,750 --> 00:03:09,160 >> The last nuance in precendence that we will look at deals with pointer notation. 45 00:03:09,160 --> 00:03:15,050 The dereference operator, star, has priority over basic math operators, 46 00:03:15,050 --> 00:03:18,550 but not over the suffix incement and decrement operators. 47 00:03:18,550 --> 00:03:20,690 This leads us to our final example. 48 00:03:20,690 --> 00:03:24,500 Let's take the integer x and set it equal to 7. 49 00:03:24,500 --> 00:03:30,540 We'll also make a pointer y and set it equal to the address of x. 50 00:03:30,540 --> 00:03:34,920 So that when we dereference y we should get the value 7. 51 00:03:34,920 --> 00:03:39,380 Now in this line of code, we have a somewhat ambiguous situation. 52 00:03:39,380 --> 00:03:44,310 Are we dereferencing y first, and then incrementing the value 7? 53 00:03:44,310 --> 00:03:48,300 Or are we incrementing the pointer and then dereferencing it? 54 00:03:48,300 --> 00:03:52,800 In fact, because the suffix increment operator has precedence over 55 00:03:52,800 --> 00:03:55,370 the dereference operator, we're attempting to increment the pointer y, 56 00:03:55,370 --> 00:03:59,170 which would move the pointer by size of int bytes. 57 00:03:59,170 --> 00:04:03,040 Essentially giving us an address in some entirely different point in memory, 58 00:04:03,040 --> 00:04:05,010 and then we're dereferencing it. 59 00:04:05,010 --> 00:04:07,350 So this is very meaningless line. 60 00:04:07,350 --> 00:04:10,250 If we actually wanted to increment the value of 7, 61 00:04:10,250 --> 00:04:14,260 we would have to put the dereference operator with y in parentheses. 62 00:04:14,260 --> 00:04:17,290 Then we could increment it. 63 00:04:17,290 --> 00:04:21,089 So while we wouldn't be incrementing the value x with the second to last line of code, 64 00:04:21,089 --> 00:04:23,380 in the last line of code we would infact dereference y 65 00:04:23,380 --> 00:04:26,380 to get the value x and increment that. 66 00:04:26,380 --> 00:04:29,540 We would be left with the value x equals 8. 67 00:04:31,580 --> 00:04:33,580 >> Here's a quick recap of the precendence rules that we've talked about. 68 00:04:33,580 --> 00:04:37,210 We'll start with the innermost parentheses and work outward. 69 00:04:37,210 --> 00:04:41,210 Then we move on to suffix operators like i++ or i--. 70 00:04:41,210 --> 00:04:45,920 Then dereference and address of operators like star x or ampersand x, 71 00:04:45,920 --> 00:04:50,260 and the prefix operators like ++i or --i. 72 00:04:50,260 --> 00:04:54,920 Finally we do the simple math operations like multiplication, division, modulo. 73 00:04:54,920 --> 00:04:58,400 Then addition, subtraction. 74 00:04:58,400 --> 00:05:02,170 That's precendence. I'm Jordan Jozwiak, and this is CS50. 75 00:05:04,160 --> 00:05:10,480 We'll dereference and use the address and--how do you phrase that? 76 00:05:12,380 --> 00:05:13,190 I'm done. Okay.