Locked lesson.
About this lesson
We learn how to increment and decrement variables using ++ and -- commands.
Exercise files
Download this lesson’s related exercise files.
17 - Math Incrementation ++ and --.docx60.9 KB 17 - Math Incrementation ++ and -- SOLUTION.docx
58.1 KB
Quick reference
Math Incrementation ++ and --
++ Increment and -- Decrement allow you to add 1 or subtract 1 to a variable.
When to use
Use this generally to create counters.
Instructions
int x = 4;
x++ // add one later
++x // add one now
x-- // subtract one later
--x // subtract one now
Hints & tips
- x++
- ++x
- x--
- --x
- 00:03 Okay, in this video, I want to talk about the increment and the decrement operators.
- 00:08 And increment and decrement means add 1 and subtract 1.
- 00:12 So this is sort of a shorthand way to, like I said, add 1 or subtract 1.
- 00:16 And this is very common in all programming languages.
- 00:19 There are many times you need to add just 1 to a variable at a time.
- 00:23 And often, you do this to create a counter.
- 00:26 So every time we do something, we want to keep track of it.
- 00:28 So every time we do this, add 1 to the counter, do it again,
- 00:31 add 1 to the counter, do it again, add 1 to the counter.
- 00:34 And we might say after we've done it 20 times, stop, right?
- 00:37 So increment and decrement are very important for programming in general.
- 00:42 So for C sharp, the increment and decrement operators are plus plus and
- 00:46 minus minus.
- 00:46 And this usually the case for most programming languages.
- 00:50 Now, this is a little tricky.
- 00:51 Now, I want to show you something.
- 00:52 So let's create a variable called x, and let's just set it to 10.
- 00:56 Now, to use this, we call the variable, and
- 01:01 then you go like this, or you go like this.
- 01:06 Now, what are the differences between these two?
- 01:09 Well, if we run it like this, this is a prefix, right?
- 01:12 Versus this, which is a postfix, I guess.
- 01:17 It doesn't matter.
- 01:18 Basically, what's going on here is, when we do it like this,
- 01:21 we're saying, hey, add 1, and then print it to the screen.
- 01:25 So this will print out 11.
- 01:27 When we do it like this, we're saying, hey, add 1 but
- 01:30 don't print it to the screen, but keep it in memory for later.
- 01:35 So let me just show you the difference.
- 01:36 Let's run this.
- 01:37 So if we go ++x, remember we started at 10, we expect this to print out 11.
- 01:43 So when we run this guy, sure enough, we get 11.
- 01:47 Well, what happens if we do it the other way, like this?
- 01:50 You might still think, okay, it should be 11, but it's not,
- 01:53 it's going to only print out 10.
- 01:55 Why? Because think of the program flow coming
- 01:57 from left to right.
- 01:59 It says, hey, here's the variable, which is 10.
- 02:02 Print that out.
- 02:03 So it does, it prints out 10.
- 02:05 And then it says, well, add 1 to it.
- 02:07 Okay, it does that, but it doesn't say print it out again as 11.
- 02:12 So it just sort of keeps it in memory.
- 02:14 So if we run this, we're just going to get 10, right?
- 02:18 Has it stored it in memory?
- 02:20 How do we know?
- 02:20 Well, let's write another line and see.
- 02:24 So if we copy this guy and print out a second one with just x, right?
- 02:28 We're not adding or subtracting anything on this line.
- 02:31 Now, we're going to see 10 on this line, and it's going to add 1, 11.
- 02:37 And we're going to say, hey, print it out now, then it'll print 11.
- 02:41 So this should print out 10 and 11.
- 02:43 So we go ahead and run this, And we see sure enough 10 and 11.
- 02:48 So it really just depends on what your programming is trying to do.
- 02:51 If you just want to put it out onto the screen immediately,
- 02:54 you don't care what happens later.
- 02:56 So for instance, if we run this guy again, we should get 11 and 11.
- 02:59 Sure enough, 11 and 11.
- 03:03 And that's cool.
- 03:04 So that's the increment operator, super easy.
- 03:07 We can also do the decrement operator, so decreased by 1.
- 03:11 So 10 minus 1 is going to be 9, right?
- 03:14 So this should return 9 and 9.
- 03:16 Sure enough, same thing here.
- 03:19 When we do it afterwards like that, if we save this and run it,
- 03:25 We're going to be 10 and 9, which is what we would expect.
- 03:29 because we start out, hey, it's 10 here, then we subtract 1, it becomes 9.
- 03:33 We don't print it to the screen till we pop down to this line.
- 03:36 And when we do print it out, it becomes 9.
- 03:38 So increment and decrement, super, super important.
- 03:41 And we're going to come across these a lot going forward in the course when we do
- 03:44 things like loops and things like that.
- 03:46 So remember those.
- 03:48 Again, just a very, very common thing in all programming languages.
- 03:51 You're always going to want to be able to increment or decrement.
- 03:53 Decrement, does that sound right?
- 03:56 Decrease or increase a number by 1, and that's how you do it.
- 04:00 So that's all for this video.
- 04:01 In the next video, we're going to look at math functions.
Lesson notes are only available for subscribers.