Lesson 5: Why do we have to use i+=3 instead of i+3 to count up by 3's?


#1


Lesson 5. Controlling the for loop


This is not about a code, but rather something I don't understand.

The lesson says we use
i+=3 to count up by 3's
why can't we just write
i+3, especially because i+1 means we count up by 1



#2

Because i + 3 would just add three to i rather than adding on 3 as many times as you wanted.

If you right your for loop like this:

`
for(var i = 0; i < 12; i+3) {

}
`
You can see that i is a variable containing a number and if you add three to i then you are going to get three. Basically it makes the language more safe so that it can tell that you mean count up by threes to this point instead of thinking add i + 3.

Hope this helped and that the explanation made sense :slight_smile:


#3

Yes! Thank you :smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.