1 00:00:05,660 --> 00:00:10,360 In this video, we'll go over c++'s increment and decrement operators. 2 00:00:11,060 --> 00:00:13,060 I'm in the section 8 workspace 3 00:00:13,060 --> 00:00:15,760 in the increment decrement operators project. 4 00:00:16,560 --> 00:00:20,560 This is a pretty simple looking operator, but there's a lot to it, and it can be extremely 5 00:00:20,560 --> 00:00:23,560 confusing if you overuse it. Let's talk about it. 6 00:00:24,440 --> 00:00:27,940 Basically, it's the ++ and -- operator. 7 00:00:27,940 --> 00:00:31,240 You've got two +es side by side or two minuses side by side. 8 00:00:31,540 --> 00:00:34,340 All that means is increment something by 1 9 00:00:34,340 --> 00:00:36,340 or decrement something by 1. 10 00:00:37,140 --> 00:00:40,440 In this example, I'm only going to be using the increment operator, 11 00:00:40,440 --> 00:00:42,940 but everything will apply to the decrement operator. 12 00:00:42,940 --> 00:00:47,440 Okay. So all it does is increment or decrement its operand by 1. 13 00:00:47,440 --> 00:00:50,740 So if you use it with an integer, it increases the integer by 1 14 00:00:50,740 --> 00:00:53,730 or decreases the integer by 1,depending on which one you're using. 15 00:00:54,030 --> 00:00:57,830 If you're using it with a floating point number, same thing. It'll increase it by 1 16 00:00:57,830 --> 00:00:59,330 or decrease it by 1. 17 00:00:59,330 --> 00:01:02,630 And we can also use it with pointers to move pointers along. 18 00:01:02,630 --> 00:01:04,290 We haven't talked about pointers yet, 19 00:01:04,290 --> 00:01:07,790 but we'll use this operator when we talk about pointers in a future section. 20 00:01:08,450 --> 00:01:12,150 There are two variants to this operator. There's a prefix notation 21 00:01:12,150 --> 00:01:14,030 and a postfix notation. 22 00:01:14,030 --> 00:01:16,530 All that means is that the operator 23 00:01:16,530 --> 00:01:20,520 is either before its operand or after its operand. 24 00:01:20,520 --> 00:01:23,880 You can see here on line number 9 25 00:01:23,880 --> 00:01:26,080 that the operand is num 26 00:01:26,080 --> 00:01:29,780 and the operator is to the left of it, so that's prefix notation. 27 00:01:29,780 --> 00:01:33,140 The postfix notation is when it appears to the right of the operand. 28 00:01:33,840 --> 00:01:37,340 Okay. I'll say this a few times. Don't overuse this operator, 29 00:01:37,840 --> 00:01:41,140 and never use it twice for the same variable in the same statement 30 00:01:41,140 --> 00:01:44,140 because the behavior is undefined. You really don't know what you're going to get. 31 00:01:44,840 --> 00:01:48,940 Good, so now let's talk about the operator. Let me scroll down just a little bit. 32 00:01:50,240 --> 00:01:53,600 And I've got some examples here, and I'll walk through these examples 33 00:01:53,600 --> 00:01:57,100 really really carefully and slowly so you can understand what's going on. 34 00:01:57,800 --> 00:02:02,300 For all of the examples, I'll be using these two variables right here. 35 00:02:02,300 --> 00:02:06,400 I'll be using a variable called counter and a variable called result. 36 00:02:06,400 --> 00:02:07,900 They're both integers, 37 00:02:07,900 --> 00:02:11,200 counter will be initialized to 10 and result to 0. 38 00:02:11,200 --> 00:02:15,200 Okay so real straightforward. Counter will be 10, result will be 0. 39 00:02:15,200 --> 00:02:19,600 And we're going to manipulate those variables, and we'll see how this operator really works. 40 00:02:19,850 --> 00:02:23,840 So let's start right here. Let me uncomment example one. 41 00:02:26,200 --> 00:02:29,600 I'll walk through this really slowly. You can see what's happening. 42 00:02:29,600 --> 00:02:31,400 I'm outputting counter. 43 00:02:32,100 --> 00:02:36,000 Okay, well counter is 10, so this you expect it to display a 10. 44 00:02:36,500 --> 00:02:38,000 And that's exactly what happens. 45 00:02:38,360 --> 00:02:41,460 In this case, I'm saying counter = counter +1, 46 00:02:41,710 --> 00:02:44,010 right. So what's counter 10, 47 00:02:44,670 --> 00:02:49,570 I'm adding 1 to it, and then I'm putting it right back into counter. So I'm changing this to an 11. 48 00:02:50,770 --> 00:02:53,270 And this output statement will display an 11. 49 00:02:54,770 --> 00:02:57,270 In this case, I'm using counter++. 50 00:02:57,870 --> 00:03:02,070 That's the increment operator. That's going to increment the variable counter 51 00:03:02,070 --> 00:03:02,970 by 1. 52 00:03:03,670 --> 00:03:06,970 When you use it all alone like that if you've got an operand, 53 00:03:08,070 --> 00:03:12,170 followed by a ++ or a ++ before the operand, 54 00:03:12,670 --> 00:03:16,770 it means exactly the same thing. All it means is increment the operand by 1. 55 00:03:17,170 --> 00:03:20,470 Okay. It'll get a little more confusing in the next few examples, 56 00:03:20,470 --> 00:03:23,970 but let's just get this one under our belt so we can understand what's going on. 57 00:03:23,970 --> 00:03:28,410 So what's happening here is we're incrementing that l value counter by 1. 58 00:03:28,770 --> 00:03:32,570 So we're going to counter incrementing it by 1, now it's 12. 59 00:03:33,370 --> 00:03:36,370 And we're going to display counter here which is 12. 60 00:03:37,730 --> 00:03:41,230 The pre-increment works exactly the same as the post 61 00:03:41,230 --> 00:03:44,730 increment when that thing is all alone on the line, just like I said over here. 62 00:03:44,730 --> 00:03:47,430 So in this case, exactly the same thing will happen. 63 00:03:47,430 --> 00:03:51,030 I'm going to take the 12 and add 1 to it, make it a 13. 64 00:03:51,530 --> 00:03:54,530 Contour will now be 13, and I'll output 13. 65 00:03:55,160 --> 00:03:59,360 All right so in this context, wherever you want to increment something, you could say 66 00:03:59,610 --> 00:04:03,610 counter = counter +1 or you could just say counter++ 67 00:04:03,610 --> 00:04:07,410 or you could say ++counter.All three of them mean the same thing. 68 00:04:07,410 --> 00:04:09,910 Later on we'll talk about a different operator, 69 00:04:09,910 --> 00:04:13,570 the + = operator, that does does the same thing as well. 70 00:04:13,570 --> 00:04:17,170 So there's a bunch of different ways that you can achieve the same result. 71 00:04:17,170 --> 00:04:21,730 I'm going to clear this. And remember, we've got 10, 11, 12 and 13 as the output. 72 00:04:21,730 --> 00:04:25,130 So I'll clear this. And let's run this example. 73 00:04:26,430 --> 00:04:28,530 And you can see that counter is 74 00:04:29,130 --> 00:04:32,330 10, 11, 12 and 13, just like what we expected. 75 00:04:33,530 --> 00:04:37,730 All right. So now let's look at a little bit more complicated example. And I'm going to comment this out 76 00:04:38,030 --> 00:04:42,230 and I'll comment this out, not delete it. 77 00:04:43,730 --> 00:04:47,090 And I will uncomment out example two. 78 00:04:50,300 --> 00:04:53,100 Okay. So let's go through this one again really carefully. 79 00:04:53,500 --> 00:04:57,500 What we've got here is, again I've reset counter to 10 80 00:04:57,500 --> 00:05:01,060 and result to 0. All of these examples are going to start in the same place. 81 00:05:01,060 --> 00:05:03,060 Counter will be 10, result will be 0. 82 00:05:03,560 --> 00:05:07,660 So here, I'm going to say I want to output counter. Well, counter is 10, right. 83 00:05:07,960 --> 00:05:10,060 So we expect this to display a 10. 84 00:05:11,050 --> 00:05:13,850 Now we're doing a pre-increment of counter, 85 00:05:14,740 --> 00:05:18,240 and we're storing the result into that variable called result. 86 00:05:18,240 --> 00:05:21,940 So what's happening here is there's really a few things going on here. 87 00:05:21,940 --> 00:05:25,930 If you just see that ++ and just take it out for a second 88 00:05:26,130 --> 00:05:29,130 and the result is something like this, result 89 00:05:29,730 --> 00:05:31,030 = counter, 90 00:05:33,330 --> 00:05:36,330 that's the real assignment statement that's going on here. 91 00:05:36,930 --> 00:05:41,330 The ++ in front of counter means it's prefix notation, 92 00:05:41,330 --> 00:05:45,630 which means that counter will be incremented before it's used. 93 00:05:45,930 --> 00:05:46,930 So in this case, 94 00:05:47,730 --> 00:05:50,530 before happens here. Its uses right here. 95 00:05:50,530 --> 00:05:52,930 But before it's used,we're going to say counter 96 00:05:53,330 --> 00:05:57,130 =, I'll just say counter +1 just to be really explicit. 97 00:05:57,930 --> 00:06:01,230 So this statement, right here, that's the effect of that statement. 98 00:06:01,530 --> 00:06:04,330 I want to increment counter before I use it, 99 00:06:04,330 --> 00:06:06,930 and then I want to use it by assigning it to result. 100 00:06:07,590 --> 00:06:10,090 All right. So let's do those two steps and see what happens. 101 00:06:10,590 --> 00:06:14,190 Counter = counter +1. We just made counter 11. 102 00:06:15,380 --> 00:06:19,530 And then we're going to assign counter to result. Well, counter is 11. So result 103 00:06:19,530 --> 00:06:20,530 will now be 11. 104 00:06:21,530 --> 00:06:24,330 Now I want to display counter. Counter will be 11, 105 00:06:25,030 --> 00:06:29,230 and result will be 11. So this is what I expect is the output. 10, 106 00:06:29,230 --> 00:06:30,330 11 and 11. 107 00:06:30,930 --> 00:06:33,430 Okay. Remember, this is the use, 108 00:06:34,330 --> 00:06:36,930 and this is the effect of that pre-increment. 109 00:06:38,130 --> 00:06:42,230 All right. So let me clear this and run it. Again, we're expecting 10, 11 and 11. 110 00:06:44,430 --> 00:06:45,750 Build and run, 111 00:06:46,550 --> 00:06:50,250 and there it is. 10, 11 and 11, exactly what we expected. 112 00:06:51,350 --> 00:06:53,350 All right. I'll comment out that piece, 113 00:06:53,650 --> 00:06:57,750 and we'll do another example this time with a post increment. 114 00:06:58,350 --> 00:07:01,950 And remember, I'm using the increment but the decrement works exactly the same way 115 00:07:01,950 --> 00:07:03,750 it just decreases the value by 1. 116 00:07:04,950 --> 00:07:08,550 All right. I'll uncomment this example, and let's walk through this one again 117 00:07:09,150 --> 00:07:10,650 one more time. 118 00:07:10,950 --> 00:07:14,850 I'm initializing count or I'm assigning 10 to counter and 0 to result. 119 00:07:15,350 --> 00:07:18,350 And I'm saying print out the counter. Well, it's 10. 120 00:07:18,350 --> 00:07:20,550 We expect the 10 here. That's an easy one. 121 00:07:21,050 --> 00:07:23,050 But now we're doing a post increment. 122 00:07:23,050 --> 00:07:26,950 Notice it's on the right side or it's following counter it's operand. 123 00:07:26,950 --> 00:07:29,250 So this becomes result 124 00:07:30,610 --> 00:07:31,910 = counter 125 00:07:34,510 --> 00:07:38,310 and the post increment simply means that we're going to increment counter 126 00:07:38,310 --> 00:07:39,410 after we use it. 127 00:07:39,410 --> 00:07:43,210 Well, we're using it here. So let's increment it after this time. 128 00:07:43,710 --> 00:07:47,810 So again, I'll be really explicit here and I'll say counter = counter +1. 129 00:07:49,410 --> 00:07:52,110 So that's the effect of that statement. 130 00:07:52,470 --> 00:07:55,370 So let's execute this statement. Result is counter. 131 00:07:55,370 --> 00:07:58,170 Well, counter is 10, so let's put 10 in here. 132 00:07:59,070 --> 00:08:03,670 Now we increment counter by 1. So that becomes 11. 133 00:08:04,670 --> 00:08:07,670 And now we display counter, which is 11 134 00:08:07,670 --> 00:08:09,020 and result which is 10. 135 00:08:09,520 --> 00:08:12,520 So in this case, this is the output we expect. 10, 136 00:08:12,520 --> 00:08:16,120 11 and then 10. All right. Let's give that a shot. 137 00:08:19,020 --> 00:08:21,320 And there we go. 10, 11 and 10. 138 00:08:23,220 --> 00:08:26,620 Let's do a few more examples so I want to be sure that you really understand this. 139 00:08:26,620 --> 00:08:29,120 Let's uncomment example 4 here. 140 00:08:30,320 --> 00:08:33,980 And example 4 just uses the expression that's a little bit more complicated, 141 00:08:33,980 --> 00:08:37,580 not much but a little bit so you can see how this works. In this case, 142 00:08:38,179 --> 00:08:41,179 one more time I've set those to 10 and 10, 143 00:08:41,840 --> 00:08:45,740 and I'm displaying counter. Well, counter is 10. Again, real easy. 144 00:08:45,740 --> 00:08:48,240 And now we've got this statement going on right here. 145 00:08:48,240 --> 00:08:50,740 Result = ++counter +10. 146 00:08:51,340 --> 00:08:53,940 The best way to really look at these things is just 147 00:08:53,940 --> 00:08:57,600 get rid of that guy right now. So just say result 148 00:08:59,300 --> 00:09:02,300 = counter +10. 149 00:09:03,200 --> 00:09:04,900 That's the statement. 150 00:09:05,700 --> 00:09:09,500 And now since the ++ is a pre-increment, 151 00:09:09,650 --> 00:09:14,050 it's on the left side of counter that means that we increment counter before we use it. 152 00:09:14,250 --> 00:09:18,250 We're using it here. So that means that we use, we do this before we execute 153 00:09:18,250 --> 00:09:19,150 that statement. 154 00:09:19,510 --> 00:09:22,510 So again, counter = counter +1. All right. 155 00:09:23,870 --> 00:09:28,170 So let's do this counter is counter +1. This becomes 11. 156 00:09:29,050 --> 00:09:33,350 Then we say result is counter +10. Well, counter is 11. 157 00:09:34,010 --> 00:09:38,410 So 11 + 10 is going to give us a 21, which we're going to store in result. 158 00:09:39,110 --> 00:09:40,990 So result here, we'll get a 21. 159 00:09:42,290 --> 00:09:46,690 Got it. Perfect. Now we're displaying counter. It's 11. 160 00:09:47,090 --> 00:09:51,090 Result is 21. That's what we expect to display. 161 00:09:51,090 --> 00:09:54,750 10, 11 and 21. This may seem a little 162 00:09:54,750 --> 00:09:57,900 contrived and puzzle like and it may be, 163 00:09:57,900 --> 00:10:01,300 but this kind of code is seen out there. And you really need to understand the effect 164 00:10:01,300 --> 00:10:04,400 of the pre-increment and the post increment with these operators. 165 00:10:05,100 --> 00:10:08,420 So 10, 11 and 21 is what we expect. Let's run this. 166 00:10:11,620 --> 00:10:13,980 And 10, 11 and 21 is what we get. 167 00:10:15,780 --> 00:10:17,780 Okay. So let's comment this one out. 168 00:10:19,280 --> 00:10:22,680 And you'll notice what this one the next one example 5 169 00:10:22,680 --> 00:10:25,180 is exactly like example 4 except. 170 00:10:25,180 --> 00:10:27,780 We've got a post increment right here. 171 00:10:28,480 --> 00:10:32,380 Right there, you can see it's the same idea except it's the ++ is 172 00:10:32,380 --> 00:10:35,040 after the operand. Okay. 173 00:10:35,040 --> 00:10:38,540 So again, 10 and 0, we're starting out. We're saying display the counter. 174 00:10:38,540 --> 00:10:41,040 Well, the counter is 10. That's pretty easy. 175 00:10:41,640 --> 00:10:43,840 Here's the statement. Let's just rewrite it 176 00:10:44,340 --> 00:10:47,340 without the increment. So we're going to say result = 177 00:10:47,740 --> 00:10:49,040 again counter + 178 00:10:50,040 --> 00:10:53,840 +10. I'm trying to write nice and big, so hopefully everybody can see this, okay. 179 00:10:54,640 --> 00:10:58,140 And we're doing the increment of counter after we use it. That means 180 00:10:58,390 --> 00:11:01,690 after we use it. We're using it here. So this becomes counter 181 00:11:03,290 --> 00:11:05,090 is counter +1. 182 00:11:06,650 --> 00:11:11,050 All right. So let's execute these two statements. We'll put some semicolons there just to be complete. 183 00:11:11,050 --> 00:11:15,050 So result is counter +10. Well, what's counter. Counter is 10. 184 00:11:15,650 --> 00:11:18,950 So that becomes a 20, and we're storing 20 into result. 185 00:11:20,450 --> 00:11:24,550 And then we're incrementing counter by 1, so counter becomes 11. 186 00:11:25,430 --> 00:11:29,530 Okay. The output says what's counter, 11. What's result, 20. 187 00:11:30,190 --> 00:11:34,180 So in this case 10, 11 and 20 is what we expect. 188 00:11:34,180 --> 00:11:36,180 10, 11 and 20. Let's run this. 189 00:11:39,840 --> 00:11:42,640 And 10, 11 and 20 is exactly what we get. 190 00:11:45,940 --> 00:11:48,240 Okay. So hopefully that clears it up. 191 00:11:48,240 --> 00:11:51,790 And one of the examples if you know if you remember up at the top I mentioned, 192 00:11:51,790 --> 00:11:53,590 I'll scroll up a little bit it says here 193 00:11:53,590 --> 00:11:57,190 never used it twice for the same variable in the same statement. 194 00:11:57,190 --> 00:12:00,190 That could be something like suppose you do 195 00:12:01,190 --> 00:12:02,390 cout, 196 00:12:03,990 --> 00:12:06,190 and you've got something like, 197 00:12:06,190 --> 00:12:08,690 I don't know, let's just say we have an integer I. 198 00:12:08,690 --> 00:12:12,390 You'll see I++, and then you'll do something like ++I 199 00:12:12,890 --> 00:12:15,890 and something like that, you really have no clue what's going to happen 200 00:12:15,890 --> 00:12:19,690 because you're incrementing I two times in the same statement. 201 00:12:19,690 --> 00:12:23,490 And that's not a good idea. Sometimes you'll see people do silly things like 202 00:12:23,790 --> 00:12:27,790 I++ +++I or something 203 00:12:27,790 --> 00:12:30,290 like that just because you can in 204 00:12:30,290 --> 00:12:33,490 c++ but that's a bad thing to do because you're 205 00:12:33,490 --> 00:12:36,790 using it twice on the same variable in the same statement. 206 00:12:36,790 --> 00:12:40,590 And that's that behavior is totally undefined. So your guess is as good as mine 207 00:12:40,590 --> 00:12:42,290 as to what the result is going to be. 208 00:12:42,990 --> 00:12:46,290 Okay. So hopefully, this gives you a little bit of 209 00:12:46,290 --> 00:12:50,650 a little bit of an insight into this operator. a simple operator when you just say 210 00:12:50,650 --> 00:12:54,950 I++ or count++ or ++count. Really, really simple. 211 00:12:54,950 --> 00:12:59,050 But when you start combining it with other operands and other expressions and operators, 212 00:12:59,050 --> 00:13:03,050 it can get pretty complicated. And that's why I say don't overuse this operator. 213 00:13:03,350 --> 00:13:06,450 It's really a great operator to use when we loop, 214 00:13:06,450 --> 00:13:10,810 and we'll do that later and we use pointers. But keep your eye on it, don't overuse it.