0

increment a loop

why is that when you use x++ in the for-loop during incrementing gives a different result unlike using x+1.

9/12/2019 5:42:46 PM

victorYegon

1 Answer

New Answer

0

When you increment your loop, or any variable for that matter, you are adding the value you increment in (in most cases, 1) to the variable. In order to have x + 1 become the new x, you have to type x = x + 1. Thankfully, there are ways to streamline this process that reduce the amount of typing. When you increase a variable, you can shorten the code to x += v (v is the value you want to increase x by). This can be really useful in situations that involve incrementing several variables by saving time, and your hands. In a for-loop, (as this is the only example of this usage I have ever come across) this can be shortened even further by using x++, which can be expanded into x += 1 or x = x + 1.