increment a loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

12th Sep 2019, 5:42 PM
Victor Yegon
Victor Yegon - avatar
1 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.
19th Sep 2019, 2:16 PM
Desmos
Desmos - avatar