Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Increment Operators.docx58.8 KB Increment Operators - Solution.docx
59 KB
Quick reference
Increment Operators
Increment operators allow us to increase or decrease a number by 1.
When to use
Whenever you want to increase or decrease a number by just 1, use an increment operator.
Instructions
These are the increment operators:
$num++; Output the variable then increment
$num--; Output the variable then decrement
++$num; Increment the variable then output it
--$num; Decrement the variable then output it
Where you put your ++ or -- is important. Put them before the variable to increment/decrement and then output. Put them after to output first, then increment/decrement.
Hints & tips
- Increment operators allow you to increase or decrease a number by 1.
- ++
- --
- putting them before or after the variable matters!
- 00:05 In this video I want to talk about increment operators.
- 00:07 A couple of videos ago we talked about assignment operators, and
- 00:10 we had the plus equal to operator.
- 00:12 We have echo number 1 plus equal to 4, let's say.
- 00:18 We save this, come back.
- 00:20 41 plus 4 is 45.
- 00:22 That's useful, but a lot of times, we just want to increase by one.
- 00:25 It's useful for loops and all kinds of things.
- 00:28 We'll talk about loops later.
- 00:29 But to increase something by one we're going to use an increment operator, and
- 00:33 it's very similar to this assignment operator.
- 00:36 To increase we just go plus plus.
- 00:39 Or to decrease minus minus.
- 00:41 What that will do is it will increase your variable by 1 or decrease it by 1.
- 00:45 Now there is a catch here.
- 00:46 There's a couple of different increment operators that we can work with.
- 00:49 Let's just go plus plus.
- 00:51 Let's save this and hit reload, and it's 41 so, what's going on here.
- 00:55 There is two different ways to do your increment operators.
- 00:57 You could after the variable, like we've done right here, or
- 01:02 you can do before the variable.
- 01:04 We save this and hit reload now it says 42 the order of before you put this,
- 01:09 the plus plus or the minus minus is important.
- 01:12 If we do it here and save this, it looks like nothing has happened, but
- 01:16 we've still actually incremented our number.
- 01:19 It's just not returning it.
- 01:21 When you put it afterward like this, what it's doing is,
- 01:24 it's returning the variable first and then incrementing.
- 01:27 So if we came down here and typed in echo, this an HTML line break, but
- 01:32 now if we echoed number 1, what do you think's gonna happen?
- 01:37 This is kinda interesting, we have 41 and 42.
- 01:39 So let's look at this code.
- 01:41 We're incrementing, but we're outputting it first and then incrementing.
- 01:45 It's echoing 41 but then it's heading 1 to 41.
- 01:49 So then later on if we echo out number 1 again our new number is 42.
- 01:54 Likewise if we put it before and save, they're both can be 42.
- 02:00 Most of the time you want to echo out the new number.
- 02:04 In that case, you're going to put your pluses in front of the variable.
- 02:07 There are circumstances where you're going to want to echo out the old number but
- 02:11 still have the number incremented.
- 02:13 In that case you'll put them behind.
- 02:15 So that's increment we can also decrement.
- 02:18 Or decrease, so 41 minus 1 is 40 same thing, works the same way.
- 02:24 If you put it behind, it'll output the old number first and
- 02:28 then increment it so, 41 and then 40.
- 02:30 Now like I said, these are important.
- 02:32 We'll use these for loops in the future and for counters,
- 02:35 and all kinds of things like that.
- 02:36 In most programming languages there's a lot of times where you just wanna
- 02:40 increment or decrease just by one and in that case,
- 02:43 you'll use this increment operator plus plus and minus minus.
- 02:47 In the next video we're gonna talk about logic operators.
Lesson notes are only available for subscribers.